Update server ping with each game action (#239).
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
|
||||
#include <game_defs.h>
|
||||
#include <gamedata.h>
|
||||
#include <serverdata.h>
|
||||
|
||||
class Game;
|
||||
|
||||
@@ -51,6 +52,7 @@ public:
|
||||
virtual void SignalNetClientError(int errorID, int osErrorID) = 0;
|
||||
virtual void SignalNetClientNotification(int notificationId) = 0;
|
||||
virtual void SignalNetClientStatsUpdate(const ServerStats &stats) = 0;
|
||||
virtual void SignalNetClientPingUpdate(unsigned minPing, unsigned avgPing, unsigned maxPing) = 0;
|
||||
virtual void SignalNetClientShowTimeoutDialog(NetTimeoutReason reason, unsigned remainingSec) = 0;
|
||||
virtual void SignalNetClientRemovedFromGame(int notificationId) = 0;
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/enable_shared_from_this.hpp>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
|
||||
#include <core/thread.h>
|
||||
#include <net/sessiondatacallback.h>
|
||||
@@ -56,6 +58,33 @@ class Log;
|
||||
class QtToolsInterface;
|
||||
struct Gsasl;
|
||||
|
||||
#define SIZE_PING_BACKLOG 20
|
||||
|
||||
class PingData
|
||||
{
|
||||
public:
|
||||
PingData() : pingTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::manual_start) {}
|
||||
|
||||
unsigned MinPing() {return *std::min_element(pingValues.begin(), pingValues.end());}
|
||||
unsigned MaxPing() {return *std::max_element(pingValues.begin(), pingValues.end());}
|
||||
unsigned AveragePing() {return std::accumulate(pingValues.begin(), pingValues.end(), 0) / (unsigned)pingValues.size();}
|
||||
void StartPing() {pingTimer.start();}
|
||||
void EndPing()
|
||||
{
|
||||
if (pingTimer.is_running()) {
|
||||
pingValues.push_back((unsigned)pingTimer.elapsed().total_milliseconds());
|
||||
if (pingValues.size() > SIZE_PING_BACKLOG) {
|
||||
pingValues.pop_front();
|
||||
}
|
||||
pingTimer.reset();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<unsigned> pingValues;
|
||||
boost::timers::portable::microsec_timer pingTimer;
|
||||
};
|
||||
|
||||
class ClientThread : public Thread, public boost::enable_shared_from_this<ClientThread>, public SessionDataCallback
|
||||
{
|
||||
public:
|
||||
@@ -228,6 +257,7 @@ protected:
|
||||
void EndPetition(unsigned petitionId);
|
||||
|
||||
void UpdateStatData(const ServerStats &stats);
|
||||
void EndPing();
|
||||
|
||||
bool IsSessionEstablished() const;
|
||||
void SetSessionEstablished(bool flag);
|
||||
@@ -298,6 +328,9 @@ private:
|
||||
mutable boost::mutex m_curStatsMutex;
|
||||
ServerStats m_curStats;
|
||||
|
||||
mutable boost::mutex m_pingDataMutex;
|
||||
PingData m_pingData;
|
||||
|
||||
boost::asio::deadline_timer m_stateTimer;
|
||||
boost::asio::deadline_timer m_avatarTimer;
|
||||
|
||||
|
||||
@@ -1771,6 +1771,10 @@ ClientStateRunHand::InternalHandlePacket(boost::shared_ptr<ClientThread> client,
|
||||
client->GetClientLog()->transformPlayerActionLog(PlayerAction(netActionDone.playeraction())),
|
||||
netActionDone.totalplayerbet() - tmpPlayer->getMySet()
|
||||
);
|
||||
if (tmpPlayer->getMyID() == 0)
|
||||
{
|
||||
client->EndPing();
|
||||
}
|
||||
}
|
||||
// Update last players turn only after the blinds.
|
||||
curGame->getCurrentHand()->setPreviousPlayerID(tmpPlayer->getMyID());
|
||||
|
||||
@@ -158,6 +158,10 @@ ClientThread::SendPlayerAction()
|
||||
{
|
||||
// Warning: This function is called in the context of the GUI thread.
|
||||
// Create a network packet containing the current player action.
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_pingDataMutex);
|
||||
m_pingData.StartPing();
|
||||
}
|
||||
boost::shared_ptr<NetPacket> packet(new NetPacket);
|
||||
packet->GetMsg()->set_messagetype(PokerTHMessage::Type_MyActionRequestMessage);
|
||||
MyActionRequestMessage *netMyAction = packet->GetMsg()->mutable_myactionrequestmessage();
|
||||
@@ -1558,6 +1562,14 @@ ClientThread::UpdateStatData(const ServerStats &stats)
|
||||
GetCallback().SignalNetClientStatsUpdate(m_curStats);
|
||||
}
|
||||
|
||||
void
|
||||
ClientThread::EndPing()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_pingDataMutex);
|
||||
m_pingData.EndPing();
|
||||
GetCallback().SignalNetClientPingUpdate(m_pingData.MinPing(), m_pingData.AveragePing(), m_pingData.MaxPing());
|
||||
}
|
||||
|
||||
ServerStats
|
||||
ClientThread::GetStatData() const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user