(netplay.c) Style nits/cleanups

This commit is contained in:
twinaphex 2015-04-01 17:59:34 +02:00
parent 9f319e60e1
commit fed283ffd7

111
netplay.c
View File

@ -21,10 +21,8 @@
#include <stdlib.h>
#include <string.h>
#include <net/net_compat.h>
#include <queues/message_queue.h>
#include "netplay.h"
#include "general.h"
#include "runloop.h"
#include "autosave.h"
#include "dynamic.h"
@ -47,9 +45,6 @@ struct delta_frame
#define NETPLAY_CMD_NAK 1
#define NETPLAY_CMD_FLIP_PLAYERS 2
#define PREV_PTR(x) ((x) == 0 ? netplay->buffer_size - 1 : (x) - 1)
#define NEXT_PTR(x) ((x + 1) % netplay->buffer_size)
struct netplay
{
char nick[32];
@ -115,6 +110,18 @@ struct netplay
uint32_t flip_frame;
};
static INLINE size_t get_prev_ptr(netplay_t *netplay, size_t ptr)
{
if (ptr == 0)
return netplay->buffer_size - 1;
return ptr - 1;
}
static INLINE size_t get_next_ptr(netplay_t *netplay, size_t ptr)
{
return (ptr + 1) & netplay->buffer_size;
}
/**
* warn_hangup:
*
@ -216,7 +223,7 @@ static bool get_self_input_state(netplay_t *netplay)
}
ptr->self_state = state;
netplay->self_ptr = NEXT_PTR(netplay->self_ptr);
netplay->self_ptr = get_next_ptr(netplay, netplay->self_ptr);
return true;
}
@ -298,8 +305,8 @@ static bool netplay_get_cmd(netplay_t *netplay)
static int poll_input(netplay_t *netplay, bool block)
{
int max_fd = (netplay->fd > netplay->udp_fd ? netplay->fd : netplay->udp_fd) + 1;
struct timeval tv = {0};
int max_fd = (netplay->fd > netplay->udp_fd ? netplay->fd : netplay->udp_fd) + 1;
tv.tv_sec = 0;
tv.tv_usec = block ? (RETRY_MS * 1000) : 0;
@ -377,7 +384,7 @@ static void parse_packet(netplay_t *netplay, uint32_t *buffer, unsigned size)
netplay->buffer[netplay->read_ptr].is_simulated = false;
netplay->buffer[netplay->read_ptr].real_input_state = state;
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
netplay->read_ptr = get_next_ptr(netplay, netplay->read_ptr);
netplay->read_frame_count++;
netplay->timeout_cnt = 0;
}
@ -386,8 +393,8 @@ static void parse_packet(netplay_t *netplay, uint32_t *buffer, unsigned size)
/* TODO: Somewhat better prediction. :P */
static void simulate_input(netplay_t *netplay)
{
size_t ptr = PREV_PTR(netplay->self_ptr);
size_t prev = PREV_PTR(netplay->read_ptr);
size_t ptr = get_prev_ptr(netplay, netplay->self_ptr);
size_t prev = get_prev_ptr(netplay, netplay->read_ptr);
netplay->buffer[ptr].simulated_input_state =
netplay->buffer[prev].real_input_state;
@ -424,7 +431,7 @@ static bool netplay_poll(netplay_t *netplay)
netplay->buffer[0].used_real = true;
netplay->buffer[0].is_simulated = false;
netplay->buffer[0].real_input_state = 0;
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
netplay->read_ptr = get_next_ptr(netplay, netplay->read_ptr);
netplay->read_frame_count++;
return true;
}
@ -442,6 +449,7 @@ static bool netplay_poll(netplay_t *netplay)
if (res == 1)
{
uint32_t first_read = netplay->read_frame_count;
do
{
uint32_t buffer[UDP_FRAME_PACKETS * 2];
@ -452,7 +460,6 @@ static bool netplay_poll(netplay_t *netplay)
return false;
}
parse_packet(netplay, buffer, UDP_FRAME_PACKETS);
}while ((netplay->read_frame_count <= netplay->frame_count) &&
poll_input(netplay, (netplay->other_ptr == netplay->self_ptr) &&
(first_read == netplay->read_frame_count)) == 1);
@ -470,7 +477,10 @@ static bool netplay_poll(netplay_t *netplay)
if (netplay->read_ptr != netplay->self_ptr)
simulate_input(netplay);
else
netplay->buffer[PREV_PTR(netplay->self_ptr)].used_real = true;
{
size_t prev_ptr = get_prev_ptr(netplay, netplay->self_ptr);
netplay->buffer[prev_ptr].used_real = true;
}
return true;
}
@ -478,7 +488,8 @@ static bool netplay_poll(netplay_t *netplay)
void input_poll_net(void)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
if (!netplay_should_skip(netplay) && netplay_can_poll(netplay))
netplay_poll(netplay);
}
@ -487,7 +498,8 @@ void video_frame_net(const void *data, unsigned width,
unsigned height, size_t pitch)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
if (!netplay_should_skip(netplay))
netplay->cbs.frame_cb(data, width, height, pitch);
}
@ -495,7 +507,7 @@ void video_frame_net(const void *data, unsigned width,
void audio_sample_net(int16_t left, int16_t right)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
if (!netplay_should_skip(netplay))
netplay->cbs.sample_cb(left, right);
}
@ -503,7 +515,8 @@ void audio_sample_net(int16_t left, int16_t right)
size_t audio_sample_batch_net(const int16_t *data, size_t frames)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
if (!netplay_should_skip(netplay))
return netplay->cbs.sample_batch_cb(data, frames);
return frames;
@ -541,7 +554,7 @@ static int16_t netplay_input_state(netplay_t *netplay, bool port, unsigned devic
unsigned idx, unsigned id)
{
size_t ptr = netplay->is_replay ?
netplay->tmp_ptr : PREV_PTR(netplay->self_ptr);
netplay->tmp_ptr : get_prev_ptr(netplay, netplay->self_ptr);
uint16_t curr_input_state = netplay->buffer[ptr].self_state;
if (netplay->port == (netplay_flip_port(netplay, port) ? 1 : 0))
@ -559,7 +572,8 @@ int16_t input_state_net(unsigned port, unsigned device,
unsigned idx, unsigned id)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
if (netplay_is_alive(netplay))
return netplay_input_state(netplay, port, device, idx, id);
return netplay->cbs.state_cb(port, device, idx, id);
@ -816,11 +830,10 @@ static uint32_t implementation_magic_value(void)
{
size_t i, len;
uint32_t res = 0;
const char *lib = NULL;
const char *ver = PACKAGE_VERSION;
unsigned api = pretro_api_version();
global_t *global = global_get_ptr();
lib = global->system.info.library_name;
const char *lib = global ? global->system.info.library_name : NULL;
res |= api;
@ -1000,7 +1013,7 @@ static uint32_t *bsv_header_generate(size_t *size, uint32_t magic)
header = (uint32_t*)malloc(header_size);
if (!header)
return NULL;
goto error;
bsv_header[MAGIC_INDEX] = swap_if_little32(BSV_MAGIC);
bsv_header[SERIALIZER_INDEX] = swap_if_big32(magic);
@ -1008,13 +1021,15 @@ static uint32_t *bsv_header_generate(size_t *size, uint32_t magic)
bsv_header[STATE_SIZE_INDEX] = swap_if_big32(serialize_size);
if (serialize_size && !pretro_serialize(header + 4, serialize_size))
{
free(header);
return NULL;
}
goto error;
memcpy(header, bsv_header, sizeof(bsv_header));
return header;
error:
if (header)
free(header);
return NULL;
}
static bool bsv_parse_header(const uint32_t *header, uint32_t magic)
@ -1080,7 +1095,6 @@ static bool get_info_spectate(netplay_t *netplay)
rarch_main_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
if (!socket_receive_all_blocking(netplay->fd, header, sizeof(header)))
{
RARCH_ERR("Cannot get header from host.\n");
@ -1096,15 +1110,14 @@ static bool get_info_spectate(netplay_t *netplay)
buf = malloc(save_state_size);
if (!buf)
return false;
goto error;
size = save_state_size;
if (!socket_receive_all_blocking(netplay->fd, buf, size))
{
RARCH_ERR("Failed to receive save state from host.\n");
free(buf);
return false;
goto error;
}
if (save_state_size)
@ -1112,6 +1125,11 @@ static bool get_info_spectate(netplay_t *netplay)
free(buf);
return ret;
error:
if (buf)
free(buf);
return false;
}
static bool init_buffers(netplay_t *netplay)
@ -1158,16 +1176,10 @@ static bool init_buffers(netplay_t *netplay)
**/
netplay_t *netplay_new(const char *server, uint16_t port,
unsigned frames, const struct retro_callbacks *cb,
bool spectate,
const char *nick)
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));
netplay_t *netplay = (netplay_t*)calloc(1, sizeof(*netplay));
if (!netplay)
return NULL;
@ -1179,6 +1191,9 @@ netplay_t *netplay_new(const char *server, uint16_t port,
netplay->spectate_client = server != NULL;
strlcpy(netplay->nick, nick, sizeof(netplay->nick));
if (frames > UDP_FRAME_PACKETS)
frames = UDP_FRAME_PACKETS;
if (!init_socket(netplay, server, port))
{
free(netplay);
@ -1225,6 +1240,7 @@ error:
if (netplay->udp_fd >= 0)
socket_close(netplay->udp_fd);
if (netplay)
free(netplay);
return NULL;
}
@ -1367,8 +1383,8 @@ int16_t input_state_spectate(unsigned port, unsigned device,
unsigned idx, unsigned id)
{
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_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
int16_t res = netplay ? netplay->cbs.state_cb(port, device, idx, id) : 0;
netplay_set_spectate_input(netplay, res);
return res;
@ -1393,8 +1409,9 @@ int16_t input_state_spectate_client(unsigned port, unsigned device,
unsigned idx, unsigned id)
{
driver_t *driver = driver_get_ptr();
return netplay_get_spectate_input((netplay_t*)driver->netplay_data, port,
device, idx, id);
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
return netplay_get_spectate_input(netplay, port, device, idx, id);
}
/**
@ -1407,11 +1424,12 @@ static void netplay_pre_frame_spectate(netplay_t *netplay)
{
unsigned i;
uint32_t *header;
int new_fd, idx, bufsize;
int new_fd, bufsize;
size_t header_size;
struct sockaddr_storage their_addr;
socklen_t addr_size;
fd_set fds;
int idx = -1;
struct timeval tmp_tv = {0};
if (netplay->spectate_client)
@ -1434,7 +1452,6 @@ static void netplay_pre_frame_spectate(netplay_t *netplay)
return;
}
idx = -1;
for (i = 0; i < MAX_SPECTATORS; i++)
{
if (netplay->spectate_fds[i] == -1)
@ -1444,7 +1461,7 @@ static void netplay_pre_frame_spectate(netplay_t *netplay)
}
}
/* No vacant client streams :( */
/* No vacant client streams. */
if (idx == -1)
{
socket_close(new_fd);
@ -1534,7 +1551,7 @@ static void netplay_post_frame_net(netplay_t *netplay)
if ((ptr->simulated_input_state != ptr->real_input_state)
&& !ptr->used_real)
break;
netplay->other_ptr = NEXT_PTR(netplay->other_ptr);
netplay->other_ptr = get_next_ptr(netplay, netplay->other_ptr);
netplay->other_frame_count++;
}
@ -1561,7 +1578,7 @@ static void netplay_post_frame_net(netplay_t *netplay)
#if defined(HAVE_THREADS) && !defined(RARCH_CONSOLE)
unlock_autosave();
#endif
netplay->tmp_ptr = NEXT_PTR(netplay->tmp_ptr);
netplay->tmp_ptr = get_next_ptr(netplay, netplay->tmp_ptr);
netplay->tmp_frame_count++;
first = false;
}