This commit is contained in:
twinaphex 2017-05-21 09:16:45 +02:00
parent f653cf4e7c
commit 0bb1de9561

View File

@ -152,14 +152,14 @@ static bool init_tcp_socket(netplay_t *netplay, void *direct_host,
} }
else else
{ {
/* I'll build my own addrinfo! With blackjack and hookers! */ /* I'll build my own addrinfo! */
struct netplay_host *host = (struct netplay_host *) direct_host; struct netplay_host *host = (struct netplay_host *)direct_host;
hints.ai_family = host->addr.sa_family; hints.ai_family = host->addr.sa_family;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0; hints.ai_protocol = 0;
hints.ai_addrlen = host->addrlen; hints.ai_addrlen = host->addrlen;
hints.ai_addr = &host->addr; hints.ai_addr = &host->addr;
res = &hints; res = &hints;
} }
@ -169,7 +169,7 @@ static bool init_tcp_socket(netplay_t *netplay, void *direct_host,
if (!direct_host && !server && res->ai_family == AF_INET6) if (!direct_host && !server && res->ai_family == AF_INET6)
{ {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) res->ai_addr; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) res->ai_addr;
sin6->sin6_addr = in6addr_any; sin6->sin6_addr = in6addr_any;
} }
#endif #endif
@ -192,8 +192,8 @@ static bool init_tcp_socket(netplay_t *netplay, void *direct_host,
if (direct_host || server) if (direct_host || server)
{ {
netplay->connections[0].active = true; netplay->connections[0].active = true;
netplay->connections[0].fd = fd; netplay->connections[0].fd = fd;
netplay->connections[0].addr = sad; netplay->connections[0].addr = sad;
} }
else else
{ {
@ -214,7 +214,8 @@ static bool init_tcp_socket(netplay_t *netplay, void *direct_host,
return ret; return ret;
} }
static bool init_socket(netplay_t *netplay, void *direct_host, const char *server, uint16_t port) static bool init_socket(netplay_t *netplay, void *direct_host,
const char *server, uint16_t port)
{ {
if (!network_init()) if (!network_init())
return false; return false;
@ -321,8 +322,8 @@ bool netplay_try_init_serialization(netplay_t *netplay)
/* Check if we can actually save */ /* Check if we can actually save */
serial_info.data_const = NULL; serial_info.data_const = NULL;
serial_info.data = netplay->buffer[netplay->run_ptr].state; serial_info.data = netplay->buffer[netplay->run_ptr].state;
serial_info.size = netplay->state_size; serial_info.size = netplay->state_size;
if (!core_serialize(&serial_info)) if (!core_serialize(&serial_info))
return false; return false;
@ -368,8 +369,7 @@ bool netplay_wait_and_init_serialization(netplay_t *netplay)
static bool netplay_init_buffers(netplay_t *netplay) static bool netplay_init_buffers(netplay_t *netplay)
{ {
if (!netplay) struct delta_frame *delta_frames = NULL;
return false;
/* Enough to get ahead or behind by MAX_STALL_FRAMES frames */ /* Enough to get ahead or behind by MAX_STALL_FRAMES frames */
netplay->buffer_size = NETPLAY_MAX_STALL_FRAMES + 1; netplay->buffer_size = NETPLAY_MAX_STALL_FRAMES + 1;
@ -379,12 +379,14 @@ static bool netplay_init_buffers(netplay_t *netplay)
if (netplay->is_server) if (netplay->is_server)
netplay->buffer_size *= 2; netplay->buffer_size *= 2;
netplay->buffer = (struct delta_frame*)calloc(netplay->buffer_size, delta_frames = (struct delta_frame*)calloc(netplay->buffer_size,
sizeof(*netplay->buffer)); sizeof(*delta_frames));
if (!netplay->buffer) if (!delta_frames)
return false; return false;
netplay->buffer = delta_frames;
if (!(netplay->quirks & (NETPLAY_QUIRK_NO_SAVESTATES|NETPLAY_QUIRK_INITIALIZATION))) if (!(netplay->quirks & (NETPLAY_QUIRK_NO_SAVESTATES|NETPLAY_QUIRK_INITIALIZATION)))
netplay_init_serialization(netplay); netplay_init_serialization(netplay);
@ -417,36 +419,38 @@ netplay_t *netplay_new(void *direct_host, const char *server, uint16_t port,
if (!netplay) if (!netplay)
return NULL; return NULL;
netplay->listen_fd = -1; netplay->listen_fd = -1;
netplay->tcp_port = port; netplay->tcp_port = port;
netplay->cbs = *cb; netplay->cbs = *cb;
netplay->connected_players = 0; netplay->connected_players = 0;
netplay->player_max = 1; netplay->player_max = 1;
netplay->is_server = (direct_host == NULL && server == NULL); netplay->is_server = (direct_host == NULL && server == NULL);
netplay->is_connected = false;; netplay->is_connected = false;;
netplay->nat_traversal = netplay->is_server ? nat_traversal : false; netplay->nat_traversal = netplay->is_server ? nat_traversal : false;
netplay->stateless_mode = stateless_mode; netplay->stateless_mode = stateless_mode;
netplay->check_frames = check_frames; netplay->check_frames = check_frames;
netplay->crc_validity_checked = false; netplay->crc_validity_checked = false;
netplay->crcs_valid = true; netplay->crcs_valid = true;
netplay->quirks = quirks; netplay->quirks = quirks;
netplay->self_mode = netplay->is_server ? netplay->self_mode = netplay->is_server ?
NETPLAY_CONNECTION_SPECTATING : NETPLAY_CONNECTION_SPECTATING :
NETPLAY_CONNECTION_NONE; NETPLAY_CONNECTION_NONE;
if (netplay->is_server) if (netplay->is_server)
{ {
netplay->connections = NULL; netplay->connections = NULL;
netplay->connections_size = 0; netplay->connections_size = 0;
} }
else else
{ {
netplay->connections = &netplay->one_connection; netplay->connections = &netplay->one_connection;
netplay->connections_size = 1; netplay->connections_size = 1;
netplay->connections[0].fd = -1; netplay->connections[0].fd = -1;
} }
strlcpy(netplay->nick, nick[0] ? nick : RARCH_DEFAULT_NICK, sizeof(netplay->nick)); strlcpy(netplay->nick, nick[0]
? nick : RARCH_DEFAULT_NICK,
sizeof(netplay->nick));
if (!init_socket(netplay, direct_host, server, port)) if (!init_socket(netplay, direct_host, server, port))
{ {
@ -463,11 +467,13 @@ netplay_t *netplay_new(void *direct_host, const char *server, uint16_t port,
if (!netplay->is_server) if (!netplay->is_server)
{ {
netplay_handshake_init_send(netplay, &netplay->connections[0]); netplay_handshake_init_send(netplay, &netplay->connections[0]);
netplay->connections[0].mode = netplay->self_mode = NETPLAY_CONNECTION_INIT;
netplay->connections[0].mode = NETPLAY_CONNECTION_INIT;
netplay->self_mode = NETPLAY_CONNECTION_INIT;
} }
/* FIXME: Not really the right place to do this, socket initialization needs /* FIXME: Not really the right place to do this,
* to be fixed in general */ * socket initialization needs to be fixed in general. */
if (netplay->is_server) if (netplay->is_server)
{ {
if (!socket_nonblock(netplay->listen_fd)) if (!socket_nonblock(netplay->listen_fd))