GBASockServer: cleanup GetNextSock and GetNextClock

This commit is contained in:
Michael Maltese 2017-03-31 14:07:28 -07:00
parent 751377256b
commit 89ca32daa6
1 changed files with 16 additions and 26 deletions

View File

@ -136,36 +136,26 @@ void GBAConnectionWaiter_Shutdown()
s_connection_thread.join(); s_connection_thread.join();
} }
static bool GetAvailableSock(std::unique_ptr<sf::TcpSocket>& sock_to_fill) template <typename T>
static std::unique_ptr<T> MoveFromFront(std::queue<std::unique_ptr<T>>& ptrs)
{ {
bool sock_filled = false; if (ptrs.empty())
return nullptr;
std::lock_guard<std::mutex> lk(s_cs_gba); std::unique_ptr<T> ptr = std::move(ptrs.front());
ptrs.pop();
if (!s_waiting_socks.empty()) return ptr;
{
sock_to_fill = std::move(s_waiting_socks.front());
s_waiting_socks.pop();
sock_filled = true;
}
return sock_filled;
} }
static bool GetNextClock(std::unique_ptr<sf::TcpSocket>& sock_to_fill) static std::unique_ptr<sf::TcpSocket> GetNextSock()
{ {
bool sock_filled = false; std::lock_guard<std::mutex> lk(s_cs_gba);
return MoveFromFront(s_waiting_socks);
}
static std::unique_ptr<sf::TcpSocket> GetNextClock()
{
std::lock_guard<std::mutex> lk(s_cs_gba_clk); std::lock_guard<std::mutex> lk(s_cs_gba_clk);
return MoveFromFront(s_waiting_clocks);
if (!s_waiting_clocks.empty())
{
sock_to_fill = std::move(s_waiting_clocks.front());
s_waiting_clocks.pop();
sock_filled = true;
}
return sock_filled;
} }
GBASockServer::GBASockServer() GBASockServer::GBASockServer()
@ -201,7 +191,7 @@ void GBASockServer::Disconnect()
void GBASockServer::ClockSync() void GBASockServer::ClockSync()
{ {
if (!m_clock_sync) if (!m_clock_sync)
if (!GetNextClock(m_clock_sync)) if (!(m_clock_sync = GetNextClock()))
return; return;
u32 time_slice = 0; u32 time_slice = 0;
@ -236,7 +226,7 @@ void GBASockServer::ClockSync()
bool GBASockServer::Connect() bool GBASockServer::Connect()
{ {
if (!IsConnected()) if (!IsConnected())
GetAvailableSock(m_client); m_client = GetNextSock();
return IsConnected(); return IsConnected();
} }