Combine some netplay files into one

This commit is contained in:
twinaphex 2021-04-08 18:09:02 +02:00
parent 42bd709f67
commit 2a2f77f0dc
7 changed files with 1617 additions and 1708 deletions

View File

@ -2079,14 +2079,10 @@ ifeq ($(HAVE_NETWORKING), 1)
# Netplay
DEFINES += -DHAVE_NETWORK_CMD
OBJ += network/netplay/netplay_delta.o \
network/netplay/netplay_handshake.o \
OBJ += network/netplay/netplay_handshake.o \
network/netplay/netplay_init.o \
network/netplay/netplay_io.o \
network/netplay/netplay_keyboard.o \
network/netplay/netplay_sync.o \
network/netplay/netplay_discovery.o \
network/netplay/netplay_buf.o \
network/netplay/netplay_room_parse.o
# RetroAchievements

View File

@ -1292,14 +1292,10 @@ THREAD
NETPLAY
============================================================ */
#ifdef HAVE_NETWORKING
#include "../network/netplay/netplay_delta.c"
#include "../network/netplay/netplay_handshake.c"
#include "../network/netplay/netplay_init.c"
#include "../network/netplay/netplay_io.c"
#include "../network/netplay/netplay_keyboard.c"
#include "../network/netplay/netplay_sync.c"
#include "../network/netplay/netplay_discovery.c"
#include "../network/netplay/netplay_buf.c"
#include "../network/netplay/netplay_room_parse.c"
#include "../libretro-common/net/net_compat.c"
#include "../libretro-common/net/net_socket.c"

View File

