- more bugfixes for ping logic

This commit is contained in:
Mark Vejvoda 2010-07-09 18:49:01 +00:00
parent af6eb0d1e6
commit eb34eb1fe5
5 changed files with 15 additions and 4 deletions

View File

@ -49,6 +49,7 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM
MenuState(program, mainMenu, "connected-game") //← set on connected-game
{
lastNetworkSendPing = 0;
pingCount = 0;
returnMenuInfo=joinMenuInfo;
Lang &lang= Lang::getInstance();
@ -366,15 +367,19 @@ void MenuStateConnectedGame::update()
if(difftime(time(NULL),lastNetworkSendPing) >= GameConstants::networkPingInterval) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to sendPingMessage...\n",__FILE__,__FUNCTION__,__LINE__);
bool isFirstPing = (lastNetworkSendPing == 0);
lastNetworkSendPing = time(NULL);
clientInterface->sendPingMessage(GameConstants::networkPingInterval, time(NULL));
if(isFirstPing == false && clientInterface->getLastPingLag() >= (GameConstants::networkPingInterval * 2)) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] pingCount = %d, clientInterface->getLastPingLag() = %f, GameConstants::networkPingInterval = %d\n",__FILE__,__FUNCTION__,__LINE__,pingCount, clientInterface->getLastPingLag(),GameConstants::networkPingInterval);
// Starting checking timeout after sending at least 3 pings to server
if(pingCount >= 3 && clientInterface->getLastPingLag() >= (GameConstants::networkPingInterval * 3)) {
string playerNameStr = Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str());
clientInterface->sendTextMessage(playerNameStr + "'s connection timed out communicating with server.",-1);
clientInterface->close();
}
pingCount++;
}
}

View File

@ -72,6 +72,7 @@ private:
JoinMenu returnMenuInfo;
bool settingsReceivedFromServer;
time_t lastNetworkSendPing;
int pingCount;
public:

View File

@ -140,7 +140,7 @@ public:
NetworkMessagePing getLastPingInfo() const { return lastPingInfo; }
double getLastPingLag() const {
return difftime(time(NULL),lastPingInfo.getPingTime());
return difftime(time(NULL),lastPingInfo.getPingReceivedLocalTime());
}
std::string getIpAddress();

View File

@ -147,6 +147,7 @@ void NetworkMessageIntro::send(Socket* socket) const{
NetworkMessagePing::NetworkMessagePing(){
data.messageType= nmtPing;
pingReceivedLocalTime = 0;
}
NetworkMessagePing::NetworkMessagePing(int32 pingFrequency, int64 pingTime){
@ -156,7 +157,9 @@ NetworkMessagePing::NetworkMessagePing(int32 pingFrequency, int64 pingTime){
}
bool NetworkMessagePing::receive(Socket* socket){
return NetworkMessage::receive(socket, &data, sizeof(data));
bool result = NetworkMessage::receive(socket, &data, sizeof(data));
pingReceivedLocalTime = time(NULL);
return result;
}
void NetworkMessagePing::send(Socket* socket) const{

View File

@ -122,6 +122,7 @@ private:
private:
Data data;
int64 pingReceivedLocalTime;
public:
NetworkMessagePing();
@ -129,6 +130,7 @@ public:
int32 getPingFrequency() const {return data.pingFrequency;}
int64 getPingTime() const {return data.pingTime;}
int64 getPingReceivedLocalTime() const { return pingReceivedLocalTime; }
virtual bool receive(Socket* socket);
virtual void send(Socket* socket) const;