netplay_t *netplay_new(const char *server, uint16_t port, unsigned frames, const struct retro_callbacks *cb, bool spectate, const char *nick) { unsigned i; if (frames > UDP_FRAME_PACKETS) frames = UDP_FRAME_PACKETS; netplay_t *handle = (netplay_t*)calloc(1, sizeof(*handle)); if (!handle) return NULL; handle->fd = -1; handle->udp_fd = -1; handle->cbs = *cb; handle->port = server ? 0 : 1; handle->spectate = spectate; handle->spectate_client = server != NULL; strlcpy(handle->nick, nick, sizeof(handle->nick)); if (!init_socket(handle, server, port)) { free(handle); return NULL; } if (spectate) { if (server) { if (!get_info_spectate(handle)) goto error; } for (i = 0; i < MAX_SPECTATORS; i++) handle->spectate_fds[i] = -1; } else { if (server) { if (!send_info(handle)) goto error; } else { if (!get_info(handle)) goto error; } handle->buffer_size = frames + 1; init_buffers(handle); handle->has_connection = true; } return handle; error: if (handle->fd >= 0) close(handle->fd); if (handle->udp_fd >= 0) close(handle->udp_fd); free(handle); return NULL; }
/** * netplay_new: * @server : IP address of server. * @port : Port of server. * @frames : Amount of lag frames. * @cb : Libretro callbacks. * @spectate : If true, enable spectator mode. * @nick : Nickname of user. * * 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, const struct retro_callbacks *cb, bool spectate, const char *nick) { unsigned i; netplay_t *netplay = NULL; if (frames > UDP_FRAME_PACKETS) frames = UDP_FRAME_PACKETS; netplay = (netplay_t*)calloc(1, sizeof(*netplay)); if (!netplay) return NULL; netplay->fd = -1; netplay->udp_fd = -1; netplay->cbs = *cb; netplay->port = server ? 0 : 1; netplay->spectate.enabled = spectate; netplay->spectate.client = server != NULL; strlcpy(netplay->nick, nick, sizeof(netplay->nick)); if (!init_socket(netplay, server, port)) { free(netplay); return NULL; } if (spectate) { if (server) { if (!get_info_spectate(netplay)) goto error; } for (i = 0; i < MAX_SPECTATORS; i++) netplay->spectate.fds[i] = -1; } else { if (server) { if (!send_info(netplay)) goto error; } else { if (!get_info(netplay)) goto error; } netplay->buffer_size = frames + 1; if (!init_buffers(netplay)) goto error; netplay->has_connection = true; } return netplay; error: if (netplay->fd >= 0) socket_close(netplay->fd); if (netplay->udp_fd >= 0) socket_close(netplay->udp_fd); free(netplay); return NULL; }