# HG changeset patch # User unc0rr # Date 1253112280 0 # Node ID bfd378dfc4e791db888a757049def68c92aa77e1 # Parent 959da8402cacdda060056d73ffb3dfd439c542b0 Optimize network traffic a bit: - Send keepalive packets every second - Don't send keepalive packet if there was action packet within a second Time between turns in net game is reduced to 1 second now (was 2 seconds) diff -r 959da8402cac -r bfd378dfc4e7 QTfrontend/game.cpp --- a/QTfrontend/game.cpp Wed Sep 16 12:48:27 2009 +0000 +++ b/QTfrontend/game.cpp Wed Sep 16 14:44:40 2009 +0000 @@ -164,6 +164,7 @@ void HWGame::ParseMessage(const QByteArray & msg) { +qDebug() << msg; switch(msg.at(1)) { case '?': { SendIPC("!"); @@ -210,7 +211,7 @@ } case 'i': { int size = msg.size(); - emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3).left(size - 5))); + emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); break; } case 'Q': { diff -r 959da8402cac -r bfd378dfc4e7 hedgewars/uConsts.pas --- a/hedgewars/uConsts.pas Wed Sep 16 12:48:27 2009 +0000 +++ b/hedgewars/uConsts.pas Wed Sep 16 14:44:40 2009 +0000 @@ -169,7 +169,8 @@ ifIgnoreCaps = $00000008; // ignore hardware capabilities when loading (i.e. image will not be drawn using OpenGL) ifLowRes = $00000010; // try loading a low resolution image when it is critical -const cMaxPower = 1500; +const + cMaxPower = 1500; cMaxAngle = 2048; cPowerDivisor = 1500; @@ -239,6 +240,8 @@ cMaxCaptions = 4; + cSendEmptyPacketTime = 1000; + gfForts = $00000001; gfMultiWeapon = $00000002; gfSolidLand = $00000004; diff -r 959da8402cac -r bfd378dfc4e7 hedgewars/uGame.pas --- a/hedgewars/uGame.pas Wed Sep 16 12:48:27 2009 +0000 +++ b/hedgewars/uGame.pas Wed Sep 16 14:44:40 2009 +0000 @@ -31,21 +31,15 @@ uses uMisc, uConsts, uWorld, uKeys, uTeams, uIO, uAI, uGears, uConsole; procedure DoGameTick(Lag: LongInt); -const SendEmptyPacketTicks: LongWord = 0; var i: LongInt; begin if isPaused then exit; if (not CurrentTeam^.ExtDriven) then - begin - NetGetNextCmd; // its for the case of receiving "/say" message - isInLag:= false; - inc(SendEmptyPacketTicks, Lag); - if (SendEmptyPacketTicks >= cSendEmptyPacketTime) then - begin - SendIPC('+'); - SendEmptyPacketTicks:= 0 - end - end; + begin + NetGetNextCmd; // its for the case of receiving "/say" message + isInLag:= false; + SendKeepAliveMessage(Lag) + end; if Lag > 100 then Lag:= 100 else if (GameType = gmtSave) or (fastUntilLag and (GameType = gmtNet)) then Lag:= 2500; if (GameType = gmtDemo) and isSpeed then Lag:= Lag * 10; diff -r 959da8402cac -r bfd378dfc4e7 hedgewars/uIO.pas --- a/hedgewars/uIO.pas Wed Sep 16 12:48:27 2009 +0000 +++ b/hedgewars/uIO.pas Wed Sep 16 14:44:40 2009 +0000 @@ -28,6 +28,7 @@ procedure SendIPCRaw(p: pointer; len: Longword); procedure SendIPCAndWaitReply(s: shortstring); procedure SendIPCTimeInc; +procedure SendKeepAliveMessage(Lag: Longword); procedure LoadRecordFromFile(fileName: shortstring); procedure IPCWaitPongEvent; procedure IPCCheckSock; @@ -52,11 +53,15 @@ 2: (str: shortstring); end; -var IPCSock: PTCPSocket = nil; - fds: PSDLNet_SocketSet; +var + IPCSock: PTCPSocket = nil; + fds: PSDLNet_SocketSet; - headcmd: PCmd = nil; - lastcmd: PCmd = nil; + headcmd: PCmd = nil; + lastcmd: PCmd = nil; + + SendEmptyPacketTicks: LongWord = 0; + function AddCmd(Time: Longword; str: shortstring): PCmd; var Result: PCmd; @@ -196,13 +201,14 @@ procedure SendIPC(s: shortstring); begin if IPCSock <> nil then - begin - if s[0]>#251 then s[0]:= #251; - SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]); - {$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s[1]);{$ENDIF} - inc(s[0], 2); - SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0]))) - end + begin + SendEmptyPacketTicks:= 0; + if s[0]>#251 then s[0]:= #251; + SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]); + {$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s[1]);{$ENDIF} + inc(s[0], 2); + SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0]))) + end end; procedure SendIPCRaw(p: pointer; len: Longword); @@ -245,6 +251,13 @@ IPCWaitPongEvent end; +procedure SendKeepAliveMessage(Lag: Longword); +begin +inc(SendEmptyPacketTicks, Lag); +if (SendEmptyPacketTicks >= cSendEmptyPacketTime) then + SendIPC('+') +end; + procedure NetGetNextCmd; var tmpflag: boolean; s: shortstring; diff -r 959da8402cac -r bfd378dfc4e7 hedgewars/uMisc.pas --- a/hedgewars/uMisc.pas Wed Sep 16 12:48:27 2009 +0000 +++ b/hedgewars/uMisc.pas Wed Sep 16 14:44:40 2009 +0000 @@ -99,9 +99,8 @@ {$WARNINGS ON} var - cSendEmptyPacketTime : LongWord = 2000; cSendCursorPosTime : LongWord = 50; - ShowCrosshair : boolean; + ShowCrosshair : boolean; cDrownSpeed, cMaxWindSpeed, cWindSpeed, @@ -275,8 +274,10 @@ procedure SendStat(sit: TStatInfoType; s: shortstring); const stc: array [TStatInfoType] of char = 'rDkKH'; +var buf: shortstring; begin -SendIPC('i' + stc[sit] + s) +buf:= 'i' + stc[sit] + s; +SendIPCRaw(@buf[0], length(buf) + 1) end; function Str2PChar(const s: shortstring): PChar;