Merge pull request #4876 from ligfx/netplay_move_semantics

NetPlay: use move semantics instead of unique_ptrs
This commit is contained in:
Markus Wick 2017-03-14 10:20:47 +01:00 committed by GitHub
commit d05f59e31d
4 changed files with 97 additions and 98 deletions

View File

@ -170,11 +170,11 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay
bool NetPlayClient::Connect() bool NetPlayClient::Connect()
{ {
// send connect message // send connect message
sf::Packet spac; sf::Packet packet;
spac << scm_rev_git_str; packet << scm_rev_git_str;
spac << netplay_dolphin_ver; packet << netplay_dolphin_ver;
spac << m_player_name; packet << m_player_name;
Send(spac); Send(packet);
enet_host_flush(m_client); enet_host_flush(m_client);
sf::Packet rpac; sf::Packet rpac;
// TODO: make this not hang // TODO: make this not hang
@ -368,15 +368,15 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
// update gui // update gui
m_dialog->OnMsgChangeGame(m_selected_game); m_dialog->OnMsgChangeGame(m_selected_game);
sf::Packet spac; sf::Packet packet;
spac << static_cast<MessageId>(NP_MSG_GAME_STATUS); packet << static_cast<MessageId>(NP_MSG_GAME_STATUS);
PlayerGameStatus status = m_dialog->FindGame(m_selected_game).empty() ? PlayerGameStatus status = m_dialog->FindGame(m_selected_game).empty() ?
PlayerGameStatus::NotFound : PlayerGameStatus::NotFound :
PlayerGameStatus::Ok; PlayerGameStatus::Ok;
spac << static_cast<u32>(status); packet << static_cast<u32>(status);
Send(spac); Send(packet);
} }
break; break;
@ -445,11 +445,11 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
u32 ping_key = 0; u32 ping_key = 0;
packet >> ping_key; packet >> ping_key;
sf::Packet spac; sf::Packet packet;
spac << (MessageId)NP_MSG_PONG; packet << (MessageId)NP_MSG_PONG;
spac << ping_key; packet << ping_key;
Send(spac); Send(packet);
} }
break; break;
@ -614,7 +614,7 @@ void NetPlayClient::Disconnect()
m_server = nullptr; m_server = nullptr;
} }
void NetPlayClient::SendAsync(std::unique_ptr<sf::Packet> packet) void NetPlayClient::SendAsync(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);
@ -635,7 +635,7 @@ void NetPlayClient::ThreadFunc()
net = enet_host_service(m_client, &netEvent, 250); net = enet_host_service(m_client, &netEvent, 250);
while (!m_async_queue.Empty()) while (!m_async_queue.Empty())
{ {
Send(*(m_async_queue.Front().get())); Send(m_async_queue.Front());
m_async_queue.Pop(); m_async_queue.Pop();
} }
if (net > 0) if (net > 0)
@ -733,57 +733,57 @@ 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)
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet packet;
*spac << static_cast<MessageId>(NP_MSG_CHAT_MESSAGE); packet << static_cast<MessageId>(NP_MSG_CHAT_MESSAGE);
*spac << msg; packet << msg;
SendAsync(std::move(spac)); SendAsync(std::move(packet));
} }
// called from ---CPU--- thread // called from ---CPU--- thread
void NetPlayClient::SendPadState(const int in_game_pad, const GCPadStatus& pad) void NetPlayClient::SendPadState(const int in_game_pad, const GCPadStatus& pad)
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet packet;
*spac << static_cast<MessageId>(NP_MSG_PAD_DATA); packet << static_cast<MessageId>(NP_MSG_PAD_DATA);
*spac << static_cast<PadMapping>(in_game_pad); packet << static_cast<PadMapping>(in_game_pad);
*spac << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX packet << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX
<< pad.substickY << pad.triggerLeft << pad.triggerRight; << pad.substickY << pad.triggerLeft << pad.triggerRight;
SendAsync(std::move(spac)); SendAsync(std::move(packet));
} }
// called from ---CPU--- thread // called from ---CPU--- thread
void NetPlayClient::SendWiimoteState(const int in_game_pad, const NetWiimote& nw) void NetPlayClient::SendWiimoteState(const int in_game_pad, const NetWiimote& nw)
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet packet;
*spac << static_cast<MessageId>(NP_MSG_WIIMOTE_DATA); packet << static_cast<MessageId>(NP_MSG_WIIMOTE_DATA);
*spac << static_cast<PadMapping>(in_game_pad); packet << static_cast<PadMapping>(in_game_pad);
*spac << static_cast<u8>(nw.size()); packet << static_cast<u8>(nw.size());
for (auto it : nw) for (auto it : nw)
{ {
*spac << it; packet << it;
} }
SendAsync(std::move(spac)); SendAsync(std::move(packet));
} }
// called from ---GUI--- thread // called from ---GUI--- thread
void NetPlayClient::SendStartGamePacket() void NetPlayClient::SendStartGamePacket()
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet packet;
*spac << static_cast<MessageId>(NP_MSG_START_GAME); packet << static_cast<MessageId>(NP_MSG_START_GAME);
*spac << m_current_game; packet << m_current_game;
SendAsync(std::move(spac)); SendAsync(std::move(packet));
} }
// called from ---GUI--- thread // called from ---GUI--- thread
void NetPlayClient::SendStopGamePacket() void NetPlayClient::SendStopGamePacket()
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet packet;
*spac << static_cast<MessageId>(NP_MSG_STOP_GAME); packet << static_cast<MessageId>(NP_MSG_STOP_GAME);
SendAsync(std::move(spac)); SendAsync(std::move(packet));
} }
// called from ---GUI--- thread // called from ---GUI--- thread
@ -1193,13 +1193,13 @@ void NetPlayClient::SendTimeBase()
u64 timebase = SystemTimers::GetFakeTimeBase(); u64 timebase = SystemTimers::GetFakeTimeBase();
auto spac = std::make_unique<sf::Packet>(); sf::Packet packet;
*spac << static_cast<MessageId>(NP_MSG_TIMEBASE); packet << static_cast<MessageId>(NP_MSG_TIMEBASE);
*spac << static_cast<u32>(timebase); packet << static_cast<u32>(timebase);
*spac << static_cast<u32>(timebase << 32); packet << static_cast<u32>(timebase << 32);
*spac << netplay_client->m_timebase_frame++; packet << netplay_client->m_timebase_frame++;
netplay_client->SendAsync(std::move(spac)); netplay_client->SendAsync(std::move(packet));
} }
bool NetPlayClient::DoAllPlayersHaveGame() bool NetPlayClient::DoAllPlayersHaveGame()
@ -1226,27 +1226,27 @@ void NetPlayClient::ComputeMD5(const std::string& file_identifier)
if (file.empty() || !File::Exists(file)) if (file.empty() || !File::Exists(file))
{ {
sf::Packet spac; sf::Packet packet;
spac << static_cast<MessageId>(NP_MSG_MD5_ERROR); packet << static_cast<MessageId>(NP_MSG_MD5_ERROR);
spac << "file not found"; packet << "file not found";
Send(spac); Send(packet);
return; return;
} }
m_MD5_thread = std::thread([this, file]() { m_MD5_thread = std::thread([this, file]() {
std::string sum = MD5::MD5Sum(file, [&](int progress) { std::string sum = MD5::MD5Sum(file, [&](int progress) {
sf::Packet spac; sf::Packet packet;
spac << static_cast<MessageId>(NP_MSG_MD5_PROGRESS); packet << static_cast<MessageId>(NP_MSG_MD5_PROGRESS);
spac << progress; packet << progress;
Send(spac); Send(packet);
return m_should_compute_MD5; return m_should_compute_MD5;
}); });
sf::Packet spac; sf::Packet packet;
spac << static_cast<MessageId>(NP_MSG_MD5_RESULT); packet << static_cast<MessageId>(NP_MSG_MD5_RESULT);
spac << sum; packet << sum;
Send(spac); Send(packet);
}); });
m_MD5_thread.detach(); m_MD5_thread.detach();
} }

