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
|
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-05 23:02:25 +00:00
|
|
|
#include <stdio.h>
|
2015-12-25 06:33:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#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-03 05:51:11 +00:00
|
|
|
#include "../../runloop.h"
|
|
|
|
|
2015-12-23 20:25:28 +00:00
|
|
|
/**
|
|
|
|
* netplay_pre_frame_spectate:
|
|
|
|
* @netplay : pointer to netplay object
|
|
|
|
*
|
|
|
|
* Pre-frame for Netplay (spectate mode version).
|
|
|
|
**/
|
2015-12-24 18:23:46 +00:00
|
|
|
static void netplay_spectate_pre_frame(netplay_t *netplay)
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
uint32_t *header;
|
|
|
|
int new_fd, idx, bufsize;
|
|
|
|
size_t header_size;
|
|
|
|
struct sockaddr_storage their_addr;
|
|
|
|
socklen_t addr_size;
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval tmp_tv = {0};
|
|
|
|
|
2016-05-12 08:20:14 +00:00
|
|
|
if (!netplay_is_server(netplay))
|
2015-12-23 20:25:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(netplay->fd, &fds);
|
|
|
|
|
|
|
|
if (socket_select(netplay->fd + 1, &fds, NULL, NULL, &tmp_tv) <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!FD_ISSET(netplay->fd, &fds))
|
|
|
|
return;
|
|
|
|
|
|
|
|
addr_size = sizeof(their_addr);
|
|
|
|
new_fd = accept(netplay->fd, (struct sockaddr*)&their_addr, &addr_size);
|
|
|
|
if (new_fd < 0)
|
|
|
|
{
|
2016-06-28 10:08:30 +00:00
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_ACCEPT_INCOMING_SPECTATOR));
|
2015-12-23 20:25:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = -1;
|
|
|
|
for (i = 0; i < MAX_SPECTATORS; i++)
|
|
|
|
{
|
|
|
|
if (netplay->spectate.fds[i] == -1)
|
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No vacant client streams :( */
|
|
|
|
if (idx == -1)
|
|
|
|
{
|
|
|
|
socket_close(new_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:03:43 +00:00
|
|
|
if (!netplay_get_nickname(netplay, new_fd))
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-06-28 10:08:30 +00:00
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT));
|
2015-12-23 20:25:28 +00:00
|
|
|
socket_close(new_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:03:43 +00:00
|
|
|
if (!netplay_send_nickname(netplay, new_fd))
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-06-28 10:08:30 +00:00
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_SEND_NICKNAME_TO_CLIENT));
|
2015-12-23 20:25:28 +00:00
|
|
|
socket_close(new_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:03:43 +00:00
|
|
|
header = netplay_bsv_header_generate(&header_size,
|
|
|
|
netplay_impl_magic());
|
2015-12-23 20:25:28 +00:00
|
|
|
|
|
|
|
if (!header)
|
|
|
|
{
|
2016-06-28 10:08:30 +00:00
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_GENERATE_BSV_HEADER));
|
2015-12-23 20:25:28 +00:00
|
|
|
socket_close(new_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufsize = header_size;
|
|
|
|
setsockopt(new_fd, SOL_SOCKET, SO_SNDBUF, (const char*)&bufsize,
|
|
|
|
sizeof(int));
|
|
|
|
|
2016-05-01 21:17:17 +00:00
|
|
|
if (!socket_send_all_blocking(new_fd, header, header_size, false))
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-06-28 10:08:30 +00:00
|
|
|
RARCH_ERR("%s\n", msg_hash_to_str(MSG_FAILED_TO_SEND_HEADER_TO_CLIENT));
|
2015-12-23 20:25:28 +00:00
|
|
|
socket_close(new_fd);
|
|
|
|
free(header);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(header);
|
|
|
|
netplay->spectate.fds[idx] = new_fd;
|
|
|
|
|
|
|
|
#ifndef HAVE_SOCKET_LEGACY
|
2016-05-12 10:03:43 +00:00
|
|
|
netplay_log_connection(&their_addr, idx, netplay->other_nick);
|
2015-12-23 20:25:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* netplay_post_frame_spectate:
|
|
|
|
* @netplay : pointer to netplay object
|
|
|
|
*
|
|
|
|
* Post-frame for Netplay (spectate mode version).
|
|
|
|
* We check if we have new input and replay from recorded input.
|
|
|
|
**/
|
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
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2016-05-12 08:20:14 +00:00
|
|
|
if (!netplay_is_server(netplay))
|
2015-12-23 20:25:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_SPECTATORS; i++)
|
|
|
|
{
|
|
|
|
char msg[128];
|
|
|
|
|
|
|
|
if (netplay->spectate.fds[i] == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (socket_send_all_blocking(netplay->spectate.fds[i],
|
|
|
|
netplay->spectate.input,
|
2016-05-01 21:17:17 +00:00
|
|
|
netplay->spectate.input_ptr * sizeof(int16_t),
|
|
|
|
false))
|
2015-12-23 20:25:28 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
RARCH_LOG("Client (#%u) disconnected ...\n", i);
|
|
|
|
|
|
|
|
snprintf(msg, sizeof(msg), "Client (#%u) disconnected.", i);
|
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
|
|
|
|
|
|
|
socket_close(netplay->spectate.fds[i]);
|
|
|
|
netplay->spectate.fds[i] = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
netplay->spectate.input_ptr = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-24 18:23:46 +00:00
|
|
|
static bool netplay_spectate_info_cb(netplay_t *netplay, unsigned frames)
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
2016-05-12 08:20:14 +00:00
|
|
|
if(netplay_is_server(netplay))
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
2016-05-12 10:03:43 +00:00
|
|
|
if(!netplay_get_info(netplay))
|
2015-12-23 20:25:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_SPECTATORS; i++)
|
|
|
|
netplay->spectate.fds[i] = -1;
|
|
|
|
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
|
|
|
}
|