Refactoring: netplay_common.c -> netplay_handshake.c/netplay_delta.c

Refactoring netplay_common into its two actual components, the handshake
and delta-frame related functions.
This commit is contained in:
Gregor Richards 2016-12-13 21:01:31 -05:00
parent 4e905bf524
commit f619789e48
6 changed files with 154 additions and 135 deletions

View File

@ -1122,11 +1122,12 @@ ifeq ($(HAVE_NETWORKING), 1)
# Netplay
DEFINES += -DHAVE_NETWORK_CMD
OBJ += network/netplay/netplay_frontend.o \
OBJ += network/netplay/netplay_delta.o \
network/netplay/netplay_frontend.o \
network/netplay/netplay_handshake.o \
network/netplay/netplay_init.o \
network/netplay/netplay_io.o \
network/netplay/netplay_sync.o \
network/netplay/netplay_common.o \
network/netplay/netplay_discovery.o \
network/netplay/netplay_buf.o

View File

@ -881,12 +881,14 @@ THREAD
NETPLAY
============================================================ */
#ifdef HAVE_NETWORKING
#include "../network/netplay/netplay_delta.c"
#include "../network/netplay/netplay_frontend.c"
#include "../network/netplay/netplay_handshake.c"
#include "../network/netplay/netplay_init.c"
#include "../network/netplay/netplay_io.c"
#include "../network/netplay/netplay_sync.c"
#include "../network/netplay/netplay_common.c"
#include "../network/netplay/netplay_discovery.c"
#include "../network/netplay/netplay_buf.c"
#include "../libretro-common/net/net_compat.c"
#include "../libretro-common/net/net_socket.c"
#include "../libretro-common/net/net_http.c"

View File

@ -0,0 +1,51 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2016 - Daniel De Matteis
* Copyright (C) 2016 - 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"
bool netplay_delta_frame_ready(netplay_t *netplay, struct delta_frame *delta, uint32_t frame)
{
void *remember_state;
if (delta->used)
{
if (delta->frame == frame) return true;
if (netplay->other_frame_count <= delta->frame)
{
/* We haven't even replayed this frame yet, so we can't overwrite it! */
return false;
}
}
remember_state = delta->state;
memset(delta, 0, sizeof(struct delta_frame));
delta->used = true;
delta->frame = frame;
delta->state = remember_state;
return true;
}
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);
}

View File

@ -15,17 +15,16 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <boolean.h>
#include <compat/strl.h>
#include <rhash.h>
#include "netplay_private.h"
#include <net/net_socket.h>
#include <rhash.h>
#include <compat/strl.h>
#include <encodings/crc32.h>
#include "../../movie.h"
#include "../../msg_hash.h"
#include "../../configuration.h"
#include "../../content.h"
#include "../../retroarch.h"
@ -33,6 +32,96 @@
#include "../../version.h"
#include "../../menu/widgets/menu_input_dialog.h"
#ifndef HAVE_SOCKET_LEGACY
/* Custom inet_ntop. Win32 doesn't seem to support this ... */
void netplay_log_connection(const struct sockaddr_storage *their_addr,
unsigned slot, const char *nick)
{
union
{
const struct sockaddr_storage *storage;
const struct sockaddr_in *v4;
const struct sockaddr_in6 *v6;
} u;
const char *str = NULL;
char buf_v4[INET_ADDRSTRLEN] = {0};
char buf_v6[INET6_ADDRSTRLEN] = {0};
char msg[512];
msg[0] = '\0';
u.storage = their_addr;
switch (their_addr->ss_family)
{
case AF_INET:
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
str = buf_v4;
in.sin_family = AF_INET;
memcpy(&in.sin_addr, &u.v4->sin_addr, sizeof(struct in_addr));
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in),
buf_v4, sizeof(buf_v4),
NULL, 0, NI_NUMERICHOST);
}
break;
case AF_INET6:
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
str = buf_v6;
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, &u.v6->sin6_addr, sizeof(struct in6_addr));
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in6),
buf_v6, sizeof(buf_v6), NULL, 0, NI_NUMERICHOST);
}
break;
default:
break;
}
if (str)
{
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM_NAME),
nick, str);
runloop_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
}
else
{
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM),
nick);
runloop_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
}
RARCH_LOG("%s %u\n", msg_hash_to_str(MSG_CONNECTION_SLOT),
slot);
}
#else
void netplay_log_connection(const struct sockaddr_storage *their_addr,
unsigned slot, const char *nick)
{
char msg[512];
msg[0] = '\0';
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM),
nick);
runloop_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
RARCH_LOG("%s %u\n",
msg_hash_to_str(MSG_CONNECTION_SLOT), slot);
}
#endif
/**
* netplay_impl_magic:
*
@ -649,37 +738,3 @@ bool netplay_handshake_pre_sync(netplay_t *netplay, struct netplay_connection *c
/* Ask to go to player mode */
return netplay_cmd_mode(netplay, connection, NETPLAY_CONNECTION_PLAYING);
}
bool netplay_is_server(netplay_t* netplay)
{
if (!netplay)
return false;
return netplay->is_server;
}
bool netplay_delta_frame_ready(netplay_t *netplay, struct delta_frame *delta, uint32_t frame)
{
void *remember_state;
if (delta->used)
{
if (delta->frame == frame) return true;
if (netplay->other_frame_count <= delta->frame)
{
/* We haven't even replayed this frame yet, so we can't overwrite it! */
return false;
}
}
remember_state = delta->state;
memset(delta, 0, sizeof(struct delta_frame));
delta->used = true;
delta->frame = frame;
delta->state = remember_state;
return true;
}
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);
}