View File

@ -7,7 +7,6 @@
#include <SFML/Network/Packet.hpp> #include <SFML/Network/Packet.hpp>
#include <array> #include <array>
#include <map> #include <map>
#include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
@ -65,7 +64,7 @@ class NetPlayClient : public TraversalClientClient
{ {
public: public:
void ThreadFunc(); void ThreadFunc();
void SendAsync(std::unique_ptr<sf::Packet> packet); void SendAsync(sf::Packet&& packet);
NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog,
const std::string& name, bool traversal, const std::string& centralServer, const std::string& name, bool traversal, const std::string& centralServer,
@ -111,7 +110,7 @@ protected:
std::recursive_mutex async_queue_write; std::recursive_mutex async_queue_write;
} m_crit; } m_crit;
Common::FifoQueue<std::unique_ptr<sf::Packet>, false> m_async_queue; Common::FifoQueue<sf::Packet, false> m_async_queue;
std::array<Common::FifoQueue<GCPadStatus>, 4> m_pad_buffer; std::array<Common::FifoQueue<GCPadStatus>, 4> m_pad_buffer;
std::array<Common::FifoQueue<NetWiimote>, 4> m_wiimote_buffer; std::array<Common::FifoQueue<NetWiimote>, 4> m_wiimote_buffer;

View File

@ -129,7 +129,7 @@ void NetPlayServer::ThreadFunc()
{ {
{ {
std::lock_guard<std::recursive_mutex> lkp(m_crit.players); std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
SendToClients(*(m_async_queue.Front().get())); SendToClients(m_async_queue.Front());
} }
m_async_queue.Pop(); m_async_queue.Pop();
} }
@ -456,14 +456,14 @@ 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
auto spac = std::make_unique<sf::Packet>(); sf::Packet spac;
*spac << static_cast<MessageId>(NP_MSG_PAD_BUFFER); spac << static_cast<MessageId>(NP_MSG_PAD_BUFFER);
*spac << static_cast<u32>(m_target_buffer_size); spac << static_cast<u32>(m_target_buffer_size);
SendAsyncToClients(std::move(spac)); SendAsyncToClients(std::move(spac));
} }
void NetPlayServer::SendAsyncToClients(std::unique_ptr<sf::Packet> packet) void NetPlayServer::SendAsyncToClients(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);
@ -723,10 +723,10 @@ 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)
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet spac;
*spac << static_cast<MessageId>(NP_MSG_CHAT_MESSAGE); spac << static_cast<MessageId>(NP_MSG_CHAT_MESSAGE);
*spac << static_cast<PlayerId>(0); // server id always 0 spac << static_cast<PlayerId>(0); // server id always 0
*spac << msg; spac << msg;
SendAsyncToClients(std::move(spac)); SendAsyncToClients(std::move(spac));
} }
@ -739,9 +739,9 @@ 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
auto spac = std::make_unique<sf::Packet>(); sf::Packet spac;
*spac << static_cast<MessageId>(NP_MSG_CHANGE_GAME); spac << static_cast<MessageId>(NP_MSG_CHANGE_GAME);
*spac << game; spac << game;
SendAsyncToClients(std::move(spac)); SendAsyncToClients(std::move(spac));
@ -751,9 +751,9 @@ bool NetPlayServer::ChangeGame(const std::string& game)
// called from ---GUI--- thread // called from ---GUI--- thread
bool NetPlayServer::ComputeMD5(const std::string& file_identifier) bool NetPlayServer::ComputeMD5(const std::string& file_identifier)
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet spac;
*spac << static_cast<MessageId>(NP_MSG_COMPUTE_MD5); spac << static_cast<MessageId>(NP_MSG_COMPUTE_MD5);
*spac << file_identifier; spac << file_identifier;
SendAsyncToClients(std::move(spac)); SendAsyncToClients(std::move(spac));
@ -763,8 +763,8 @@ bool NetPlayServer::ComputeMD5(const std::string& file_identifier)
// called from ---GUI--- thread // called from ---GUI--- thread
bool NetPlayServer::AbortMD5() bool NetPlayServer::AbortMD5()
{ {
auto spac = std::make_unique<sf::Packet>(); sf::Packet spac;
*spac << static_cast<MessageId>(NP_MSG_MD5_ABORT); spac << static_cast<MessageId>(NP_MSG_MD5_ABORT);
SendAsyncToClients(std::move(spac)); SendAsyncToClients(std::move(spac));
@ -794,26 +794,26 @@ bool NetPlayServer::StartGame()
g_netplay_initial_rtc = Common::Timer::GetLocalTimeSinceJan1970(); g_netplay_initial_rtc = Common::Timer::GetLocalTimeSinceJan1970();
// tell clients to start game // tell clients to start game
auto spac = std::make_unique<sf::Packet>(); sf::Packet spac;
*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;
*spac << m_settings.m_CPUcore; spac << m_settings.m_CPUcore;
*spac << m_settings.m_EnableCheats; spac << m_settings.m_EnableCheats;
*spac << m_settings.m_SelectedLanguage; spac << m_settings.m_SelectedLanguage;
*spac << m_settings.m_OverrideGCLanguage; spac << m_settings.m_OverrideGCLanguage;
*spac << m_settings.m_ProgressiveScan; spac << m_settings.m_ProgressiveScan;
*spac << m_settings.m_PAL60; spac << m_settings.m_PAL60;
*spac << m_settings.m_DSPEnableJIT; spac << m_settings.m_DSPEnableJIT;
*spac << m_settings.m_DSPHLE; spac << m_settings.m_DSPHLE;
*spac << m_settings.m_WriteToMemcard; spac << m_settings.m_WriteToMemcard;
*spac << m_settings.m_CopyWiiSave; spac << m_settings.m_CopyWiiSave;
*spac << m_settings.m_OCEnable; spac << m_settings.m_OCEnable;
*spac << m_settings.m_OCFactor; spac << m_settings.m_OCFactor;
*spac << m_settings.m_EXIDevice[0]; spac << m_settings.m_EXIDevice[0];
*spac << m_settings.m_EXIDevice[1]; spac << m_settings.m_EXIDevice[1];
*spac << (u32)g_netplay_initial_rtc; spac << (u32)g_netplay_initial_rtc;
*spac << (u32)(g_netplay_initial_rtc >> 32); spac << (u32)(g_netplay_initial_rtc >> 32);
SendAsyncToClients(std::move(spac)); SendAsyncToClients(std::move(spac));

View File

@ -26,7 +26,7 @@ class NetPlayServer : public TraversalClientClient
{ {
public: public:
void ThreadFunc(); void ThreadFunc();
void SendAsyncToClients(std::unique_ptr<sf::Packet> packet); void SendAsyncToClients(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();
@ -118,7 +118,7 @@ private:
std::string m_selected_game; std::string m_selected_game;
std::thread m_thread; std::thread m_thread;
Common::FifoQueue<std::unique_ptr<sf::Packet>, false> m_async_queue; Common::FifoQueue<sf::Packet, false> m_async_queue;
ENetHost* m_server = nullptr; ENetHost* m_server = nullptr;
TraversalClient* m_traversal_client = nullptr; TraversalClient* m_traversal_client = nullptr;