hedgewars/uIO.pas
changeset 649 26166c87dc75
parent 648 fc5234aa6493
child 650 14400b47ed6e
equal deleted inserted replaced
648:fc5234aa6493 649:26166c87dc75
    25 
    25 
    26 procedure SendIPC(s: shortstring);
    26 procedure SendIPC(s: shortstring);
    27 procedure SendIPCXY(cmd: char; X, Y: SmallInt);
    27 procedure SendIPCXY(cmd: char; X, Y: SmallInt);
    28 procedure SendIPCRaw(p: pointer; len: Longword);
    28 procedure SendIPCRaw(p: pointer; len: Longword);
    29 procedure SendIPCAndWaitReply(s: shortstring);
    29 procedure SendIPCAndWaitReply(s: shortstring);
       
    30 procedure SendIPCTimeInc;
    30 procedure IPCWaitPongEvent;
    31 procedure IPCWaitPongEvent;
    31 procedure IPCCheckSock;
    32 procedure IPCCheckSock;
    32 procedure InitIPC;
    33 procedure InitIPC;
    33 procedure CloseIPC;
    34 procedure CloseIPC;
    34 procedure NetGetNextCmd;
    35 procedure NetGetNextCmd;
    52      fds: PSDLNet_SocketSet;
    53      fds: PSDLNet_SocketSet;
    53 
    54 
    54      headcmd: PCmd = nil;
    55      headcmd: PCmd = nil;
    55      lastcmd: PCmd = nil;
    56      lastcmd: PCmd = nil;
    56 
    57 
       
    58      hiTicks: Word = 0;
       
    59 
    57 function AddCmd(Time: Longword; str: shortstring): PCmd;
    60 function AddCmd(Time: Longword; str: shortstring): PCmd;
    58 var Result: PCmd;
    61 var Result: PCmd;
    59 begin
    62 begin
    60 new(Result);
    63 new(Result);
    61 FillChar(Result^, sizeof(TCmd), 0);
    64 FillChar(Result^, sizeof(TCmd), 0);
    62 Result^.Time:= Time;
    65 Result^.Time:= Time;
    63 Result^.str:= str;
    66 Result^.str:= str;
    64 dec(Result^.len, 4);
    67 dec(Result^.len, 2); // cut timestamp
    65 if headcmd = nil then
    68 if headcmd = nil then
    66    begin
    69    begin
    67    headcmd:= Result;
    70    headcmd:= Result;
    68    lastcmd:= Result
    71    lastcmd:= Result
    69    end else
    72    end else
   105 SDLNet_TCP_Close(IPCSock);
   108 SDLNet_TCP_Close(IPCSock);
   106 SDLNet_Quit
   109 SDLNet_Quit
   107 end;
   110 end;
   108 
   111 
   109 procedure ParseIPCCommand(s: shortstring);
   112 procedure ParseIPCCommand(s: shortstring);
       
   113 var loTicks: Word;
   110 begin
   114 begin
   111 case s[1] of
   115 case s[1] of
   112      '!': begin {$IFDEF DEBUGFILE}AddFileLog('Ping? Pong!');{$ENDIF}isPonged:= true; end;
   116      '!': begin {$IFDEF DEBUGFILE}AddFileLog('Ping? Pong!');{$ENDIF}isPonged:= true; end;
   113      '?': SendIPC('!');
   117      '?': SendIPC('!');
       
   118      '#': inc(hiTicks);
   114      'e': ParseCommand(copy(s, 2, Length(s) - 1), true);
   119      'e': ParseCommand(copy(s, 2, Length(s) - 1), true);
   115      'E': OutError(copy(s, 2, Length(s) - 1), true);
   120      'E': OutError(copy(s, 2, Length(s) - 1), true);
   116      'W': OutError(copy(s, 2, Length(s) - 1), false);
   121      'W': OutError(copy(s, 2, Length(s) - 1), false);
   117      'M': CheckLandDigest(s);
   122      'M': CheckLandDigest(s);
   118      'T': case s[2] of
   123      'T': case s[2] of
   120                'D': GameType:= gmtDemo;
   125                'D': GameType:= gmtDemo;
   121                'N': GameType:= gmtNet;
   126                'N': GameType:= gmtNet;
   122                'S': GameType:= gmtSave;
   127                'S': GameType:= gmtSave;
   123                else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end;
   128                else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end;
   124      else
   129      else
   125      AddCmd(SDLNet_Read32(@s[byte(s[0]) - 3]), s);
   130      loTicks:= SDLNet_Read16(@s[byte(s[0]) - 1]);
       
   131      AddCmd(hiTicks shl 16 + loTicks, s);
   126      {$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(lastcmd^.Time));{$ENDIF}
   132      {$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(lastcmd^.Time));{$ENDIF}
   127      end
   133      end
   128 end;
   134 end;
   129 
   135 
   130 procedure IPCCheckSock;
   136 procedure IPCCheckSock;
   155 procedure SendIPC(s: shortstring);
   161 procedure SendIPC(s: shortstring);
   156 begin
   162 begin
   157 if IPCSock <> nil then
   163 if IPCSock <> nil then
   158    begin
   164    begin
   159    if s[0]>#251 then s[0]:= #251;
   165    if s[0]>#251 then s[0]:= #251;
   160    SDLNet_Write32(GameTicks, @s[Succ(byte(s[0]))]);
   166    SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
   161    {$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s);{$ENDIF}
   167    {$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s);{$ENDIF}
   162    inc(s[0],4);
   168    inc(s[0], 2);
   163    SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
   169    SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
   164    end
   170    end
   165 end;
   171 end;
   166 
   172 
   167 procedure SendIPCRaw(p: pointer; len: Longword);
   173 procedure SendIPCRaw(p: pointer; len: Longword);
   178 s[0]:= #5;
   184 s[0]:= #5;
   179 s[1]:= cmd;
   185 s[1]:= cmd;
   180 SDLNet_Write16(X, @s[2]);
   186 SDLNet_Write16(X, @s[2]);
   181 SDLNet_Write16(Y, @s[4]);
   187 SDLNet_Write16(Y, @s[4]);
   182 SendIPC(s)
   188 SendIPC(s)
       
   189 end;
       
   190 
       
   191 procedure SendIPCTimeInc;
       
   192 const timeinc: shortstring = '#';
       
   193 begin
       
   194 SendIPCRaw(@timeinc, 2)
   183 end;
   195 end;
   184 
   196 
   185 procedure IPCWaitPongEvent;
   197 procedure IPCWaitPongEvent;
   186 begin
   198 begin
   187 isPonged:= false;
   199 isPonged:= false;