From 8588c8fd3139c3e14d8f57cc88fcdbf7a2f6fa1d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 24 Jan 2016 22:10:38 -0500 Subject: [PATCH] NetPlayClient: Make SendAsync take a unique_ptr as a parameter --- Source/Core/Core/NetPlayClient.cpp | 82 +++++++++++++++++++----------- Source/Core/Core/NetPlayClient.h | 6 ++- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index 2c435a5375..b59845c97b 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include #include "Common/Common.h" #include "Common/CommonTypes.h" #include "Common/ENetUtil.h" @@ -512,11 +513,11 @@ void NetPlayClient::Disconnect() m_server = nullptr; } -void NetPlayClient::SendAsync(sf::Packet* packet) +void NetPlayClient::SendAsync(std::unique_ptr packet) { { std::lock_guard lkq(m_crit.async_queue_write); - m_async_queue.Push(std::unique_ptr(packet)); + m_async_queue.Push(std::move(packet)); } ENetUtil::WakeupThread(m_client); } @@ -616,46 +617,71 @@ std::vector NetPlayClient::GetPlayers() // called from ---GUI--- thread void NetPlayClient::SendChatMessage(const std::string& msg) { - sf::Packet* spac = new sf::Packet; - *spac << (MessageId)NP_MSG_CHAT_MESSAGE; + auto spac = std::make_unique(); + *spac << static_cast(NP_MSG_CHAT_MESSAGE); *spac << msg; - SendAsync(spac); + + SendAsync(std::move(spac)); } // called from ---CPU--- thread void NetPlayClient::SendPadState(const PadMapping in_game_pad, const GCPadStatus& pad) { - sf::Packet* spac = new sf::Packet; - *spac << (MessageId)NP_MSG_PAD_DATA; + auto spac = std::make_unique(); + *spac << static_cast(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; + *spac << pad.button + << pad.analogA + << pad.analogB + << pad.stickX + << pad.stickY + << pad.substickX + << pad.substickY + << pad.triggerLeft + << pad.triggerRight; - SendAsync(spac); + SendAsync(std::move(spac)); } // called from ---CPU--- thread void NetPlayClient::SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw) { - sf::Packet* spac = new sf::Packet; - *spac << (MessageId)NP_MSG_WIIMOTE_DATA; + auto spac = std::make_unique(); + *spac << static_cast(NP_MSG_WIIMOTE_DATA); *spac << in_game_pad; - *spac << (u8)nw.size(); + *spac << static_cast(nw.size()); for (auto it : nw) { *spac << it; } - SendAsync(spac); + + SendAsync(std::move(spac)); +} + +// called from ---GUI--- thread +void NetPlayClient::SendStartGamePacket() +{ + auto spac = std::make_unique(); + *spac << static_cast(NP_MSG_START_GAME); + *spac << m_current_game; + + SendAsync(std::move(spac)); +} + +// called from ---GUI--- thread +void NetPlayClient::SendStopGamePacket() +{ + auto spac = std::make_unique(); + *spac << static_cast(NP_MSG_STOP_GAME); + + SendAsync(std::move(spac)); } // called from ---GUI--- thread bool NetPlayClient::StartGame(const std::string &path) { std::lock_guard 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); + SendStartGamePacket(); if (m_is_running.load()) { @@ -1006,13 +1032,10 @@ void NetPlayClient::Stop() isPadMapped = true; } } - // tell the server to stop if we have a pad mapped in game. + + // Tell the server to stop if we have a pad mapped in game. if (isPadMapped) - { - sf::Packet* spac = new sf::Packet; - *spac << (MessageId)NP_MSG_STOP_GAME; - SendAsync(spac); - } + SendStopGamePacket(); } u8 NetPlayClient::InGamePadToLocalPad(u8 ingame_pad) @@ -1077,12 +1100,13 @@ void NetPlayClient::SendTimeBase() u64 timebase = SystemTimers::GetFakeTimeBase(); - sf::Packet* spac = new sf::Packet; - *spac << (MessageId)NP_MSG_TIMEBASE; - *spac << (u32)timebase; - *spac << (u32)(timebase << 32); + 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++; - netplay_client->SendAsync(spac); + + netplay_client->SendAsync(std::move(spac)); } // stuff hacked into dolphin diff --git a/Source/Core/Core/NetPlayClient.h b/Source/Core/Core/NetPlayClient.h index 30f7f5421b..0e1c2ec70e 100644 --- a/Source/Core/Core/NetPlayClient.h +++ b/Source/Core/Core/NetPlayClient.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -48,7 +49,7 @@ class NetPlayClient : public TraversalClientClient { public: void ThreadFunc(); - void SendAsync(sf::Packet* packet); + void SendAsync(std::unique_ptr packet); NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, const std::string& name, bool traversal, const std::string& centralServer, u16 centralPort); ~NetPlayClient(); @@ -128,6 +129,9 @@ private: Failure }; + void SendStartGamePacket(); + void SendStopGamePacket(); + void UpdateDevices(); void SendPadState(const PadMapping in_game_pad, const GCPadStatus& np); void SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw);