hedgewars/uIO.pas
branchwebgl
changeset 8833 c13ebed437cb
parent 8444 75db7bb8dce8
parent 8645 809e328bab99
child 8841 338f7c2bac2c
equal deleted inserted replaced
8450:404ddce27b23 8833:c13ebed437cb
    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);
   138                   ParseCommand('campvar ' + copy(s, 3, length(s) - 2), true);
   143                   ParseCommand('campvar ' + copy(s, 3, length(s) - 2), true);
   139           end
   144           end
   140      else
   145      else
   141      loTicks:= SDLNet_Read16(@s[byte(s[0]) - 1]);
   146      loTicks:= SDLNet_Read16(@s[byte(s[0]) - 1]);
   142      AddCmd(loTicks, s);
   147      AddCmd(loTicks, s);
   143      AddFileLog('[IPC in] '+s[1]+' ticks '+IntToStr(lastcmd^.loTime));
   148      AddFileLog('[IPC in] ' + sanitizeCharForLog(s[1]) + ' ticks ' + IntToStr(lastcmd^.loTime));
   144      end
   149      end
   145 end;
   150 end;
   146 
   151 
   147 procedure IPCCheckSock;
   152 procedure IPCCheckSock;
   148 var i: LongInt;
   153 var i: LongInt;
   213 begin
   218 begin
   214 buf:= 'i' + stc[sit] + s;
   219 buf:= 'i' + stc[sit] + s;
   215 SendIPCRaw(@buf[0], length(buf) + 1)
   220 SendIPCRaw(@buf[0], length(buf) + 1)
   216 end;
   221 end;
   217 
   222 
       
   223 function isSyncedCommand(c: char): boolean;
       
   224 begin
       
   225     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)))
       
   226 end;
       
   227 
       
   228 procedure flushBuffer();
       
   229 begin
       
   230     if IPCSock <> nil then
       
   231         begin
       
   232         SDLNet_TCP_Send(IPCSock, @sendBuffer.buf, sendBuffer.count);
       
   233         flushDelayTicks:= 0;
       
   234         sendBuffer.count:= 0
       
   235         end
       
   236 end;
   218 
   237 
   219 procedure SendIPC(s: shortstring);
   238 procedure SendIPC(s: shortstring);
   220 begin
   239 begin
   221 if IPCSock <> nil then
   240 if IPCSock <> nil then
   222     begin
   241     begin
   223     SendEmptyPacketTicks:= 0;
   242     if s[0] > #251 then
   224     if s[0]>#251 then
       
   225         s[0]:= #251;
   243         s[0]:= #251;
   226 
   244 
   227     SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
   245     SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
   228     AddFileLog('[IPC out] '+ s[1]);
   246     
       
   247     AddFileLog('[IPC out] '+ sanitizeCharForLog(s[1]));
   229     inc(s[0], 2);
   248     inc(s[0], 2);
   230        SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])));
   249     
   231        //log('SendIPC');
   250     if isSyncedCommand(s[1]) then
       
   251         begin
       
   252         if sendBuffer.count + byte(s[0]) >= cSendBufferSize then
       
   253             flushBuffer();
       
   254             
       
   255         Move(s, sendBuffer.buf[sendBuffer.count], byte(s[0]) + 1);
       
   256         inc(sendBuffer.count, byte(s[0]) + 1);
       
   257         
       
   258         if (s[1] = 'N') or (s[1] = '#') then
       
   259             flushBuffer();
       
   260         end else
       
   261         SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
   232     end
   262     end
   233 end;
   263 end;
   234 
   264 
   235 procedure SendIPCRaw(p: pointer; len: Longword);
   265 procedure SendIPCRaw(p: pointer; len: Longword);
   236 begin
   266 begin
   264 SendIPC(s);
   294 SendIPC(s);
   265 SendIPC(_S'?');
   295 SendIPC(_S'?');
   266 IPCWaitPongEvent
   296 IPCWaitPongEvent
   267 end;
   297 end;
   268 
   298 
   269 procedure SendKeepAliveMessage(Lag: Longword);
   299 procedure FlushMessages(Lag: Longword);
   270 begin
   300 begin
   271 inc(SendEmptyPacketTicks, Lag);
   301 inc(flushDelayTicks, Lag);
   272 if (SendEmptyPacketTicks >= cSendEmptyPacketTime) then
   302 if (flushDelayTicks >= cSendEmptyPacketTime) then
   273     SendIPC(_S'+')
   303     begin
       
   304     if sendBuffer.count = 0 then
       
   305         SendIPC(_S'+');
       
   306         
       
   307      flushBuffer()    
       
   308     end
   274 end;
   309 end;
   275 
   310 
   276 procedure NetGetNextCmd;
   311 procedure NetGetNextCmd;
   277 var tmpflag: boolean;
   312 var tmpflag: boolean;
   278     s: shortstring;
   313     s: shortstring;
   434     lastcmd:= nil;
   469     lastcmd:= nil;
   435     isPonged:= false;
   470     isPonged:= false;
   436     SocketString:= '';
   471     SocketString:= '';
   437 
   472 
   438     hiTicks:= 0;
   473     hiTicks:= 0;
   439     SendEmptyPacketTicks:= 0;
   474     flushDelayTicks:= 0;
   440 
   475     sendBuffer.count:= 0;
   441 end;
   476 end;
   442 
   477 
   443 procedure freeModule;
   478 procedure freeModule;
   444 begin
   479 begin
   445     while headcmd <> nil do RemoveCmd;
   480     while headcmd <> nil do RemoveCmd;