@ -1,378 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2016-2017 - Gregor Richards
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* 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.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <net/net_compat.h>
#include <net/net_socket.h>
#include "netplay_private.h"
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;
return newend - sbuf->start;
}
return sbuf->end - sbuf->start;
}
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;
return newend - sbuf->read;
}
return sbuf->end - sbuf->read;
}
static size_t buf_remaining(struct socket_buffer *sbuf)
{
return sbuf->bufsz - buf_used(sbuf) - 1;
}
/**
* netplay_init_socket_buffer
*
* Initialize a new socket buffer.
*/
bool netplay_init_socket_buffer(
struct socket_buffer *sbuf, size_t size)
{
sbuf->data = (unsigned char*)malloc(size);
if (!sbuf->data)
return false;
sbuf->bufsz = size;
sbuf->start = sbuf->read = sbuf->end = 0;
return true;
}
/**
* netplay_resize_socket_buffer
*
* Resize the given socket_buffer's buffer to the requested size.
*/
bool netplay_resize_socket_buffer(
struct socket_buffer *sbuf, size_t newsize)
{
unsigned char *newdata = (unsigned char*)malloc(newsize);
if (!newdata)
return false;
/* Copy in the old data */
if (sbuf->end < sbuf->start)
{
memcpy(newdata,
sbuf->data + sbuf->start,
sbuf->bufsz - sbuf->start);
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)
sbuf->read += sbuf->bufsz - sbuf->start;
else
sbuf->read -= sbuf->start;
/* Adjust start and end */
sbuf->end = buf_used(sbuf);
sbuf->start = 0;
/* Free the old one and replace it with the new one */
free(sbuf->data);
sbuf->data = newdata;
sbuf->bufsz = newsize;
return true;
}
/**
* netplay_deinit_socket_buffer
*
* Free a socket buffer.
*/
void netplay_deinit_socket_buffer(struct socket_buffer *sbuf)
{
if (sbuf->data)
free(sbuf->data);
}
void netplay_clear_socket_buffer(struct socket_buffer *sbuf)
{
sbuf->start = sbuf->read = sbuf->end = 0;
}
/**
* netplay_send
*
* Queue the given data for sending.
*/
bool netplay_send(
struct socket_buffer *sbuf,
int sockfd, const void *buf,
size_t len)
{
if (buf_remaining(sbuf) < len)
{
/* Need to force a blocking send */
if (!netplay_send_flush(sbuf, sockfd, true))
return false;
}
if (buf_remaining(sbuf) < len)
{
/* Can only be that this is simply too big
* for our buffer, in which case we just
* need to do a blocking send */
if (!socket_send_all_blocking(sockfd, buf, len, false))
return false;
return true;
}
/* Copy it into our buffer */
if (sbuf->bufsz - sbuf->end < len)
{
/* Half at a time */
size_t chunka = sbuf->bufsz - sbuf->end,
chunkb = len - chunka;
memcpy(sbuf->data + sbuf->end, buf, chunka);
memcpy(sbuf->data, (const unsigned char *)buf + chunka, chunkb);
sbuf->end = chunkb;
}
else
{
/* Straight in */
memcpy(sbuf->data + sbuf->end, buf, len);
sbuf->end += len;
}
return true;
}
/**
* netplay_send_flush
*
* Flush unsent data in the given socket buffer, blocking to do so if
* requested.
*
* Returns false only on socket failures, true otherwise.
*/
bool netplay_send_flush(struct socket_buffer *sbuf, int sockfd, bool block)
{
ssize_t sent;
if (buf_used(sbuf) == 0)
return true;
if (sbuf->end > sbuf->start)
{
/* Usual case: Everything's in order */
if (block)
{
if (!socket_send_all_blocking(
sockfd, sbuf->data + sbuf->start,
buf_used(sbuf), true))
return false;
sbuf->start = sbuf->end = 0;
}
else
{
sent = socket_send_all_nonblocking(
sockfd, sbuf->data + sbuf->start,
buf_used(sbuf), true);
if (sent < 0)
return false;
sbuf->start += sent;
if (sbuf->start == sbuf->end)
sbuf->start = sbuf->end = 0;
}
}
else
{
/* Unusual case: Buffer overlaps break */
if (block)
{
if (!socket_send_all_blocking(
sockfd, sbuf->data + sbuf->start,
sbuf->bufsz - sbuf->start, true))
return false;
sbuf->start = 0;
return netplay_send_flush(sbuf, sockfd, true);
}
else
{
sent = socket_send_all_nonblocking(
sockfd, sbuf->data + sbuf->start,
sbuf->bufsz - sbuf->start, true);
if (sent < 0)
return false;
sbuf->start += sent;
if (sbuf->start >= sbuf->bufsz)
{
sbuf->start = 0;
return netplay_send_flush(sbuf, sockfd, false);
}
}
}
return true;
}
/**
* netplay_recv
*
* Receive buffered or fresh data.
*
* Returns number of bytes returned, which may be short or 0, or -1 on error.
*/
ssize_t netplay_recv(struct socket_buffer *sbuf, int sockfd, void *buf,
size_t len, bool block)
{
ssize_t recvd;
bool error = false;
/* Receive whatever we can into the buffer */
if (sbuf->end >= sbuf->start)
{
recvd = socket_receive_all_nonblocking(sockfd, &error,
sbuf->data + sbuf->end, sbuf->bufsz - sbuf->end -
((sbuf->start == 0) ? 1 : 0));
if (recvd < 0 || error)
return -1;
sbuf->end += recvd;
if (sbuf->end >= sbuf->bufsz)
{
sbuf->end = 0;
error = false;
recvd = socket_receive_all_nonblocking(
sockfd, &error, sbuf->data, sbuf->start - 1);
if (recvd < 0 || error)
return -1;
sbuf->end += recvd;
}
}
else
{
recvd = socket_receive_all_nonblocking(
sockfd, &error, sbuf->data + sbuf->end,
sbuf->start - sbuf->end - 1);
if (recvd < 0 || error)
return -1;
sbuf->end += recvd;
}
/* Now copy it into the reader */
if (sbuf->end >= sbuf->read || (sbuf->bufsz - sbuf->read) >= len)
{
size_t unread = buf_unread(sbuf);
if (len <= unread)
{
memcpy(buf, sbuf->data + sbuf->read, len);
sbuf->read += len;
if (sbuf->read >= sbuf->bufsz)
sbuf->read = 0;
recvd = len;
}
else
{
memcpy(buf, sbuf->data + sbuf->read, unread);
sbuf->read += unread;
if (sbuf->read >= sbuf->bufsz)
sbuf->read = 0;
recvd = unread;
}
}
else
{
/* Our read goes around the edge */
size_t chunka = sbuf->bufsz - sbuf->read,
pchunklen = len - chunka,
chunkb = (pchunklen >= sbuf->end) ? sbuf->end : pchunklen;
memcpy(buf, sbuf->data + sbuf->read, chunka);
memcpy((unsigned char *) buf + chunka, sbuf->data, chunkb);
sbuf->read = chunkb;
recvd = chunka + chunkb;
}
/* Perhaps block for more data */
if (block)
{
sbuf->start = sbuf->read;
if (recvd < 0 || recvd < (ssize_t) len)
{
if (!socket_receive_all_blocking(
sockfd, (unsigned char *)buf + recvd, len - recvd))
return -1;
recvd = len;
}
}
return recvd;
}
/**
* netplay_recv_reset
*
* Reset our recv buffer so that future netplay_recvs
* will read the same data again.
*/
void netplay_recv_reset(struct socket_buffer *sbuf)
{
sbuf->read = sbuf->start;
}
/**
* netplay_recv_flush
*
* Flush our recv buffer, so a future netplay_recv_reset will reset to this
* point.
*/
void netplay_recv_flush(struct socket_buffer *sbuf)
{
sbuf->start = sbuf->read;
}

