diff options
| author | Lioncash <mathew1800@gmail.com> | 2016-01-24 22:10:38 -0500 |
|---|---|---|
| committer | Lioncash <mathew1800@gmail.com> | 2016-01-25 04:57:18 -0500 |
| commit | 8588c8fd3139c3e14d8f57cc88fcdbf7a2f6fa1d (patch) | |
| tree | 5a3e36e253cdb1aebddc8c5772b4cd9a66b10970 /Source | |
| parent | c2deb337aa7d8c6a881564faffeca15b046ad439 (diff) | |
NetPlayClient: Make SendAsync take a unique_ptr as a parameter
Diffstat (limited to 'Source')
| -rw-r--r-- | Source/Core/Core/NetPlayClient.cpp | 84 | ||||
| -rw-r--r-- | Source/Core/Core/NetPlayClient.h | 6 |
2 files changed, 59 insertions, 31 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 <memory> #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<sf::Packet> packet) { { 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_client); } @@ -616,46 +617,71 @@ std::vector<const Player*> 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<sf::Packet>(); + *spac << static_cast<MessageId>(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<sf::Packet>(); + *spac << static_cast<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; - - SendAsync(spac); + *spac << pad.button + << pad.analogA + << pad.analogB + << pad.stickX + << pad.stickY + << pad.substickX + << pad.substickY + << pad.triggerLeft + << pad.triggerRight; + + 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<sf::Packet>(); + *spac << static_cast<MessageId>(NP_MSG_WIIMOTE_DATA); *spac << in_game_pad; - *spac << (u8)nw.size(); + *spac << static_cast<u8>(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<sf::Packet>(); + *spac << static_cast<MessageId>(NP_MSG_START_GAME); + *spac << m_current_game; + + SendAsync(std::move(spac)); +} + +// called from ---GUI--- thread +void NetPlayClient::SendStopGamePacket() +{ + auto spac = std::make_unique<sf::Packet>(); + *spac << static_cast<MessageId>(NP_MSG_STOP_GAME); + + SendAsync(std::move(spac)); } // called from ---GUI--- thread bool NetPlayClient::StartGame(const std::string &path) { 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); + 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<sf::Packet>(); + *spac << static_cast<MessageId>(NP_MSG_TIMEBASE); + *spac << static_cast<u32>(timebase); + *spac << static_cast<u32>(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 <atomic> #include <map> +#include <memory> #include <mutex> #include <string> #include <thread> @@ -48,7 +49,7 @@ class NetPlayClient : public TraversalClientClient { public: void ThreadFunc(); - void SendAsync(sf::Packet* packet); + void SendAsync(std::unique_ptr<sf::Packet> 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); |
