Renames for readability, merging frame and sram into sync packet.

This commit is contained in:
Gregor Richards 2016-12-03 18:53:57 -05:00
parent f89e54fcb7
commit 80be19a7da
5 changed files with 85 additions and 106 deletions

View File

@ -272,7 +272,7 @@ static void hangup(netplay_t *netplay)
{
if (!netplay)
return;
if (netplay->status == RARCH_NETPLAY_CONNECTION_NONE)
if (netplay->mode == NETPLAY_CONNECTION_NONE)
return;
RARCH_WARN("Netplay has disconnected. Will continue without connection ...\n");
@ -291,7 +291,7 @@ static void hangup(netplay_t *netplay)
}
}
netplay->status = RARCH_NETPLAY_CONNECTION_NONE;
netplay->mode = NETPLAY_CONNECTION_NONE;
/* Reset things that will behave oddly if we get a new connection */
netplay->remote_paused = false;
@ -319,7 +319,7 @@ static bool netplay_should_skip(netplay_t *netplay)
{
if (!netplay)
return false;
return netplay->is_replay && (netplay->status == RARCH_NETPLAY_CONNECTION_PLAYING);
return netplay->is_replay && (netplay->mode == NETPLAY_CONNECTION_PLAYING);
}
static bool netplay_can_poll(netplay_t *netplay)
@ -334,7 +334,7 @@ static bool netplay_can_poll(netplay_t *netplay)
static void send_input(netplay_t *netplay)
{
if (!netplay->spectate.enabled && /* Spectate sends in its own way */
netplay->status == RARCH_NETPLAY_CONNECTION_PLAYING)
netplay->mode == NETPLAY_CONNECTION_PLAYING)
{
netplay->input_packet_buffer[2] = htonl(netplay->self_frame_count);
if (!netplay_send(&netplay->send_packet_buffer, netplay->fd,
@ -480,24 +480,22 @@ static bool netplay_get_cmd(netplay_t *netplay, bool *had_input)
ssize_t recvd;
/* We don't handle the initial handshake here */
switch (netplay->status)
switch (netplay->mode)
{
case RARCH_NETPLAY_CONNECTION_NONE:
case NETPLAY_CONNECTION_NONE:
/* Huh?! */
return false;
case RARCH_NETPLAY_CONNECTION_INIT:
case NETPLAY_CONNECTION_INIT:
return netplay_handshake_init(netplay, had_input);
case RARCH_NETPLAY_CONNECTION_PRE_NICK:
case NETPLAY_CONNECTION_PRE_NICK:
{
bool ret = netplay_handshake_pre_nick(netplay, had_input);
send_input(netplay);
return ret;
}
case RARCH_NETPLAY_CONNECTION_PRE_SRAM:
return netplay_handshake_pre_sram(netplay, had_input);
case RARCH_NETPLAY_CONNECTION_PRE_FRAME:
case NETPLAY_CONNECTION_PRE_SYNC:
{
bool ret = netplay_handshake_pre_frame(netplay, had_input);
bool ret = netplay_handshake_pre_sync(netplay, had_input);
send_input(netplay);
return ret;
}
@ -927,7 +925,7 @@ static bool netplay_poll(void)
{
int res;
if (netplay_data->status == RARCH_NETPLAY_CONNECTION_NONE)
if (netplay_data->mode == NETPLAY_CONNECTION_NONE)
return false;
netplay_data->can_poll = false;
@ -1029,7 +1027,7 @@ static bool netplay_is_alive(void)
{
if (!netplay_data)
return false;
return (netplay_data->status == RARCH_NETPLAY_CONNECTION_PLAYING);
return (netplay_data->mode == NETPLAY_CONNECTION_PLAYING);
}
static bool netplay_flip_port(netplay_t *netplay, bool port)
@ -1591,7 +1589,7 @@ bool netplay_pre_frame(netplay_t *netplay)
if (!netplay->net_cbs->pre_frame(netplay))
return false;
return ((netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING) ||
return ((netplay->mode != NETPLAY_CONNECTION_PLAYING) ||
(!netplay->stall && !netplay->remote_paused));
}
@ -1625,7 +1623,7 @@ void netplay_frontend_paused(netplay_t *netplay, bool paused)
return;
netplay->local_paused = paused;
if (netplay->status != RARCH_NETPLAY_CONNECTION_NONE &&
if (netplay->mode != NETPLAY_CONNECTION_NONE &&
!netplay->spectate.enabled)
{
netplay_send_raw_cmd(netplay, paused
@ -1653,7 +1651,7 @@ void netplay_load_savestate(netplay_t *netplay,
retro_ctx_serialize_info_t tmp_serial_info;
uint32_t rd, wn;
if (netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING)
if (netplay->mode != NETPLAY_CONNECTION_PLAYING)
return;
/* Record it in our own buffer */
@ -1749,7 +1747,7 @@ void netplay_load_savestate(netplay_t *netplay,
**/
bool netplay_disconnect(netplay_t *netplay)
{
if (!netplay || (netplay->status == RARCH_NETPLAY_CONNECTION_NONE))
if (!netplay || (netplay->mode == NETPLAY_CONNECTION_NONE))
return true;
hangup(netplay);
return true;

View File

@ -286,7 +286,7 @@ bool netplay_handshake_init(netplay_t *netplay, bool *had_input)
return false;
/* Move on to the next mode */
netplay->status = RARCH_NETPLAY_CONNECTION_PRE_NICK;
netplay->mode = NETPLAY_CONNECTION_PRE_NICK;
*had_input = true;
netplay_recv_flush(&netplay->recv_packet_buffer);
return true;
@ -328,7 +328,7 @@ static void netplay_handshake_ready(netplay_t *netplay)
if (netplay->stall == RARCH_NETPLAY_STALL_NO_CONNECTION)
netplay->stall = 0;
netplay->status = RARCH_NETPLAY_CONNECTION_PLAYING;
netplay->mode = NETPLAY_CONNECTION_PLAYING;
}
bool netplay_handshake_pre_nick(netplay_t *netplay, bool *had_input)
@ -364,28 +364,22 @@ bool netplay_handshake_pre_nick(netplay_t *netplay, bool *had_input)
if (netplay->is_server)
{
/* If we're the server, now we send our SRAM and frame number */
/* If we're the server, now we send sync info */
uint32_t cmd[3];
retro_ctx_memory_info_t mem_info;
mem_info.id = RETRO_MEMORY_SAVE_RAM;
core_get_memory(&mem_info);
cmd[0] = htonl(NETPLAY_CMD_SRAM);
cmd[1] = htonl(mem_info.size);
cmd[0] = htonl(NETPLAY_CMD_SYNC);
cmd[1] = htonl(sizeof(uint32_t) + mem_info.size);
cmd[2] = htonl(netplay->self_frame_count);
if (!netplay_send(&netplay->send_packet_buffer, netplay->fd, cmd,
2*sizeof(uint32_t)))
sizeof(uint32_t)))
return false;
if (!netplay_send(&netplay->send_packet_buffer, netplay->fd,
mem_info.data, mem_info.size))
return false;
cmd[0] = htonl(NETPLAY_CMD_FRAME);
cmd[1] = htonl(sizeof(uint32_t));
cmd[2] = htonl(netplay->self_frame_count);
if (!netplay_send(&netplay->send_packet_buffer, netplay->fd, cmd,
sizeof(cmd)) ||
mem_info.data, mem_info.size) ||
!netplay_send_flush(&netplay->send_packet_buffer, netplay->fd,
false))
return false;
@ -396,8 +390,8 @@ bool netplay_handshake_pre_nick(netplay_t *netplay, bool *had_input)
}
else
{
/* Client needs to wait for SRAM */
netplay->status = RARCH_NETPLAY_CONNECTION_PRE_SRAM;
/* Client needs to wait for sync info */
netplay->mode = NETPLAY_CONNECTION_PRE_SYNC;
}
@ -406,29 +400,57 @@ bool netplay_handshake_pre_nick(netplay_t *netplay, bool *had_input)
return true;
}
bool netplay_handshake_pre_sram(netplay_t *netplay, bool *had_input)
bool netplay_handshake_pre_sync(netplay_t *netplay, bool *had_input)
{
uint32_t cmd[2];
uint32_t local_sram_size, remote_sram_size;
uint32_t new_frame_count;
size_t i;
ssize_t recvd;
retro_ctx_memory_info_t mem_info;
RECV(cmd, sizeof(cmd))
return false;
/* Only expecting an SRAM command */
if (ntohl(cmd[0]) != NETPLAY_CMD_SRAM)
/* Only expecting a sync command */
if (ntohl(cmd[0]) != NETPLAY_CMD_SYNC ||
ntohl(cmd[1]) < sizeof(uint32_t))
{
RARCH_ERR("%s\n",
msg_hash_to_str(MSG_FAILED_TO_RECEIVE_SRAM_DATA_FROM_HOST));
return false;
}
/* Get the frame count */
RECV(&new_frame_count, sizeof(new_frame_count))
return false;
new_frame_count = ntohl(new_frame_count);
/* Reset our frame buffer so it's consistent between server and client */
netplay->self_frame_count = netplay->other_frame_count =
netplay->read_frame_count = new_frame_count;
for (i = 0; i < netplay->buffer_size; i++)
{
if (i == netplay->self_ptr)
{
struct delta_frame *ptr = &netplay->buffer[i];
if (!ptr->used)
netplay_delta_frame_ready(netplay, ptr, 0);
ptr->frame = new_frame_count;
netplay->other_ptr = netplay->read_ptr = i;
}
else
{
netplay->buffer[i].used = false;
}
}
/* Now check the SRAM */
mem_info.id = RETRO_MEMORY_SAVE_RAM;
core_get_memory(&mem_info);
local_sram_size = mem_info.size;
remote_sram_size = ntohl(cmd[1]);
remote_sram_size = ntohl(cmd[1]) - sizeof(uint32_t);
if (local_sram_size != 0 && local_sram_size == remote_sram_size)
{
@ -460,51 +482,6 @@ bool netplay_handshake_pre_sram(netplay_t *netplay, bool *had_input)
}
/* Finally, wait for the frame number */
netplay->status = RARCH_NETPLAY_CONNECTION_PRE_FRAME;
*had_input = true;
netplay_recv_flush(&netplay->recv_packet_buffer);
return true;
}
bool netplay_handshake_pre_frame(netplay_t *netplay, bool *had_input)
{
uint32_t cmd[3];
ssize_t recvd;
size_t i;
RECV(cmd, sizeof(cmd))
return false;
/* Only expecting an frame command */
if (ntohl(cmd[0]) != NETPLAY_CMD_FRAME ||
ntohl(cmd[1]) != sizeof(uint32_t))
{
/* FIXME: Correct error message */
RARCH_ERR("%s\n",
msg_hash_to_str(MSG_FAILED_TO_RECEIVE_SRAM_DATA_FROM_HOST));
return false;
}
/* Reset our frame count so it's consistent between server and client */
netplay->self_frame_count = netplay->other_frame_count =
netplay->read_frame_count = ntohl(cmd[2]);
for (i = 0; i < netplay->buffer_size; i++)
{
if (i == netplay->self_ptr)
{
struct delta_frame *ptr = &netplay->buffer[i];
if (!ptr->used)
netplay_delta_frame_ready(netplay, ptr, 0);
ptr->frame = netplay->self_frame_count;
netplay->other_ptr = netplay->read_ptr = i;
}
else
{
netplay->buffer[i].used = false;
}
}
/* We're ready! */
netplay_handshake_ready(netplay);
*had_input = true;

View File

@ -105,13 +105,13 @@ static bool netplay_net_pre_frame(netplay_t *netplay)
}
/* If we can't transmit savestates, we must stall until the client is ready */
if (netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING &&
if (netplay->mode != NETPLAY_CONNECTION_PLAYING &&
netplay->self_frame_count > 0 &&
(netplay->quirks & (NETPLAY_QUIRK_NO_SAVESTATES|NETPLAY_QUIRK_NO_TRANSMISSION)))
netplay->stall = RARCH_NETPLAY_STALL_NO_CONNECTION;
}
if (netplay->is_server && netplay->status == RARCH_NETPLAY_CONNECTION_NONE)
if (netplay->is_server && netplay->mode == NETPLAY_CONNECTION_NONE)
{
fd_set fds;
struct timeval tmp_tv = {0};
@ -168,7 +168,7 @@ static bool netplay_net_pre_frame(netplay_t *netplay)
}
netplay_handshake_init_send(netplay);
netplay->status = RARCH_NETPLAY_CONNECTION_INIT;
netplay->mode = NETPLAY_CONNECTION_INIT;
}
}
@ -192,7 +192,7 @@ static void netplay_net_post_frame(netplay_t *netplay)
netplay->self_frame_count++;
/* Only relevant if we're connected */
if (netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING)
if (netplay->mode != NETPLAY_CONNECTION_PLAYING)
{
netplay->read_frame_count = netplay->other_frame_count = netplay->self_frame_count;
netplay->read_ptr = netplay->other_ptr = netplay->self_ptr;
@ -321,7 +321,7 @@ static bool netplay_net_info_cb(netplay_t* netplay, unsigned frames)
if (!netplay_is_server(netplay))
{
netplay_handshake_init_send(netplay);
netplay->status = RARCH_NETPLAY_CONNECTION_INIT;
netplay->mode = NETPLAY_CONNECTION_INIT;
}
return true;

View File

@ -99,17 +99,17 @@ enum netplay_cmd
/* Inform the other side of our nick (must be first command) */
NETPLAY_CMD_NICK = 0x0020,
/* Send SRAM data (must be second command from server) */
NETPLAY_CMD_SRAM = 0x0021,
/* Inform client of current frame number (must be third command from server) */
NETPLAY_CMD_FRAME = 0x0022,
/* Initial synchronization info (frame, sram, player info) */
NETPLAY_CMD_SYNC = 0x0021,
/* Join spectator mode */
NETPLAY_CMD_SPECTATE = 0x0023,
NETPLAY_CMD_SPECTATE = 0x0022,
/* Join play mode */
NETPLAY_CMD_PLAY = 0x0024,
NETPLAY_CMD_PLAY = 0x0023,
/* Report player mode */
NETPLAY_CMD_MODE = 0x0024,
/* Loading and synchronization */
@ -162,14 +162,19 @@ enum netplay_cmd_cfg
NETPLAY_CFG_PLAYER_SLOT = 0x0008
};
enum rarch_netplay_connection_status
enum rarch_netplay_connection_mode
{
RARCH_NETPLAY_CONNECTION_NONE = 0,
RARCH_NETPLAY_CONNECTION_INIT, /* Waiting for header */
RARCH_NETPLAY_CONNECTION_PRE_NICK, /* Waiting for nick */
RARCH_NETPLAY_CONNECTION_PRE_SRAM, /* Waiting for SRAM */
RARCH_NETPLAY_CONNECTION_PRE_FRAME, /* Waiting for frame number */
RARCH_NETPLAY_CONNECTION_PLAYING /* Normal ready state */
NETPLAY_CONNECTION_NONE = 0,
/* Initialization: */
NETPLAY_CONNECTION_INIT, /* Waiting for header */
NETPLAY_CONNECTION_PRE_NICK, /* Waiting for nick */
NETPLAY_CONNECTION_PRE_SYNC, /* Waiting for sync */
/* Ready: */
NETPLAY_CONNECTION_CONNECTED, /* Modes above this are connected */
NETPLAY_CONNECTION_SPECTATING, /* Spectator mode */
NETPLAY_CONNECTION_PLAYING /* Normal ready state */
};
enum rarch_netplay_stall_reason
@ -225,7 +230,7 @@ struct netplay
struct sockaddr_storage other_addr;
/* Status of our connection */
enum rarch_netplay_connection_status status;
enum rarch_netplay_connection_mode mode;
struct retro_callbacks cbs;
/* TCP connection for state sending, etc. Also used for commands */
@ -442,8 +447,7 @@ bool netplay_send_nickname(netplay_t *netplay, int fd);
bool netplay_handshake_init_send(netplay_t *netplay);
bool netplay_handshake_init(netplay_t *netplay, bool *had_input);
bool netplay_handshake_pre_nick(netplay_t *netplay, bool *had_input);
bool netplay_handshake_pre_sram(netplay_t *netplay, bool *had_input);
bool netplay_handshake_pre_frame(netplay_t *netplay, bool *had_input);
bool netplay_handshake_pre_sync(netplay_t *netplay, bool *had_input);
uint32_t netplay_impl_magic(void);

View File

@ -285,7 +285,7 @@ static bool netplay_spectate_info_cb(netplay_t* netplay, unsigned frames)
return false;
}
netplay->status = RARCH_NETPLAY_CONNECTION_PLAYING;
netplay->mode = NETPLAY_CONNECTION_PLAYING;
return true;
}