Files
2025-03-05 08:37:43 +01:00

112 lines
2.7 KiB
C

/*
* PVVMUD a 3D MUD
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <algorithm>
#include "pvvmud.H"
#include "pingpong.H"
CPingPong::CPingPong(BYTE maxPing){
m_currPing = 0;
m_numPing = 0;
m_maxPing = maxPing;
m_pingPong = (pingPong_t*)malloc(m_maxPing*sizeof(pingPong_t));
for (int index = 0; index < m_maxPing; index++)
m_pingPong[index].state = PS_NOTUSED;
m_firstPing = TRUE;
}
CPingPong::~CPingPong(){
free(m_pingPong);
}
delta_t CPingPong::getDeltaVector(DWORD * deltaTime){
delta_t delta(m_numPing);
for (int index = 0; index < m_numPing; index++){
delta[index] = m_pingPong[index].reciveTime
- m_pingPong[index].sendTime;
}
sort(delta.begin(),delta.end());
*deltaTime = delta[m_numPing/2];
return delta;
}
BYTE CPingPong::sendPing(DWORD sendTime){
BYTE currPing = m_currPing;
m_currPing++;
if (m_numPing < m_currPing) m_numPing = m_currPing;
if (m_currPing >= m_maxPing) m_currPing = 0;
m_pingPong[currPing].sendTime = sendTime;
m_pingPong[currPing].state = PS_WAIT;
return currPing;
}
long CPingPong::recivePong(DWORD reciveTime, BYTE sequenceNumber,
DWORD serverTime)
{
m_pingPong[sequenceNumber].reciveTime = reciveTime;
m_pingPong[sequenceNumber].serverTime = serverTime;
m_pingPong[sequenceNumber].state = PS_OK;
DWORD sendTime = m_pingPong[sequenceNumber].sendTime;
DWORD timeDelta = reciveTime - sendTime;
if (timeDelta > 10000){
m_pingPong[sequenceNumber].state = PS_BAD;
}
DWORD deltaTime;
delta_t delta = getDeltaVector(&deltaTime);
long timediff = sendTime - (serverTime + deltaTime/2);
if (m_firstPing){
m_firstPing = FALSE;
return timediff;
}
return timediff/2;
}
ostream& operator<<(ostream&s,CPingPong&p){
s << (int)p.m_numPing << "/" << (int)p.m_maxPing << " :";
DWORD deltaTime;
delta_t delta = p.getDeltaVector(&deltaTime);
for (int index = 0; index < p.m_numPing; index++){
s << " " << delta[index];
}
s << " : " << deltaTime/2;
return s;
}