mirror of
https://github.com/libretro/RetroArch
synced 2025-04-09 21:45:45 +00:00
Reuse socket_init for net_http code
This commit is contained in:
parent
8b9456f419
commit
78bb85e2f3
14
command.c
14
command.c
@ -123,25 +123,23 @@ static const struct cmd_map map[] = {
|
|||||||
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
|
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
|
||||||
static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port)
|
static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port)
|
||||||
{
|
{
|
||||||
int *file_desc = (int*)&handle->net_fd;
|
|
||||||
struct addrinfo *res = NULL;
|
struct addrinfo *res = NULL;
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
|
|
||||||
RARCH_LOG("Bringing up command interface on port %hu.\n",
|
RARCH_LOG("Bringing up command interface on port %hu.\n",
|
||||||
(unsigned short)port);
|
(unsigned short)port);
|
||||||
|
|
||||||
if (!socket_init(res, file_desc, port, NULL, SOCKET_TYPE_DATAGRAM))
|
handle->net_fd = socket_init((void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM);
|
||||||
|
|
||||||
|
if (handle->net_fd < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (*file_desc < 0)
|
if (!socket_nonblock(handle->net_fd))
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (!socket_nonblock(*file_desc))
|
setsockopt(handle->net_fd, SOL_SOCKET,
|
||||||
goto error;
|
|
||||||
|
|
||||||
setsockopt(*file_desc, SOL_SOCKET,
|
|
||||||
SO_REUSEADDR, (const char*)&yes, sizeof(int));
|
SO_REUSEADDR, (const char*)&yes, sizeof(int));
|
||||||
if (bind(*file_desc, res->ai_addr, res->ai_addrlen) < 0)
|
if (bind(handle->net_fd, res->ai_addr, res->ai_addrlen) < 0)
|
||||||
{
|
{
|
||||||
RARCH_ERR("Failed to bind socket.\n");
|
RARCH_ERR("Failed to bind socket.\n");
|
||||||
goto error;
|
goto error;
|
||||||
|
@ -37,7 +37,7 @@ enum socket_type
|
|||||||
SOCKET_TYPE_STREAM
|
SOCKET_TYPE_STREAM
|
||||||
};
|
};
|
||||||
|
|
||||||
bool socket_init(void *address, int *fd, uint16_t port, const char *server, enum socket_type type);
|
int socket_init(void **address, uint16_t port, const char *server, enum socket_type type);
|
||||||
|
|
||||||
int socket_close(int fd);
|
int socket_close(int fd);
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
#include <net/net_http.h>
|
#include <net/net_http.h>
|
||||||
#include <net/net_compat.h>
|
#include <net/net_compat.h>
|
||||||
|
#include <net/net_socket.h>
|
||||||
#include <compat/strl.h>
|
#include <compat/strl.h>
|
||||||
|
|
||||||
enum
|
enum
|
||||||
@ -72,34 +73,17 @@ struct http_connection_t
|
|||||||
|
|
||||||
static int net_http_new_socket(const char *domain, int port)
|
static int net_http_new_socket(const char *domain, int port)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
int ret;
|
int ret;
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#ifndef VITA
|
#ifndef VITA
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
struct addrinfo hints, *addr = NULL;
|
struct addrinfo *addr = NULL;
|
||||||
char portstr[16] = {0};
|
int fd = socket_init((void**)&addr, port, domain, SOCKET_TYPE_STREAM);
|
||||||
|
if (fd == -1)
|
||||||
/* Initialize the network. */
|
|
||||||
if (!network_init())
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
snprintf(portstr, sizeof(portstr), "%i", port);
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = AF_UNSPEC;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_flags = 0;
|
|
||||||
|
|
||||||
if (getaddrinfo_retro(domain, portstr, &hints, &addr) < 0)
|
|
||||||
return -1;
|
|
||||||
if (!addr)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#ifndef VITA
|
#ifndef VITA
|
||||||
timeout.tv_sec=4;
|
timeout.tv_sec=4;
|
||||||
@ -107,6 +91,7 @@ static int net_http_new_socket(const char *domain, int port)
|
|||||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof timeout);
|
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof timeout);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ret = connect(fd, addr->ai_addr, addr->ai_addrlen);
|
ret = connect(fd, addr->ai_addr, addr->ai_addrlen);
|
||||||
|
|
||||||
freeaddrinfo_retro(addr);
|
freeaddrinfo_retro(addr);
|
||||||
|
@ -24,14 +24,13 @@
|
|||||||
#include <net/net_compat.h>
|
#include <net/net_compat.h>
|
||||||
#include <net/net_socket.h>
|
#include <net/net_socket.h>
|
||||||
|
|
||||||
bool socket_init(void *address, int *fd, uint16_t port, const char *server, enum socket_type type)
|
int socket_init(void **address, uint16_t port, const char *server, enum socket_type type)
|
||||||
{
|
{
|
||||||
char port_buf[16] = {0};
|
char port_buf[16] = {0};
|
||||||
struct addrinfo hints = {0};
|
struct addrinfo hints = {0};
|
||||||
struct addrinfo *addr = (struct addrinfo*)address;
|
struct addrinfo **addrinfo = (struct addrinfo**)address;
|
||||||
|
struct addrinfo *addr = NULL;
|
||||||
if (!fd)
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
if (!network_init())
|
if (!network_init())
|
||||||
goto error;
|
goto error;
|
||||||
@ -51,23 +50,24 @@ bool socket_init(void *address, int *fd, uint16_t port, const char *server, enum
|
|||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!server)
|
if (!server)
|
||||||
hints.ai_flags = AI_PASSIVE;
|
hints.ai_flags = AI_PASSIVE;
|
||||||
|
|
||||||
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
|
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
|
||||||
|
|
||||||
if (getaddrinfo_retro(server, port_buf, &hints, &addr) < 0)
|
if (getaddrinfo_retro(server, port_buf, &hints, addrinfo) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
addr = (struct addrinfo*)*addrinfo;
|
||||||
|
|
||||||
if (!addr)
|
if (!addr)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
*fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
return socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
error:
|
error:
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int socket_receive_all_blocking(int fd, void *data_, size_t size)
|
int socket_receive_all_blocking(int fd, void *data_, size_t size)
|
||||||
|
@ -762,11 +762,9 @@ static bool init_tcp_socket(netplay_t *netplay, const char *server,
|
|||||||
static bool init_udp_socket(netplay_t *netplay, const char *server,
|
static bool init_udp_socket(netplay_t *netplay, const char *server,
|
||||||
uint16_t port)
|
uint16_t port)
|
||||||
{
|
{
|
||||||
int *file_desc = (int*)&netplay->udp_fd;
|
netplay->udp_fd = socket_init((void**)&netplay->addr, port, server, SOCKET_TYPE_DATAGRAM);
|
||||||
if (!socket_init(&netplay->addr, file_desc, port, server, SOCKET_TYPE_DATAGRAM))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (*file_desc < 0)
|
if (netplay->udp_fd < 0)
|
||||||
{
|
{
|
||||||
RARCH_ERR("Failed to initialize socket.\n");
|
RARCH_ERR("Failed to initialize socket.\n");
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user