2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 02:43:11 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-01-06 22:46:50 +00:00
|
|
|
#include "Common/Common.h"
|
2016-01-25 02:46:46 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-05-08 21:28:03 +00:00
|
|
|
#include "Common/ENetUtil.h"
|
2016-01-25 02:46:46 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2015-05-08 21:28:03 +00:00
|
|
|
#include "Common/Timer.h"
|
2014-02-19 18:09:14 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2015-02-02 10:08:58 +00:00
|
|
|
#include "Core/Movie.h"
|
|
|
|
#include "Core/NetPlayClient.h"
|
2014-02-19 18:09:14 +00:00
|
|
|
#include "Core/HW/EXI_DeviceIPL.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/SI.h"
|
|
|
|
#include "Core/HW/SI_DeviceGCController.h"
|
2015-06-14 11:59:41 +00:00
|
|
|
#include "Core/HW/Sram.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
|
2014-02-19 18:09:14 +00:00
|
|
|
#include "Core/HW/WiimoteReal/WiimoteReal.h"
|
|
|
|
#include "Core/IPC_HLE/WII_IPC_HLE_Device_usb.h"
|
2013-08-05 09:05:06 +00:00
|
|
|
|
2016-01-06 22:46:50 +00:00
|
|
|
static const char* NETPLAY_VERSION = scm_rev_git_str;
|
2014-07-08 13:58:25 +00:00
|
|
|
static std::mutex crit_netplay_client;
|
2014-03-09 20:14:26 +00:00
|
|
|
static NetPlayClient * netplay_client = nullptr;
|
2013-08-05 09:05:06 +00:00
|
|
|
NetSettings g_NetPlaySettings;
|
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
// called from ---GUI--- thread
|
|
|
|
NetPlayClient::~NetPlayClient()
|
|
|
|
{
|
2013-08-05 08:56:30 +00:00
|
|
|
// not perfect
|
2015-05-21 23:52:26 +00:00
|
|
|
if (m_is_running.load())
|
2013-08-05 08:56:30 +00:00
|
|
|
StopGame();
|
|
|
|
|
2016-01-25 02:52:02 +00:00
|
|
|
if (m_is_connected)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2015-05-21 23:52:26 +00:00
|
|
|
m_do_loop.store(false);
|
2011-01-27 20:47:58 +00:00
|
|
|
m_thread.join();
|
2013-08-05 08:56:30 +00:00
|
|
|
}
|
2015-02-02 09:27:06 +00:00
|
|
|
|
|
|
|
if (m_server)
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
if (g_MainNetHost.get() == m_client)
|
|
|
|
{
|
|
|
|
g_MainNetHost.release();
|
|
|
|
}
|
|
|
|
if (m_client)
|
|
|
|
{
|
|
|
|
enet_host_destroy(m_client);
|
|
|
|
m_client = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_traversal_client)
|
|
|
|
{
|
|
|
|
ReleaseTraversalClient();
|
|
|
|
}
|
2010-05-05 04:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---GUI--- thread
|
2015-05-29 00:28:48 +00:00
|
|
|
NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, const std::string& name, bool traversal, const std::string& centralServer, u16 centralPort)
|
2016-01-25 02:58:56 +00:00
|
|
|
: m_dialog(dialog)
|
2016-01-25 03:06:57 +00:00
|
|
|
, m_player_name(name)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2013-08-05 08:56:30 +00:00
|
|
|
ClearBuffers();
|
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
if (!traversal)
|
2015-02-02 09:27:06 +00:00
|
|
|
{
|
2015-02-02 09:56:53 +00:00
|
|
|
//Direct Connection
|
|
|
|
m_client = enet_host_create(nullptr, 1, 3, 0, 0);
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
if (m_client == nullptr)
|
|
|
|
{
|
|
|
|
PanicAlertT("Couldn't Create Client");
|
|
|
|
}
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
ENetAddress addr;
|
|
|
|
enet_address_set_host(&addr, address.c_str());
|
|
|
|
addr.port = port;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
m_server = enet_host_connect(m_client, &addr, 3, 0);
|
|
|
|
|
|
|
|
if (m_server == nullptr)
|
|
|
|
{
|
|
|
|
PanicAlertT("Couldn't create peer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
ENetEvent netEvent;
|
|
|
|
int net = enet_host_service(m_client, &netEvent, 5000);
|
|
|
|
if (net > 0 && netEvent.type == ENET_EVENT_TYPE_CONNECT)
|
|
|
|
{
|
|
|
|
if (Connect())
|
2015-03-14 14:19:18 +00:00
|
|
|
{
|
|
|
|
m_client->intercept = ENetUtil::InterceptCallback;
|
2015-02-02 09:56:53 +00:00
|
|
|
m_thread = std::thread(&NetPlayClient::ThreadFunc, this);
|
2015-03-14 14:19:18 +00:00
|
|
|
}
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PanicAlertT("Failed to Connect!");
|
|
|
|
}
|
2015-02-02 09:27:06 +00:00
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
}
|
|
|
|
else
|
2014-02-10 14:26:29 +00:00
|
|
|
{
|
2015-02-15 03:51:08 +00:00
|
|
|
if (address.size() > NETPLAY_CODE_SIZE)
|
|
|
|
{
|
|
|
|
PanicAlertT("Host code size is to large.\nPlease recheck that you have the correct code");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureTraversalClient(centralServer, centralPort))
|
2015-02-02 09:56:53 +00:00
|
|
|
return;
|
|
|
|
m_client = g_MainNetHost.get();
|
|
|
|
|
|
|
|
m_traversal_client = g_TraversalClient.get();
|
|
|
|
|
|
|
|
// If we were disconnected in the background, reconnect.
|
|
|
|
if (m_traversal_client->m_State == TraversalClient::Failure)
|
|
|
|
m_traversal_client->ReconnectToServer();
|
|
|
|
m_traversal_client->m_Client = this;
|
|
|
|
m_host_spec = address;
|
2016-01-25 02:58:56 +00:00
|
|
|
m_connection_state = ConnectionState::WaitingForTraversalClientConnection;
|
2015-02-02 09:56:53 +00:00
|
|
|
OnTraversalStateChanged();
|
|
|
|
m_connecting = true;
|
|
|
|
|
2015-03-28 19:15:34 +00:00
|
|
|
Common::Timer connect_timer;
|
|
|
|
connect_timer.Start();
|
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
while (m_connecting)
|
|
|
|
{
|
|
|
|
ENetEvent netEvent;
|
|
|
|
if (m_traversal_client)
|
|
|
|
m_traversal_client->HandleResends();
|
|
|
|
|
|
|
|
while (enet_host_service(m_client, &netEvent, 4) > 0)
|
|
|
|
{
|
|
|
|
sf::Packet rpac;
|
|
|
|
switch (netEvent.type)
|
|
|
|
{
|
|
|
|
case ENET_EVENT_TYPE_CONNECT:
|
|
|
|
m_server = netEvent.peer;
|
|
|
|
if (Connect())
|
|
|
|
{
|
2016-01-25 02:58:56 +00:00
|
|
|
m_connection_state = ConnectionState::Connected;
|
2015-02-02 09:56:53 +00:00
|
|
|
m_thread = std::thread(&NetPlayClient::ThreadFunc, this);
|
|
|
|
}
|
|
|
|
return;
|
2015-02-02 10:08:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-28 19:15:34 +00:00
|
|
|
if (connect_timer.GetTimeElapsed() > 5000)
|
|
|
|
break;
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
PanicAlertT("Failed To Connect!");
|
2014-02-10 14:26:29 +00:00
|
|
|
}
|
2010-05-05 04:44:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
bool NetPlayClient::Connect()
|
|
|
|
{
|
|
|
|
// send connect message
|
|
|
|
sf::Packet spac;
|
|
|
|
spac << NETPLAY_VERSION;
|
|
|
|
spac << netplay_dolphin_ver;
|
|
|
|
spac << m_player_name;
|
|
|
|
Send(spac);
|
|
|
|
enet_host_flush(m_client);
|
|
|
|
sf::Packet rpac;
|
|
|
|
// TODO: make this not hang
|
|
|
|
ENetEvent netEvent;
|
|
|
|
if (enet_host_service(m_client, &netEvent, 5000) > 0 && netEvent.type == ENET_EVENT_TYPE_RECEIVE)
|
|
|
|
{
|
|
|
|
rpac.append(netEvent.packet->data, netEvent.packet->dataLength);
|
2015-08-18 12:09:17 +00:00
|
|
|
enet_packet_destroy(netEvent.packet);
|
2015-02-02 09:27:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageId error;
|
|
|
|
rpac >> error;
|
|
|
|
|
|
|
|
// got error message
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case CON_ERR_SERVER_FULL:
|
|
|
|
PanicAlertT("The server is full!");
|
|
|
|
break;
|
|
|
|
case CON_ERR_VERSION_MISMATCH:
|
|
|
|
PanicAlertT("The server and client's NetPlay versions are incompatible!");
|
|
|
|
break;
|
|
|
|
case CON_ERR_GAME_RUNNING:
|
|
|
|
PanicAlertT("The server responded: the game is currently running!");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PanicAlertT("The server sent an unknown error message!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rpac >> m_pid;
|
|
|
|
|
|
|
|
Player player;
|
|
|
|
player.name = m_player_name;
|
|
|
|
player.pid = m_pid;
|
|
|
|
player.revision = netplay_dolphin_ver;
|
|
|
|
|
|
|
|
// add self to player list
|
|
|
|
m_players[m_pid] = player;
|
|
|
|
m_local_player = &m_players[m_pid];
|
|
|
|
|
|
|
|
m_dialog->Update();
|
|
|
|
|
2016-01-25 02:52:02 +00:00
|
|
|
m_is_connected = true;
|
2015-02-02 09:27:06 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
// called from ---NETPLAY--- thread
|
|
|
|
unsigned int NetPlayClient::OnData(sf::Packet& packet)
|
|
|
|
{
|
|
|
|
MessageId mid;
|
|
|
|
packet >> mid;
|
|
|
|
|
|
|
|
switch (mid)
|
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_PLAYER_JOIN:
|
|
|
|
{
|
|
|
|
Player player;
|
|
|
|
packet >> player.pid;
|
|
|
|
packet >> player.name;
|
|
|
|
packet >> player.revision;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
{
|
2011-03-05 06:11:26 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
|
2010-05-05 04:44:19 +00:00
|
|
|
m_players[player.pid] = player;
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
m_dialog->Update();
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_PLAYER_LEAVE:
|
|
|
|
{
|
|
|
|
PlayerId pid;
|
|
|
|
packet >> pid;
|
|
|
|
|
|
|
|
{
|
2011-03-05 06:11:26 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
|
2010-05-05 04:44:19 +00:00
|
|
|
m_players.erase(m_players.find(pid));
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
m_dialog->Update();
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_CHAT_MESSAGE:
|
|
|
|
{
|
|
|
|
PlayerId pid;
|
|
|
|
packet >> pid;
|
|
|
|
std::string msg;
|
|
|
|
packet >> msg;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
// don't need lock to read in this thread
|
|
|
|
const Player& player = m_players[pid];
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
// add to gui
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << player.name << '[' << (char)(pid + '0') << "]: " << msg;
|
|
|
|
|
|
|
|
m_dialog->AppendChat(ss.str());
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_PAD_MAPPING:
|
|
|
|
{
|
|
|
|
for (PadMapping& mapping : m_pad_map)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
packet >> mapping;
|
|
|
|
}
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
UpdateDevices();
|
2013-08-24 00:24:45 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
m_dialog->Update();
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_WIIMOTE_MAPPING:
|
|
|
|
{
|
|
|
|
for (PadMapping& mapping : m_wiimote_map)
|
2013-08-07 03:48:52 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
packet >> mapping;
|
2013-08-07 03:48:52 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
m_dialog->Update();
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_PAD_DATA:
|
|
|
|
{
|
|
|
|
PadMapping map = 0;
|
|
|
|
GCPadStatus pad;
|
|
|
|
packet >> map >> pad.button >> pad.analogA >> pad.analogB >> pad.stickX >> pad.stickY >> pad.substickX >> pad.substickY >> pad.triggerLeft >> pad.triggerRight;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
// trusting server for good map value (>=0 && <4)
|
|
|
|
// add to pad buffer
|
|
|
|
m_pad_buffer[map].Push(pad);
|
|
|
|
}
|
|
|
|
break;
|
2013-08-07 03:48:52 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_WIIMOTE_DATA:
|
|
|
|
{
|
|
|
|
PadMapping map = 0;
|
|
|
|
NetWiimote nw;
|
|
|
|
u8 size;
|
|
|
|
packet >> map >> size;
|
|
|
|
|
|
|
|
nw.resize(size);
|
2015-05-25 02:53:25 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
for (unsigned int i = 0; i < size; ++i)
|
2015-05-25 02:53:25 +00:00
|
|
|
packet >> nw[i];
|
2015-02-02 09:27:06 +00:00
|
|
|
|
|
|
|
// trusting server for good map value (>=0 && <4)
|
|
|
|
// add to Wiimote buffer
|
|
|
|
m_wiimote_buffer[(unsigned)map].Push(nw);
|
|
|
|
}
|
|
|
|
break;
|
2013-08-07 03:48:52 +00:00
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_PAD_BUFFER:
|
|
|
|
{
|
|
|
|
u32 size = 0;
|
|
|
|
packet >> size;
|
|
|
|
|
|
|
|
m_target_buffer_size = size;
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_CHANGE_GAME:
|
|
|
|
{
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2011-03-05 06:11:26 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
|
2010-05-05 04:44:19 +00:00
|
|
|
packet >> m_selected_game;
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
// update gui
|
|
|
|
m_dialog->OnMsgChangeGame(m_selected_game);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NP_MSG_START_GAME:
|
|
|
|
{
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2011-03-05 06:11:26 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
|
2010-11-16 01:55:29 +00:00
|
|
|
packet >> m_current_game;
|
2013-08-18 05:43:49 +00:00
|
|
|
packet >> g_NetPlaySettings.m_CPUthread;
|
2014-07-07 03:30:06 +00:00
|
|
|
packet >> g_NetPlaySettings.m_CPUcore;
|
2015-06-14 11:59:41 +00:00
|
|
|
packet >> g_NetPlaySettings.m_SelectedLanguage;
|
|
|
|
packet >> g_NetPlaySettings.m_OverrideGCLanguage;
|
2015-06-24 10:41:10 +00:00
|
|
|
packet >> g_NetPlaySettings.m_ProgressiveScan;
|
|
|
|
packet >> g_NetPlaySettings.m_PAL60;
|
2013-07-19 22:48:27 +00:00
|
|
|
packet >> g_NetPlaySettings.m_DSPEnableJIT;
|
|
|
|
packet >> g_NetPlaySettings.m_DSPHLE;
|
2013-07-22 10:15:08 +00:00
|
|
|
packet >> g_NetPlaySettings.m_WriteToMemcard;
|
2014-12-31 18:24:14 +00:00
|
|
|
packet >> g_NetPlaySettings.m_OCEnable;
|
|
|
|
packet >> g_NetPlaySettings.m_OCFactor;
|
2015-02-02 09:27:06 +00:00
|
|
|
|
2013-09-05 00:16:28 +00:00
|
|
|
int tmp;
|
|
|
|
packet >> tmp;
|
2015-02-02 09:27:06 +00:00
|
|
|
g_NetPlaySettings.m_EXIDevice[0] = (TEXIDevices)tmp;
|
2013-09-05 00:16:28 +00:00
|
|
|
packet >> tmp;
|
2015-02-02 09:27:06 +00:00
|
|
|
g_NetPlaySettings.m_EXIDevice[1] = (TEXIDevices)tmp;
|
2015-02-02 10:08:58 +00:00
|
|
|
|
2015-05-24 02:58:39 +00:00
|
|
|
u32 time_low, time_high;
|
|
|
|
packet >> time_low;
|
|
|
|
packet >> time_high;
|
|
|
|
g_netplay_initial_gctime = time_low | ((u64)time_high << 32);
|
2010-05-05 04:44:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
m_dialog->OnMsgStartGame();
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_STOP_GAME:
|
|
|
|
{
|
|
|
|
m_dialog->OnMsgStopGame();
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_DISABLE_GAME:
|
|
|
|
{
|
2015-03-07 09:40:24 +00:00
|
|
|
PanicAlertT("Other client disconnected while game is running!! NetPlay is disabled. You must manually stop the game.");
|
2015-05-21 23:52:26 +00:00
|
|
|
m_is_running.store(false);
|
2015-02-02 09:27:06 +00:00
|
|
|
NetPlay_Disable();
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
case NP_MSG_PING:
|
|
|
|
{
|
|
|
|
u32 ping_key = 0;
|
|
|
|
packet >> ping_key;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
sf::Packet spac;
|
|
|
|
spac << (MessageId)NP_MSG_PONG;
|
|
|
|
spac << ping_key;
|
|
|
|
|
|
|
|
Send(spac);
|
|
|
|
}
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2013-08-05 09:50:26 +00:00
|
|
|
case NP_MSG_PLAYER_PING_DATA:
|
2015-02-02 09:27:06 +00:00
|
|
|
{
|
|
|
|
PlayerId pid;
|
|
|
|
packet >> pid;
|
2013-08-05 09:50:26 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
{
|
2013-08-05 09:50:26 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
|
|
|
|
Player& player = m_players[pid];
|
|
|
|
packet >> player.ping;
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
m_dialog->Update();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-08 10:50:47 +00:00
|
|
|
case NP_MSG_DESYNC_DETECTED:
|
|
|
|
{
|
2015-06-05 23:00:26 +00:00
|
|
|
int pid_to_blame;
|
2015-03-08 10:50:47 +00:00
|
|
|
u32 frame;
|
2015-06-05 23:00:26 +00:00
|
|
|
packet >> pid_to_blame;
|
2015-03-08 10:50:47 +00:00
|
|
|
packet >> frame;
|
2015-06-05 23:00:26 +00:00
|
|
|
const char* blame_str = "";
|
|
|
|
const char* blame_name = "";
|
|
|
|
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
|
|
|
|
if (pid_to_blame != -1)
|
|
|
|
{
|
|
|
|
auto it = m_players.find(pid_to_blame);
|
|
|
|
blame_str = " from player ";
|
|
|
|
blame_name = it != m_players.end() ? it->second.name.c_str() : "??";
|
|
|
|
}
|
2015-03-08 10:50:47 +00:00
|
|
|
|
2015-06-05 23:00:26 +00:00
|
|
|
m_dialog->AppendChat(StringFromFormat("/!\\ Possible desync detected%s%s on frame %u", blame_str, blame_name, frame));
|
2015-03-08 10:50:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-06-14 11:59:41 +00:00
|
|
|
case NP_MSG_SYNC_GC_SRAM:
|
|
|
|
{
|
|
|
|
u8 sram[sizeof(g_SRAM.p_SRAM)];
|
2015-06-23 19:11:56 +00:00
|
|
|
for (size_t i = 0; i < sizeof(g_SRAM.p_SRAM); ++i)
|
2015-06-14 11:59:41 +00:00
|
|
|
{
|
|
|
|
packet >> sram[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
|
|
|
|
memcpy(g_SRAM.p_SRAM, sram, sizeof(g_SRAM.p_SRAM));
|
|
|
|
g_SRAM_netplay_initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
default:
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("Unknown message received with id : %d", mid);
|
2010-05-05 04:44:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
void NetPlayClient::Send(sf::Packet& packet)
|
|
|
|
{
|
|
|
|
ENetPacket* epac = enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(m_server, 0, epac);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetPlayClient::Disconnect()
|
|
|
|
{
|
|
|
|
ENetEvent netEvent;
|
2015-02-15 03:51:08 +00:00
|
|
|
m_connecting = false;
|
2016-01-25 02:58:56 +00:00
|
|
|
m_connection_state = ConnectionState::Failure;
|
2015-02-15 03:51:08 +00:00
|
|
|
if (m_server)
|
|
|
|
enet_peer_disconnect(m_server, 0);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
while (enet_host_service(m_client, &netEvent, 3000) > 0)
|
|
|
|
{
|
|
|
|
switch (netEvent.type)
|
|
|
|
{
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
|
|
|
enet_packet_destroy(netEvent.packet);
|
|
|
|
break;
|
|
|
|
case ENET_EVENT_TYPE_DISCONNECT:
|
|
|
|
m_server = nullptr;
|
|
|
|
return;
|
2015-02-02 10:08:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
2015-02-02 09:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//didn't disconnect gracefully force disconnect
|
|
|
|
enet_peer_reset(m_server);
|
|
|
|
m_server = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-13 01:03:09 +00:00
|
|
|
void NetPlayClient::SendAsync(sf::Packet* packet)
|
2015-03-09 16:31:13 +00:00
|
|
|
{
|
|
|
|
{
|
2015-03-13 01:03:09 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkq(m_crit.async_queue_write);
|
|
|
|
m_async_queue.Push(std::unique_ptr<sf::Packet>(packet));
|
2015-03-09 16:31:13 +00:00
|
|
|
}
|
2015-03-09 17:37:02 +00:00
|
|
|
ENetUtil::WakeupThread(m_client);
|
2015-03-09 16:31:13 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
// called from ---NETPLAY--- thread
|
2011-01-31 08:19:27 +00:00
|
|
|
void NetPlayClient::ThreadFunc()
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2015-05-21 23:52:26 +00:00
|
|
|
while (m_do_loop.load())
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
ENetEvent netEvent;
|
|
|
|
int net;
|
2015-03-09 16:31:13 +00:00
|
|
|
if (m_traversal_client)
|
|
|
|
m_traversal_client->HandleResends();
|
2015-03-14 14:19:18 +00:00
|
|
|
net = enet_host_service(m_client, &netEvent, 250);
|
2015-03-13 01:03:09 +00:00
|
|
|
while (!m_async_queue.Empty())
|
2015-02-02 09:27:06 +00:00
|
|
|
{
|
2015-03-13 01:03:09 +00:00
|
|
|
Send(*(m_async_queue.Front().get()));
|
|
|
|
m_async_queue.Pop();
|
2015-02-02 09:27:06 +00:00
|
|
|
}
|
|
|
|
if (net > 0)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
|
|
|
sf::Packet rpac;
|
2015-02-02 09:27:06 +00:00
|
|
|
switch (netEvent.type)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
|
|
|
rpac.append(netEvent.packet->data, netEvent.packet->dataLength);
|
2010-05-05 04:44:19 +00:00
|
|
|
OnData(rpac);
|
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
enet_packet_destroy(netEvent.packet);
|
|
|
|
break;
|
|
|
|
case ENET_EVENT_TYPE_DISCONNECT:
|
2015-05-21 23:52:26 +00:00
|
|
|
m_is_running.store(false);
|
2010-05-05 04:44:19 +00:00
|
|
|
NetPlay_Disable();
|
2011-02-08 15:36:15 +00:00
|
|
|
m_dialog->AppendChat("< LOST CONNECTION TO SERVER >");
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("Lost connection to server!");
|
2015-05-21 23:52:26 +00:00
|
|
|
m_do_loop.store(false);
|
2015-02-02 09:27:06 +00:00
|
|
|
|
|
|
|
netEvent.peer->data = nullptr;
|
2010-05-05 04:44:19 +00:00
|
|
|
break;
|
2015-02-02 10:08:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
2010-05-05 04:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-02 09:27:06 +00:00
|
|
|
}
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
Disconnect();
|
2010-05-05 04:44:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---GUI--- thread
|
2010-06-06 03:52:11 +00:00
|
|
|
void NetPlayClient::GetPlayerList(std::string& list, std::vector<int>& pid_list)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2011-03-05 06:11:26 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
|
2010-05-05 04:44:19 +00:00
|
|
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
|
|
|
std::map<PlayerId, Player>::const_iterator
|
|
|
|
i = m_players.begin(),
|
|
|
|
e = m_players.end();
|
2015-02-02 09:27:06 +00:00
|
|
|
for (; i != e; ++i)
|
2010-06-06 03:52:11 +00:00
|
|
|
{
|
2013-08-18 13:43:01 +00:00
|
|
|
const Player *player = &(i->second);
|
|
|
|
ss << player->name << "[" << (int)player->pid << "] : " << player->revision << " | ";
|
|
|
|
for (unsigned int j = 0; j < 4; j++)
|
|
|
|
{
|
|
|
|
if (m_pad_map[j] == player->pid)
|
|
|
|
ss << j + 1;
|
|
|
|
else
|
|
|
|
ss << '-';
|
|
|
|
}
|
2013-09-22 18:27:52 +00:00
|
|
|
for (unsigned int j = 0; j < 4; j++)
|
|
|
|
{
|
|
|
|
if (m_wiimote_map[j] == player->pid)
|
|
|
|
ss << j + 1;
|
|
|
|
else
|
|
|
|
ss << '-';
|
|
|
|
}
|
2015-02-15 03:51:08 +00:00
|
|
|
ss << " |\nPing: " << player->ping << "ms\n\n";
|
2013-08-18 13:43:01 +00:00
|
|
|
pid_list.push_back(player->pid);
|
2010-06-06 03:52:11 +00:00
|
|
|
}
|
2010-05-05 04:44:19 +00:00
|
|
|
|
|
|
|
list = ss.str();
|
|
|
|
}
|
|
|
|
|
2013-08-18 13:43:01 +00:00
|
|
|
// called from ---GUI--- thread
|
2015-08-16 04:58:15 +00:00
|
|
|
std::vector<const Player*> NetPlayClient::GetPlayers()
|
2013-08-18 13:43:01 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
|
2015-08-16 04:58:15 +00:00
|
|
|
std::vector<const Player*> players;
|
|
|
|
|
|
|
|
for (const auto& pair : m_players)
|
|
|
|
players.push_back(&pair.second);
|
|
|
|
|
|
|
|
return players;
|
2013-08-18 13:43:01 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
// called from ---GUI--- thread
|
|
|
|
void NetPlayClient::SendChatMessage(const std::string& msg)
|
|
|
|
{
|
2015-03-13 01:03:09 +00:00
|
|
|
sf::Packet* spac = new sf::Packet;
|
|
|
|
*spac << (MessageId)NP_MSG_CHAT_MESSAGE;
|
|
|
|
*spac << msg;
|
|
|
|
SendAsync(spac);
|
2010-05-05 04:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---CPU--- thread
|
2014-10-16 06:36:39 +00:00
|
|
|
void NetPlayClient::SendPadState(const PadMapping in_game_pad, const GCPadStatus& pad)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
2015-03-13 01:03:09 +00:00
|
|
|
sf::Packet* spac = new sf::Packet;
|
|
|
|
*spac << (MessageId)NP_MSG_PAD_DATA;
|
|
|
|
*spac << in_game_pad;
|
|
|
|
*spac << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX << pad.substickY << pad.triggerLeft << pad.triggerRight;
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-03-13 01:03:09 +00:00
|
|
|
SendAsync(spac);
|
2010-05-05 04:44:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 03:48:52 +00:00
|
|
|
// called from ---CPU--- thread
|
2013-09-22 18:27:52 +00:00
|
|
|
void NetPlayClient::SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw)
|
2013-08-07 03:48:52 +00:00
|
|
|
{
|
2015-03-13 01:03:09 +00:00
|
|
|
sf::Packet* spac = new sf::Packet;
|
|
|
|
*spac << (MessageId)NP_MSG_WIIMOTE_DATA;
|
|
|
|
*spac << in_game_pad;
|
|
|
|
*spac << (u8)nw.size();
|
|
|
|
for (auto it : nw)
|
|
|
|
{
|
|
|
|
*spac << it;
|
|
|
|
}
|
|
|
|
SendAsync(spac);
|
2013-08-07 03:48:52 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
// called from ---GUI--- thread
|
|
|
|
bool NetPlayClient::StartGame(const std::string &path)
|
|
|
|
{
|
2015-03-13 01:03:09 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
|
|
|
|
// tell server i started the game
|
|
|
|
sf::Packet* spac = new sf::Packet;
|
|
|
|
*spac << (MessageId)NP_MSG_START_GAME;
|
|
|
|
*spac << m_current_game;
|
|
|
|
SendAsync(spac);
|
2010-05-05 04:44:19 +00:00
|
|
|
|
2015-05-21 23:52:26 +00:00
|
|
|
if (m_is_running.load())
|
2013-08-05 08:56:30 +00:00
|
|
|
{
|
|
|
|
PanicAlertT("Game is already running!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dialog->AppendChat(" -- STARTING GAME -- ");
|
|
|
|
|
2015-06-06 05:20:51 +00:00
|
|
|
m_timebase_frame = 0;
|
|
|
|
|
2015-05-21 23:52:26 +00:00
|
|
|
m_is_running.store(true);
|
2013-08-05 08:56:30 +00:00
|
|
|
NetPlay_Enable(this);
|
|
|
|
|
|
|
|
ClearBuffers();
|
|
|
|
|
2013-09-03 19:50:41 +00:00
|
|
|
if (m_dialog->IsRecording())
|
|
|
|
{
|
|
|
|
|
|
|
|
if (Movie::IsReadOnly())
|
|
|
|
Movie::SetReadOnly(false);
|
|
|
|
|
|
|
|
u8 controllers_mask = 0;
|
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (m_pad_map[i] > 0)
|
|
|
|
controllers_mask |= (1 << i);
|
2013-09-22 21:32:11 +00:00
|
|
|
if (m_wiimote_map[i] > 0)
|
2013-09-23 02:01:25 +00:00
|
|
|
controllers_mask |= (1 << (i + 4));
|
2013-09-03 19:50:41 +00:00
|
|
|
}
|
|
|
|
Movie::BeginRecordingInput(controllers_mask);
|
|
|
|
}
|
|
|
|
|
2013-08-05 08:56:30 +00:00
|
|
|
// boot game
|
2013-09-25 09:44:16 +00:00
|
|
|
|
2013-08-05 08:56:30 +00:00
|
|
|
m_dialog->BootGame(path);
|
|
|
|
|
2013-08-24 00:24:45 +00:00
|
|
|
UpdateDevices();
|
|
|
|
|
2015-06-12 11:56:53 +00:00
|
|
|
if (SConfig::GetInstance().bWii)
|
2013-09-23 06:56:17 +00:00
|
|
|
{
|
2013-09-23 15:07:15 +00:00
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
2013-09-25 09:44:16 +00:00
|
|
|
WiimoteReal::ChangeWiimoteSource(i, m_wiimote_map[i] > 0 ? WIIMOTE_SRC_EMU : WIIMOTE_SRC_NONE);
|
2013-09-23 06:56:17 +00:00
|
|
|
|
2013-09-25 09:44:16 +00:00
|
|
|
// Needed to prevent locking up at boot if (when) the wiimotes connect out of order.
|
|
|
|
NetWiimote nw;
|
|
|
|
nw.resize(4, 0);
|
2013-08-19 17:45:02 +00:00
|
|
|
|
2013-09-25 09:44:16 +00:00
|
|
|
for (unsigned int w = 0; w < 4; ++w)
|
|
|
|
{
|
|
|
|
if (m_wiimote_map[w] != -1)
|
|
|
|
// probably overkill, but whatever
|
|
|
|
for (unsigned int i = 0; i < 7; ++i)
|
|
|
|
m_wiimote_buffer[w].Push(nw);
|
|
|
|
}
|
2013-08-19 17:45:02 +00:00
|
|
|
}
|
2013-08-05 08:56:30 +00:00
|
|
|
|
2010-05-05 04:44:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---GUI--- thread
|
2010-11-16 01:55:29 +00:00
|
|
|
bool NetPlayClient::ChangeGame(const std::string&)
|
2010-05-05 04:44:19 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-05 09:05:06 +00:00
|
|
|
|
2013-08-24 00:24:45 +00:00
|
|
|
// called from ---NETPLAY--- thread
|
|
|
|
void NetPlayClient::UpdateDevices()
|
|
|
|
{
|
|
|
|
for (PadMapping i = 0; i < 4; i++)
|
|
|
|
{
|
2016-01-23 16:29:07 +00:00
|
|
|
// Use local controller types for local controllers
|
|
|
|
if (m_pad_map[i] == m_local_player->pid)
|
|
|
|
SerialInterface::AddDevice(SConfig::GetInstance().m_SIDevice[i], i);
|
|
|
|
else
|
|
|
|
SerialInterface::AddDevice(m_pad_map[i] > 0 ? SIDEVICE_GC_CONTROLLER : SIDEVICE_NONE, i);
|
2013-08-24 00:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:05:06 +00:00
|
|
|
// called from ---NETPLAY--- thread
|
|
|
|
void NetPlayClient::ClearBuffers()
|
|
|
|
{
|
|
|
|
// clear pad buffers, Clear method isn't thread safe
|
2015-02-02 09:27:06 +00:00
|
|
|
for (unsigned int i = 0; i<4; ++i)
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
|
|
|
while (m_pad_buffer[i].Size())
|
|
|
|
m_pad_buffer[i].Pop();
|
|
|
|
|
|
|
|
while (m_wiimote_buffer[i].Size())
|
|
|
|
m_wiimote_buffer[i].Pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
// called from ---NETPLAY--- thread
|
|
|
|
void NetPlayClient::OnTraversalStateChanged()
|
|
|
|
{
|
2016-01-25 02:58:56 +00:00
|
|
|
if (m_connection_state == ConnectionState::WaitingForTraversalClientConnection &&
|
2015-02-02 09:56:53 +00:00
|
|
|
m_traversal_client->m_State == TraversalClient::Connected)
|
|
|
|
{
|
2016-01-25 02:58:56 +00:00
|
|
|
m_connection_state = ConnectionState::WaitingForTraversalClientConnectReady;
|
2015-02-02 09:56:53 +00:00
|
|
|
m_traversal_client->ConnectToClient(m_host_spec);
|
|
|
|
}
|
2016-01-25 02:58:56 +00:00
|
|
|
else if (m_connection_state != ConnectionState::Failure &&
|
2015-02-02 09:56:53 +00:00
|
|
|
m_traversal_client->m_State == TraversalClient::Failure)
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---NETPLAY--- thread
|
|
|
|
void NetPlayClient::OnConnectReady(ENetAddress addr)
|
|
|
|
{
|
2016-01-25 02:58:56 +00:00
|
|
|
if (m_connection_state == ConnectionState::WaitingForTraversalClientConnectReady)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
2016-01-25 02:58:56 +00:00
|
|
|
m_connection_state = ConnectionState::Connecting;
|
2015-02-02 09:56:53 +00:00
|
|
|
enet_host_connect(m_client, &addr, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---NETPLAY--- thread
|
|
|
|
void NetPlayClient::OnConnectFailed(u8 reason)
|
|
|
|
{
|
|
|
|
m_connecting = false;
|
2016-01-25 02:58:56 +00:00
|
|
|
m_connection_state = ConnectionState::Failure;
|
2015-03-01 15:17:32 +00:00
|
|
|
switch (reason)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
2015-03-01 15:17:32 +00:00
|
|
|
case TraversalConnectFailedClientDidntRespond:
|
2015-02-02 09:56:53 +00:00
|
|
|
PanicAlertT("Traversal server timed out connecting to the host");
|
|
|
|
break;
|
2015-03-01 15:17:32 +00:00
|
|
|
case TraversalConnectFailedClientFailure:
|
2015-02-02 09:56:53 +00:00
|
|
|
PanicAlertT("Server rejected traversal attempt");
|
|
|
|
break;
|
2015-03-01 15:17:32 +00:00
|
|
|
case TraversalConnectFailedNoSuchClient:
|
2015-02-02 09:56:53 +00:00
|
|
|
PanicAlertT("Invalid host");
|
|
|
|
break;
|
|
|
|
default:
|
2015-03-01 15:17:32 +00:00
|
|
|
PanicAlertT("Unknown error %x", reason);
|
2015-02-02 09:56:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:05:06 +00:00
|
|
|
// called from ---CPU--- thread
|
2014-10-16 06:36:39 +00:00
|
|
|
bool NetPlayClient::GetNetPads(const u8 pad_nb, GCPadStatus* pad_status)
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
2013-08-18 13:43:01 +00:00
|
|
|
// The interface for this is extremely silly.
|
|
|
|
//
|
2014-11-14 02:28:27 +00:00
|
|
|
// Imagine a physical device that links three GameCubes together
|
2014-06-07 02:30:39 +00:00
|
|
|
// and emulates NetPlay that way. Which GameCube controls which
|
2013-08-18 13:43:01 +00:00
|
|
|
// in-game controllers can be configured on the device (m_pad_map)
|
2014-06-07 02:30:39 +00:00
|
|
|
// but which sockets on each individual GameCube should be used
|
2013-08-18 13:43:01 +00:00
|
|
|
// to control which players? The solution that Dolphin uses is
|
|
|
|
// that we hardcode the knowledge that they go in order, so if
|
2014-11-14 02:28:27 +00:00
|
|
|
// you have a 3P game with three GameCubes, then every single
|
2013-08-18 13:43:01 +00:00
|
|
|
// controller should be plugged into slot 1.
|
|
|
|
//
|
2014-11-14 02:28:27 +00:00
|
|
|
// If you have a 4P game, then one of the GameCubes will have
|
2013-08-18 13:43:01 +00:00
|
|
|
// a controller plugged into slot 1, and another in slot 2.
|
|
|
|
//
|
|
|
|
// The slot number is the "local" pad number, and what player
|
|
|
|
// it actually means is the "in-game" pad number.
|
|
|
|
//
|
|
|
|
// The interface here gives us the status of local pads, and
|
|
|
|
// expects to get back "in-game" pad numbers back in response.
|
|
|
|
// e.g. it asks "here's the input that slot 1 has, and by the
|
|
|
|
// way, what's the state of P1?"
|
|
|
|
//
|
|
|
|
// We should add this split between "in-game" pads and "local"
|
|
|
|
// pads higher up.
|
|
|
|
|
2013-09-09 07:12:42 +00:00
|
|
|
int in_game_num = LocalPadToInGamePad(pad_nb);
|
2013-08-18 13:43:01 +00:00
|
|
|
|
|
|
|
// If this in-game pad is one of ours, then update from the
|
|
|
|
// information given.
|
2013-08-05 09:05:06 +00:00
|
|
|
if (in_game_num < 4)
|
|
|
|
{
|
|
|
|
// adjust the buffer either up or down
|
|
|
|
// inserting multiple padstates or dropping states
|
|
|
|
while (m_pad_buffer[in_game_num].Size() <= m_target_buffer_size)
|
|
|
|
{
|
|
|
|
// add to buffer
|
2014-10-16 06:36:39 +00:00
|
|
|
m_pad_buffer[in_game_num].Push(*pad_status);
|
2013-08-05 09:05:06 +00:00
|
|
|
|
|
|
|
// send
|
2014-10-16 06:36:39 +00:00
|
|
|
SendPadState(in_game_num, *pad_status);
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-18 13:43:01 +00:00
|
|
|
// Now, we need to swap out the local value with the values
|
|
|
|
// retrieved from NetPlay. This could be the value we pushed
|
|
|
|
// above if we're configured as P1 and the code is trying
|
|
|
|
// to retrieve data for slot 1.
|
2014-10-16 06:36:39 +00:00
|
|
|
while (!m_pad_buffer[pad_nb].Pop(*pad_status))
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
2015-05-21 23:52:26 +00:00
|
|
|
if (!m_is_running.load())
|
2013-08-05 09:05:06 +00:00
|
|
|
return false;
|
|
|
|
|
2013-08-18 13:43:01 +00:00
|
|
|
// TODO: use a condition instead of sleeping
|
|
|
|
Common::SleepCurrentThread(1);
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 20:09:56 +00:00
|
|
|
if (Movie::IsRecordingInput())
|
|
|
|
{
|
2014-10-16 06:36:39 +00:00
|
|
|
Movie::RecordInput(pad_status, pad_nb);
|
2013-09-05 20:09:56 +00:00
|
|
|
Movie::InputUpdate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-16 06:36:39 +00:00
|
|
|
Movie::CheckPadStatus(pad_status, pad_nb);
|
2013-09-05 20:09:56 +00:00
|
|
|
}
|
2013-09-03 19:50:41 +00:00
|
|
|
|
2013-08-05 09:05:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// called from ---CPU--- thread
|
2013-08-19 17:45:02 +00:00
|
|
|
bool NetPlayClient::WiimoteUpdate(int _number, u8* data, const u8 size)
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
2013-08-07 22:00:38 +00:00
|
|
|
NetWiimote nw;
|
2015-02-02 09:27:06 +00:00
|
|
|
static u8 previousSize[4] = { 4, 4, 4, 4 };
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
|
2013-08-05 09:05:06 +00:00
|
|
|
|
2015-02-02 09:27:06 +00:00
|
|
|
// in game mapping for this local Wiimote
|
|
|
|
unsigned int in_game_num = LocalWiimoteToInGameWiimote(_number);
|
|
|
|
// does this local Wiimote map in game?
|
|
|
|
if (in_game_num < 4)
|
2013-08-22 15:37:38 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
if (previousSize[in_game_num] == size)
|
2013-08-19 17:45:02 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
nw.assign(data, data + size);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// add to buffer
|
|
|
|
m_wiimote_buffer[in_game_num].Push(nw);
|
|
|
|
|
|
|
|
SendWiimoteState(in_game_num, nw);
|
|
|
|
} while (m_wiimote_buffer[in_game_num].Size() <= m_target_buffer_size * 200 / 120); // TODO: add a seperate setting for wiimote buffer?
|
|
|
|
}
|
|
|
|
else
|
2013-08-19 17:45:02 +00:00
|
|
|
{
|
2015-02-02 09:27:06 +00:00
|
|
|
while (m_wiimote_buffer[in_game_num].Size() > 0)
|
|
|
|
{
|
|
|
|
// Reporting mode changed, so previous buffer is no good.
|
|
|
|
m_wiimote_buffer[in_game_num].Pop();
|
|
|
|
}
|
|
|
|
nw.resize(size, 0);
|
|
|
|
|
|
|
|
m_wiimote_buffer[in_game_num].Push(nw);
|
|
|
|
m_wiimote_buffer[in_game_num].Push(nw);
|
|
|
|
m_wiimote_buffer[in_game_num].Push(nw);
|
|
|
|
m_wiimote_buffer[in_game_num].Push(nw);
|
|
|
|
m_wiimote_buffer[in_game_num].Push(nw);
|
|
|
|
m_wiimote_buffer[in_game_num].Push(nw);
|
|
|
|
previousSize[in_game_num] = size;
|
2013-08-19 17:45:02 +00:00
|
|
|
}
|
2013-08-07 03:48:52 +00:00
|
|
|
}
|
2013-08-05 09:05:06 +00:00
|
|
|
|
|
|
|
} // unlock players
|
|
|
|
|
2013-08-19 17:45:02 +00:00
|
|
|
while (previousSize[_number] == size && !m_wiimote_buffer[_number].Pop(nw))
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
2013-08-07 03:48:52 +00:00
|
|
|
// wait for receiving thread to push some data
|
|
|
|
Common::SleepCurrentThread(1);
|
2015-05-21 23:52:26 +00:00
|
|
|
if (!m_is_running.load())
|
2013-08-07 03:48:52 +00:00
|
|
|
return false;
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 15:37:38 +00:00
|
|
|
// Use a blank input, since we may not have any valid input.
|
2013-08-19 17:45:02 +00:00
|
|
|
if (previousSize[_number] != size)
|
|
|
|
{
|
2013-08-22 15:37:38 +00:00
|
|
|
nw.resize(size, 0);
|
2013-08-19 17:45:02 +00:00
|
|
|
m_wiimote_buffer[_number].Push(nw);
|
|
|
|
m_wiimote_buffer[_number].Push(nw);
|
2013-09-29 03:39:29 +00:00
|
|
|
m_wiimote_buffer[_number].Push(nw);
|
|
|
|
m_wiimote_buffer[_number].Push(nw);
|
|
|
|
m_wiimote_buffer[_number].Push(nw);
|
2013-08-19 17:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We should have used a blank input last time, so now we just need to pop through the old buffer, until we reach a good input
|
2013-08-22 15:37:38 +00:00
|
|
|
if (nw.size() != size)
|
2013-08-07 03:48:52 +00:00
|
|
|
{
|
2013-08-07 14:10:40 +00:00
|
|
|
u8 tries = 0;
|
2013-08-22 15:37:38 +00:00
|
|
|
// Clear the buffer and wait for new input, since we probably just changed reporting mode.
|
|
|
|
while (nw.size() != size)
|
2013-08-07 03:48:52 +00:00
|
|
|
{
|
2013-08-07 14:10:40 +00:00
|
|
|
while (!m_wiimote_buffer[_number].Pop(nw))
|
|
|
|
{
|
|
|
|
Common::SleepCurrentThread(1);
|
2015-05-21 23:52:26 +00:00
|
|
|
if (!m_is_running.load())
|
2013-08-07 14:10:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++tries;
|
2013-09-22 18:27:52 +00:00
|
|
|
if (tries > m_target_buffer_size * 200 / 120)
|
2013-08-22 15:37:38 +00:00
|
|
|
break;
|
2013-08-19 17:45:02 +00:00
|
|
|
}
|
2013-08-07 03:48:52 +00:00
|
|
|
|
|
|
|
// If it still mismatches, it surely desynced
|
2013-08-22 15:37:38 +00:00
|
|
|
if (size != nw.size())
|
2013-08-07 03:48:52 +00:00
|
|
|
{
|
2015-03-15 15:42:22 +00:00
|
|
|
PanicAlertT("Netplay has desynced. There is no way to recover from this.");
|
2013-08-22 15:37:38 +00:00
|
|
|
return false;
|
2013-08-07 03:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:45:02 +00:00
|
|
|
previousSize[_number] = size;
|
2013-08-22 15:37:38 +00:00
|
|
|
memcpy(data, nw.data(), size);
|
2013-08-07 03:48:52 +00:00
|
|
|
return true;
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---GUI--- thread and ---NETPLAY--- thread (client side)
|
|
|
|
bool NetPlayClient::StopGame()
|
|
|
|
{
|
2015-05-21 23:52:26 +00:00
|
|
|
if (!m_is_running.load())
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
|
|
|
PanicAlertT("Game isn't running!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dialog->AppendChat(" -- STOPPING GAME -- ");
|
|
|
|
|
2015-05-21 23:52:26 +00:00
|
|
|
m_is_running.store(false);
|
2013-08-05 09:05:06 +00:00
|
|
|
NetPlay_Disable();
|
|
|
|
|
|
|
|
// stop game
|
|
|
|
m_dialog->StopGame();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:31:13 +00:00
|
|
|
// called from ---GUI--- thread
|
2013-09-03 01:54:28 +00:00
|
|
|
void NetPlayClient::Stop()
|
|
|
|
{
|
2015-05-21 23:52:26 +00:00
|
|
|
if (!m_is_running.load())
|
2013-09-03 03:33:53 +00:00
|
|
|
return;
|
2015-05-21 23:52:26 +00:00
|
|
|
|
2013-09-03 01:54:28 +00:00
|
|
|
bool isPadMapped = false;
|
2014-03-03 05:25:15 +00:00
|
|
|
for (PadMapping mapping : m_pad_map)
|
2013-09-03 01:54:28 +00:00
|
|
|
{
|
2014-03-03 05:25:15 +00:00
|
|
|
if (mapping == m_local_player->pid)
|
|
|
|
{
|
2013-09-03 01:54:28 +00:00
|
|
|
isPadMapped = true;
|
2014-03-03 05:25:15 +00:00
|
|
|
}
|
2013-09-03 01:54:28 +00:00
|
|
|
}
|
2014-03-03 05:25:15 +00:00
|
|
|
for (PadMapping mapping : m_wiimote_map)
|
2013-09-22 18:27:52 +00:00
|
|
|
{
|
2014-03-03 05:25:15 +00:00
|
|
|
if (mapping == m_local_player->pid)
|
|
|
|
{
|
2013-09-22 18:27:52 +00:00
|
|
|
isPadMapped = true;
|
2014-03-03 05:25:15 +00:00
|
|
|
}
|
2013-09-22 18:27:52 +00:00
|
|
|
}
|
2013-09-03 01:54:28 +00:00
|
|
|
// tell the server to stop if we have a pad mapped in game.
|
|
|
|
if (isPadMapped)
|
|
|
|
{
|
2015-03-13 01:03:09 +00:00
|
|
|
sf::Packet* spac = new sf::Packet;
|
|
|
|
*spac << (MessageId)NP_MSG_STOP_GAME;
|
|
|
|
SendAsync(spac);
|
2013-09-03 01:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:09:45 +00:00
|
|
|
u8 NetPlayClient::InGamePadToLocalPad(u8 ingame_pad)
|
|
|
|
{
|
|
|
|
// not our pad
|
|
|
|
if (m_pad_map[ingame_pad] != m_local_player->pid)
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
int local_pad = 0;
|
|
|
|
int pad = 0;
|
|
|
|
|
|
|
|
for (; pad < ingame_pad; pad++)
|
|
|
|
{
|
|
|
|
if (m_pad_map[pad] == m_local_player->pid)
|
|
|
|
local_pad++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return local_pad;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 NetPlayClient::LocalPadToInGamePad(u8 local_pad)
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
2013-08-18 13:43:01 +00:00
|
|
|
// Figure out which in-game pad maps to which local pad.
|
|
|
|
// The logic we have here is that the local slots always
|
|
|
|
// go in order.
|
|
|
|
int local_pad_count = -1;
|
|
|
|
int ingame_pad = 0;
|
|
|
|
for (; ingame_pad < 4; ingame_pad++)
|
|
|
|
{
|
|
|
|
if (m_pad_map[ingame_pad] == m_local_player->pid)
|
|
|
|
local_pad_count++;
|
|
|
|
|
2013-09-09 07:09:45 +00:00
|
|
|
if (local_pad_count == local_pad)
|
2013-08-05 09:05:06 +00:00
|
|
|
break;
|
2013-08-18 13:43:01 +00:00
|
|
|
}
|
2013-08-05 09:05:06 +00:00
|
|
|
|
2013-08-18 13:43:01 +00:00
|
|
|
return ingame_pad;
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-22 18:27:52 +00:00
|
|
|
u8 NetPlayClient::LocalWiimoteToInGameWiimote(u8 local_pad)
|
|
|
|
{
|
|
|
|
// Figure out which in-game pad maps to which local pad.
|
|
|
|
// The logic we have here is that the local slots always
|
|
|
|
// go in order.
|
|
|
|
int local_pad_count = -1;
|
|
|
|
int ingame_pad = 0;
|
|
|
|
for (; ingame_pad < 4; ingame_pad++)
|
|
|
|
{
|
|
|
|
if (m_wiimote_map[ingame_pad] == m_local_player->pid)
|
|
|
|
local_pad_count++;
|
|
|
|
|
|
|
|
if (local_pad_count == local_pad)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ingame_pad;
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 10:50:47 +00:00
|
|
|
void NetPlayClient::SendTimeBase()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(crit_netplay_client);
|
|
|
|
|
|
|
|
u64 timebase = SystemTimers::GetFakeTimeBase();
|
|
|
|
|
|
|
|
sf::Packet* spac = new sf::Packet;
|
|
|
|
*spac << (MessageId)NP_MSG_TIMEBASE;
|
|
|
|
*spac << (u32)timebase;
|
|
|
|
*spac << (u32)(timebase << 32);
|
2015-06-06 05:20:51 +00:00
|
|
|
*spac << netplay_client->m_timebase_frame++;
|
2015-03-08 10:50:47 +00:00
|
|
|
netplay_client->SendAsync(spac);
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:05:06 +00:00
|
|
|
// stuff hacked into dolphin
|
|
|
|
|
|
|
|
// called from ---CPU--- thread
|
|
|
|
// Actual Core function which is called on every frame
|
2014-10-16 06:36:39 +00:00
|
|
|
bool CSIDevice_GCController::NetPlay_GetInput(u8 numPAD, GCPadStatus* PadStatus)
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(crit_netplay_client);
|
|
|
|
|
|
|
|
if (netplay_client)
|
2014-10-16 06:36:39 +00:00
|
|
|
return netplay_client->GetNetPads(numPAD, PadStatus);
|
2013-08-05 09:05:06 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-07 03:48:52 +00:00
|
|
|
bool WiimoteEmu::Wiimote::NetPlay_GetWiimoteData(int wiimote, u8* data, u8 size)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(crit_netplay_client);
|
|
|
|
|
|
|
|
if (netplay_client)
|
|
|
|
return netplay_client->WiimoteUpdate(wiimote, data, size);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:05:06 +00:00
|
|
|
// called from ---CPU--- thread
|
|
|
|
// so all players' games get the same time
|
2015-03-08 12:21:03 +00:00
|
|
|
u64 CEXIIPL::NetPlay_GetGCTime()
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(crit_netplay_client);
|
|
|
|
|
|
|
|
if (netplay_client)
|
2015-02-02 10:08:58 +00:00
|
|
|
return g_netplay_initial_gctime;
|
2013-08-05 09:05:06 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called from ---CPU--- thread
|
|
|
|
// return the local pad num that should rumble given a ingame pad num
|
2013-09-09 07:09:45 +00:00
|
|
|
u8 CSIDevice_GCController::NetPlay_InGamePadToLocalPad(u8 numPAD)
|
2013-08-05 09:05:06 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(crit_netplay_client);
|
|
|
|
|
|
|
|
if (netplay_client)
|
2013-09-09 07:09:45 +00:00
|
|
|
return netplay_client->InGamePadToLocalPad(numPAD);
|
2013-08-05 09:05:06 +00:00
|
|
|
else
|
|
|
|
return numPAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetPlay::IsNetPlayRunning()
|
|
|
|
{
|
2014-03-09 20:14:26 +00:00
|
|
|
return netplay_client != nullptr;
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetPlay_Enable(NetPlayClient* const np)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(crit_netplay_client);
|
|
|
|
netplay_client = np;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetPlay_Disable()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(crit_netplay_client);
|
2014-03-09 20:14:26 +00:00
|
|
|
netplay_client = nullptr;
|
2013-08-05 09:05:06 +00:00
|
|
|
}
|