NetPlayClient: Make SendAsync take a unique_ptr as a parameter

This commit is contained in:
Lioncash 2016-01-24 22:10:38 -05:00
parent c2deb337aa
commit 8588c8fd31
2 changed files with 58 additions and 30 deletions

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <memory>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/ENetUtil.h" #include "Common/ENetUtil.h"
@ -512,11 +513,11 @@ void NetPlayClient::Disconnect()
m_server = nullptr; 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); 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); ENetUtil::WakeupThread(m_client);
} }
@ -616,46 +617,71 @@ std::vector<const Player*> NetPlayClient::GetPlayers()
// called from ---GUI--- thread // called from ---GUI--- thread
void NetPlayClient::SendChatMessage(const std::string& msg) void NetPlayClient::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 << static_cast<MessageId>(NP_MSG_CHAT_MESSAGE);
*spac << msg; *spac << msg;
SendAsync(spac);
SendAsync(std::move(spac));
} }
// called from ---CPU--- thread // called from ---CPU--- thread
void NetPlayClient::SendPadState(const PadMapping in_game_pad, const GCPadStatus& pad) void NetPlayClient::SendPadState(const PadMapping in_game_pad, const GCPadStatus& pad)
{ {
sf::Packet* spac = new sf::Packet; auto spac = std::make_unique<sf::Packet>();
*spac << (MessageId)NP_MSG_PAD_DATA; *spac << static_cast<MessageId>(NP_MSG_PAD_DATA);
*spac << in_game_pad; *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 // called from ---CPU--- thread
void NetPlayClient::SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw) void NetPlayClient::SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw)
{ {
sf::Packet* spac = new sf::Packet; auto spac = std::make_unique<sf::Packet>();
*spac << (MessageId)NP_MSG_WIIMOTE_DATA; *spac << static_cast<MessageId>(NP_MSG_WIIMOTE_DATA);
*spac << in_game_pad; *spac << in_game_pad;
*spac << (u8)nw.size(); *spac << static_cast<u8>(nw.size());
for (auto it : nw) for (auto it : nw)
{ {
*spac << it; *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 // called from ---GUI--- thread
bool NetPlayClient::StartGame(const std::string &path) bool NetPlayClient::StartGame(const std::string &path)
{ {
std::lock_guard<std::recursive_mutex> lkg(m_crit.game); std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
// tell server i started the game SendStartGamePacket();
sf::Packet* spac = new sf::Packet;
*spac << (MessageId)NP_MSG_START_GAME;
*spac << m_current_game;
SendAsync(spac);
if (m_is_running.load()) if (m_is_running.load())
{ {
@ -1006,13 +1032,10 @@ void NetPlayClient::Stop()
isPadMapped = true; 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) if (isPadMapped)
{ SendStopGamePacket();
sf::Packet* spac = new sf::Packet;
*spac << (MessageId)NP_MSG_STOP_GAME;
SendAsync(spac);
}
} }
u8 NetPlayClient::InGamePadToLocalPad(u8 ingame_pad) u8 NetPlayClient::InGamePadToLocalPad(u8 ingame_pad)
@ -1077,12 +1100,13 @@ void NetPlayClient::SendTimeBase()
u64 timebase = SystemTimers::GetFakeTimeBase(); u64 timebase = SystemTimers::GetFakeTimeBase();
sf::Packet* spac = new sf::Packet; auto spac = std::make_unique<sf::Packet>();
*spac << (MessageId)NP_MSG_TIMEBASE; *spac << static_cast<MessageId>(NP_MSG_TIMEBASE);
*spac << (u32)timebase; *spac << static_cast<u32>(timebase);
*spac << (u32)(timebase << 32); *spac << static_cast<u32>(timebase << 32);
*spac << netplay_client->m_timebase_frame++; *spac << netplay_client->m_timebase_frame++;
netplay_client->SendAsync(spac);
netplay_client->SendAsync(std::move(spac));
} }
// stuff hacked into dolphin // stuff hacked into dolphin

View File

@ -6,6 +6,7 @@
#include <atomic> #include <atomic>
#include <map> #include <map>
#include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
@ -48,7 +49,7 @@ class NetPlayClient : public TraversalClientClient
{ {
public: public:
void ThreadFunc(); 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(const std::string& address, const u16 port, NetPlayUI* dialog, const std::string& name, bool traversal, const std::string& centralServer, u16 centralPort);
~NetPlayClient(); ~NetPlayClient();
@ -128,6 +129,9 @@ private:
Failure Failure
}; };
void SendStartGamePacket();
void SendStopGamePacket();
void UpdateDevices(); void UpdateDevices();
void SendPadState(const PadMapping in_game_pad, const GCPadStatus& np); void SendPadState(const PadMapping in_game_pad, const GCPadStatus& np);
void SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw); void SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw);