Merge pull request #3575 from mathieui/netplay-server-cleanup
Small netplay server cleanup & fix
This commit is contained in:
commit
ff5d6887d1
|
@ -58,17 +58,6 @@ NetPlayServer::~NetPlayServer()
|
||||||
|
|
||||||
// called from ---GUI--- thread
|
// called from ---GUI--- thread
|
||||||
NetPlayServer::NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, u16 centralPort)
|
NetPlayServer::NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, u16 centralPort)
|
||||||
: is_connected(false)
|
|
||||||
, m_is_running(false)
|
|
||||||
, m_do_loop(false)
|
|
||||||
, m_ping_key(0)
|
|
||||||
, m_update_pings(false)
|
|
||||||
, m_current_game(0)
|
|
||||||
, m_target_buffer_size(0)
|
|
||||||
, m_selected_game("")
|
|
||||||
, m_server(nullptr)
|
|
||||||
, m_traversal_client(nullptr)
|
|
||||||
, m_dialog(nullptr)
|
|
||||||
{
|
{
|
||||||
//--use server time
|
//--use server time
|
||||||
if (enet_initialize() != 0)
|
if (enet_initialize() != 0)
|
||||||
|
@ -461,18 +450,18 @@ void NetPlayServer::AdjustPadBufferSize(unsigned int size)
|
||||||
m_target_buffer_size = size;
|
m_target_buffer_size = size;
|
||||||
|
|
||||||
// tell clients to change buffer size
|
// tell clients to change buffer size
|
||||||
sf::Packet* spac = new sf::Packet;
|
auto spac = std::make_unique<sf::Packet>();
|
||||||
*spac << (MessageId)NP_MSG_PAD_BUFFER;
|
*spac << static_cast<MessageId>(NP_MSG_PAD_BUFFER);
|
||||||
*spac << (u32)m_target_buffer_size;
|
*spac << static_cast<u32>(m_target_buffer_size);
|
||||||
|
|
||||||
SendAsyncToClients(spac);
|
SendAsyncToClients(std::move(spac));
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetPlayServer::SendAsyncToClients(sf::Packet* packet)
|
void NetPlayServer::SendAsyncToClients(std::unique_ptr<sf::Packet> packet)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::recursive_mutex> lkq(m_crit.async_queue_write);
|
std::lock_guard<std::recursive_mutex> lkq(m_crit.async_queue_write);
|
||||||
m_async_queue.Push(std::unique_ptr<sf::Packet>(packet));
|
m_async_queue.Push(std::move(packet));
|
||||||
}
|
}
|
||||||
ENetUtil::WakeupThread(m_server);
|
ENetUtil::WakeupThread(m_server);
|
||||||
}
|
}
|
||||||
|
@ -511,17 +500,37 @@ unsigned int NetPlayServer::OnData(sf::Packet& packet, Client& player)
|
||||||
|
|
||||||
PadMapping map = 0;
|
PadMapping map = 0;
|
||||||
GCPadStatus pad;
|
GCPadStatus pad;
|
||||||
packet >> map >> pad.button >> pad.analogA >> pad.analogB >> pad.stickX >> pad.stickY >> pad.substickX >> pad.substickY >> pad.triggerLeft >> pad.triggerRight;
|
packet >> map
|
||||||
|
>> pad.button
|
||||||
|
>> pad.analogA
|
||||||
|
>> pad.analogB
|
||||||
|
>> pad.stickX
|
||||||
|
>> pad.stickY
|
||||||
|
>> pad.substickX
|
||||||
|
>> pad.substickY
|
||||||
|
>> pad.triggerLeft
|
||||||
|
>> pad.triggerRight;
|
||||||
|
|
||||||
// If the data is not from the correct player,
|
// If the data is not from the correct player,
|
||||||
// then disconnect them.
|
// then disconnect them.
|
||||||
if (m_pad_map[map] != player.pid)
|
if (m_pad_map.at(map) != player.pid)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Relay to clients
|
// Relay to clients
|
||||||
sf::Packet spac;
|
sf::Packet spac;
|
||||||
spac << (MessageId)NP_MSG_PAD_DATA;
|
spac << (MessageId)NP_MSG_PAD_DATA;
|
||||||
spac << map << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX << pad.substickY << pad.triggerLeft << pad.triggerRight;
|
spac << map
|
||||||
|
<< pad.button
|
||||||
|
<< pad.analogA
|
||||||
|
<< pad.analogB
|
||||||
|
<< pad.stickX
|
||||||
|
<< pad.stickY
|
||||||
|
<< pad.substickX
|
||||||
|
<< pad.substickY
|
||||||
|
<< pad.triggerLeft
|
||||||
|
<< pad.triggerRight;
|
||||||
|
|
||||||
SendToClients(spac, player.pid);
|
SendToClients(spac, player.pid);
|
||||||
}
|
}
|
||||||
|
@ -542,7 +551,7 @@ unsigned int NetPlayServer::OnData(sf::Packet& packet, Client& player)
|
||||||
|
|
||||||
// If the data is not from the correct player,
|
// If the data is not from the correct player,
|
||||||
// then disconnect them.
|
// then disconnect them.
|
||||||
if (m_wiimote_map[map] != player.pid)
|
if (m_wiimote_map.at(map) != player.pid)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -665,12 +674,12 @@ void NetPlayServer::OnTraversalStateChanged()
|
||||||
// called from ---GUI--- thread
|
// called from ---GUI--- thread
|
||||||
void NetPlayServer::SendChatMessage(const std::string& msg)
|
void NetPlayServer::SendChatMessage(const std::string& msg)
|
||||||
{
|
{
|
||||||
sf::Packet* spac = new sf::Packet;
|
auto spac = std::make_unique<sf::Packet>();
|
||||||
*spac << (MessageId)NP_MSG_CHAT_MESSAGE;
|
*spac << (MessageId)NP_MSG_CHAT_MESSAGE;
|
||||||
*spac << (PlayerId)0; // server id always 0
|
*spac << (PlayerId)0; // server id always 0
|
||||||
*spac << msg;
|
*spac << msg;
|
||||||
|
|
||||||
SendAsyncToClients(spac);
|
SendAsyncToClients(std::move(spac));
|
||||||
}
|
}
|
||||||
|
|
||||||
// called from ---GUI--- thread
|
// called from ---GUI--- thread
|
||||||
|
@ -681,11 +690,11 @@ bool NetPlayServer::ChangeGame(const std::string &game)
|
||||||
m_selected_game = game;
|
m_selected_game = game;
|
||||||
|
|
||||||
// send changed game to clients
|
// send changed game to clients
|
||||||
sf::Packet* spac = new sf::Packet;
|
auto spac = std::make_unique<sf::Packet>();
|
||||||
*spac << (MessageId)NP_MSG_CHANGE_GAME;
|
*spac << (MessageId)NP_MSG_CHANGE_GAME;
|
||||||
*spac << game;
|
*spac << game;
|
||||||
|
|
||||||
SendAsyncToClients(spac);
|
SendAsyncToClients(std::move(spac));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -710,7 +719,7 @@ bool NetPlayServer::StartGame()
|
||||||
g_netplay_initial_gctime = Common::Timer::GetLocalTimeSinceJan1970();
|
g_netplay_initial_gctime = Common::Timer::GetLocalTimeSinceJan1970();
|
||||||
|
|
||||||
// tell clients to start game
|
// tell clients to start game
|
||||||
sf::Packet* spac = new sf::Packet;
|
auto spac = std::make_unique<sf::Packet>();
|
||||||
*spac << (MessageId)NP_MSG_START_GAME;
|
*spac << (MessageId)NP_MSG_START_GAME;
|
||||||
*spac << m_current_game;
|
*spac << m_current_game;
|
||||||
*spac << m_settings.m_CPUthread;
|
*spac << m_settings.m_CPUthread;
|
||||||
|
@ -729,7 +738,7 @@ bool NetPlayServer::StartGame()
|
||||||
*spac << (u32)g_netplay_initial_gctime;
|
*spac << (u32)g_netplay_initial_gctime;
|
||||||
*spac << (u32)(g_netplay_initial_gctime >> 32);
|
*spac << (u32)(g_netplay_initial_gctime >> 32);
|
||||||
|
|
||||||
SendAsyncToClients(spac);
|
SendAsyncToClients(std::move(spac));
|
||||||
|
|
||||||
m_is_running = true;
|
m_is_running = true;
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ class NetPlayServer : public TraversalClientClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void ThreadFunc();
|
void ThreadFunc();
|
||||||
void SendAsyncToClients(sf::Packet* packet);
|
void SendAsyncToClients(std::unique_ptr<sf::Packet> packet);
|
||||||
|
|
||||||
NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, u16 centralPort);
|
NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, u16 centralPort);
|
||||||
~NetPlayServer();
|
~NetPlayServer();
|
||||||
|
@ -50,7 +50,7 @@ public:
|
||||||
std::unordered_set<std::string> GetInterfaceSet();
|
std::unordered_set<std::string> GetInterfaceSet();
|
||||||
std::string GetInterfaceHost(const std::string& inter);
|
std::string GetInterfaceHost(const std::string& inter);
|
||||||
|
|
||||||
bool is_connected;
|
bool is_connected = false;
|
||||||
|
|
||||||
#ifdef USE_UPNP
|
#ifdef USE_UPNP
|
||||||
void TryPortmapping(u16 port);
|
void TryPortmapping(u16 port);
|
||||||
|
@ -90,13 +90,13 @@ private:
|
||||||
|
|
||||||
NetSettings m_settings;
|
NetSettings m_settings;
|
||||||
|
|
||||||
bool m_is_running;
|
bool m_is_running = false;
|
||||||
bool m_do_loop;
|
bool m_do_loop = false;
|
||||||
Common::Timer m_ping_timer;
|
Common::Timer m_ping_timer;
|
||||||
u32 m_ping_key;
|
u32 m_ping_key = 0;
|
||||||
bool m_update_pings;
|
bool m_update_pings = false;
|
||||||
u32 m_current_game;
|
u32 m_current_game = 0;
|
||||||
unsigned int m_target_buffer_size;
|
unsigned int m_target_buffer_size = 0;
|
||||||
PadMappingArray m_pad_map;
|
PadMappingArray m_pad_map;
|
||||||
PadMappingArray m_wiimote_map;
|
PadMappingArray m_wiimote_map;
|
||||||
|
|
||||||
|
@ -117,9 +117,9 @@ private:
|
||||||
std::thread m_thread;
|
std::thread m_thread;
|
||||||
Common::FifoQueue<std::unique_ptr<sf::Packet>, false> m_async_queue;
|
Common::FifoQueue<std::unique_ptr<sf::Packet>, false> m_async_queue;
|
||||||
|
|
||||||
ENetHost* m_server;
|
ENetHost* m_server = nullptr;
|
||||||
TraversalClient* m_traversal_client;
|
TraversalClient* m_traversal_client = nullptr;
|
||||||
NetPlayUI* m_dialog;
|
NetPlayUI* m_dialog = nullptr;
|
||||||
|
|
||||||
#ifdef USE_UPNP
|
#ifdef USE_UPNP
|
||||||
static void mapPortThread(const u16 port);
|
static void mapPortThread(const u16 port);
|
||||||
|
|
Loading…
Reference in New Issue