From 1834833edd9503b50986e6f6a19be7239888d211 Mon Sep 17 00:00:00 2001 From: Michael Maltese Date: Thu, 9 Feb 2017 16:58:27 -0800 Subject: [PATCH] NetPlay: use move semantics instead of unique_ptrs --- Source/Core/Core/NetPlayClient.cpp | 50 ++++++++++---------- Source/Core/Core/NetPlayClient.h | 5 +- Source/Core/Core/NetPlayServer.cpp | 74 +++++++++++++++--------------- Source/Core/Core/NetPlayServer.h | 4 +- 4 files changed, 66 insertions(+), 67 deletions(-) diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index d3f253fb74..a9847636e8 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -614,7 +614,7 @@ void NetPlayClient::Disconnect() m_server = nullptr; } -void NetPlayClient::SendAsync(std::unique_ptr packet) +void NetPlayClient::SendAsync(sf::Packet&& packet) { { std::lock_guard lkq(m_crit.async_queue_write); @@ -635,7 +635,7 @@ void NetPlayClient::ThreadFunc() net = enet_host_service(m_client, &netEvent, 250); while (!m_async_queue.Empty()) { - Send(*(m_async_queue.Front().get())); + Send(m_async_queue.Front()); m_async_queue.Pop(); } if (net > 0) @@ -733,9 +733,9 @@ std::vector NetPlayClient::GetPlayers() // called from ---GUI--- thread void NetPlayClient::SendChatMessage(const std::string& msg) { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_CHAT_MESSAGE); - *spac << msg; + sf::Packet spac; + spac << static_cast(NP_MSG_CHAT_MESSAGE); + spac << msg; SendAsync(std::move(spac)); } @@ -743,11 +743,11 @@ void NetPlayClient::SendChatMessage(const std::string& msg) // called from ---CPU--- thread void NetPlayClient::SendPadState(const int in_game_pad, const GCPadStatus& pad) { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_PAD_DATA); - *spac << static_cast(in_game_pad); - *spac << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX - << pad.substickY << pad.triggerLeft << pad.triggerRight; + sf::Packet spac; + spac << static_cast(NP_MSG_PAD_DATA); + spac << static_cast(in_game_pad); + spac << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX + << pad.substickY << pad.triggerLeft << pad.triggerRight; SendAsync(std::move(spac)); } @@ -755,13 +755,13 @@ void NetPlayClient::SendPadState(const int in_game_pad, const GCPadStatus& pad) // called from ---CPU--- thread void NetPlayClient::SendWiimoteState(const int in_game_pad, const NetWiimote& nw) { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_WIIMOTE_DATA); - *spac << static_cast(in_game_pad); - *spac << static_cast(nw.size()); + sf::Packet spac; + spac << static_cast(NP_MSG_WIIMOTE_DATA); + spac << static_cast(in_game_pad); + spac << static_cast(nw.size()); for (auto it : nw) { - *spac << it; + spac << it; } SendAsync(std::move(spac)); @@ -770,9 +770,9 @@ void NetPlayClient::SendWiimoteState(const int in_game_pad, const NetWiimote& nw // called from ---GUI--- thread void NetPlayClient::SendStartGamePacket() { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_START_GAME); - *spac << m_current_game; + sf::Packet spac; + spac << static_cast(NP_MSG_START_GAME); + spac << m_current_game; SendAsync(std::move(spac)); } @@ -780,8 +780,8 @@ void NetPlayClient::SendStartGamePacket() // called from ---GUI--- thread void NetPlayClient::SendStopGamePacket() { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_STOP_GAME); + sf::Packet spac; + spac << static_cast(NP_MSG_STOP_GAME); SendAsync(std::move(spac)); } @@ -1193,11 +1193,11 @@ void NetPlayClient::SendTimeBase() u64 timebase = SystemTimers::GetFakeTimeBase(); - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_TIMEBASE); - *spac << static_cast(timebase); - *spac << static_cast(timebase << 32); - *spac << netplay_client->m_timebase_frame++; + sf::Packet spac; + spac << static_cast(NP_MSG_TIMEBASE); + spac << static_cast(timebase); + spac << static_cast(timebase << 32); + spac << netplay_client->m_timebase_frame++; netplay_client->SendAsync(std::move(spac)); } diff --git a/Source/Core/Core/NetPlayClient.h b/Source/Core/Core/NetPlayClient.h index 6e7f06c11b..5b9907319a 100644 --- a/Source/Core/Core/NetPlayClient.h +++ b/Source/Core/Core/NetPlayClient.h @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -65,7 +64,7 @@ class NetPlayClient : public TraversalClientClient { public: void ThreadFunc(); - void SendAsync(std::unique_ptr packet); + void SendAsync(sf::Packet&& packet); NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, const std::string& name, bool traversal, const std::string& centralServer, @@ -111,7 +110,7 @@ protected: std::recursive_mutex async_queue_write; } m_crit; - Common::FifoQueue, false> m_async_queue; + Common::FifoQueue m_async_queue; std::array, 4> m_pad_buffer; std::array, 4> m_wiimote_buffer; diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 19680ade8b..c5da53ca94 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -129,7 +129,7 @@ void NetPlayServer::ThreadFunc() { { std::lock_guard lkp(m_crit.players); - SendToClients(*(m_async_queue.Front().get())); + SendToClients(m_async_queue.Front()); } m_async_queue.Pop(); } @@ -456,14 +456,14 @@ void NetPlayServer::AdjustPadBufferSize(unsigned int size) m_target_buffer_size = size; // tell clients to change buffer size - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_PAD_BUFFER); - *spac << static_cast(m_target_buffer_size); + sf::Packet spac; + spac << static_cast(NP_MSG_PAD_BUFFER); + spac << static_cast(m_target_buffer_size); SendAsyncToClients(std::move(spac)); } -void NetPlayServer::SendAsyncToClients(std::unique_ptr packet) +void NetPlayServer::SendAsyncToClients(sf::Packet&& packet) { { std::lock_guard lkq(m_crit.async_queue_write); @@ -723,10 +723,10 @@ void NetPlayServer::OnTraversalStateChanged() // called from ---GUI--- thread void NetPlayServer::SendChatMessage(const std::string& msg) { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_CHAT_MESSAGE); - *spac << static_cast(0); // server id always 0 - *spac << msg; + sf::Packet spac; + spac << static_cast(NP_MSG_CHAT_MESSAGE); + spac << static_cast(0); // server id always 0 + spac << msg; SendAsyncToClients(std::move(spac)); } @@ -739,9 +739,9 @@ bool NetPlayServer::ChangeGame(const std::string& game) m_selected_game = game; // send changed game to clients - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_CHANGE_GAME); - *spac << game; + sf::Packet spac; + spac << static_cast(NP_MSG_CHANGE_GAME); + spac << game; SendAsyncToClients(std::move(spac)); @@ -751,9 +751,9 @@ bool NetPlayServer::ChangeGame(const std::string& game) // called from ---GUI--- thread bool NetPlayServer::ComputeMD5(const std::string& file_identifier) { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_COMPUTE_MD5); - *spac << file_identifier; + sf::Packet spac; + spac << static_cast(NP_MSG_COMPUTE_MD5); + spac << file_identifier; SendAsyncToClients(std::move(spac)); @@ -763,8 +763,8 @@ bool NetPlayServer::ComputeMD5(const std::string& file_identifier) // called from ---GUI--- thread bool NetPlayServer::AbortMD5() { - auto spac = std::make_unique(); - *spac << static_cast(NP_MSG_MD5_ABORT); + sf::Packet spac; + spac << static_cast(NP_MSG_MD5_ABORT); SendAsyncToClients(std::move(spac)); @@ -794,26 +794,26 @@ bool NetPlayServer::StartGame() g_netplay_initial_rtc = Common::Timer::GetLocalTimeSinceJan1970(); // tell clients to start game - auto spac = std::make_unique(); - *spac << (MessageId)NP_MSG_START_GAME; - *spac << m_current_game; - *spac << m_settings.m_CPUthread; - *spac << m_settings.m_CPUcore; - *spac << m_settings.m_EnableCheats; - *spac << m_settings.m_SelectedLanguage; - *spac << m_settings.m_OverrideGCLanguage; - *spac << m_settings.m_ProgressiveScan; - *spac << m_settings.m_PAL60; - *spac << m_settings.m_DSPEnableJIT; - *spac << m_settings.m_DSPHLE; - *spac << m_settings.m_WriteToMemcard; - *spac << m_settings.m_CopyWiiSave; - *spac << m_settings.m_OCEnable; - *spac << m_settings.m_OCFactor; - *spac << m_settings.m_EXIDevice[0]; - *spac << m_settings.m_EXIDevice[1]; - *spac << (u32)g_netplay_initial_rtc; - *spac << (u32)(g_netplay_initial_rtc >> 32); + sf::Packet spac; + spac << (MessageId)NP_MSG_START_GAME; + spac << m_current_game; + spac << m_settings.m_CPUthread; + spac << m_settings.m_CPUcore; + spac << m_settings.m_EnableCheats; + spac << m_settings.m_SelectedLanguage; + spac << m_settings.m_OverrideGCLanguage; + spac << m_settings.m_ProgressiveScan; + spac << m_settings.m_PAL60; + spac << m_settings.m_DSPEnableJIT; + spac << m_settings.m_DSPHLE; + spac << m_settings.m_WriteToMemcard; + spac << m_settings.m_CopyWiiSave; + spac << m_settings.m_OCEnable; + spac << m_settings.m_OCFactor; + spac << m_settings.m_EXIDevice[0]; + spac << m_settings.m_EXIDevice[1]; + spac << (u32)g_netplay_initial_rtc; + spac << (u32)(g_netplay_initial_rtc >> 32); SendAsyncToClients(std::move(spac)); diff --git a/Source/Core/Core/NetPlayServer.h b/Source/Core/Core/NetPlayServer.h index 1e356cd271..33829d5669 100644 --- a/Source/Core/Core/NetPlayServer.h +++ b/Source/Core/Core/NetPlayServer.h @@ -26,7 +26,7 @@ class NetPlayServer : public TraversalClientClient { public: void ThreadFunc(); - void SendAsyncToClients(std::unique_ptr packet); + void SendAsyncToClients(sf::Packet&& packet); NetPlayServer(const u16 port, bool traversal, const std::string& centralServer, u16 centralPort); ~NetPlayServer(); @@ -118,7 +118,7 @@ private: std::string m_selected_game; std::thread m_thread; - Common::FifoQueue, false> m_async_queue; + Common::FifoQueue m_async_queue; ENetHost* m_server = nullptr; TraversalClient* m_traversal_client = nullptr;