(netplay.c) Restore
This commit is contained in:
parent
4f02cc306b
commit
ad446249af
113
netplay.c
113
netplay.c
|
@ -45,6 +45,9 @@ 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];
|
||||
|
@ -110,18 +113,6 @@ 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:
|
||||
*
|
||||
|
@ -223,7 +214,7 @@ static bool get_self_input_state(netplay_t *netplay)
|
|||
}
|
||||
|
||||
ptr->self_state = state;
|
||||
netplay->self_ptr = get_next_ptr(netplay, netplay->self_ptr);
|
||||
netplay->self_ptr = NEXT_PTR(netplay->self_ptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -305,8 +296,8 @@ static bool netplay_get_cmd(netplay_t *netplay)
|
|||
|
||||
static int poll_input(netplay_t *netplay, bool block)
|
||||
{
|
||||
struct timeval tv = {0};
|
||||
int max_fd = (netplay->fd > netplay->udp_fd ? netplay->fd : netplay->udp_fd) + 1;
|
||||
struct timeval tv = {0};
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = block ? (RETRY_MS * 1000) : 0;
|
||||
|
||||
|
@ -384,7 +375,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 = get_next_ptr(netplay, netplay->read_ptr);
|
||||
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
|
||||
netplay->read_frame_count++;
|
||||
netplay->timeout_cnt = 0;
|
||||
}
|
||||
|
@ -393,8 +384,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 = get_prev_ptr(netplay, netplay->self_ptr);
|
||||
size_t prev = get_prev_ptr(netplay, netplay->read_ptr);
|
||||
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;
|
||||
|
@ -431,7 +422,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 = get_next_ptr(netplay, netplay->read_ptr);
|
||||
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
|
||||
netplay->read_frame_count++;
|
||||
return true;
|
||||
}
|
||||
|
@ -449,7 +440,6 @@ 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];
|
||||
|
@ -460,7 +450,8 @@ static bool netplay_poll(netplay_t *netplay)
|
|||
return false;
|
||||
}
|
||||
parse_packet(netplay, buffer, UDP_FRAME_PACKETS);
|
||||
}while ((netplay->read_frame_count <= netplay->frame_count) &&
|
||||
|
||||
} while ((netplay->read_frame_count <= netplay->frame_count) &&
|
||||
poll_input(netplay, (netplay->other_ptr == netplay->self_ptr) &&
|
||||
(first_read == netplay->read_frame_count)) == 1);
|
||||
}
|
||||
|
@ -477,10 +468,7 @@ static bool netplay_poll(netplay_t *netplay)
|
|||
if (netplay->read_ptr != netplay->self_ptr)
|
||||
simulate_input(netplay);
|
||||
else
|
||||
{
|
||||
size_t prev_ptr = get_prev_ptr(netplay, netplay->self_ptr);
|
||||
netplay->buffer[prev_ptr].used_real = true;
|
||||
}
|
||||
netplay->buffer[PREV_PTR(netplay->self_ptr)].used_real = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -488,8 +476,7 @@ static bool netplay_poll(netplay_t *netplay)
|
|||
void input_poll_net(void)
|
||||
{
|
||||
driver_t *driver = driver_get_ptr();
|
||||
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
|
||||
|
||||
netplay_t *netplay = (netplay_t*)driver->netplay_data;
|
||||
if (!netplay_should_skip(netplay) && netplay_can_poll(netplay))
|
||||
netplay_poll(netplay);
|
||||
}
|
||||
|
@ -498,8 +485,7 @@ void video_frame_net(const void *data, unsigned width,
|
|||
unsigned height, size_t pitch)
|
||||
{
|
||||
driver_t *driver = driver_get_ptr();
|
||||
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
|
||||
|
||||
netplay_t *netplay = (netplay_t*)driver->netplay_data;
|
||||
if (!netplay_should_skip(netplay))
|
||||
netplay->cbs.frame_cb(data, width, height, pitch);
|
||||
}
|
||||
|
@ -507,7 +493,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 = driver ? (netplay_t*)driver->netplay_data : NULL;
|
||||
netplay_t *netplay = (netplay_t*)driver->netplay_data;
|
||||
if (!netplay_should_skip(netplay))
|
||||
netplay->cbs.sample_cb(left, right);
|
||||
}
|
||||
|
@ -515,8 +501,7 @@ 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 = driver ? (netplay_t*)driver->netplay_data : NULL;
|
||||
|
||||
netplay_t *netplay = (netplay_t*)driver->netplay_data;
|
||||
if (!netplay_should_skip(netplay))
|
||||
return netplay->cbs.sample_batch_cb(data, frames);
|
||||
return frames;
|
||||
|
@ -554,7 +539,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 : get_prev_ptr(netplay, netplay->self_ptr);
|
||||
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))
|
||||
|
@ -572,8 +557,7 @@ int16_t input_state_net(unsigned port, unsigned device,
|
|||
unsigned idx, unsigned id)
|
||||
{
|
||||
driver_t *driver = driver_get_ptr();
|
||||
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
|
||||
|
||||
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);
|
||||
|
@ -830,10 +814,11 @@ 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();
|
||||
const char *lib = global ? global->system.info.library_name : NULL;
|
||||
lib = global->system.info.library_name;
|
||||
|
||||
res |= api;
|
||||
|
||||
|
@ -1013,7 +998,7 @@ static uint32_t *bsv_header_generate(size_t *size, uint32_t magic)
|
|||
|
||||
header = (uint32_t*)malloc(header_size);
|
||||
if (!header)
|
||||
goto error;
|
||||
return NULL;
|
||||
|
||||
bsv_header[MAGIC_INDEX] = swap_if_little32(BSV_MAGIC);
|
||||
bsv_header[SERIALIZER_INDEX] = swap_if_big32(magic);
|
||||
|
@ -1021,15 +1006,13 @@ 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))
|
||||
goto error;
|
||||
{
|
||||
free(header);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -1095,6 +1078,7 @@ 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");
|
||||
|
@ -1110,14 +1094,15 @@ static bool get_info_spectate(netplay_t *netplay)
|
|||
|
||||
buf = malloc(save_state_size);
|
||||
if (!buf)
|
||||
goto error;
|
||||
return false;
|
||||
|
||||
size = save_state_size;
|
||||
|
||||
if (!socket_receive_all_blocking(netplay->fd, buf, size))
|
||||
{
|
||||
RARCH_ERR("Failed to receive save state from host.\n");
|
||||
goto error;
|
||||
free(buf);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (save_state_size)
|
||||
|
@ -1125,11 +1110,6 @@ 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)
|
||||
|
@ -1176,10 +1156,16 @@ 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 = (netplay_t*)calloc(1, sizeof(*netplay));
|
||||
netplay_t *netplay = NULL;
|
||||
|
||||
if (frames > UDP_FRAME_PACKETS)
|
||||
frames = UDP_FRAME_PACKETS;
|
||||
|
||||
netplay = (netplay_t*)calloc(1, sizeof(*netplay));
|
||||
if (!netplay)
|
||||
return NULL;
|
||||
|
||||
|
@ -1191,9 +1177,6 @@ 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);
|
||||
|
@ -1240,7 +1223,6 @@ error:
|
|||
if (netplay->udp_fd >= 0)
|
||||
socket_close(netplay->udp_fd);
|
||||
|
||||
if (netplay)
|
||||
free(netplay);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1370,7 +1352,7 @@ static void netplay_set_spectate_input(netplay_t *netplay, int16_t input)
|
|||
{
|
||||
if (netplay->spectate_input_ptr >= netplay->spectate_input_size)
|
||||
{
|
||||
netplay->spectate_input_size ++;
|
||||
netplay->spectate_input_size++;
|
||||
netplay->spectate_input_size *= 2;
|
||||
netplay->spectate_input = (uint16_t*)realloc(netplay->spectate_input,
|
||||
netplay->spectate_input_size * sizeof(uint16_t));
|
||||
|
@ -1383,8 +1365,8 @@ int16_t input_state_spectate(unsigned port, unsigned device,
|
|||
unsigned idx, unsigned id)
|
||||
{
|
||||
driver_t *driver = driver_get_ptr();
|
||||
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
|
||||
int16_t res = netplay ? netplay->cbs.state_cb(port, device, idx, id) : 0;
|
||||
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);
|
||||
return res;
|
||||
|
@ -1409,9 +1391,8 @@ int16_t input_state_spectate_client(unsigned port, unsigned device,
|
|||
unsigned idx, unsigned id)
|
||||
{
|
||||
driver_t *driver = driver_get_ptr();
|
||||
netplay_t *netplay = driver ? (netplay_t*)driver->netplay_data : NULL;
|
||||
|
||||
return netplay_get_spectate_input(netplay, port, device, idx, id);
|
||||
return netplay_get_spectate_input((netplay_t*)driver->netplay_data, port,
|
||||
device, idx, id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1424,12 +1405,11 @@ static void netplay_pre_frame_spectate(netplay_t *netplay)
|
|||
{
|
||||
unsigned i;
|
||||
uint32_t *header;
|
||||
int new_fd, bufsize;
|
||||
int new_fd, idx, 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)
|
||||
|
@ -1452,6 +1432,7 @@ 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)
|
||||
|
@ -1461,7 +1442,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);
|
||||
|
@ -1551,7 +1532,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 = get_next_ptr(netplay, netplay->other_ptr);
|
||||
netplay->other_ptr = NEXT_PTR(netplay->other_ptr);
|
||||
netplay->other_frame_count++;
|
||||
}
|
||||
|
||||
|
@ -1578,7 +1559,7 @@ static void netplay_post_frame_net(netplay_t *netplay)
|
|||
#if defined(HAVE_THREADS) && !defined(RARCH_CONSOLE)
|
||||
unlock_autosave();
|
||||
#endif
|
||||
netplay->tmp_ptr = get_next_ptr(netplay, netplay->tmp_ptr);
|
||||
netplay->tmp_ptr = NEXT_PTR(netplay->tmp_ptr);
|
||||
netplay->tmp_frame_count++;
|
||||
first = false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue