- More flib IPC routines qmlfrontend
authorunc0rr
Thu, 18 Sep 2014 23:02:05 +0400
branchqmlfrontend
changeset 10412 9a8d4efcf3fa
parent 10410 669bfa55cd70
child 10414 50bcefec5bf6
- More flib IPC routines - Rework engine's uIO to use new IPC mechanism
hedgewars/uFLIPC.pas
hedgewars/uIO.pas
--- a/hedgewars/uFLIPC.pas	Thu Sep 18 00:19:05 2014 +0400
+++ b/hedgewars/uFLIPC.pas	Thu Sep 18 23:02:05 2014 +0400
@@ -19,42 +19,76 @@
 function  ipcReadFromEngine: shortstring;
 function  ipcCheckFromEngine: boolean;
 
+procedure ipcToFrontend(s: shortstring);
+function ipcReadFromFrontend: shortstring;
+function ipcCheckFromFrontend: boolean;
+
 implementation
 
+procedure ipcSend(var s: shortstring; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
+begin
+    SDL_LockMutex(mut);
+    while (msg.str[0] > #0) or (msg.buf <> nil) do
+        SDL_CondWait(cond, mut);
+
+    msg.str:= s;
+    SDL_CondSignal(cond);
+    SDL_UnlockMutex(mut)
+end;
+
+function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): shortstring;
+begin
+    SDL_LockMutex(mut);
+    while (msg.str[0] = #0) and (msg.buf = nil) do
+        SDL_CondWait(cond, mut);
+
+    ipcRead:= msg.str;
+    msg.str[0]:= #0;
+    if msg.buf <> nil then
+    begin
+        FreeMem(msg.buf, msg.len);
+        msg.buf:= nil
+    end;
+
+    SDL_CondSignal(cond);
+    SDL_UnlockMutex(mut)
+end;
+
+function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean;
+begin
+    SDL_LockMutex(mut);
+    ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
+    SDL_UnlockMutex(mut)
+end;
+
 procedure ipcToEngine(s: shortstring);
 begin
-    SDL_LockMutex(mutEngine);
-    while (msgEngine.str[0] > #0) or (msgEngine.buf <> nil) do
-        SDL_CondWait(condEngine, mutEngine);
+    ipcSend(s, msgEngine, mutEngine, condEngine)
+end;
 
-    msgEngine.str:= s;
-    SDL_CondSignal(condEngine);
-    SDL_UnlockMutex(mutEngine)
+procedure ipcToFrontend(s: shortstring);
+begin
+    ipcSend(s, msgFrontend, mutFrontend, condFrontend)
 end;
 
 function ipcReadFromEngine: shortstring;
 begin
-    SDL_LockMutex(mutFrontend);
-    while (msgFrontend.str[0] = #0) and (msgFrontend.buf = nil) do
-        SDL_CondWait(condFrontend, mutFrontend);
+    ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
+end;
 
-    ipcReadFromEngine:= msgFrontend.str;
-    msgFrontend.str[0]:= #0;
-    if msgFrontend.buf <> nil then
-    begin
-        FreeMem(msgFrontend.buf, msgFrontend.len);
-        msgFrontend.buf:= nil
-    end;
-
-    SDL_CondSignal(condFrontend);
-    SDL_UnlockMutex(mutFrontend)
+function ipcReadFromFrontend: shortstring;
+begin
+    ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine)
 end;
 
 function ipcCheckFromEngine: boolean;
 begin
-    SDL_LockMutex(mutFrontend);
-    ipcCheckFromEngine:= (msgFrontend.str[0] > #0) or (msgFrontend.buf <> nil);
-    SDL_UnlockMutex(mutFrontend)
+    ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
+end;
+
+function ipcCheckFromFrontend: boolean;
+begin
+    ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine)
 end;
 
 procedure initIPC;
--- a/hedgewars/uIO.pas	Thu Sep 18 00:19:05 2014 +0400
+++ b/hedgewars/uIO.pas	Thu Sep 18 23:02:05 2014 +0400
@@ -55,11 +55,9 @@
             2: (str: shortstring);
             end;
 
-var IPCSock: PTCPSocket;
-    fds: PSDLNet_SocketSet;
+var
     isPonged: boolean;
-    SocketString: shortstring;
-
+    
     headcmd: PCmd;
     lastcmd: PCmd;
 
@@ -102,20 +100,8 @@
 end;
 
 procedure InitIPC;
-var ipaddr: TIPAddress;
 begin
-    WriteToConsole('Init SDL_Net... ');
-    SDLTry(SDLNet_Init = 0, true);
-    fds:= SDLNet_AllocSocketSet(1);
-    SDLTry(fds <> nil, true);
-    WriteLnToConsole(msgOK);
-    WriteToConsole('Establishing IPC connection to tcp 127.0.0.1:' + IntToStr(ipcPort) + ' ');
-    {$HINTS OFF}
-    SDLTry(SDLNet_ResolveHost(ipaddr, PChar('127.0.0.1'), ipcPort) = 0, true);
-    {$HINTS ON}
-    IPCSock:= SDLNet_TCP_Open(ipaddr);
-    SDLTry(IPCSock <> nil, true);
-    WriteLnToConsole(msgOK)
+    // do nothing, flib initialized everything for us
 end;
 
 procedure ParseChatCommand(command: shortstring; message: shortstring;
@@ -176,31 +162,9 @@
 end;
 
 procedure IPCCheckSock;
-var i: LongInt;
-    s: shortstring;
 begin
-    if IPCSock = nil then
-        exit;
-
-    fds^.numsockets:= 0;
-    SDLNet_AddSocket(fds, IPCSock);
-
-    while SDLNet_CheckSockets(fds, 0) > 0 do
-    begin
-        i:= SDLNet_TCP_Recv(IPCSock, @s[1], 255 - Length(SocketString));
-        if i > 0 then
-        begin
-            s[0]:= char(i);
-            SocketString:= SocketString + s;
-            while (Length(SocketString) > 1) and (Length(SocketString) > byte(SocketString[1])) do
-            begin
-                ParseIPCCommand(copy(SocketString, 2, byte(SocketString[1])));
-                Delete(SocketString, 1, Succ(byte(SocketString[1])))
-            end
-        end
-    else
-        OutError('IPC connection lost', true)
-    end;
+    while ipcCheckFromFrontend() do
+        ParseIPCCommand(ipcReadFromFrontend())
 end;
 
 procedure LoadRecordFromFile(fileName: shortstring);
@@ -260,18 +224,11 @@
 
 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
     if s[0] > #251 then
         s[0]:= #251;
 
@@ -280,37 +237,25 @@
     AddFileLog('[IPC out] '+ sanitizeCharForLog(s[1]));
     inc(s[0], 2);
 
-    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
+    ipcToFrontend(s)
 end;
 
 procedure SendIPCRaw(p: pointer; len: Longword);
 begin
-if IPCSock <> nil then
+{    if IPCSock <> nil then
     begin
     SDLNet_TCP_Send(IPCSock, p, len)
-    end
+    end}
 end;
 
 procedure SendIPCXY(cmd: char; X, Y: LongInt);
 var s: shortstring;
 begin
-s[0]:= #9;
-s[1]:= cmd;
-SDLNet_Write32(X, @s[2]);
-SDLNet_Write32(Y, @s[6]);
-SendIPC(s)
+    s[0]:= #9;
+    s[1]:= cmd;
+    SDLNet_Write32(X, @s[2]);
+    SDLNet_Write32(Y, @s[6]);
+    SendIPC(s)
 end;
 
 procedure IPCWaitPongEvent;
@@ -440,13 +385,13 @@
 procedure chFatalError(var s: shortstring);
 begin
     SendIPC('E' + s);
-    // TODO: should we try to clean more stuff here?
+{    // TODO: should we try to clean more stuff here?
     SDL_Quit;
 
     if IPCSock <> nil then
         halt(HaltFatalError)
     else
-        halt(HaltFatalErrorNoIPC);
+        halt(HaltFatalErrorNoIPC);}
 end;
 
 procedure doPut(putX, putY: LongInt; fromAI: boolean);
@@ -498,12 +443,9 @@
 begin
     RegisterVariable('fatal', @chFatalError, true );
 
-    IPCSock:= nil;
-
     headcmd:= nil;
     lastcmd:= nil;
     isPonged:= false;
-    SocketString:= '';
 
     hiTicks:= 0;
     flushDelayTicks:= 0;
@@ -513,10 +455,6 @@
 procedure freeModule;
 begin
     while headcmd <> nil do RemoveCmd;
-    SDLNet_FreeSocketSet(fds);
-    SDLNet_TCP_Close(IPCSock);
-    SDLNet_Quit();
-
 end;
 
 end.