Optimize network traffic a bit:
authorunc0rr
Wed, 16 Sep 2009 14:44:40 +0000
changeset 2382 bfd378dfc4e7
parent 2381 959da8402cac
child 2383 cc7d1ad986c6
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)
QTfrontend/game.cpp
hedgewars/uConsts.pas
hedgewars/uGame.pas
hedgewars/uIO.pas
hedgewars/uMisc.pas
--- 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': {
--- 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;
--- 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;
--- 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;
--- 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;