(netplay)/network) Style nits/cleanups

This commit is contained in:
twinaphex 2019-06-26 07:13:50 +02:00
parent e97c35ec2d
commit 5f2e3c283d
9 changed files with 187 additions and 171 deletions

View File

@ -74,7 +74,7 @@ int net_http_get(const char **result, size_t *size, const char *url, retro_time_
res = (char*)malloc(length + 1);
/* Allocation error. */
if ( !res )
if (!res)
goto error;
memcpy((void*)res, (void*)data, length);
@ -92,11 +92,11 @@ int net_http_get(const char **result, size_t *size, const char *url, retro_time_
*size = length;
error:
if ( http )
net_http_delete( http );
if (http)
net_http_delete(http);
if ( conn )
net_http_connection_free( conn );
if (conn)
net_http_connection_free(conn);
if (timeout)
{

View File

@ -28,7 +28,8 @@
typedef struct netplay netplay_t;
typedef struct mitm_server {
typedef struct mitm_server
{
const char *name;
const char *description;
} mitm_server_t;

View File

@ -25,7 +25,8 @@ static size_t buf_used(struct socket_buffer *sbuf)
if (sbuf->end < sbuf->start)
{
size_t newend = sbuf->end;
while (newend < sbuf->start) newend += sbuf->bufsz;
while (newend < sbuf->start)
newend += sbuf->bufsz;
return newend - sbuf->start;
}
@ -37,7 +38,8 @@ static size_t buf_unread(struct socket_buffer *sbuf)
if (sbuf->end < sbuf->read)
{
size_t newend = sbuf->end;
while (newend < sbuf->read) newend += sbuf->bufsz;
while (newend < sbuf->read)
newend += sbuf->bufsz;
return newend - sbuf->read;
}
@ -57,7 +59,7 @@ static size_t buf_remaining(struct socket_buffer *sbuf)
bool netplay_init_socket_buffer(struct socket_buffer *sbuf, size_t size)
{
sbuf->data = (unsigned char*)malloc(size);
if (sbuf->data == NULL)
if (!sbuf->data)
return false;
sbuf->bufsz = size;
sbuf->start = sbuf->read = sbuf->end = 0;
@ -72,7 +74,7 @@ bool netplay_init_socket_buffer(struct socket_buffer *sbuf, size_t size)
bool netplay_resize_socket_buffer(struct socket_buffer *sbuf, size_t newsize)
{
unsigned char *newdata = (unsigned char*)malloc(newsize);
if (newdata == NULL)
if (!newdata)
return false;
/* Copy in the old data */
@ -82,9 +84,7 @@ bool netplay_resize_socket_buffer(struct socket_buffer *sbuf, size_t newsize)
memcpy(newdata + sbuf->bufsz - sbuf->start, sbuf->data, sbuf->end);
}
else if (sbuf->end > sbuf->start)
{
memcpy(newdata, sbuf->data + sbuf->start, sbuf->end - sbuf->start);
}
/* Adjust our read offset */
if (sbuf->read < sbuf->start)
@ -185,7 +185,8 @@ bool netplay_send_flush(struct socket_buffer *sbuf, int sockfd, bool block)
/* Usual case: Everything's in order */
if (block)
{
if (!socket_send_all_blocking(sockfd, sbuf->data + sbuf->start, buf_used(sbuf), true))
if (!socket_send_all_blocking(
sockfd, sbuf->data + sbuf->start, buf_used(sbuf), true))
return false;
sbuf->start = sbuf->end = 0;
@ -300,9 +301,7 @@ ssize_t netplay_recv(struct socket_buffer *sbuf, int sockfd, void *buf,
if (sbuf->read >= sbuf->bufsz)
sbuf->read = 0;
recvd = unread;
}
}
else
{
@ -314,7 +313,6 @@ ssize_t netplay_recv(struct socket_buffer *sbuf, int sockfd, void *buf,
memcpy((unsigned char *) buf + chunka, sbuf->data, chunkb);
sbuf->read = chunkb;
recvd = chunka + chunkb;
}
/* Perhaps block for more data */

View File

@ -47,16 +47,18 @@ bool netplay_delta_frame_ready(netplay_t *netplay, struct delta_frame *delta,
size_t i;
if (delta->used)
{
if (delta->frame == frame) return true;
if (delta->frame == frame)
return true;
/* We haven't even replayed this frame yet,
* so we can't overwrite it! */
if (netplay->other_frame_count <= delta->frame)
{
/* We haven't even replayed this frame yet, so we can't overwrite it! */
return false;
}
}
delta->used = true;
delta->frame = frame;
delta->crc = 0;
for (i = 0; i < MAX_INPUT_DEVICES; i++)
{
clear_input(delta->resolved_input[i]);
@ -170,20 +172,35 @@ netplay_input_state_t netplay_input_state_for(netplay_input_state_t *list,
uint32_t netplay_expected_input_size(netplay_t *netplay, uint32_t devices)
{
uint32_t ret = 0, device;
for (device = 0; device < MAX_INPUT_DEVICES; device++)
{
if (!(devices & (1<<device))) continue;
if (!(devices & (1<<device)))
continue;
switch (netplay->config_devices[device]&RETRO_DEVICE_MASK)
{
/* These are all essentially magic numbers, but each device has a
* fixed size, documented in network/netplay/README */
case RETRO_DEVICE_JOYPAD: ret += 1; break;
case RETRO_DEVICE_MOUSE: ret += 2; break;
case RETRO_DEVICE_KEYBOARD: ret += 5; break;
case RETRO_DEVICE_LIGHTGUN: ret += 2; break;
case RETRO_DEVICE_ANALOG: ret += 3; break;
default: break; /* Unsupported */
case RETRO_DEVICE_JOYPAD:
ret += 1;
break;
case RETRO_DEVICE_MOUSE:
ret += 2;
break;
case RETRO_DEVICE_KEYBOARD:
ret += 5;
break;
case RETRO_DEVICE_LIGHTGUN:
ret += 2;
break;
case RETRO_DEVICE_ANALOG:
ret += 3;
break;
default:
break; /* Unsupported */
}
}
return ret;
}

View File

@ -135,14 +135,13 @@ static bool get_self_input_state(netplay_t *netplay)
if (!netplay_delta_frame_ready(netplay, ptr, netplay->self_frame_count))
return false;
if (ptr->have_local)
{
/* We've already read this frame! */
if (ptr->have_local)
return true;
}
devices = netplay->self_devices;
used_devices = 0;
for (devi = 0; devi < MAX_INPUT_DEVICES; devi++)
{
if (!(devices & (1<<devi)))
@ -150,11 +149,15 @@ static bool get_self_input_state(netplay_t *netplay)
/* Find an appropriate local device */
dev_type = netplay->config_devices[devi]&RETRO_DEVICE_MASK;
for (local_device = 0; local_device < MAX_INPUT_DEVICES; local_device++)
{
if (used_devices & (1<<local_device)) continue;
if ((netplay->config_devices[local_device]&RETRO_DEVICE_MASK) == dev_type) break;
if (used_devices & (1<<local_device))
continue;
if ((netplay->config_devices[local_device]&RETRO_DEVICE_MASK) == dev_type)
break;
}
if (local_device == MAX_INPUT_DEVICES)
local_device = 0;
used_devices |= (1<<local_device);
@ -327,7 +330,7 @@ static bool netplay_poll(void)
{
/* FIXME: Using fixed 60fps for this calculation */
unsigned frames_per_frame = netplay_data->frame_run_time_avg ?
(16666/netplay_data->frame_run_time_avg) :
(16666 / netplay_data->frame_run_time_avg) :
0;
unsigned frames_ahead = (netplay_data->run_frame_count > netplay_data->unread_frame_count) ?
(netplay_data->run_frame_count - netplay_data->unread_frame_count) :
@ -352,17 +355,12 @@ static bool netplay_poll(void)
if (netplay_data->input_latency_frames < input_latency_frames_min ||
(netplay_data->unread_frame_count == netplay_data->run_frame_count + 1 &&
netplay_data->input_latency_frames < input_latency_frames_max))
{
netplay_data->input_latency_frames++;
}
else if (netplay_data->input_latency_frames > input_latency_frames_max ||
(netplay_data->unread_frame_count > netplay_data->run_frame_count + 2 &&
netplay_data->input_latency_frames > input_latency_frames_min))
{
netplay_data->input_latency_frames--;
}
}
else if (netplay_data->input_latency_frames < input_latency_frames_min ||
(frames_per_frame < frames_ahead &&
netplay_data->input_latency_frames < input_latency_frames_max))
@ -410,7 +408,6 @@ static bool netplay_poll(void)
break;
case NETPLAY_STALL_SERVER_REQUESTED:
{
/* See if the stall is done */
if (netplay_data->connections[0].stall_frame == 0)
{
@ -419,16 +416,11 @@ static bool netplay_poll(void)
netplay_data->stall = NETPLAY_STALL_NONE;
}
else
{
netplay_data->connections[0].stall_frame--;
}
break;
}
case NETPLAY_STALL_NO_CONNECTION:
/* We certainly haven't fixed this */
break;
default: /* not stalling */
break;
}
@ -513,7 +505,8 @@ static bool netplay_poll(void)
}
}
if (fixed) {
if (fixed)
{
/* Not stalled now :) */
netplay_data->stall = NETPLAY_STALL_NONE;
return true;
@ -619,27 +612,23 @@ static int16_t netplay_input_state(netplay_t *netplay,
case RETRO_DEVICE_MOUSE:
case RETRO_DEVICE_LIGHTGUN:
{
if (istate->size != 2)
return 0;
if (id <= RETRO_DEVICE_ID_MOUSE_Y)
return (int16_t)(uint16_t)(curr_input_state[1] >> (id * 16));
return ((1 << id) & curr_input_state[0]) ? 1 : 0;
}
case RETRO_DEVICE_KEYBOARD:
{
unsigned key, word, bit;
key = netplay_key_hton(id);
unsigned word, bit;
unsigned key = netplay_key_hton(id);
if (key == NETPLAY_KEY_UNKNOWN)
return 0;
word = key/32;
bit = key%32;
word = key / 32;
bit = key % 32;
if (word <= istate->size)
return ((1U<<bit) & curr_input_state[word]) ? 1 : 0;
return 0;
}
default:
return 0;
}
@ -1051,21 +1040,17 @@ bool netplay_pre_frame(netplay_t *netplay)
if ((netplay->is_server || is_mitm) && (reannounce % 600 == 0))
netplay_announce();
}
else
{
/* Make sure that if announcement is turned on mid-game, it gets announced */
else
reannounce = -1;
}
/* FIXME: This is an ugly way to learn we're not paused anymore */
if (netplay->local_paused)
netplay_frontend_paused(netplay, false);
if (netplay->quirks & NETPLAY_QUIRK_INITIALIZATION)
{
/* Are we ready now? */
if (netplay->quirks & NETPLAY_QUIRK_INITIALIZATION)
netplay_try_init_serialization(netplay);
}
if (netplay->is_server && !settings->bools.netplay_use_mitm_server)
{
@ -1157,12 +1142,15 @@ static void netplay_force_future(netplay_t *netplay)
/* We need to ignore any intervening data from the other side,
* and never rewind past this */
netplay_update_unread_ptr(netplay);
if (netplay->unread_frame_count < netplay->run_frame_count)
{
uint32_t client;
for (client = 0; client < MAX_CLIENTS; client++)
{
if (!(netplay->connected_players & (1<<client))) continue;
if (!(netplay->connected_players & (1<<client)))
continue;
if (netplay->read_frame_count[client] < netplay->run_frame_count)
{
netplay->read_ptr[client] = netplay->run_ptr;
@ -1271,18 +1259,14 @@ void netplay_load_savestate(netplay_t *netplay,
else
{
if (serial_info->size <= netplay->state_size)
{
memcpy(netplay->buffer[netplay->run_ptr].state,
serial_info->data_const, serial_info->size);
}
}
}
else
{
/* FIXME: This is a critical failure! */
else
return;
}
}
/* Don't send it if we're expected to be desynced */
if (netplay->desync)

View File

@ -223,7 +223,8 @@ bool netplay_handshake_init_send(netplay_t *netplay,
if (simple_rand_next == 1)
simple_srand((unsigned int) time(NULL));
connection->salt = simple_rand_uint32();
if (connection->salt == 0) connection->salt = 1;
if (connection->salt == 0)
connection->salt = 1;
header[3] = htonl(connection->salt);
}
else
@ -626,12 +627,14 @@ bool netplay_handshake_sync(netplay_t *netplay,
else
nickmangle = nicklen;
matchct = 1;
do {
do
{
nick_matched = false;
for (i = 0; i < netplay->connections_size; i++)
{
struct netplay_connection *sc = &netplay->connections[i];
if (sc == connection) continue;
if (sc == connection)
continue;
if (sc->active &&
sc->mode >= NETPLAY_CONNECTION_CONNECTED &&
!strncmp(connection->nick, sc->nick, NETPLAY_NICK_LEN))
@ -718,12 +721,10 @@ bool netplay_handshake_pre_nick(netplay_t *netplay,
{
settings_t *settings = config_get_ptr();
/* There's a password, so just put them in PRE_PASSWORD mode */
if ( settings->paths.netplay_password[0] ||
settings->paths.netplay_spectate_password[0])
{
/* There's a password, so just put them in PRE_PASSWORD mode */
connection->mode = NETPLAY_CONNECTION_PRE_PASSWORD;
}
else
{
connection->can_play = true;
@ -733,13 +734,10 @@ bool netplay_handshake_pre_nick(netplay_t *netplay,
}
}
else
{
/* Client needs to wait for INFO */
else
connection->mode = NETPLAY_CONNECTION_PRE_INFO;
}
*had_input = true;
netplay_recv_flush(&connection->recv_packet_buffer);
return true;

View File

@ -276,7 +276,8 @@ static bool send_input_frame(netplay_t *netplay, struct delta_frame *dframe,
for (i = 0; i < netplay->connections_size; i++)
{
struct netplay_connection *connection = &netplay->connections[i];
if (connection == except) continue;
if (connection == except)
continue;
if (connection->active &&
connection->mode >= NETPLAY_CONNECTION_CONNECTED &&
(connection->mode != NETPLAY_CONNECTION_PLAYING ||
@ -585,11 +586,10 @@ static void announce_play_spectate(netplay_t *netplay,
for (device = 0; device < MAX_INPUT_DEVICES; device++)
{
if (!(devices & (1<<device))) continue;
if (!(devices & (1<<device)))
continue;
if (one_device == (uint32_t) -1)
{
one_device = device;
}
else
{
one_device = (uint32_t) -1;
@ -616,7 +616,8 @@ static void announce_play_spectate(netplay_t *netplay,
device_str_len = 0;
for (device = 0; device < MAX_INPUT_DEVICES; device++)
{
if (!(devices & (1<<device))) continue;
if (!(devices & (1<<device)))
continue;
if (device_str_len)
device_str_len += snprintf(device_str + device_str_len,
sizeof(device_str) - 1 - device_str_len, ", ");
@ -782,9 +783,12 @@ static void handle_play_spectate(netplay_t *netplay, uint32_t client_num,
/* Make sure the devices are available and/or shareable */
for (device = 0; device < MAX_INPUT_DEVICES; device++)
{
if (!(devices & (1<<device))) continue;
if (!netplay->device_clients[device]) continue;
if (netplay->device_share_modes[device] && share_mode) continue;
if (!(devices & (1<<device)))
continue;
if (!netplay->device_clients[device])
continue;
if (netplay->device_share_modes[device] && share_mode)
continue;
/* Device already taken and unshareable */
payload[0] = htonl(NETPLAY_CMD_MODE_REFUSED_REASON_NOT_AVAILABLE);
@ -800,7 +804,8 @@ static void handle_play_spectate(netplay_t *netplay, uint32_t client_num,
/* Set the share mode on any new devices */
for (device = 0; device < MAX_INPUT_DEVICES; device++)
{
if (!(devices & (1<<device))) continue;
if (!(devices & (1<<device)))
continue;
if (!netplay->device_clients[device])
netplay->device_share_modes[device] = share_mode;
}
@ -864,7 +869,8 @@ static void handle_play_spectate(netplay_t *netplay, uint32_t client_num,
netplay->client_devices[client_num] = devices;
for (device = 0; device < MAX_INPUT_DEVICES; device++)
{
if (!(devices & (1<<device))) continue;
if (!(devices & (1<<device)))
continue;
netplay->device_clients[device] |= 1 << client_num;
}
@ -1301,12 +1307,14 @@ static bool netplay_get_cmd(netplay_t *netplay,
{
uint32_t dsize;
netplay_input_state_t istate;
if (!(devices & (1<<device))) continue;
if (!(devices & (1<<device)))
continue;
dsize = netplay_expected_input_size(netplay, 1 << device);
istate = netplay_input_state_for(
&dframe->real_input[device], client_num, dsize,
false, false);
if (!istate) continue;
if (!istate)
continue;
memset(istate->data, 0, dsize*sizeof(uint32_t));
}
dframe->have_local = true;
@ -1727,7 +1735,9 @@ static bool netplay_get_cmd(netplay_t *netplay,
/* Don't expect earlier data from other clients */
for (client = 0; client < MAX_CLIENTS; client++)
{
if (!(netplay->connected_players & (1<<client))) continue;
if (!(netplay->connected_players & (1<<client)))
continue;
if (frame > netplay->read_frame_count[client])
{
netplay->read_ptr[client] = load_ptr;

View File

@ -355,7 +355,9 @@ static void parse_context_error(Context* pCtx)
{
JSON_Error error = JSON_Parser_GetError(pCtx->parser);
JSON_Location errorLocation = {0, 0, 0};
(void)JSON_Parser_GetErrorLocation(pCtx->parser, &errorLocation);
RARCH_ERR("invalid JSON at line %d, column %d (input byte %d) - %s.\n",
(int)errorLocation.line + 1,
(int)errorLocation.column + 1,
@ -375,7 +377,7 @@ static int json_parse(Context* pCtx, const char *buf)
return 1;
}
void netplay_rooms_free()
void netplay_rooms_free(void)
{
if (rooms)
{
@ -388,15 +390,11 @@ void netplay_rooms_free()
struct netplay_room *next = room->next;
free(room);
room = next;
}
free(rooms);
}
else
free(rooms);
free(rooms);
rooms = NULL;
}
}
@ -451,7 +449,7 @@ struct netplay_room* netplay_room_get(int index)
return room;
}
int netplay_rooms_get_count()
int netplay_rooms_get_count(void)
{
int count = 0;
struct netplay_room *room;

View File

@ -54,8 +54,11 @@ void netplay_update_unread_ptr(netplay_t *netplay)
for (client = 0; client < MAX_CLIENTS; client++)
{
if (!(netplay->connected_players & (1<<client))) continue;
if ((netplay->connected_slaves & (1<<client))) continue;
if (!(netplay->connected_players & (1<<client)))
continue;
if ((netplay->connected_slaves & (1<<client)))
continue;
if (netplay->read_frame_count[client] < new_unread_frame_count)
{
new_unread_ptr = netplay->read_ptr[client];
@ -63,7 +66,8 @@ void netplay_update_unread_ptr(netplay_t *netplay)
}
}
if (!netplay->is_server && netplay->server_frame_count < new_unread_frame_count)
if ( !netplay->is_server &&
netplay->server_frame_count < new_unread_frame_count)
{
new_unread_ptr = netplay->server_ptr;
new_unread_frame_count = netplay->server_frame_count;
@ -96,11 +100,13 @@ struct vote_count {
netplay_input_state_t netplay_device_client_state(netplay_t *netplay,
struct delta_frame *simframe, uint32_t device, uint32_t client)
{
uint32_t dsize = netplay_expected_input_size(netplay, 1 << device);
uint32_t dsize =
netplay_expected_input_size(netplay, 1 << device);
netplay_input_state_t simstate =
netplay_input_state_for(
&simframe->real_input[device], client,
dsize, false, true);
if (!simstate)
{
if (netplay->read_frame_count[client] > simframe->frame)
@ -470,13 +476,16 @@ bool netplay_resolve_input(netplay_t *netplay, size_t sim_ptr, bool resim)
/* Most devices have all the digital parts in the first word. */
static const uint32_t digital_common[3] = {~0u, 0u, 0u};
static const uint32_t digital_keyboard[5] = {~0u, ~0u, ~0u, ~0u, ~0u};
const uint32_t *digital;
const uint32_t *digital = NULL;
if (dtype == RETRO_DEVICE_KEYBOARD)
digital = digital_keyboard;
else
digital = digital_common;
oldresstate = netplay_input_state_for(
&simframe->resolved_input[device], 1, dsize, false, false);
if (!oldresstate)
continue;
memcpy(oldresstate->data, resstate->data, dsize * sizeof(uint32_t));
@ -658,7 +667,8 @@ bool netplay_sync_pre_frame(netplay_t *netplay)
/* Allocate a connection */
for (connection_num = 0; connection_num < netplay->connections_size; connection_num++)
if (!netplay->connections[connection_num].active &&
netplay->connections[connection_num].mode != NETPLAY_CONNECTION_DELAYED_DISCONNECT) break;
netplay->connections[connection_num].mode != NETPLAY_CONNECTION_DELAYED_DISCONNECT)
break;
if (connection_num == netplay->connections_size)
{
if (connection_num == 0)
@ -762,6 +772,7 @@ void netplay_sync_post_frame(netplay_t *netplay, bool stalled)
{
netplay->other_frame_count = netplay->self_frame_count;
netplay->other_ptr = netplay->self_ptr;
/* FIXME: Duplication */
if (netplay->catch_up)
{
@ -860,8 +871,8 @@ void netplay_sync_post_frame(netplay_t *netplay, bool stalled)
while (netplay->replay_frame_count < netplay->run_frame_count)
{
retro_time_t start, tm;
struct delta_frame *ptr = &netplay->buffer[netplay->replay_ptr];
serial_info.data = ptr->state;
serial_info.size = netplay->state_size;
serial_info.data_const = NULL;
@ -935,15 +946,14 @@ void netplay_sync_post_frame(netplay_t *netplay, bool stalled)
/* Look for players that are ahead of us */
for (client = 0; client < MAX_CLIENTS; client++)
{
if (!(netplay->connected_players & (1<<client))) continue;
if (!(netplay->connected_players & (1<<client)))
continue;
if (netplay->read_frame_count[client] > hi_frame_count)
hi_frame_count = netplay->read_frame_count[client];
}
}
else
{
lo_frame_count = hi_frame_count = netplay->server_frame_count;
}
/* If we're behind, try to catch up */
if (netplay->catch_up)