View File

@ -228,96 +228,6 @@ static bool init_socket(netplay_t *netplay, void *direct_host, const char *serve
return true;
}
#ifndef HAVE_SOCKET_LEGACY
/* Custom inet_ntop. Win32 doesn't seem to support this ... */
void netplay_log_connection(const struct sockaddr_storage *their_addr,
unsigned slot, const char *nick)
{
union
{
const struct sockaddr_storage *storage;
const struct sockaddr_in *v4;
const struct sockaddr_in6 *v6;
} u;
const char *str = NULL;
char buf_v4[INET_ADDRSTRLEN] = {0};
char buf_v6[INET6_ADDRSTRLEN] = {0};
char msg[512];
msg[0] = '\0';
u.storage = their_addr;
switch (their_addr->ss_family)
{
case AF_INET:
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
str = buf_v4;
in.sin_family = AF_INET;
memcpy(&in.sin_addr, &u.v4->sin_addr, sizeof(struct in_addr));
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in),
buf_v4, sizeof(buf_v4),
NULL, 0, NI_NUMERICHOST);
}
break;
case AF_INET6:
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
str = buf_v6;
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, &u.v6->sin6_addr, sizeof(struct in6_addr));
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in6),
buf_v6, sizeof(buf_v6), NULL, 0, NI_NUMERICHOST);
}
break;
default:
break;
}
if (str)
{
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM_NAME),
nick, str);
runloop_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
}
else
{
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM),
nick);
runloop_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
}
RARCH_LOG("%s %u\n", msg_hash_to_str(MSG_CONNECTION_SLOT),
slot);
}
#else
void netplay_log_connection(const struct sockaddr_storage *their_addr,
unsigned slot, const char *nick)
{
char msg[512];
msg[0] = '\0';
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM),
nick);
runloop_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
RARCH_LOG("%s %u\n",
msg_hash_to_str(MSG_CONNECTION_SLOT), slot);
}
#endif
static bool netplay_init_socket_buffers(netplay_t *netplay)
{
/* Make our packet buffer big enough for a save state and frames-many frames

View File

@ -131,7 +131,7 @@ void netplay_simulate_input(netplay_t *netplay, size_t sim_ptr, bool resim)
static void netplay_handle_frame_hash(netplay_t *netplay, struct delta_frame *delta)
{
static bool crcs_valid = true;
if (netplay_is_server(netplay))
if (netplay->is_server)
{
if (netplay->check_frames &&
(delta->frame % netplay->check_frames == 0 || delta->frame == 1))