2015-12-23 20:25:28 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2016-01-10 03:06:50 +00:00
|
|
|
* Copyright (C) 2011-2016 - Daniel De Matteis
|
2016-09-16 03:04:48 +00:00
|
|
|
* Copyright (C) 2016 - Gregor Richards
|
2015-12-23 20:25:28 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
#include <compat/strl.h>
|
2016-09-05 23:02:25 +00:00
|
|
|
#include <stdio.h>
|
2015-12-25 06:33:21 +00:00
|
|
|
|
|
|
|
#include <net/net_compat.h>
|
2016-05-01 19:38:19 +00:00
|
|
|
#include <net/net_socket.h>
|
2015-12-25 06:33:21 +00:00
|
|
|
#include <retro_endianness.h>
|
|
|
|
|
2015-12-23 20:25:28 +00:00
|
|
|
#include "netplay_private.h"
|
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
#include "retro_assert.h"
|
|
|
|
|
|
|
|
#include "../../autosave.h"
|
2016-09-03 05:51:11 +00:00
|
|
|
|
2015-12-23 20:25:28 +00:00
|
|
|
/**
|
2016-09-16 03:04:48 +00:00
|
|
|
* netplay_spectate_pre_frame:
|
2015-12-23 20:25:28 +00:00
|
|
|
* @netplay : pointer to netplay object
|
|
|
|
*
|
2016-09-16 03:04:48 +00:00
|
|
|
* Pre-frame for Netplay (spectator version).
|
2015-12-23 20:25:28 +00:00
|
|
|
**/
|
2016-09-16 03:04:48 +00:00
|
|
|
static bool netplay_spectate_pre_frame(netplay_t *netplay)
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-09-16 03:04:48 +00:00
|
|
|
if (netplay_is_server(netplay))
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-09-16 03:04:48 +00:00
|
|
|
fd_set fds;
|
|
|
|
int new_fd, idx, i;
|
|
|
|
struct sockaddr_storage their_addr;
|
|
|
|
socklen_t addr_size;
|
|
|
|
retro_ctx_serialize_info_t serial_info;
|
|
|
|
uint32_t header[3];
|
2016-10-21 17:39:51 +00:00
|
|
|
struct timeval tmp_tv = {0};
|
2016-09-16 03:04:48 +00:00
|
|
|
|
|
|
|
netplay->can_poll = true;
|
|
|
|
input_poll_net();
|
|
|
|
|
|
|
|
/* Send our input to any connected spectators */
|
|
|
|
for (i = 0; i < MAX_SPECTATORS; i++)
|
|
|
|
{
|
|
|
|
if (netplay->spectate.fds[i] >= 0)
|
|
|
|
{
|
2016-09-25 15:05:50 +00:00
|
|
|
netplay->input_packet_buffer[2] = htonl(netplay->self_frame_count - netplay->spectate.frames[i]);
|
|
|
|
if (!socket_send_all_blocking(netplay->spectate.fds[i], netplay->input_packet_buffer, sizeof(netplay->input_packet_buffer), false))
|
2016-09-16 03:04:48 +00:00
|
|
|
{
|
|
|
|
socket_close(netplay->spectate.fds[i]);
|
|
|
|
netplay->spectate.fds[i] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
/* Check for connections */
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(netplay->fd, &fds);
|
|
|
|
if (socket_select(netplay->fd + 1, &fds, NULL, NULL, &tmp_tv) <= 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!FD_ISSET(netplay->fd, &fds))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
addr_size = sizeof(their_addr);
|
|
|
|
new_fd = accept(netplay->fd, (struct sockaddr*)&their_addr, &addr_size);
|
|
|
|
if (new_fd < 0)
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-09-16 03:04:48 +00:00
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_ACCEPT_INCOMING_SPECTATOR));
|
|
|
|
return true;
|
2015-12-23 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
idx = -1;
|
|
|
|
for (i = 0; i < MAX_SPECTATORS; i++)
|
|
|
|
{
|
|
|
|
if (netplay->spectate.fds[i] == -1)
|
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
/* No vacant client streams :( */
|
|
|
|
if (idx == -1)
|
|
|
|
{
|
|
|
|
socket_close(new_fd);
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
if (!netplay_get_nickname(netplay, new_fd))
|
|
|
|
{
|
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT));
|
|
|
|
socket_close(new_fd);
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
if (!netplay_send_nickname(netplay, new_fd))
|
|
|
|
{
|
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_SEND_NICKNAME_TO_CLIENT));
|
|
|
|
socket_close(new_fd);
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-30 18:03:18 +00:00
|
|
|
/* Wait until it's safe to serialize */
|
|
|
|
if (netplay->quirks & NETPLAY_QUIRK_INITIALIZATION)
|
|
|
|
{
|
|
|
|
netplay->is_replay = true;
|
|
|
|
netplay->replay_ptr = netplay->self_ptr;
|
|
|
|
netplay->replay_frame_count = netplay->self_frame_count;
|
|
|
|
netplay_wait_and_init_serialization(netplay);
|
|
|
|
netplay->is_replay = false;
|
|
|
|
}
|
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
/* Start them at the current frame */
|
|
|
|
netplay->spectate.frames[idx] = netplay->self_frame_count;
|
|
|
|
serial_info.data_const = NULL;
|
|
|
|
serial_info.data = netplay->buffer[netplay->self_ptr].state;
|
|
|
|
serial_info.size = netplay->state_size;
|
|
|
|
if (core_serialize(&serial_info))
|
|
|
|
{
|
|
|
|
/* Send them the savestate */
|
|
|
|
header[0] = htonl(NETPLAY_CMD_LOAD_SAVESTATE);
|
|
|
|
header[1] = htonl(serial_info.size + sizeof(uint32_t));
|
|
|
|
header[2] = htonl(0);
|
|
|
|
if (!socket_send_all_blocking(new_fd, header, sizeof(header), false))
|
|
|
|
{
|
|
|
|
socket_close(new_fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!socket_send_all_blocking(new_fd, serial_info.data, serial_info.size, false))
|
|
|
|
{
|
|
|
|
socket_close(new_fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
/* And send them this frame's input */
|
2016-09-25 15:05:50 +00:00
|
|
|
netplay->input_packet_buffer[2] = htonl(0);
|
|
|
|
if (!socket_send_all_blocking(new_fd, netplay->input_packet_buffer, sizeof(netplay->input_packet_buffer), false))
|
2016-09-16 03:04:48 +00:00
|
|
|
{
|
|
|
|
socket_close(new_fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
netplay->spectate.fds[idx] = new_fd;
|
2015-12-23 20:25:28 +00:00
|
|
|
|
|
|
|
}
|
2016-09-16 03:04:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (netplay_delta_frame_ready(netplay, &netplay->buffer[netplay->self_ptr], netplay->self_frame_count))
|
|
|
|
{
|
|
|
|
/* Mark our own data as already read, so we ignore local input */
|
|
|
|
netplay->buffer[netplay->self_ptr].have_local = true;
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
netplay->can_poll = true;
|
|
|
|
input_poll_net();
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
/* Only proceed if we have data */
|
|
|
|
if (netplay->read_frame_count <= netplay->self_frame_count)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-12-23 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-16 03:04:48 +00:00
|
|
|
* netplay_spectate_post_frame:
|
2015-12-23 20:25:28 +00:00
|
|
|
* @netplay : pointer to netplay object
|
|
|
|
*
|
2016-09-16 03:04:48 +00:00
|
|
|
* Post-frame for Netplay (spectator version).
|
|
|
|
* Not much here, just fast forward if we're behind the server.
|
2015-12-23 20:25:28 +00:00
|
|
|
**/
|
2015-12-24 18:23:46 +00:00
|
|
|
static void netplay_spectate_post_frame(netplay_t *netplay)
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-09-16 03:04:48 +00:00
|
|
|
netplay->self_ptr = NEXT_PTR(netplay->self_ptr);
|
|
|
|
netplay->self_frame_count++;
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
if (netplay_is_server(netplay))
|
|
|
|
{
|
|
|
|
/* Not expecting any client data */
|
|
|
|
netplay->read_ptr = netplay->other_ptr = netplay->self_ptr;
|
|
|
|
netplay->read_frame_count = netplay->other_frame_count = netplay->self_frame_count;
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
}
|
|
|
|
else
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-09-16 03:04:48 +00:00
|
|
|
/* If we must rewind, it's because we got a save state */
|
|
|
|
if (netplay->force_rewind)
|
|
|
|
{
|
|
|
|
retro_ctx_serialize_info_t serial_info;
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
/* Replay frames. */
|
|
|
|
netplay->is_replay = true;
|
|
|
|
netplay->replay_ptr = netplay->other_ptr;
|
|
|
|
netplay->replay_frame_count = netplay->other_frame_count;
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-30 18:03:18 +00:00
|
|
|
/* Wait until it's safe to serialize */
|
|
|
|
if (netplay->quirks & NETPLAY_QUIRK_INITIALIZATION)
|
|
|
|
netplay_wait_and_init_serialization(netplay);
|
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
serial_info.data = NULL;
|
|
|
|
serial_info.data_const = netplay->buffer[netplay->replay_ptr].state;
|
|
|
|
serial_info.size = netplay->state_size;
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
core_unserialize(&serial_info);
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
while (netplay->replay_frame_count < netplay->self_frame_count)
|
|
|
|
{
|
|
|
|
autosave_lock();
|
|
|
|
core_run();
|
|
|
|
autosave_unlock();
|
|
|
|
netplay->replay_ptr = NEXT_PTR(netplay->replay_ptr);
|
|
|
|
netplay->replay_frame_count++;
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
netplay->is_replay = false;
|
|
|
|
netplay->force_rewind = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're in sync by definition */
|
|
|
|
if (netplay->read_frame_count < netplay->self_frame_count)
|
|
|
|
{
|
|
|
|
netplay->other_ptr = netplay->read_ptr;
|
|
|
|
netplay->other_frame_count = netplay->read_frame_count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
netplay->other_ptr = netplay->self_ptr;
|
|
|
|
netplay->other_frame_count = netplay->self_frame_count;
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
/* If the server gets significantly ahead, skip to catch up */
|
2016-12-01 18:34:37 +00:00
|
|
|
if (netplay->self_frame_count + netplay->delay_frames <= netplay->read_frame_count)
|
2016-09-16 03:04:48 +00:00
|
|
|
{
|
|
|
|
/* "Replay" into the future */
|
|
|
|
netplay->is_replay = true;
|
|
|
|
netplay->replay_ptr = netplay->self_ptr;
|
|
|
|
netplay->replay_frame_count = netplay->self_frame_count;
|
|
|
|
|
|
|
|
while (netplay->replay_frame_count < netplay->read_frame_count - 1)
|
|
|
|
{
|
|
|
|
autosave_lock();
|
|
|
|
core_run();
|
|
|
|
autosave_unlock();
|
|
|
|
|
|
|
|
netplay->replay_ptr = NEXT_PTR(netplay->replay_ptr);
|
|
|
|
netplay->replay_frame_count++;
|
|
|
|
netplay->self_ptr = netplay->replay_ptr;
|
|
|
|
netplay->self_frame_count = netplay->replay_frame_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
netplay->is_replay = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-12-23 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 03:04:48 +00:00
|
|
|
static bool netplay_spectate_info_cb(netplay_t* netplay, unsigned frames)
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-09-16 03:04:48 +00:00
|
|
|
if (netplay_is_server(netplay))
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-09-16 03:04:48 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_SPECTATORS; i++)
|
|
|
|
netplay->spectate.fds[i] = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!netplay_send_nickname(netplay, netplay->fd))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!netplay_get_nickname(netplay, netplay->fd))
|
2015-12-23 20:25:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-03 16:52:27 +00:00
|
|
|
netplay->status = RARCH_NETPLAY_CONNECTION_PLAYING;
|
2016-09-16 03:04:48 +00:00
|
|
|
|
2015-12-23 20:25:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct netplay_callbacks* netplay_get_cbs_spectate(void)
|
|
|
|
{
|
|
|
|
static struct netplay_callbacks cbs = {
|
2015-12-24 18:23:46 +00:00
|
|
|
&netplay_spectate_pre_frame,
|
|
|
|
&netplay_spectate_post_frame,
|
|
|
|
&netplay_spectate_info_cb
|
2015-12-23 20:25:28 +00:00
|
|
|
};
|
|
|
|
return &cbs;
|
2016-01-10 03:06:50 +00:00
|
|
|
}
|