From bc4a1cbd095f2acd19f4aa1892d4691a6dc0e00a Mon Sep 17 00:00:00 2001 From: AleaJactaEst Date: Thu, 30 May 2019 23:46:07 +0200 Subject: [PATCH] update decodeHeader - decode number message received --- client.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 11 deletions(-) diff --git a/client.py b/client.py index b70c196..254e8d6 100755 --- a/client.py +++ b/client.py @@ -42,6 +42,7 @@ import socket import struct import xml.etree.ElementTree as ET import hashlib +import time class BitStream(): @@ -1481,7 +1482,8 @@ class ClientNetworkConnection: def __init__(self, khanat_host, khanat_port_frontend, - LanguageCode="fr"): + LanguageCode="fr", + checkMessageNumber = True): self.log = logging.getLogger('myLogger') self._CurrentSendNumber = 0 self.LanguageCode = LanguageCode @@ -1501,6 +1503,22 @@ class ClientNetworkConnection: self._DatabaseXmlMD5 = None self.msgXml = None self.databaseXml = None + self._Synchronize = 0 + self._LatestSync = 0 + self._CurrentServerTick = 0 + self._MsPerTick = 0 + self._LCT = 100 + self._UpdateTime = 0 + #self._UpdateTicks = 0 + self._ReceivedSync = False + self._LastReceivedTime = 0 + self._LastReceivedPacketInBothModes = 0 + self._TotalMessages = 0 + self._TotalLostPackets = 0 + self.checkMessageNumber = checkMessageNumber + self._LastAckBit = 0 + self._AckBitMask = 0 + self._LongAckBitField = 0 def cookiesInit(self, UserAddr, UserKey, UserId): self.UserAddr = UserAddr @@ -1550,9 +1568,10 @@ class ClientNetworkConnection: msg.pushUint8(2) # SYSTEM_ACK_SYNC_CODE msg.pushSint32(self._LastReceivedNumber) msg.pushSint32(self._LastAckInLongAck) - # msg.pushSint32(self._LongAckBitField) # - # msg.pushSint32(self._LatestSync) + msg.pushSint32(self._LongAckBitField) # Signale le nombre de packet perdu + msg.pushSint32(self._LatestSync) self.log.error("TODO") + # self._sock.sendto(msg.toBytes(), self.frontend) def readDelta(self, msg): propertyCount = msg.readUint16() @@ -1574,12 +1593,49 @@ class ClientNetworkConnection: return data, addr def decodeHeader(self, msg): + self._TotalMessages += 1 + self._LastReceivedTime = self._UpdateTime self._CurrentReceivedNumber = msg.readSint32() self._SystemMode = msg.readBool() - if self._SystemMode: - return - self._LastReceivedAck = msg.readSint32(); + + if self.checkMessageNumber and self._CurrentReceivedNumber > self._LastReceivedPacketInBothModes: + self._TotalLostPackets += self._CurrentReceivedNumber - self._LastReceivedPacketInBothModes - 1 + self._LastReceivedPacketInBothModes = self._CurrentReceivedNumber + else: + self._LastReceivedPacketInBothModes = self._CurrentReceivedNumber - 1 + + if not self._SystemMode: + self._LastReceivedAck = msg.readSint32(); + + if self._CurrentReceivedNumber > self._LastReceivedNumber+1: + self.log.debug("lost messages server->client [%d; %d]" %(self._LastReceivedPacketInBothModes + 1, self._CurrentReceivedNumber - 1)) + elif self._CurrentReceivedNumber == self._LastReceivedNumber: + self.log.debug("awaiting packet %d, received packet %d" %(self._LastReceivedPacketInBothModes + 1, self._CurrentReceivedNumber)) + return False + elif self._CurrentReceivedNumber < self._LastReceivedNumber: + self.log.debug("received an old message, awaiting packet %d, received packet %d" %(self._LastReceivedPacketInBothModes + 1, self._CurrentReceivedNumber)) + return false + ackBool = ( not self._SystemMode ) and (self._ConnectionState == TConnectionState.Connected or self._ConnectionState == TConnectionState.Synchronize) + if ackBool: + ackBit = 1 + else: + ackBit = 0 + if self._CurrentReceivedNumber - self._LastReceivedNumber < 32: + self._AckBitMask <<= self._CurrentReceivedNumber - self._LastReceivedNumber; + elif (self_CurrentReceivedNumber - self_LastReceivedNumber) == 32 and self._LastAckBit != 0: + self._AckBitMask = 0x80000000 + else: + self._AckBitMask = 0x00000000 + self._LastAckBit = ackBit; + for i in range(self._LastReceivedNumber+1, self._CurrentReceivedNumber+1): + if self._LongAckBitField & (i & (512-1)) > 1: + self._LongAckBitField = self._LongAckBitField ^ (i & (512-1)) + if ackBool: + self._LongAckBitField = self._LongAckBitField | (self._CurrentReceivedNumber & (512 - 1)) + self._LongAckBitField %= 512 self._LastReceivedNumber = self._CurrentReceivedNumber + self.log.debug("_CurrentReceivedNumber:%d, _LastReceivedNumber:%d, ackBit:%d, _AckBitMask:%d _LongAckBitField:%d" % (self._CurrentReceivedNumber, self._LastReceivedNumber, ackBit, self._AckBitMask, self._LongAckBitField)) + return True def receiveSystemProbe(self, msg): self._LatestProbe = msg.readSint32() @@ -1589,10 +1645,10 @@ class ClientNetworkConnection: self.log.debug("received STALLED") def receiveSystemSync(self, msg): - _Synchronize = msg.readUint32() + self._Synchronize = msg.readUint32() stime = msg.readSint64() - _LatestSync = msg.readUint32() - self.log.debug("%d %d %d" %(_Synchronize, stime, _LatestSync)) + self._LatestSync = msg.readUint32() + self.log.debug("%d %d %d" %(self._Synchronize, stime, self._LatestSync)) # khanat-opennel-code/code/ryzom/client/src/network_connection.cpp : void CNetworkConnection::receiveSystemSync(CBitMemStream &msgin) MsgData = msg.readArrayUint8(16) DatabaseData = msg.readArrayUint8(16) @@ -1608,12 +1664,21 @@ class ClientNetworkConnection: self.log.info("Check MD5 database.xml : OK") else: self.log.error("Check MD5 database.xml : KO") - self.log.error("TODO") - #self.sendSystemAckSync() + self._MsPerTick = 100 + self._CurrentServerTick = self._Synchronize + self._CurrentReceivedNumber + 2 + self._CurrentClientTick = self._CurrentServerTick - ( self._LCT + self._MsPerTick ) / self._MsPerTick + self._CurrentClientTime = self._UpdateTime - (self._LCT + self._MsPerTick) + self.sendSystemAckSync() def disconnect(self): pass + def update(self): + # khanat-opennel-code/code/ryzom/client/src/network_connection.cpp # bool CNetworkConnection::update() + self._UpdateTime = int(time.clock_gettime(1)*1000) + #self._UpdateTicks = 0 + self._ReceivedSync = False + def EmulateFirst(self, msgRawXml, databaseRawXml): self.msgXml = ET.fromstring(msgRawXml) #ET.dump(msgXml) @@ -1628,6 +1693,7 @@ class ClientNetworkConnection: self.log.info("Receive Message") for _ in range(0, 20): # while True: + self.update() data, addr = self.buildStream() self.log.debug("received message: %s" % data) msg = BitStream()