/** * netplay_new: * @server : IP address of server. * @port : Port of server. * @frames : Amount of lag frames. * @check_frames : Frequency with which to check CRCs. * @cb : Libretro callbacks. * @spectate : If true, enable spectator mode. * @nick : Nickname of user. * @quirks : Netplay quirks required for this session. * * Creates a new netplay handle. A NULL host means we're * hosting (user 1). * * Returns: new netplay handle. **/ netplay_t *netplay_new(const char *server, uint16_t port, unsigned frames, unsigned check_frames, const struct retro_callbacks *cb, bool spectate, const char *nick, uint64_t quirks) { netplay_t *netplay = (netplay_t*)calloc(1, sizeof(*netplay)); if (!netplay) return NULL; netplay->fd = -1; netplay->tcp_port = port; netplay->cbs = *cb; netplay->port = server ? 0 : 1; netplay->spectate.enabled = spectate; netplay->is_server = server == NULL; netplay->stall_frames = frames; netplay->check_frames = check_frames; netplay->quirks = quirks; strlcpy(netplay->nick, nick, sizeof(netplay->nick)); if (!netplay_init_buffers(netplay, frames)) { free(netplay); return NULL; } if(spectate) netplay->net_cbs = netplay_get_cbs_spectate(); else netplay->net_cbs = netplay_get_cbs_net(); if (!init_socket(netplay, server, port)) { free(netplay); return NULL; } if(!netplay_info_cb(netplay, frames)) goto error; return netplay; error: if (netplay->fd >= 0) socket_close(netplay->fd); free(netplay); return NULL; }
/** * netplay_new: * @direct_host : Netplay host discovered from scanning. * @server : IP address of server. * @port : Port of server. * @stateless_mode : Shall we use stateless mode? * @check_frames : Frequency with which to check CRCs. * @cb : Libretro callbacks. * @nat_traversal : If true, attempt NAT traversal. * @nick : Nickname of user. * @quirks : Netplay quirks required for this session. * * Creates a new netplay handle. A NULL server means we're * hosting. * * Returns: new netplay data. */ netplay_t *netplay_new(void *direct_host, const char *server, uint16_t port, bool stateless_mode, int check_frames, const struct retro_callbacks *cb, bool nat_traversal, const char *nick, uint64_t quirks) { netplay_t *netplay = (netplay_t*)calloc(1, sizeof(*netplay)); if (!netplay) return NULL; netplay->listen_fd = -1; netplay->tcp_port = port; netplay->cbs = *cb; netplay->connected_players = 0; netplay->player_max = 1; netplay->is_server = (direct_host == NULL && server == NULL); netplay->nat_traversal = netplay->is_server ? nat_traversal : false; netplay->stateless_mode = stateless_mode; netplay->check_frames = check_frames; netplay->crc_validity_checked = false; netplay->crcs_valid = true; netplay->quirks = quirks; netplay->self_mode = netplay->is_server ? NETPLAY_CONNECTION_PLAYING : NETPLAY_CONNECTION_NONE; if (netplay->is_server) { netplay->connections = NULL; netplay->connections_size = 0; } else { netplay->connections = &netplay->one_connection; netplay->connections_size = 1; netplay->connections[0].fd = -1; } strlcpy(netplay->nick, nick[0] ? nick : RARCH_DEFAULT_NICK, sizeof(netplay->nick)); if (!init_socket(netplay, direct_host, server, port)) { free(netplay); return NULL; } if (!netplay_init_buffers(netplay)) { free(netplay); return NULL; } if (!netplay->is_server) { netplay_handshake_init_send(netplay, &netplay->connections[0]); netplay->connections[0].mode = netplay->self_mode = NETPLAY_CONNECTION_INIT; } /* FIXME: Not really the right place to do this, socket initialization needs * to be fixed in general */ if (netplay->is_server) { if (!socket_nonblock(netplay->listen_fd)) goto error; } else { if (!socket_nonblock(netplay->connections[0].fd)) goto error; } return netplay; error: if (netplay->listen_fd >= 0) socket_close(netplay->listen_fd); if (netplay->connections && netplay->connections[0].fd >= 0) socket_close(netplay->connections[0].fd); free(netplay); return NULL; }