Reuse socket_init for net_http code

This commit is contained in:
twinaphex 2016-05-01 22:45:32 +02:00
parent 8b9456f419
commit 78bb85e2f3
5 changed files with 24 additions and 43 deletions

View File

@ -123,25 +123,23 @@ static const struct cmd_map map[] = {
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port)
{
int *file_desc = (int*)&handle->net_fd;
struct addrinfo *res = NULL;
int yes = 1;
RARCH_LOG("Bringing up command interface on port %hu.\n",
(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;
if (*file_desc < 0)
if (!socket_nonblock(handle->net_fd))
goto error;
if (!socket_nonblock(*file_desc))
goto error;
setsockopt(*file_desc, SOL_SOCKET,
setsockopt(handle->net_fd, SOL_SOCKET,
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");
goto error;

View File

@ -37,7 +37,7 @@ enum socket_type
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);

View File

@ -26,6 +26,7 @@
#include <net/net_http.h>
#include <net/net_compat.h>
#include <net/net_socket.h>
#include <compat/strl.h>
enum
@ -72,34 +73,17 @@ struct http_connection_t
static int net_http_new_socket(const char *domain, int port)
{
int fd;
int ret;
#ifndef _WIN32
#ifndef VITA
struct timeval timeout;
#endif
#endif
struct addrinfo hints, *addr = NULL;
char portstr[16] = {0};
/* Initialize the network. */
if (!network_init())
struct addrinfo *addr = NULL;
int fd = socket_init((void**)&addr, port, domain, SOCKET_TYPE_STREAM);
if (fd == -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 VITA
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);
#endif
#endif
ret = connect(fd, addr->ai_addr, addr->ai_addrlen);
freeaddrinfo_retro(addr);

View File

@ -24,14 +24,13 @@
#include <net/net_compat.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};
struct addrinfo hints = {0};
struct addrinfo *addr = (struct addrinfo*)address;
if (!fd)
goto error;
struct addrinfo **addrinfo = (struct addrinfo**)address;
struct addrinfo *addr = NULL;
if (!network_init())
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;
break;
}
if (!server)
hints.ai_flags = AI_PASSIVE;
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;
addr = (struct addrinfo*)*addrinfo;
if (!addr)
goto error;
*fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
return true;
return socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
error:
return false;
return -1;
}
int socket_receive_all_blocking(int fd, void *data_, size_t size)

View File

@ -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,
uint16_t port)
{
int *file_desc = (int*)&netplay->udp_fd;
if (!socket_init(&netplay->addr, file_desc, port, server, SOCKET_TYPE_DATAGRAM))
return false;
netplay->udp_fd = socket_init((void**)&netplay->addr, port, server, SOCKET_TYPE_DATAGRAM);
if (*file_desc < 0)
if (netplay->udp_fd < 0)
{
RARCH_ERR("Failed to initialize socket.\n");
return false;