View File

@ -1,213 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2016-2017 - Gregor Richards
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* 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.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <sys/types.h>
#include <boolean.h>
#include <encodings/crc32.h>
#include "netplay_private.h"
static void clear_input(netplay_input_state_t istate)
{
while (istate)
{
istate->used = false;
istate = istate->next;
}
}
/**
* netplay_delta_frame_ready
*
* Prepares, if possible, a delta frame for input, and reports whether it is
* ready.
*
* Returns: True if the delta frame is ready for input at the given frame,
* false otherwise.
*/
bool netplay_delta_frame_ready(netplay_t *netplay, struct delta_frame *delta,
uint32_t frame)
{
size_t i;
if (delta->used)
{
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)
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]);
clear_input(delta->real_input[i]);
clear_input(delta->simlated_input[i]);
}
delta->have_local = false;
for (i = 0; i < MAX_CLIENTS; i++)
delta->have_real[i] = false;
return true;
}
/**
* netplay_delta_frame_crc
*
* Get the CRC for the serialization of this frame.
*/
uint32_t netplay_delta_frame_crc(netplay_t *netplay,
struct delta_frame *delta)
{
if (!netplay->state_size)
return 0;
return encoding_crc32(0L, (const unsigned char*)delta->state,
netplay->state_size);
}
/*
* Free an input state list
*/
static void free_input_state(netplay_input_state_t *list)
{
netplay_input_state_t cur, next;
cur = *list;
while (cur)
{
next = cur->next;
free(cur);
cur = next;
}
*list = NULL;
}
/**
* netplay_delta_frame_free
*
* Free a delta frame's dependencies
*/
void netplay_delta_frame_free(struct delta_frame *delta)
{
uint32_t i;
if (delta->state)
{
free(delta->state);
delta->state = NULL;
}
for (i = 0; i < MAX_INPUT_DEVICES; i++)
{
free_input_state(&delta->resolved_input[i]);
free_input_state(&delta->real_input[i]);
free_input_state(&delta->simlated_input[i]);
}
}
/**
* netplay_input_state_for
*
* Get an input state for a particular client
*/
netplay_input_state_t netplay_input_state_for(
netplay_input_state_t *list,
uint32_t client_num, size_t size,
bool must_create, bool must_not_create)
{
netplay_input_state_t ret;
while (*list)
{
ret = *list;
if (!ret->used && !must_not_create && ret->size == size)
{
ret->client_num = client_num;
ret->used = true;
memset(ret->data, 0, size*sizeof(uint32_t));
return ret;
}
else if (ret->used && ret->client_num == client_num)
{
if (!must_create && ret->size == size)
return ret;
return NULL;
}
list = &(ret->next);
}
if (must_not_create)
return NULL;
/* Couldn't find a slot, allocate a fresh one */
if (size > 1)
ret = (netplay_input_state_t)calloc(1, sizeof(struct netplay_input_state) + (size-1) * sizeof(uint32_t));
else
ret = (netplay_input_state_t)calloc(1, sizeof(struct netplay_input_state));
if (!ret)
return NULL;
*list = ret;
ret->client_num = client_num;
ret->used = true;
ret->size = (uint32_t)size;
return ret;
}
/**
* netplay_expected_input_size
*
* Size in words for a given set of devices.
*/
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;
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 */
}
}
return ret;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,55 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2017 - Gregor Richards
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* 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.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "netplay_private.h"
/* The mapping of keys from netplay (network) to libretro (host) */
const uint16_t netplay_key_ntoh_mapping[] = {
(uint16_t) RETROK_UNKNOWN,
#define K(k) (uint16_t) RETROK_ ## k,
#define KL(k,l) (uint16_t) l,
#include "netplay_keys.h"
#undef KL
#undef K
0
};
/* TODO/FIXME - static global variables */
static uint16_t mapping[RETROK_LAST];
/* The mapping of keys from libretro (host) to netplay (network) */
uint32_t netplay_key_hton(unsigned key)
{
if (key >= RETROK_LAST)
return NETPLAY_KEY_UNKNOWN;
return mapping[key];
}
/* Because the hton keymapping has to be generated, call this before using
* netplay_key_hton */
void netplay_key_hton_init(void)
{
static bool mapping_defined = false;
if (!mapping_defined)
{
uint16_t i;
for (i = 0; i < NETPLAY_KEY_LAST; i++)
mapping[netplay_key_ntoh(i)] = i;
mapping_defined = true;
}
}

File diff suppressed because it is too large Load Diff