hedgewars/uIO.pas
changeset 8472 da6b569ac930
parent 8373 209c9ba77a09
child 8498 eecadca7db50
equal deleted inserted replaced
8471:7681d14b9f01 8472:da6b569ac930
    28 procedure InitIPC;
    28 procedure InitIPC;
    29 procedure SendIPC(s: shortstring);
    29 procedure SendIPC(s: shortstring);
    30 procedure SendIPCXY(cmd: char; X, Y: LongInt);
    30 procedure SendIPCXY(cmd: char; X, Y: LongInt);
    31 procedure SendIPCRaw(p: pointer; len: Longword);
    31 procedure SendIPCRaw(p: pointer; len: Longword);
    32 procedure SendIPCAndWaitReply(s: shortstring);
    32 procedure SendIPCAndWaitReply(s: shortstring);
    33 procedure SendKeepAliveMessage(Lag: Longword);
    33 procedure FlushMessages(Lag: Longword);
    34 procedure LoadRecordFromFile(fileName: shortstring);
    34 procedure LoadRecordFromFile(fileName: shortstring);
    35 procedure SendStat(sit: TStatInfoType; s: shortstring);
    35 procedure SendStat(sit: TStatInfoType; s: shortstring);
    36 procedure IPCWaitPongEvent;
    36 procedure IPCWaitPongEvent;
    37 procedure IPCCheckSock;
    37 procedure IPCCheckSock;
    38 procedure NetGetNextCmd;
    38 procedure NetGetNextCmd;
    41 implementation
    41 implementation
    42 uses uConsole, uConsts, uVariables, uCommands, uUtils, uDebug;
    42 uses uConsole, uConsts, uVariables, uCommands, uUtils, uDebug;
    43 
    43 
    44 const
    44 const
    45     cSendEmptyPacketTime = 1000;
    45     cSendEmptyPacketTime = 1000;
       
    46     cSendBufferSize = 1024;
    46 
    47 
    47 type PCmd = ^TCmd;
    48 type PCmd = ^TCmd;
    48      TCmd = packed record
    49      TCmd = packed record
    49             Next: PCmd;
    50             Next: PCmd;
    50             loTime: Word;
    51             loTime: Word;
    61     SocketString: shortstring;
    62     SocketString: shortstring;
    62 
    63 
    63     headcmd: PCmd;
    64     headcmd: PCmd;
    64     lastcmd: PCmd;
    65     lastcmd: PCmd;
    65 
    66 
    66     SendEmptyPacketTicks: LongWord;
    67     flushDelayTicks: LongWord;
       
    68     sendBuffer: record
       
    69                 buf: array[0..Pred(cSendBufferSize)] of byte;
       
    70                 count: Word;
       
    71                 end;
    67 
    72 
    68 function AddCmd(Time: Word; str: shortstring): PCmd;
    73 function AddCmd(Time: Word; str: shortstring): PCmd;
    69 var command: PCmd;
    74 var command: PCmd;
    70 begin
    75 begin
    71 new(command);
    76 new(command);
   212 begin
   217 begin
   213 buf:= 'i' + stc[sit] + s;
   218 buf:= 'i' + stc[sit] + s;
   214 SendIPCRaw(@buf[0], length(buf) + 1)
   219 SendIPCRaw(@buf[0], length(buf) + 1)
   215 end;
   220 end;
   216 
   221 
       
   222 function isSyncedCommand(c: char): boolean;
       
   223 begin
       
   224     isSyncedCommand:= (c in ['+', '#', 'L', 'l', 'R', 'r', 'U', 'u', 'D', 'd', 'Z', 'z', 'A', 'a', 'S', 'j', 'J', ',', 'c', 's', 'b', 'F', 'N', 'p', 'P', 'w', 't', 'h', '1', '2', '3', '4', '5']) or ((c >= #128) and (c <= char(128 + cMaxSlotIndex)))
       
   225 end;
       
   226 
       
   227 procedure flushBuffer();
       
   228 begin
       
   229     SDLNet_TCP_Send(IPCSock, @sendBuffer.buf, sendBuffer.count);
       
   230     flushDelayTicks:= 0;
       
   231     sendBuffer.count:= 0
       
   232 end;
   217 
   233 
   218 procedure SendIPC(s: shortstring);
   234 procedure SendIPC(s: shortstring);
   219 begin
   235 begin
   220 if IPCSock <> nil then
   236 if IPCSock <> nil then
   221     begin
   237     begin
   222     SendEmptyPacketTicks:= 0;
   238     if s[0] > #251 then
   223     if s[0]>#251 then
       
   224         s[0]:= #251;
   239         s[0]:= #251;
   225         
   240         
   226     SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
   241     SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
   227     AddFileLog('[IPC out] '+ s[1]);
   242     AddFileLog('[IPC out] '+ s[1]);
   228     inc(s[0], 2);
   243     inc(s[0], 2);
   229     SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
   244     
       
   245     if isSyncedCommand(s[1]) then
       
   246         begin
       
   247         if sendBuffer.count + byte(s[0]) >= cSendBufferSize then
       
   248             flushBuffer();
       
   249             
       
   250         Move(s, sendBuffer.buf[sendBuffer.count], byte(s[0]) + 1);
       
   251         inc(sendBuffer.count, byte(s[0]) + 1)
       
   252         end else
       
   253         SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
   230     end
   254     end
   231 end;
   255 end;
   232 
   256 
   233 procedure SendIPCRaw(p: pointer; len: Longword);
   257 procedure SendIPCRaw(p: pointer; len: Longword);
   234 begin
   258 begin
   262 SendIPC(s);
   286 SendIPC(s);
   263 SendIPC(_S'?');
   287 SendIPC(_S'?');
   264 IPCWaitPongEvent
   288 IPCWaitPongEvent
   265 end;
   289 end;
   266 
   290 
   267 procedure SendKeepAliveMessage(Lag: Longword);
   291 procedure FlushMessages(Lag: Longword);
   268 begin
   292 begin
   269 inc(SendEmptyPacketTicks, Lag);
   293 inc(flushDelayTicks, Lag);
   270 if (SendEmptyPacketTicks >= cSendEmptyPacketTime) then
   294 if (flushDelayTicks >= cSendEmptyPacketTime) then
   271     SendIPC(_S'+')
   295     begin
       
   296     if sendBuffer.count = 0 then
       
   297         SendIPC(_S'+');
       
   298         
       
   299      flushBuffer()    
       
   300     end
   272 end;
   301 end;
   273 
   302 
   274 procedure NetGetNextCmd;
   303 procedure NetGetNextCmd;
   275 var tmpflag: boolean;
   304 var tmpflag: boolean;
   276     s: shortstring;
   305     s: shortstring;
   432     lastcmd:= nil;
   461     lastcmd:= nil;
   433     isPonged:= false;
   462     isPonged:= false;
   434     SocketString:= '';
   463     SocketString:= '';
   435     
   464     
   436     hiTicks:= 0;
   465     hiTicks:= 0;
   437     SendEmptyPacketTicks:= 0;
   466     flushDelayTicks:= 0;
       
   467     sendBuffer.count:= 0;
   438 end;
   468 end;
   439 
   469 
   440 procedure freeModule;
   470 procedure freeModule;
   441 begin
   471 begin
   442     while headcmd <> nil do RemoveCmd;
   472     while headcmd <> nil do RemoveCmd;