RetroArch/netplay.c

1847 lines
46 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 16:46:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2011-02-13 15:40:24 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-02-13 15:40:24 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-02-13 15:40:24 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-02-13 15:40:24 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
2011-12-04 17:03:08 +00:00
2012-11-15 13:32:06 +00:00
#if defined(_MSC_VER) && !defined(_XBOX)
2012-11-15 08:40:31 +00:00
#pragma comment(lib, "ws2_32")
#endif
2015-11-17 23:18:04 +00:00
#include <assert.h>
#include <stdlib.h>
#include <string.h>
2015-09-04 19:11:00 +00:00
#include <net/net_compat.h>
#include <retro_endianness.h>
2015-09-04 19:11:00 +00:00
2011-10-18 17:22:20 +00:00
#include "netplay.h"
#include "general.h"
#include "autosave.h"
#include "dynamic.h"
2015-07-02 16:39:57 +00:00
#include "msg_hash.h"
2015-06-25 09:02:01 +00:00
#include "system.h"
2015-11-17 23:18:04 +00:00
#include "runloop.h"
2015-11-23 11:03:38 +00:00
#include "verbosity.h"
2011-10-18 17:22:20 +00:00
struct delta_frame
{
2012-04-07 09:55:37 +00:00
void *state;
uint16_t real_input_state;
uint16_t simulated_input_state;
uint16_t self_state;
bool is_simulated;
bool used_real;
};
#define UDP_FRAME_PACKETS 16
2012-01-11 18:22:18 +00:00
#define MAX_SPECTATORS 16
2015-04-11 00:49:30 +00:00
#define PREV_PTR(x) ((x) == 0 ? netplay->buffer_size - 1 : (x) - 1)
#define NEXT_PTR(x) ((x + 1) % netplay->buffer_size)
2011-02-13 15:40:24 +00:00
struct netplay
{
2012-01-21 17:12:42 +00:00
char nick[32];
char other_nick[32];
struct sockaddr_storage other_addr;
2012-04-07 09:55:37 +00:00
struct retro_callbacks cbs;
2014-09-07 03:47:18 +00:00
/* TCP connection for state sending, etc. Also used for commands */
int fd;
/* UDP connection for game state updates. */
int udp_fd;
2014-12-05 12:48:54 +00:00
/* Which port is governed by netplay (other user)? */
2014-09-07 03:47:18 +00:00
unsigned port;
2011-02-13 15:40:24 +00:00
bool has_connection;
struct delta_frame *buffer;
size_t buffer_size;
2014-09-15 05:03:54 +00:00
/* Pointer where we are now. */
size_t self_ptr;
/* Points to the last reliable state that self ever had. */
size_t other_ptr;
/* Pointer to where we are reading.
* Generally, other_ptr <= read_ptr <= self_ptr. */
size_t read_ptr;
/* A temporary pointer used on replay. */
size_t tmp_ptr;
size_t state_size;
2014-09-15 05:03:54 +00:00
/* Are we replaying old frames? */
bool is_replay;
/* We don't want to poll several times on a frame. */
bool can_poll;
2014-09-15 05:03:54 +00:00
/* To compat UDP packet loss we also send
* old data along with the packets. */
2014-09-07 03:47:18 +00:00
uint32_t packet_buffer[UDP_FRAME_PACKETS * 2];
uint32_t frame_count;
uint32_t read_frame_count;
2011-10-05 00:09:54 +00:00
uint32_t other_frame_count;
2012-01-21 15:14:10 +00:00
uint32_t tmp_frame_count;
struct addrinfo *addr;
struct sockaddr_storage their_addr;
bool has_client_addr;
unsigned timeout_cnt;
2012-01-11 18:22:18 +00:00
2014-09-15 05:03:54 +00:00
/* Spectating. */
struct {
bool enabled;
bool client;
int fds[MAX_SPECTATORS];
uint16_t *input;
size_t input_ptr;
size_t input_sz;
} spectate;
2012-01-21 13:00:11 +00:00
2015-01-05 01:03:17 +00:00
/* User flipping
2014-09-15 05:03:54 +00:00
* Flipping state. If ptr >= flip_frame, we apply the flip.
* If not, we apply the opposite, effectively creating a trigger point.
* To avoid collition we need to make sure our client/host is synced up
* well after flip_frame before allowing another flip. */
2012-01-21 13:00:11 +00:00
bool flip;
uint32_t flip_frame;
2015-11-17 23:18:04 +00:00
/* Netplay pausing
*/
bool pause;
uint32_t pause_frame;
};
2015-11-19 12:03:23 +00:00
enum
{
2015-11-17 23:18:04 +00:00
CMD_OPT_ALLOWED_IN_SPECTATE_MODE = 0x1,
CMD_OPT_REQUIRE_ACK = 0x2,
CMD_OPT_HOST_ONLY = 0x4,
CMD_OPT_CLIENT_ONLY = 0x8,
2015-11-19 12:03:23 +00:00
CMD_OPT_REQUIRE_SYNC = 0x10
2011-02-13 15:40:24 +00:00
};
2015-01-09 17:34:00 +00:00
/**
* warn_hangup:
*
* Warns that netplay has disconnected.
**/
static void warn_hangup(void)
{
2012-04-21 21:25:32 +00:00
RARCH_WARN("Netplay has disconnected. Will continue without connection ...\n");
rarch_main_msg_queue_push("Netplay has disconnected. Will continue without connection.", 0, 480, false);
}
2015-11-17 23:18:04 +00:00
/**
* check_netplay_synched:
* @netplay: pointer to the netplay object.
* Checks to see if the host and client have synchronized states. Returns true
* on success and false on failure.
*/
bool check_netplay_synched(netplay_t* netplay)
{
assert(netplay);
return netplay->frame_count < (netplay->flip_frame + 2 * UDP_FRAME_PACKETS);
}
2015-01-09 17:34:00 +00:00
/**
* netplay_should_skip:
* @netplay : pointer to netplay object
*
* If we're fast-forward replaying to resync, check if we
* should actually show frame.
*
* Returns: bool (1) if we should skip this frame, otherwise
* false (0).
**/
static bool netplay_should_skip(netplay_t *netplay)
2011-02-13 15:40:24 +00:00
{
if (!netplay)
return false;
return netplay->is_replay && netplay->has_connection;
2011-02-13 15:40:24 +00:00
}
static bool netplay_can_poll(netplay_t *netplay)
2011-02-13 15:40:24 +00:00
{
if (!netplay)
return false;
return netplay->can_poll;
2011-02-13 15:40:24 +00:00
}
static bool send_chunk(netplay_t *netplay)
2011-02-13 15:40:24 +00:00
{
const struct sockaddr *addr = NULL;
2015-01-09 17:34:00 +00:00
if (netplay->addr)
addr = netplay->addr->ai_addr;
else if (netplay->has_client_addr)
addr = (const struct sockaddr*)&netplay->their_addr;
2011-02-13 15:40:24 +00:00
if (addr)
{
if (sendto(netplay->udp_fd, (const char*)netplay->packet_buffer,
sizeof(netplay->packet_buffer), 0, addr,
2015-09-15 03:48:05 +00:00
#ifdef ANDROID
sizeof(struct sockaddr_in6)
#else
sizeof(struct sockaddr_in)
#endif
)
!= sizeof(netplay->packet_buffer))
{
warn_hangup();
netplay->has_connection = false;
return false;
}
}
return true;
2012-04-07 09:55:37 +00:00
}
2015-01-09 17:34:00 +00:00
/**
* get_self_input_state:
* @netplay : pointer to netplay object
*
* Grab our own input state and send this over the network.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool get_self_input_state(netplay_t *netplay)
2011-02-13 15:40:24 +00:00
{
2015-03-20 19:43:22 +00:00
uint32_t state = 0;
struct delta_frame *ptr = &netplay->buffer[netplay->self_ptr];
2015-03-20 19:43:22 +00:00
settings_t *settings = config_get_ptr();
2011-02-13 15:40:24 +00:00
if (!input_driver_ctl(RARCH_INPUT_CTL_IS_LIBRETRO_INPUT_BLOCKED, NULL) && netplay->frame_count > 0)
{
2015-09-29 16:08:33 +00:00
unsigned i;
/* First frame we always give zero input since relying on
* input from first frame screws up when we use -F 0. */
retro_input_state_t cb = netplay->cbs.state_cb;
2015-11-17 04:02:15 +00:00
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
int16_t tmp = cb(settings->input.netplay_client_swap_input ?
0 : !netplay->port,
RETRO_DEVICE_JOYPAD, 0, i);
state |= tmp ? 1 << i : 0;
}
for (i = RARCH_FIRST_CUSTOM_BIND; i < RARCH_FIRST_META_KEY; i++)
{
2015-04-11 00:49:30 +00:00
int16_t tmp = cb(settings->input.netplay_client_swap_input ?
0 : !netplay->port,
RETRO_DEVICE_ANALOG, 0, i);
2015-04-11 00:49:30 +00:00
state |= tmp ? 1 << i : 0;
}
}
memmove(netplay->packet_buffer, netplay->packet_buffer + 2,
sizeof (netplay->packet_buffer) - 2 * sizeof(uint32_t));
2015-04-11 00:49:30 +00:00
netplay->packet_buffer[(UDP_FRAME_PACKETS - 1) * 2] = htonl(netplay->frame_count);
netplay->packet_buffer[(UDP_FRAME_PACKETS - 1) * 2 + 1] = htonl(state);
2012-01-21 13:58:39 +00:00
if (!send_chunk(netplay))
{
warn_hangup();
netplay->has_connection = false;
return false;
}
2015-04-11 00:49:30 +00:00
ptr->self_state = state;
netplay->self_ptr = NEXT_PTR(netplay->self_ptr);
return true;
}
static bool netplay_cmd_ack(netplay_t *netplay)
{
uint32_t cmd = htonl(NETPLAY_CMD_ACK);
return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd));
}
static bool netplay_cmd_nak(netplay_t *netplay)
{
uint32_t cmd = htonl(NETPLAY_CMD_NAK);
return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd));
}
static bool netplay_get_response(netplay_t *netplay)
{
uint32_t response;
if (!socket_receive_all_blocking(netplay->fd, &response, sizeof(response)))
return false;
return ntohl(response) == NETPLAY_CMD_ACK;
}
static bool netplay_get_cmd(netplay_t *netplay)
{
uint32_t cmd;
uint32_t flip_frame;
size_t cmd_size;
if (!socket_receive_all_blocking(netplay->fd, &cmd, sizeof(cmd)))
return false;
2015-04-11 00:49:30 +00:00
cmd = ntohl(cmd);
cmd_size = cmd & 0xffff;
2015-03-16 01:17:04 +00:00
cmd = cmd >> 16;
switch (cmd)
{
case NETPLAY_CMD_FLIP_PLAYERS:
if (cmd_size != sizeof(uint32_t))
{
RARCH_ERR("CMD_FLIP_PLAYERS recieved an unexpected command size.\n");
return netplay_cmd_nak(netplay);
}
if (!socket_receive_all_blocking(netplay->fd, &flip_frame, sizeof(flip_frame)))
{
RARCH_ERR("Failed to receive CMD_FLIP_PLAYERS argument.\n");
return netplay_cmd_nak(netplay);
}
flip_frame = ntohl(flip_frame);
if (flip_frame < netplay->flip_frame)
{
RARCH_ERR("Host asked us to flip users in the past. Not possible ...\n");
return netplay_cmd_nak(netplay);
}
netplay->flip ^= true;
netplay->flip_frame = flip_frame;
RARCH_LOG("Netplay users are flipped.\n");
rarch_main_msg_queue_push("Netplay users are flipped.", 1, 180, false);
return netplay_cmd_ack(netplay);
2015-11-17 23:18:04 +00:00
case NETPLAY_CMD_SPECTATE:
RARCH_ERR("NETPLAY_CMD_SPECTATE unimplemented.\n");
return netplay_cmd_nak(netplay);
case NETPLAY_CMD_DISCONNECT:
warn_hangup();
return netplay_cmd_ack(netplay);
case NETPLAY_CMD_LOAD_SAVESTATE:
RARCH_ERR("NETPLAY_CMD_LOAD_SAVESTATE unimplemented.\n");
return netplay_cmd_nak(netplay);
case NETPLAY_CMD_PAUSE:
event_command(EVENT_CMD_PAUSE);
return netplay_cmd_ack(netplay);
case NETPLAY_CMD_RESUME:
event_command(EVENT_CMD_UNPAUSE);
return netplay_cmd_ack(netplay);
default: break;
}
RARCH_ERR("Unknown netplay command received.\n");
return netplay_cmd_nak(netplay);
}
#define MAX_RETRIES 16
#define RETRY_MS 500
static int poll_input(netplay_t *netplay, bool block)
2011-02-13 15:40:24 +00:00
{
2015-04-01 15:59:34 +00:00
int max_fd = (netplay->fd > netplay->udp_fd ? netplay->fd : netplay->udp_fd) + 1;
2015-04-11 00:49:30 +00:00
struct timeval tv = {0};
2015-03-16 01:17:04 +00:00
tv.tv_sec = 0;
tv.tv_usec = block ? (RETRY_MS * 1000) : 0;
do
{
2015-01-23 19:01:25 +00:00
fd_set fds;
/* select() does not take pointer to const struct timeval.
* Technically possible for select() to modify tmp_tv, so
* we go paranoia mode. */
struct timeval tmp_tv = tv;
2015-01-23 19:01:25 +00:00
netplay->timeout_cnt++;
FD_ZERO(&fds);
FD_SET(netplay->udp_fd, &fds);
FD_SET(netplay->fd, &fds);
if (socket_select(max_fd, &fds, NULL, NULL, &tmp_tv) < 0)
return -1;
/* Somewhat hacky,
* but we aren't using the TCP connection for anything useful atm. */
if (FD_ISSET(netplay->fd, &fds) && !netplay_get_cmd(netplay))
return -1;
if (FD_ISSET(netplay->udp_fd, &fds))
return 1;
2015-01-23 19:01:25 +00:00
if (!block)
continue;
if (!send_chunk(netplay))
{
warn_hangup();
netplay->has_connection = false;
return -1;
}
2015-01-23 19:01:25 +00:00
RARCH_LOG("Network is stalling, resending packet... Count %u of %d ...\n",
netplay->timeout_cnt, MAX_RETRIES);
} while ((netplay->timeout_cnt < MAX_RETRIES) && block);
if (block)
return -1;
return 0;
}
static bool receive_data(netplay_t *netplay, uint32_t *buffer, size_t size)
{
socklen_t addrlen = sizeof(netplay->their_addr);
if (recvfrom(netplay->udp_fd, (char*)buffer, size, 0,
(struct sockaddr*)&netplay->their_addr, &addrlen) != (ssize_t)size)
return false;
2015-03-16 01:17:04 +00:00
netplay->has_client_addr = true;
2015-03-16 01:17:04 +00:00
return true;
}
static void parse_packet(netplay_t *netplay, uint32_t *buffer, unsigned size)
{
unsigned i;
2015-01-23 08:00:53 +00:00
for (i = 0; i < size * 2; i++)
buffer[i] = ntohl(buffer[i]);
for (i = 0; i < size && netplay->read_frame_count <= netplay->frame_count; i++)
{
uint32_t frame = buffer[2 * i + 0];
uint32_t state = buffer[2 * i + 1];
2015-01-23 07:51:22 +00:00
if (frame != netplay->read_frame_count)
continue;
2015-04-11 00:49:30 +00:00
netplay->buffer[netplay->read_ptr].is_simulated = false;
2015-01-23 07:51:22 +00:00
netplay->buffer[netplay->read_ptr].real_input_state = state;
2015-04-11 00:49:30 +00:00
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
2015-01-23 07:51:22 +00:00
netplay->read_frame_count++;
netplay->timeout_cnt = 0;
}
}
/* TODO: Somewhat better prediction. :P */
static void simulate_input(netplay_t *netplay)
{
2015-04-11 00:49:30 +00:00
size_t ptr = PREV_PTR(netplay->self_ptr);
size_t prev = PREV_PTR(netplay->read_ptr);
netplay->buffer[ptr].simulated_input_state =
netplay->buffer[prev].real_input_state;
2015-04-11 00:49:30 +00:00
netplay->buffer[ptr].is_simulated = true;
netplay->buffer[ptr].used_real = false;
}
2015-01-09 17:34:00 +00:00
/**
* netplay_poll:
* @netplay : pointer to netplay object
*
* Polls network to see if we have anything new. If our
* network buffer is full, we simply have to block
* for new input data.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool netplay_poll(netplay_t *netplay)
{
int res;
if (!netplay->has_connection)
return false;
netplay->can_poll = false;
if (!get_self_input_state(netplay))
return false;
/* We skip reading the first frame so the host has a chance to grab
* our host info so we don't block forever :') */
if (netplay->frame_count == 0)
{
2015-03-16 01:17:04 +00:00
netplay->buffer[0].used_real = true;
netplay->buffer[0].is_simulated = false;
netplay->buffer[0].real_input_state = 0;
2015-04-11 00:49:30 +00:00
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
netplay->read_frame_count++;
return true;
}
/* We might have reached the end of the buffer, where we
* simply have to block. */
res = poll_input(netplay, netplay->other_ptr == netplay->self_ptr);
if (res == -1)
{
netplay->has_connection = false;
warn_hangup();
return false;
}
if (res == 1)
{
uint32_t first_read = netplay->read_frame_count;
do
{
uint32_t buffer[UDP_FRAME_PACKETS * 2];
if (!receive_data(netplay, buffer, sizeof(buffer)))
{
warn_hangup();
netplay->has_connection = false;
return false;
}
parse_packet(netplay, buffer, UDP_FRAME_PACKETS);
2015-04-11 00:49:30 +00:00
} while ((netplay->read_frame_count <= netplay->frame_count) &&
poll_input(netplay, (netplay->other_ptr == netplay->self_ptr) &&
(first_read == netplay->read_frame_count)) == 1);
}
else
{
/* Cannot allow this. Should not happen though. */
if (netplay->self_ptr == netplay->other_ptr)
{
warn_hangup();
return false;
}
}
if (netplay->read_ptr != netplay->self_ptr)
simulate_input(netplay);
else
2015-04-11 00:49:30 +00:00
netplay->buffer[PREV_PTR(netplay->self_ptr)].used_real = true;
return true;
}
void input_poll_net(void)
{
2015-04-11 00:49:30 +00:00
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay) && netplay_can_poll(netplay))
netplay_poll(netplay);
}
void video_frame_net(const void *data, unsigned width,
unsigned height, size_t pitch)
{
2015-04-11 00:49:30 +00:00
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay))
netplay->cbs.frame_cb(data, width, height, pitch);
}
void audio_sample_net(int16_t left, int16_t right)
{
2015-04-11 00:49:30 +00:00
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay))
netplay->cbs.sample_cb(left, right);
}
size_t audio_sample_batch_net(const int16_t *data, size_t frames)
{
2015-04-11 00:49:30 +00:00
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay))
return netplay->cbs.sample_batch_cb(data, frames);
return frames;
}
2015-01-09 17:34:00 +00:00
/**
* netplay_is_alive:
* @netplay : pointer to netplay object
*
* Checks if input port/index is controlled by netplay or not.
*
* Returns: true (1) if alive, otherwise false (0).
**/
static bool netplay_is_alive(netplay_t *netplay)
{
if (!netplay)
return false;
return netplay->has_connection;
}
static bool netplay_flip_port(netplay_t *netplay, bool port)
{
size_t frame = netplay->frame_count;
if (netplay->flip_frame == 0)
return port;
if (netplay->is_replay)
frame = netplay->tmp_frame_count;
return port ^ netplay->flip ^ (frame < netplay->flip_frame);
}
static int16_t netplay_input_state(netplay_t *netplay, bool port, unsigned device,
unsigned idx, unsigned id)
{
size_t ptr = netplay->is_replay ?
2015-04-11 00:49:30 +00:00
netplay->tmp_ptr : PREV_PTR(netplay->self_ptr);
uint16_t curr_input_state = netplay->buffer[ptr].self_state;
if (netplay->port == (netplay_flip_port(netplay, port) ? 1 : 0))
{
if (netplay->buffer[ptr].is_simulated)
curr_input_state = netplay->buffer[ptr].simulated_input_state;
else
curr_input_state = netplay->buffer[ptr].real_input_state;
}
return ((1 << id) & curr_input_state) ? 1 : 0;
}
int16_t input_state_net(unsigned port, unsigned device,
unsigned idx, unsigned id)
{
2015-04-11 00:49:30 +00:00
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (netplay_is_alive(netplay))
return netplay_input_state(netplay, port, device, idx, id);
return netplay->cbs.state_cb(port, device, idx, id);
}
#ifndef HAVE_SOCKET_LEGACY
/* Custom inet_ntop. Win32 doesn't seem to support this ... */
static void log_connection(const struct sockaddr_storage *their_addr,
unsigned slot, const char *nick)
{
union
{
const struct sockaddr_storage *storage;
const struct sockaddr_in *v4;
const struct sockaddr_in6 *v6;
} u;
2015-06-12 15:00:37 +00:00
const char *str = NULL;
char buf_v4[INET_ADDRSTRLEN] = {0};
char buf_v6[INET6_ADDRSTRLEN] = {0};
2015-01-23 08:00:53 +00:00
u.storage = their_addr;
if (their_addr->ss_family == AF_INET)
{
struct sockaddr_in in;
2015-01-23 08:00:53 +00:00
str = buf_v4;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, &u.v4->sin_addr, sizeof(struct in_addr));
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in),
buf_v4, sizeof(buf_v4),
NULL, 0, NI_NUMERICHOST);
}
else if (their_addr->ss_family == AF_INET6)
{
struct sockaddr_in6 in;
2015-01-23 08:00:53 +00:00
str = buf_v6;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, &u.v6->sin6_addr, sizeof(struct in6_addr));
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in6),
buf_v6, sizeof(buf_v6), NULL, 0, NI_NUMERICHOST);
}
if (str)
{
2015-06-12 15:00:37 +00:00
char msg[512] = {0};
2015-06-13 01:06:11 +00:00
snprintf(msg, sizeof(msg), "Got connection from: \"%s (%s)\" (#%u)",
nick, str, slot);
rarch_main_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
}
}
#endif
static int init_tcp_connection(const struct addrinfo *res,
bool server, bool spectate,
struct sockaddr *other_addr, socklen_t addr_size)
{
bool ret = true;
2015-04-11 00:49:30 +00:00
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2015-01-23 08:00:53 +00:00
if (fd < 0)
{
ret = false;
goto end;
}
if (server)
{
if (connect(fd, res->ai_addr, res->ai_addrlen) < 0)
{
ret = false;
goto end;
}
}
else
{
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(int));
if (bind(fd, res->ai_addr, res->ai_addrlen) < 0 ||
listen(fd, spectate ? MAX_SPECTATORS : 1) < 0)
{
ret = false;
goto end;
}
if (!spectate)
{
int new_fd = accept(fd, other_addr, &addr_size);
if (new_fd < 0)
{
ret = false;
goto end;
}
socket_close(fd);
fd = new_fd;
}
}
end:
if (!ret && fd >= 0)
{
socket_close(fd);
fd = -1;
}
return fd;
}
static bool init_tcp_socket(netplay_t *netplay, const char *server,
uint16_t port, bool spectate)
{
2015-06-12 15:00:37 +00:00
char port_buf[16] = {0};
bool ret = false;
2015-01-23 07:51:22 +00:00
const struct addrinfo *tmp_info = NULL;
2015-06-12 15:00:37 +00:00
struct addrinfo hints, *res = NULL;
2015-01-23 08:00:53 +00:00
memset(&hints, 0, sizeof(hints));
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET;
2011-02-13 15:40:24 +00:00
#else
hints.ai_family = AF_UNSPEC;
#endif
2011-02-13 15:40:24 +00:00
hints.ai_socktype = SOCK_STREAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo_retro(server, port_buf, &hints, &res) < 0)
2011-02-13 15:40:24 +00:00
return false;
if (!res)
return false;
2014-09-07 03:47:18 +00:00
/* If "localhost" is used, it is important to check every possible
* address for IPv4/IPv6. */
2015-01-23 07:51:22 +00:00
tmp_info = res;
while (tmp_info)
2011-02-13 15:40:24 +00:00
{
int fd;
if ((fd = init_tcp_connection(tmp_info, server, netplay->spectate.enabled,
(struct sockaddr*)&netplay->other_addr,
sizeof(netplay->other_addr))) >= 0)
2011-02-13 15:40:24 +00:00
{
ret = true;
netplay->fd = fd;
break;
2011-02-13 15:40:24 +00:00
}
2012-01-11 18:22:18 +00:00
tmp_info = tmp_info->ai_next;
2012-01-11 18:22:18 +00:00
}
2011-02-13 19:24:54 +00:00
if (res)
freeaddrinfo_retro(res);
if (!ret)
RARCH_ERR("Failed to set up netplay sockets.\n");
2011-02-13 15:40:24 +00:00
return ret;
}
static bool init_udp_socket(netplay_t *netplay, const char *server,
2014-09-07 03:47:18 +00:00
uint16_t port)
{
2015-06-12 15:00:37 +00:00
char port_buf[16] = {0};
struct addrinfo hints = {0};
2015-01-23 07:51:22 +00:00
memset(&hints, 0, sizeof(hints));
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_DGRAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
2015-01-23 08:00:53 +00:00
if (getaddrinfo_retro(server, port_buf, &hints, &netplay->addr) < 0)
return false;
if (!netplay->addr)
return false;
netplay->udp_fd = socket(netplay->addr->ai_family,
netplay->addr->ai_socktype, netplay->addr->ai_protocol);
2014-09-07 03:47:18 +00:00
if (netplay->udp_fd < 0)
{
RARCH_ERR("Failed to initialize socket.\n");
return false;
}
if (!server)
{
2014-09-07 03:47:18 +00:00
/* Not sure if we have to do this for UDP, but hey :) */
int yes = 1;
2015-01-23 07:51:22 +00:00
setsockopt(netplay->udp_fd, SOL_SOCKET, SO_REUSEADDR,
(const char*)&yes, sizeof(int));
if (bind(netplay->udp_fd, netplay->addr->ai_addr,
netplay->addr->ai_addrlen) < 0)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to bind socket.\n");
socket_close(netplay->udp_fd);
netplay->udp_fd = -1;
}
freeaddrinfo_retro(netplay->addr);
netplay->addr = NULL;
}
2011-02-13 15:40:24 +00:00
return true;
}
static bool init_socket(netplay_t *netplay, const char *server, uint16_t port)
{
if (!network_init())
return false;
if (!init_tcp_socket(netplay, server, port, netplay->spectate.enabled))
return false;
if (!netplay->spectate.enabled && !init_udp_socket(netplay, server, port))
return false;
return true;
}
2015-01-09 17:34:00 +00:00
/**
* implementation_magic_value:
*
* Not really a hash, but should be enough to differentiate
2014-09-07 03:47:18 +00:00
* implementations from each other.
*
* Subtle differences in the implementation will not be possible to spot.
* The alternative would have been checking serialization sizes, but it
* was troublesome for cross platform compat.
2015-01-09 17:34:00 +00:00
**/
static uint32_t implementation_magic_value(void)
{
size_t i, len;
2015-06-25 09:02:01 +00:00
uint32_t res = 0;
const char *ver = PACKAGE_VERSION;
unsigned api = core.retro_api_version();
rarch_system_info_t *info = rarch_system_info_get_ptr();
const char *lib = info ? info->info.library_name : NULL;
2012-01-21 17:12:42 +00:00
2012-04-07 09:55:37 +00:00
res |= api;
len = strlen(lib);
2013-10-22 19:26:33 +00:00
for (i = 0; i < len; i++)
res ^= lib[i] << (i & 0xf);
lib = info->info.library_version;
2012-04-07 09:55:37 +00:00
len = strlen(lib);
2015-03-21 03:43:18 +00:00
2013-10-22 19:26:33 +00:00
for (i = 0; i < len; i++)
2012-04-07 09:55:37 +00:00
res ^= lib[i] << (i & 0xf);
2012-01-21 17:12:42 +00:00
len = strlen(ver);
2013-10-22 19:26:33 +00:00
for (i = 0; i < len; i++)
2012-01-21 17:12:42 +00:00
res ^= ver[i] << ((i & 0xf) + 16);
return res;
}
static bool send_nickname(netplay_t *netplay, int fd)
2012-01-21 17:12:42 +00:00
{
uint8_t nick_size = strlen(netplay->nick);
2012-01-21 17:12:42 +00:00
if (!socket_send_all_blocking(fd, &nick_size, sizeof(nick_size)))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send nick size.\n");
2012-01-21 17:12:42 +00:00
return false;
}
if (!socket_send_all_blocking(fd, netplay->nick, nick_size))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send nick.\n");
2012-01-21 17:12:42 +00:00
return false;
}
return true;
}
static bool get_nickname(netplay_t *netplay, int fd)
2012-01-21 17:12:42 +00:00
{
uint8_t nick_size;
if (!socket_receive_all_blocking(fd, &nick_size, sizeof(nick_size)))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to receive nick size from host.\n");
2012-01-21 17:12:42 +00:00
return false;
}
if (nick_size >= sizeof(netplay->other_nick))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Invalid nick size.\n");
2012-01-21 17:12:42 +00:00
return false;
}
if (!socket_receive_all_blocking(fd, netplay->other_nick, nick_size))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to receive nick.\n");
2012-01-21 17:12:42 +00:00
return false;
}
return true;
}
static bool send_info(netplay_t *netplay)
2011-02-13 15:40:24 +00:00
{
unsigned sram_size;
2015-06-12 15:04:25 +00:00
char msg[512] = {0};
void *sram = NULL;
uint32_t header[3] = {0};
global_t *global = global_get_ptr();
2015-03-21 03:43:18 +00:00
header[0] = htonl(global->content_crc);
header[1] = htonl(implementation_magic_value());
header[2] = htonl(core.retro_get_memory_size(RETRO_MEMORY_SAVE_RAM));
2012-01-21 17:12:42 +00:00
if (!socket_send_all_blocking(netplay->fd, header, sizeof(header)))
2011-02-13 15:40:24 +00:00
return false;
2011-02-13 16:45:14 +00:00
if (!send_nickname(netplay, netplay->fd))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send nick to host.\n");
2012-01-21 17:12:42 +00:00
return false;
}
2015-01-05 01:03:17 +00:00
/* Get SRAM data from User 1. */
sram = core.retro_get_memory_data(RETRO_MEMORY_SAVE_RAM);
sram_size = core.retro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
2012-01-21 13:00:11 +00:00
if (!socket_receive_all_blocking(netplay->fd, sram, sram_size))
2011-02-13 16:45:14 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to receive SRAM data from host.\n");
2012-01-21 13:00:11 +00:00
return false;
2011-02-13 16:45:14 +00:00
}
if (!get_nickname(netplay, netplay->fd))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to receive nick from host.\n");
2012-01-21 17:12:42 +00:00
return false;
}
snprintf(msg, sizeof(msg), "Connected to: \"%s\"", netplay->other_nick);
2012-04-21 21:25:32 +00:00
RARCH_LOG("%s\n", msg);
rarch_main_msg_queue_push(msg, 1, 180, false);
2012-01-21 17:12:42 +00:00
2011-02-13 15:40:24 +00:00
return true;
}
static bool get_info(netplay_t *netplay)
2011-02-13 15:40:24 +00:00
{
unsigned sram_size;
2011-02-13 16:45:14 +00:00
uint32_t header[3];
2015-03-16 01:17:04 +00:00
const void *sram = NULL;
2015-03-21 03:43:18 +00:00
global_t *global = global_get_ptr();
2012-01-21 17:12:42 +00:00
if (!socket_receive_all_blocking(netplay->fd, header, sizeof(header)))
2011-02-13 19:24:54 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to receive header from client.\n");
2011-02-13 15:40:24 +00:00
return false;
2011-02-13 19:24:54 +00:00
}
2012-01-21 17:12:42 +00:00
2015-03-21 03:43:18 +00:00
if (global->content_crc != ntohl(header[0]))
2011-02-13 19:24:54 +00:00
{
2014-08-12 01:19:02 +00:00
RARCH_ERR("Content CRC32s differ. Cannot use different games.\n");
2011-02-13 15:40:24 +00:00
return false;
2011-02-13 19:24:54 +00:00
}
2012-01-21 17:12:42 +00:00
2011-10-31 21:51:54 +00:00
if (implementation_magic_value() != ntohl(header[1]))
2011-02-13 19:24:54 +00:00
{
2012-07-07 15:08:55 +00:00
RARCH_ERR("Implementations differ, make sure you're using exact same libretro implementations and RetroArch version.\n");
2011-02-13 15:40:24 +00:00
return false;
2011-02-13 19:24:54 +00:00
}
2012-01-21 17:12:42 +00:00
if (core.retro_get_memory_size(RETRO_MEMORY_SAVE_RAM) != ntohl(header[2]))
2011-02-13 19:24:54 +00:00
{
2014-08-12 01:19:02 +00:00
RARCH_ERR("Content SRAM sizes do not correspond.\n");
2011-02-13 16:45:14 +00:00
return false;
2011-02-13 19:24:54 +00:00
}
2011-02-13 16:45:14 +00:00
if (!get_nickname(netplay, netplay->fd))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to get nickname from client.\n");
2012-01-21 17:12:42 +00:00
return false;
}
2015-01-05 01:03:17 +00:00
/* Send SRAM data to our User 2. */
sram = core.retro_get_memory_data(RETRO_MEMORY_SAVE_RAM);
sram_size = core.retro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
if (!socket_send_all_blocking(netplay->fd, sram, sram_size))
2011-02-13 16:45:14 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send SRAM data to client.\n");
2012-01-21 13:00:11 +00:00
return false;
2011-02-13 16:45:14 +00:00
}
if (!send_nickname(netplay, netplay->fd))
2012-01-21 17:12:42 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send nickname to client.\n");
2012-01-21 17:12:42 +00:00
return false;
}
2012-01-24 22:14:11 +00:00
#ifndef HAVE_SOCKET_LEGACY
log_connection(&netplay->other_addr, 0, netplay->other_nick);
2012-01-24 22:14:11 +00:00
#endif
2012-01-21 17:12:42 +00:00
2011-02-13 15:40:24 +00:00
return true;
}
static uint32_t *bsv_header_generate(size_t *size, uint32_t magic)
{
uint32_t *header, bsv_header[4] = {0};
size_t serialize_size = core.retro_serialize_size();
2015-04-11 00:49:30 +00:00
size_t header_size = sizeof(bsv_header) + serialize_size;
global_t *global = global_get_ptr();
*size = header_size;
header = (uint32_t*)malloc(header_size);
if (!header)
2015-04-11 00:49:30 +00:00
return NULL;
2015-03-21 03:43:18 +00:00
bsv_header[MAGIC_INDEX] = swap_if_little32(BSV_MAGIC);
bsv_header[SERIALIZER_INDEX] = swap_if_big32(magic);
2015-03-21 03:43:18 +00:00
bsv_header[CRC_INDEX] = swap_if_big32(global->content_crc);
bsv_header[STATE_SIZE_INDEX] = swap_if_big32(serialize_size);
if (serialize_size && !core.retro_serialize(header + 4, serialize_size))
2015-04-11 00:49:30 +00:00
{
free(header);
return NULL;
}
memcpy(header, bsv_header, sizeof(bsv_header));
return header;
}
static bool bsv_parse_header(const uint32_t *header, uint32_t magic)
{
uint32_t in_crc, in_magic, in_state_size;
2015-04-11 00:49:30 +00:00
uint32_t in_bsv = swap_if_little32(header[MAGIC_INDEX]);
2015-03-21 03:43:18 +00:00
global_t *global = global_get_ptr();
if (in_bsv != BSV_MAGIC)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("BSV magic mismatch, got 0x%x, expected 0x%x.\n",
in_bsv, BSV_MAGIC);
return false;
}
in_magic = swap_if_big32(header[SERIALIZER_INDEX]);
if (in_magic != magic)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Magic mismatch, got 0x%x, expected 0x%x.\n", in_magic, magic);
return false;
}
in_crc = swap_if_big32(header[CRC_INDEX]);
2015-03-21 03:43:18 +00:00
if (in_crc != global->content_crc)
{
2014-09-15 05:03:54 +00:00
RARCH_ERR("CRC32 mismatch, got 0x%x, expected 0x%x.\n", in_crc,
2015-03-21 03:43:18 +00:00
global->content_crc);
return false;
}
in_state_size = swap_if_big32(header[STATE_SIZE_INDEX]);
if (in_state_size != core.retro_serialize_size())
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Serialization size mismatch, got 0x%x, expected 0x%x.\n",
(unsigned)in_state_size, (unsigned)core.retro_serialize_size());
return false;
}
return true;
}
static bool get_info_spectate(netplay_t *netplay)
2012-01-11 18:22:18 +00:00
{
size_t save_state_size, size;
2015-06-12 15:04:25 +00:00
void *buf = NULL;
uint32_t header[4] = {0};
char msg[512] = {0};
bool ret = true;
if (!send_nickname(netplay, netplay->fd))
2012-01-21 17:34:07 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send nickname to host.\n");
2012-01-21 17:34:07 +00:00
return false;
}
if (!get_nickname(netplay, netplay->fd))
2012-01-21 17:34:07 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to receive nickname from host.\n");
2012-01-21 17:34:07 +00:00
return false;
}
snprintf(msg, sizeof(msg), "Connected to \"%s\"", netplay->other_nick);
rarch_main_msg_queue_push(msg, 1, 180, false);
2012-04-21 21:25:32 +00:00
RARCH_LOG("%s\n", msg);
2012-01-21 17:34:07 +00:00
2015-04-11 00:49:30 +00:00
if (!socket_receive_all_blocking(netplay->fd, header, sizeof(header)))
2012-01-11 18:22:18 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Cannot get header from host.\n");
2012-01-11 18:22:18 +00:00
return false;
}
save_state_size = core.retro_serialize_size();
2012-01-21 17:12:42 +00:00
if (!bsv_parse_header(header, implementation_magic_value()))
2012-01-11 18:22:18 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Received invalid BSV header from host.\n");
2012-01-11 18:22:18 +00:00
return false;
}
buf = malloc(save_state_size);
2012-01-11 18:22:18 +00:00
if (!buf)
2015-04-11 00:49:30 +00:00
return false;
2012-01-11 18:22:18 +00:00
size = save_state_size;
2012-01-11 18:22:18 +00:00
if (!socket_receive_all_blocking(netplay->fd, buf, size))
2012-01-21 13:00:11 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to receive save state from host.\n");
2015-04-11 00:49:30 +00:00
free(buf);
return false;
2012-01-11 18:22:18 +00:00
}
if (save_state_size)
ret = core.retro_unserialize(buf, save_state_size);
free(buf);
return ret;
}
static bool init_buffers(netplay_t *netplay)
{
2013-10-22 19:26:33 +00:00
unsigned i;
2015-01-09 17:34:00 +00:00
if (!netplay)
return false;
netplay->buffer = (struct delta_frame*)calloc(netplay->buffer_size,
sizeof(*netplay->buffer));
if (!netplay->buffer)
return false;
netplay->state_size = core.retro_serialize_size();
for (i = 0; i < netplay->buffer_size; i++)
{
netplay->buffer[i].state = malloc(netplay->state_size);
if (!netplay->buffer[i].state)
return false;
netplay->buffer[i].is_simulated = true;
}
return true;
}
2015-01-09 17:34:00 +00:00
/**
* 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,
2015-04-11 00:49:30 +00:00
bool spectate,
const char *nick)
{
unsigned i;
2015-04-11 00:49:30 +00:00
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;
}
2011-02-13 15:40:24 +00:00
netplay->buffer_size = frames + 1;
2011-02-13 15:40:24 +00:00
if (!init_buffers(netplay))
goto error;
2012-01-21 13:00:11 +00:00
netplay->has_connection = true;
}
2012-01-21 13:00:11 +00:00
return netplay;
2012-01-21 13:00:11 +00:00
error:
if (netplay->fd >= 0)
socket_close(netplay->fd);
if (netplay->udp_fd >= 0)
socket_close(netplay->udp_fd);
2012-01-21 13:00:11 +00:00
2015-04-11 00:49:30 +00:00
free(netplay);
return NULL;
2012-01-21 13:00:11 +00:00
}
2015-11-17 23:18:04 +00:00
static bool netplay_send_raw_cmd(netplay_t *netplay, uint32_t cmd,
const void *data, size_t size)
{
cmd = (cmd << 16) | (size & 0xffff);
cmd = htonl(cmd);
2012-01-21 13:00:11 +00:00
if (!socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd)))
return false;
2012-01-21 13:00:11 +00:00
if (!socket_send_all_blocking(netplay->fd, data, size))
return false;
2012-01-21 13:24:38 +00:00
return true;
}
2012-01-21 13:00:11 +00:00
2015-01-09 17:34:00 +00:00
/**
2015-11-17 23:18:04 +00:00
* netplay_command:
* @netplay : pointer to netplay object
* @cmd : command to send
* @data : data to send as argument
* @sz : size of data
* @flags : flags of CMD_OPT_*
* @command_str : name of action
* @success_msg : message to display upon success
*
* Sends a single netplay command and waits for response.
*/
bool netplay_command(netplay_t* netplay, enum netplay_cmd cmd,
void* data, size_t sz,
uint32_t flags,
const char* command_str,
const char* success_msg)
2012-01-21 13:00:11 +00:00
{
2015-11-19 12:03:23 +00:00
char m[256];
2015-11-17 23:18:04 +00:00
const char* msg = NULL;
bool allowed_spectate = !!(flags & CMD_OPT_ALLOWED_IN_SPECTATE_MODE);
bool host_only = !!(flags & CMD_OPT_HOST_ONLY);
bool require_sync = !!(flags & CMD_OPT_REQUIRE_SYNC);
2015-11-19 12:03:23 +00:00
assert(netplay);
if (netplay->spectate.enabled && !allowed_spectate)
2012-01-21 13:00:11 +00:00
{
2015-11-17 23:18:04 +00:00
msg = "Cannot %s in spectate mode.";
goto error;
2012-01-21 13:00:11 +00:00
}
2015-11-17 23:18:04 +00:00
if (host_only && netplay->port == 0)
2012-01-21 13:00:11 +00:00
{
2015-11-17 23:18:04 +00:00
msg = "Cannot %s as a client.";
2012-01-21 13:00:11 +00:00
goto error;
}
2015-11-17 23:18:04 +00:00
if(require_sync && check_netplay_synched(netplay))
2012-01-21 13:00:11 +00:00
{
2015-11-17 23:18:04 +00:00
msg = "Cannot %s while host and client are not in sync.";
2012-01-21 13:00:11 +00:00
goto error;
}
2015-11-17 23:18:04 +00:00
if(netplay_send_raw_cmd(netplay, cmd, data, sz)) {
if(netplay_get_response(netplay))
rarch_main_msg_queue_push(success_msg, 1, 180, false);
else
{
msg = "Failed to send command \"%s\"";
goto error;
}
}
return true;
2015-11-19 12:03:23 +00:00
error:
snprintf(m, sizeof(m), msg, command_str);
2015-11-17 23:18:04 +00:00
RARCH_WARN("%s\n", m);
rarch_main_msg_queue_push(m, 1, 180, false);
return false;
}
2012-01-21 13:24:38 +00:00
2015-11-17 23:18:04 +00:00
/**
* netplay_flip_users:
* @netplay : pointer to netplay object
*
* On regular netplay, flip who controls user 1 and 2.
**/
void netplay_flip_users(netplay_t *netplay)
{
2015-11-17 23:35:53 +00:00
uint32_t flip_frame = netplay->frame_count + 2 * UDP_FRAME_PACKETS;
uint32_t flip_frame_net = htonl(flip_frame);
2015-11-17 23:18:04 +00:00
bool command = netplay_command(
netplay, NETPLAY_CMD_FLIP_PLAYERS,
&flip_frame_net, sizeof flip_frame_net,
CMD_OPT_HOST_ONLY | CMD_OPT_REQUIRE_SYNC,
"flip users", "Succesfully flipped users.\n");
if(command)
{
2015-11-17 23:32:45 +00:00
netplay->flip ^= true;
2015-11-17 23:37:43 +00:00
netplay->flip_frame = flip_frame;
2012-01-21 13:00:11 +00:00
}
}
2015-01-09 17:34:00 +00:00
/**
* netplay_free:
* @netplay : pointer to netplay object
*
* Frees netplay handle.
**/
void netplay_free(netplay_t *netplay)
2011-02-13 15:40:24 +00:00
{
2013-10-22 19:26:33 +00:00
unsigned i;
2015-01-09 17:34:00 +00:00
socket_close(netplay->fd);
if (netplay->spectate.enabled)
2012-01-11 18:22:18 +00:00
{
2013-10-22 19:26:33 +00:00
for (i = 0; i < MAX_SPECTATORS; i++)
if (netplay->spectate.fds[i] >= 0)
socket_close(netplay->spectate.fds[i]);
2012-01-11 18:22:18 +00:00
free(netplay->spectate.input);
2012-01-11 18:22:18 +00:00
}
else
{
socket_close(netplay->udp_fd);
2012-01-11 18:22:18 +00:00
for (i = 0; i < netplay->buffer_size; i++)
free(netplay->buffer[i].state);
2012-01-11 18:22:18 +00:00
free(netplay->buffer);
2012-01-11 18:22:18 +00:00
}
if (netplay->addr)
freeaddrinfo_retro(netplay->addr);
2012-01-11 18:22:18 +00:00
free(netplay);
2011-02-13 15:40:24 +00:00
}
2015-01-09 17:34:00 +00:00
/**
* netplay_pre_frame_net:
* @netplay : pointer to netplay object
*
* Pre-frame for Netplay (normal version).
**/
static void netplay_pre_frame_net(netplay_t *netplay)
{
core.retro_serialize(netplay->buffer[netplay->self_ptr].state,
netplay->state_size);
netplay->can_poll = true;
input_poll_net();
}
static void netplay_set_spectate_input(netplay_t *netplay, int16_t input)
2012-01-11 18:22:18 +00:00
{
if (netplay->spectate.input_ptr >= netplay->spectate.input_sz)
2012-01-11 18:22:18 +00:00
{
netplay->spectate.input_sz++;
netplay->spectate.input_sz *= 2;
netplay->spectate.input = (uint16_t*)realloc(netplay->spectate.input,
netplay->spectate.input_sz * sizeof(uint16_t));
2012-01-11 18:22:18 +00:00
}
netplay->spectate.input[netplay->spectate.input_ptr++] = swap_if_big16(input);
2012-01-11 18:22:18 +00:00
}
2014-09-07 03:47:18 +00:00
int16_t input_state_spectate(unsigned port, unsigned device,
2014-10-20 17:29:49 +00:00
unsigned idx, unsigned id)
2012-01-11 18:22:18 +00:00
{
2015-04-11 00:49:30 +00:00
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
int16_t res = netplay->cbs.state_cb(port, device, idx, id);
netplay_set_spectate_input(netplay, res);
2012-01-11 18:22:18 +00:00
return res;
}
static int16_t netplay_get_spectate_input(netplay_t *netplay, bool port,
2014-10-20 17:29:49 +00:00
unsigned device, unsigned idx, unsigned id)
2012-01-11 18:22:18 +00:00
{
int16_t inp;
if (socket_receive_all_blocking(netplay->fd, (char*)&inp, sizeof(inp)))
2012-01-11 18:22:18 +00:00
return swap_if_big16(inp);
RARCH_ERR("Connection with host was cut.\n");
rarch_main_msg_queue_push("Connection with host was cut.", 1, 180, true);
core.retro_set_input_state(netplay->cbs.state_cb);
return netplay->cbs.state_cb(port, device, idx, id);
2012-01-11 18:22:18 +00:00
}
2014-09-07 03:47:18 +00:00
int16_t input_state_spectate_client(unsigned port, unsigned device,
2014-10-20 17:29:49 +00:00
unsigned idx, unsigned id)
2012-01-11 18:22:18 +00:00
{
2015-04-11 00:49:30 +00:00
driver_t *driver = driver_get_ptr();
return netplay_get_spectate_input((netplay_t*)driver->netplay_data, port,
device, idx, id);
2012-01-11 18:22:18 +00:00
}
2015-01-09 17:34:00 +00:00
/**
* netplay_pre_frame_spectate:
* @netplay : pointer to netplay object
*
* Pre-frame for Netplay (spectate mode version).
**/
static void netplay_pre_frame_spectate(netplay_t *netplay)
2012-01-11 18:22:18 +00:00
{
2013-10-22 19:26:33 +00:00
unsigned i;
uint32_t *header;
2015-04-11 00:49:30 +00:00
int new_fd, idx, bufsize;
size_t header_size;
struct sockaddr_storage their_addr;
2015-01-09 17:34:00 +00:00
socklen_t addr_size;
fd_set fds;
struct timeval tmp_tv = {0};
if (netplay->spectate.client)
2012-01-11 18:22:18 +00:00
return;
FD_ZERO(&fds);
FD_SET(netplay->fd, &fds);
2012-01-11 18:22:18 +00:00
if (socket_select(netplay->fd + 1, &fds, NULL, NULL, &tmp_tv) <= 0)
2012-01-11 18:22:18 +00:00
return;
if (!FD_ISSET(netplay->fd, &fds))
2012-01-11 18:22:18 +00:00
return;
2015-01-09 17:34:00 +00:00
addr_size = sizeof(their_addr);
new_fd = accept(netplay->fd, (struct sockaddr*)&their_addr, &addr_size);
2012-01-11 18:22:18 +00:00
if (new_fd < 0)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to accept incoming spectator.\n");
2012-01-11 18:22:18 +00:00
return;
}
2015-04-11 00:49:30 +00:00
idx = -1;
2013-10-22 19:26:33 +00:00
for (i = 0; i < MAX_SPECTATORS; i++)
2012-01-11 18:22:18 +00:00
{
if (netplay->spectate.fds[i] == -1)
2012-01-11 18:22:18 +00:00
{
2014-10-20 17:29:49 +00:00
idx = i;
2012-01-11 18:22:18 +00:00
break;
}
}
2015-04-11 00:49:30 +00:00
/* No vacant client streams :( */
2014-10-20 17:29:49 +00:00
if (idx == -1)
2012-01-11 18:22:18 +00:00
{
socket_close(new_fd);
2012-01-11 18:22:18 +00:00
return;
}
if (!get_nickname(netplay, new_fd))
2012-01-21 17:34:07 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to get nickname from client.\n");
socket_close(new_fd);
2012-01-21 17:34:07 +00:00
return;
}
if (!send_nickname(netplay, new_fd))
2012-01-21 17:34:07 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send nickname to client.\n");
socket_close(new_fd);
2012-01-21 17:34:07 +00:00
return;
}
header = bsv_header_generate(&header_size,
2014-09-07 03:47:18 +00:00
implementation_magic_value());
2012-01-11 18:22:18 +00:00
if (!header)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to generate BSV header.\n");
socket_close(new_fd);
2012-01-11 18:22:18 +00:00
return;
}
bufsize = header_size;
setsockopt(new_fd, SOL_SOCKET, SO_SNDBUF, (const char*)&bufsize,
2014-09-07 03:47:18 +00:00
sizeof(int));
if (!socket_send_all_blocking(new_fd, header, header_size))
2012-01-11 18:22:18 +00:00
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to send header to client.\n");
socket_close(new_fd);
2012-01-21 13:00:11 +00:00
free(header);
return;
2012-01-11 18:22:18 +00:00
}
free(header);
netplay->spectate.fds[idx] = new_fd;
2012-01-24 22:14:11 +00:00
#ifndef HAVE_SOCKET_LEGACY
2014-10-20 17:29:49 +00:00
log_connection(&their_addr, idx, netplay->other_nick);
2012-01-24 22:14:11 +00:00
#endif
2012-01-11 18:22:18 +00:00
}
2015-01-09 17:34:00 +00:00
/**
* netplay_pre_frame:
* @netplay : pointer to netplay object
*
* Pre-frame for Netplay.
* Call this before running retro_run().
**/
void netplay_pre_frame(netplay_t *netplay)
2012-01-11 18:22:18 +00:00
{
if (netplay->spectate.enabled)
netplay_pre_frame_spectate(netplay);
2012-01-11 18:22:18 +00:00
else
netplay_pre_frame_net(netplay);
2012-01-11 18:22:18 +00:00
}
2015-01-09 17:34:00 +00:00
/**
* netplay_post_frame_net:
* @netplay : pointer to netplay object
*
* Post-frame for Netplay (normal version).
* We check if we have new input and replay from recorded input.
**/
static void netplay_post_frame_net(netplay_t *netplay)
{
netplay->frame_count++;
2014-09-07 03:47:18 +00:00
/* Nothing to do... */
if (netplay->other_frame_count == netplay->read_frame_count)
return;
2014-09-07 03:47:18 +00:00
/* Skip ahead if we predicted correctly.
* Skip until our simulation failed. */
while (netplay->other_frame_count < netplay->read_frame_count)
{
const struct delta_frame *ptr = &netplay->buffer[netplay->other_ptr];
2015-01-23 08:00:53 +00:00
2014-09-07 03:47:18 +00:00
if ((ptr->simulated_input_state != ptr->real_input_state)
&& !ptr->used_real)
break;
2015-04-11 00:49:30 +00:00
netplay->other_ptr = NEXT_PTR(netplay->other_ptr);
netplay->other_frame_count++;
}
if (netplay->other_frame_count < netplay->read_frame_count)
{
2015-04-11 00:49:30 +00:00
bool first = true;
2014-09-07 03:47:18 +00:00
/* Replay frames. */
2015-04-11 00:49:30 +00:00
netplay->is_replay = true;
netplay->tmp_ptr = netplay->other_ptr;
netplay->tmp_frame_count = netplay->other_frame_count;
2012-01-21 15:14:10 +00:00
core.retro_unserialize(netplay->buffer[netplay->other_ptr].state,
netplay->state_size);
while (first || (netplay->tmp_ptr != netplay->self_ptr))
{
core.retro_serialize(netplay->buffer[netplay->tmp_ptr].state,
netplay->state_size);
2013-02-17 01:00:51 +00:00
#if defined(HAVE_THREADS) && !defined(RARCH_CONSOLE)
2011-02-18 22:51:51 +00:00
lock_autosave();
2011-11-30 16:46:58 +00:00
#endif
core.retro_run();
2013-02-17 01:00:51 +00:00
#if defined(HAVE_THREADS) && !defined(RARCH_CONSOLE)
2011-02-18 22:51:51 +00:00
unlock_autosave();
2011-11-30 16:46:58 +00:00
#endif
2015-04-11 00:49:30 +00:00
netplay->tmp_ptr = NEXT_PTR(netplay->tmp_ptr);
netplay->tmp_frame_count++;
2015-04-11 00:49:30 +00:00
first = false;
}
2012-01-21 15:14:10 +00:00
2015-04-11 00:49:30 +00:00
netplay->other_ptr = netplay->read_ptr;
netplay->other_frame_count = netplay->read_frame_count;
2015-04-11 00:49:30 +00:00
netplay->is_replay = false;
}
}
2011-12-04 17:03:08 +00:00
2015-01-09 17:34:00 +00:00
/**
* netplay_post_frame_spectate:
* @netplay : pointer to netplay object
*
* Post-frame for Netplay (spectate mode version).
* We check if we have new input and replay from recorded input.
**/
static void netplay_post_frame_spectate(netplay_t *netplay)
2012-01-11 18:22:18 +00:00
{
2013-10-22 19:26:33 +00:00
unsigned i;
2015-01-23 08:00:53 +00:00
if (netplay->spectate.client)
2012-01-11 18:22:18 +00:00
return;
2013-10-22 19:26:33 +00:00
for (i = 0; i < MAX_SPECTATORS; i++)
2012-01-11 18:22:18 +00:00
{
2015-06-12 15:04:25 +00:00
char msg[PATH_MAX_LENGTH] = {0};
2015-01-23 08:00:53 +00:00
if (netplay->spectate.fds[i] == -1)
2012-01-11 18:22:18 +00:00
continue;
if (socket_send_all_blocking(netplay->spectate.fds[i],
netplay->spectate.input,
netplay->spectate.input_ptr * sizeof(int16_t)))
2015-01-23 08:00:53 +00:00
continue;
2015-01-23 08:00:53 +00:00
RARCH_LOG("Client (#%u) disconnected ...\n", i);
2012-01-11 20:08:44 +00:00
2015-01-23 08:00:53 +00:00
snprintf(msg, sizeof(msg), "Client (#%u) disconnected.", i);
rarch_main_msg_queue_push(msg, 1, 180, false);
2012-01-11 18:22:18 +00:00
socket_close(netplay->spectate.fds[i]);
netplay->spectate.fds[i] = -1;
2015-01-23 08:00:53 +00:00
break;
2012-01-11 18:22:18 +00:00
}
netplay->spectate.input_ptr = 0;
2012-01-11 18:22:18 +00:00
}
2015-01-09 17:34:00 +00:00
/**
* netplay_post_frame:
* @netplay : pointer to netplay object
*
* Post-frame for Netplay.
* We check if we have new input and replay from recorded input.
* Call this after running retro_run().
**/
void netplay_post_frame(netplay_t *netplay)
2012-01-11 18:22:18 +00:00
{
if (netplay->spectate.enabled)
netplay_post_frame_spectate(netplay);
2012-01-11 18:22:18 +00:00
else
netplay_post_frame_net(netplay);
2012-01-11 18:22:18 +00:00
}
2015-04-11 11:31:33 +00:00
void deinit_netplay(void)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (netplay)
netplay_free(netplay);
driver->netplay_data = NULL;
}
2015-04-11 11:29:40 +00:00
#define RARCH_DEFAULT_PORT 55435
/**
* init_netplay:
*
* Initializes netplay.
*
* If netplay is already initialized, will return false (0).
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool init_netplay(void)
{
struct retro_callbacks cbs = {0};
driver_t *driver = driver_get_ptr();
settings_t *settings = config_get_ptr();
global_t *global = global_get_ptr();
2015-07-27 15:18:10 +00:00
if (!global->netplay.enable)
2015-04-11 11:29:40 +00:00
return false;
2015-11-30 23:08:02 +00:00
if (bsv_movie_ctl(BSV_MOVIE_CTL_START_PLAYBACK, NULL))
2015-04-11 11:29:40 +00:00
{
2015-07-02 16:39:57 +00:00
RARCH_WARN("%s\n", msg_hash_to_str(MSG_NETPLAY_FAILED_MOVIE_PLAYBACK_HAS_STARTED));
2015-04-11 11:29:40 +00:00
return false;
}
retro_set_default_callbacks(&cbs);
2015-07-27 15:18:10 +00:00
if (*global->netplay.server)
2015-04-11 11:29:40 +00:00
{
RARCH_LOG("Connecting to netplay host...\n");
2015-07-27 15:18:10 +00:00
global->netplay.is_client = true;
2015-04-11 11:29:40 +00:00
}
else
RARCH_LOG("Waiting for client...\n");
driver->netplay_data = (netplay_t*)netplay_new(
2015-07-27 15:18:10 +00:00
global->netplay.is_client ? global->netplay.server : NULL,
global->netplay.port ? global->netplay.port : RARCH_DEFAULT_PORT,
global->netplay.sync_frames, &cbs, global->netplay.is_spectate,
2015-04-11 11:29:40 +00:00
settings->username);
if (driver->netplay_data)
return true;
2015-07-27 15:18:10 +00:00
global->netplay.is_client = false;
2015-07-02 16:39:57 +00:00
RARCH_WARN("%s\n", msg_hash_to_str(MSG_NETPLAY_FAILED));
2015-04-11 11:29:40 +00:00
2015-07-02 16:39:57 +00:00
rarch_main_msg_queue_push_new(
MSG_NETPLAY_FAILED_MOVIE_PLAYBACK_HAS_STARTED,
2015-04-11 11:29:40 +00:00
0, 180, false);
return false;
}
2012-03-16 23:48:43 +00:00
#ifdef HAVE_SOCKET_LEGACY
#undef sockaddr_storage
#undef addrinfo
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define addrinfo addrinfo_retro__
2012-03-16 23:48:43 +00:00
#ifdef _XBOX
2014-12-23 20:21:49 +00:00
/* TODO - implement h_length and h_addrtype */
2012-03-16 23:48:43 +00:00
struct hostent
{
2014-12-23 20:21:49 +00:00
int h_addrtype; /* host address type */
int h_length; /* length of addresses */
char **h_addr_list; /* list of addresses */
2012-03-16 23:48:43 +00:00
};
static struct hostent *gethostbyname(const char *name)
{
2015-01-23 08:00:53 +00:00
WSAEVENT event;
2012-03-16 23:48:43 +00:00
static struct hostent he;
static struct in_addr addr;
static char *addr_ptr;
2015-04-11 00:49:30 +00:00
XNDNS *dns = NULL;
2012-03-16 23:48:43 +00:00
he.h_addr_list = &addr_ptr;
2015-04-11 00:49:30 +00:00
addr_ptr = (char*)&addr;
2012-03-16 23:48:43 +00:00
if (!name)
return NULL;
2015-04-11 00:49:30 +00:00
event = WSACreateEvent();
2012-03-16 23:48:43 +00:00
XNetDnsLookup(name, event, &dns);
if (!dns)
goto error;
WaitForSingleObject((HANDLE)event, INFINITE);
if (dns->iStatus)
goto error;
memcpy(&addr, dns->aina, sizeof(addr));
WSACloseEvent(event);
XNetDnsRelease(dns);
return &he;
error:
if (event)
WSACloseEvent(event);
return NULL;
}
#endif
#endif