hedgewars/uIO.pas
branchwebgl
changeset 8833 c13ebed437cb
parent 8444 75db7bb8dce8
parent 8645 809e328bab99
child 8841 338f7c2bac2c
--- a/hedgewars/uIO.pas	Wed Feb 20 02:21:58 2013 +0100
+++ b/hedgewars/uIO.pas	Tue Apr 02 21:00:57 2013 +0200
@@ -30,7 +30,7 @@
 procedure SendIPCXY(cmd: char; X, Y: LongInt);
 procedure SendIPCRaw(p: pointer; len: Longword);
 procedure SendIPCAndWaitReply(s: shortstring);
-procedure SendKeepAliveMessage(Lag: Longword);
+procedure FlushMessages(Lag: Longword);
 procedure LoadRecordFromFile(fileName: shortstring);
 procedure SendStat(sit: TStatInfoType; s: shortstring);
 procedure IPCWaitPongEvent;
@@ -43,6 +43,7 @@
 
 const
     cSendEmptyPacketTime = 1000;
+    cSendBufferSize = 1024;
 
 type PCmd = ^TCmd;
      TCmd = packed record
@@ -63,7 +64,11 @@
     headcmd: PCmd;
     lastcmd: PCmd;
 
-    SendEmptyPacketTicks: LongWord;
+    flushDelayTicks: LongWord;
+    sendBuffer: record
+                buf: array[0..Pred(cSendBufferSize)] of byte;
+                count: Word;
+                end;
 
 function AddCmd(Time: Word; str: shortstring): PCmd;
 var command: PCmd;
@@ -140,7 +145,7 @@
      else
      loTicks:= SDLNet_Read16(@s[byte(s[0]) - 1]);
      AddCmd(loTicks, s);
-     AddFileLog('[IPC in] '+s[1]+' ticks '+IntToStr(lastcmd^.loTime));
+     AddFileLog('[IPC in] ' + sanitizeCharForLog(s[1]) + ' ticks ' + IntToStr(lastcmd^.loTime));
      end
 end;
 
@@ -215,20 +220,45 @@
 SendIPCRaw(@buf[0], length(buf) + 1)
 end;
 
+function isSyncedCommand(c: char): boolean;
+begin
+    isSyncedCommand:= (c in ['+', '#', 'L', 'l', 'R', 'r', 'U', 'u', 'D', 'd', 'Z', 'z', 'A', 'a', 'S', 'j', 'J', ',', 'c', 'N', 'p', 'P', 'w', 't', '1', '2', '3', '4', '5']) or ((c >= #128) and (c <= char(128 + cMaxSlotIndex)))
+end;
+
+procedure flushBuffer();
+begin
+    if IPCSock <> nil then
+        begin
+        SDLNet_TCP_Send(IPCSock, @sendBuffer.buf, sendBuffer.count);
+        flushDelayTicks:= 0;
+        sendBuffer.count:= 0
+        end
+end;
 
 procedure SendIPC(s: shortstring);
 begin
 if IPCSock <> nil then
     begin
-    SendEmptyPacketTicks:= 0;
-    if s[0]>#251 then
+    if s[0] > #251 then
         s[0]:= #251;
 
     SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
-    AddFileLog('[IPC out] '+ s[1]);
+    
+    AddFileLog('[IPC out] '+ sanitizeCharForLog(s[1]));
     inc(s[0], 2);
-       SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])));
-       //log('SendIPC');
+    
+    if isSyncedCommand(s[1]) then
+        begin
+        if sendBuffer.count + byte(s[0]) >= cSendBufferSize then
+            flushBuffer();
+            
+        Move(s, sendBuffer.buf[sendBuffer.count], byte(s[0]) + 1);
+        inc(sendBuffer.count, byte(s[0]) + 1);
+        
+        if (s[1] = 'N') or (s[1] = '#') then
+            flushBuffer();
+        end else
+        SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
     end
 end;
 
@@ -266,11 +296,16 @@
 IPCWaitPongEvent
 end;
 
-procedure SendKeepAliveMessage(Lag: Longword);
+procedure FlushMessages(Lag: Longword);
 begin
-inc(SendEmptyPacketTicks, Lag);
-if (SendEmptyPacketTicks >= cSendEmptyPacketTime) then
-    SendIPC(_S'+')
+inc(flushDelayTicks, Lag);
+if (flushDelayTicks >= cSendEmptyPacketTime) then
+    begin
+    if sendBuffer.count = 0 then
+        SendIPC(_S'+');
+        
+     flushBuffer()    
+    end
 end;
 
 procedure NetGetNextCmd;
@@ -436,8 +471,8 @@
     SocketString:= '';
 
     hiTicks:= 0;
-    SendEmptyPacketTicks:= 0;
-
+    flushDelayTicks:= 0;
+    sendBuffer.count:= 0;
 end;
 
 procedure freeModule;