hedgewars/uFLIPC.pas
branchqmlfrontend
changeset 10935 3a65fcd7c335
parent 10933 f1da4126a61c
child 11408 b894922d58cc
equal deleted inserted replaced
10933:f1da4126a61c 10935:3a65fcd7c335
     1 unit uFLIPC;
     1 unit uFLIPC;
     2 interface
     2 interface
     3 uses SDLh, uFLTypes;
     3 uses SDLh, uFLTypes;
     4 
     4 
     5 var msgFrontend, msgEngine: TIPCMessage;
     5 var msgFrontend, msgEngine, msgNet: TIPCMessage;
     6     mutFrontend, mutEngine: PSDL_mutex;
     6     mutFrontend, mutEngine, mutNet: PSDL_mutex;
     7     condFrontend, condEngine: PSDL_cond;
     7     condFrontend, condEngine, condNet: PSDL_cond;
     8 
     8 
     9 procedure initIPC;
     9 procedure initIPC;
    10 procedure freeIPC;
    10 procedure freeIPC;
    11 
    11 
    12 procedure ipcToEngine(s: shortstring);
    12 procedure ipcToEngine(s: shortstring);
    13 procedure ipcToEngineRaw(p: pointer; len: Longword);
    13 procedure ipcToEngineRaw(p: pointer; len: Longword);
    14 //function  ipcReadFromEngine: shortstring;
    14 //function  ipcReadFromEngine: shortstring;
    15 //function  ipcCheckFromEngine: boolean;
    15 //function  ipcCheckFromEngine: boolean;
    16 
    16 
       
    17 procedure ipcToNet(s: shortstring);
       
    18 procedure ipcToNetRaw(p: pointer; len: Longword);
       
    19 
    17 procedure ipcToFrontend(s: shortstring);
    20 procedure ipcToFrontend(s: shortstring);
    18 procedure ipcToFrontendRaw(p: pointer; len: Longword);
    21 procedure ipcToFrontendRaw(p: pointer; len: Longword);
    19 function ipcReadFromFrontend: shortstring;
    22 function ipcReadFromFrontend: shortstring;
    20 function ipcCheckFromFrontend: boolean;
    23 function ipcCheckFromFrontend: boolean;
    21 
    24 
    22 procedure registerIPCCallback(p: pointer; f: TIPCCallback);
    25 procedure registerIPCCallback(p: pointer; f: TIPCCallback);
       
    26 procedure registerNetCallback(p: pointer; f: TIPCCallback);
    23 
    27 
    24 implementation
    28 implementation
    25 
    29 
    26 var callbackPointer: pointer;
    30 var callbackPointerF: pointer;
    27     callbackFunction: TIPCCallback;
    31     callbackFunctionF: TIPCCallback;
    28     callbackListenerThread: PSDL_Thread;
    32     callbackListenerThreadF: PSDL_Thread;
       
    33     callbackPointerN: pointer;
       
    34     callbackFunctionN: TIPCCallback;
       
    35     callbackListenerThreadN: PSDL_Thread;
    29 
    36 
    30 procedure ipcSend(var s: TIPCMessage; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
    37 procedure ipcSend(var s: TIPCMessage; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
    31 begin
    38 begin
    32     SDL_LockMutex(mut);
    39     SDL_LockMutex(mut);
    33 
    40 
    84     msg.str:= s;
    91     msg.str:= s;
    85     msg.buf:= nil;
    92     msg.buf:= nil;
    86     ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
    93     ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
    87 end;
    94 end;
    88 
    95 
       
    96 procedure ipcToNet(s: shortstring);
       
    97 var msg: TIPCMessage;
       
    98 begin
       
    99     msg.str:= s;
       
   100     msg.buf:= nil;
       
   101     ipcSend(msg, msgNet, mutNet, condNet)
       
   102 end;
       
   103 
    89 procedure ipcToEngineRaw(p: pointer; len: Longword);
   104 procedure ipcToEngineRaw(p: pointer; len: Longword);
    90 var msg: TIPCMessage;
   105 var msg: TIPCMessage;
    91 begin
   106 begin
    92     msg.str[0]:= #0;
   107     msg.str[0]:= #0;
    93     msg.len:= len;
   108     msg.len:= len;
   104     msg.buf:= GetMem(len);
   119     msg.buf:= GetMem(len);
   105     Move(p^, msg.buf^, len);
   120     Move(p^, msg.buf^, len);
   106     ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
   121     ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
   107 end;
   122 end;
   108 
   123 
       
   124 procedure ipcToNetRaw(p: pointer; len: Longword);
       
   125 var msg: TIPCMessage;
       
   126 begin
       
   127     msg.str[0]:= #0;
       
   128     msg.len:= len;
       
   129     msg.buf:= GetMem(len);
       
   130     Move(p^, msg.buf^, len);
       
   131     ipcSend(msg, msgNet, mutNet, condNet)
       
   132 end;
       
   133 
   109 function ipcReadFromEngine: TIPCMessage;
   134 function ipcReadFromEngine: TIPCMessage;
   110 begin
   135 begin
   111     ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
   136     ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
   112 end;
   137 end;
   113 
   138 
   114 function ipcReadFromFrontend: shortstring;
   139 function ipcReadFromFrontend: shortstring;
   115 begin
   140 begin
   116     ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine).str
   141     ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine).str
       
   142 end;
       
   143 
       
   144 function ipcReadToNet: TIPCMessage;
       
   145 begin
       
   146     ipcReadToNet:= ipcRead(msgNet, mutNet, condNet)
   117 end;
   147 end;
   118 
   148 
   119 function ipcCheckFromEngine: boolean;
   149 function ipcCheckFromEngine: boolean;
   120 begin
   150 begin
   121     ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
   151     ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
   131 begin
   161 begin
   132     engineListener:= 0;
   162     engineListener:= 0;
   133     repeat
   163     repeat
   134         msg:= ipcReadFromEngine();
   164         msg:= ipcReadFromEngine();
   135         if msg.buf = nil then
   165         if msg.buf = nil then
   136             callbackFunction(callbackPointer, @msg.str[1], byte(msg.str[0]))
   166             callbackFunctionF(callbackPointerF, @msg.str[1], byte(msg.str[0]))
   137         else
   167         else
   138         begin
   168         begin
   139             callbackFunction(callbackPointer, msg.buf, msg.len);
   169             callbackFunctionF(callbackPointerF, msg.buf, msg.len);
   140             FreeMem(msg.buf, msg.len)
   170             FreeMem(msg.buf, msg.len)
   141         end
   171         end
   142     until false
   172     until false
   143 end;
   173 end;
   144 
   174 
       
   175 function  netListener(p: pointer): Longint; cdecl; export;
       
   176 var msg: TIPCMessage;
       
   177 begin
       
   178     netListener:= 0;
       
   179     repeat
       
   180         msg:= ipcReadToNet();
       
   181         if msg.buf = nil then
       
   182             callbackFunctionN(callbackPointerN, @msg.str[1], byte(msg.str[0]))
       
   183         else
       
   184         begin
       
   185             callbackFunctionN(callbackPointerN, msg.buf, msg.len);
       
   186             FreeMem(msg.buf, msg.len)
       
   187         end
       
   188     until false
       
   189 end;
       
   190 
   145 procedure registerIPCCallback(p: pointer; f: TIPCCallback);
   191 procedure registerIPCCallback(p: pointer; f: TIPCCallback);
   146 begin
   192 begin
   147     callbackPointer:= p;
   193     callbackPointerF:= p;
   148     callbackFunction:= f;
   194     callbackFunctionF:= f;
   149     callbackListenerThread:= SDL_CreateThread(@engineListener{$IFDEF SDL2}, 'engineListener'{$ENDIF}, nil);
   195     callbackListenerThreadF:= SDL_CreateThread(@engineListener{$IFDEF SDL2}, 'engineListener'{$ENDIF}, nil);
       
   196 end;
       
   197 
       
   198 procedure registerNetCallback(p: pointer; f: TIPCCallback);
       
   199 begin
       
   200     callbackPointerN:= p;
       
   201     callbackFunctionN:= f;
       
   202     callbackListenerThreadN:= SDL_CreateThread(@netListener{$IFDEF SDL2}, 'netListener'{$ENDIF}, nil);
   150 end;
   203 end;
   151 
   204 
   152 procedure initIPC;
   205 procedure initIPC;
   153 begin
   206 begin
   154     msgFrontend.str:= '';
   207     msgFrontend.str:= '';
   155     msgFrontend.buf:= nil;
   208     msgFrontend.buf:= nil;
   156     msgEngine.str:= '';
   209     msgEngine.str:= '';
   157     msgEngine.buf:= nil;
   210     msgEngine.buf:= nil;
   158 
   211     msgNet.str:= '';
   159     callbackPointer:= nil;
   212     msgNet.buf:= nil;
   160     callbackListenerThread:= nil;
   213 
       
   214     callbackPointerF:= nil;
       
   215     callbackListenerThreadF:= nil;
   161 
   216 
   162     mutFrontend:= SDL_CreateMutex;
   217     mutFrontend:= SDL_CreateMutex;
   163     mutEngine:= SDL_CreateMutex;
   218     mutEngine:= SDL_CreateMutex;
       
   219     mutNet:= SDL_CreateMutex;
   164     condFrontend:= SDL_CreateCond;
   220     condFrontend:= SDL_CreateCond;
   165     condEngine:= SDL_CreateCond;
   221     condEngine:= SDL_CreateCond;
       
   222     condNet:= SDL_CreateCond;
   166 end;
   223 end;
   167 
   224 
   168 procedure freeIPC;
   225 procedure freeIPC;
   169 begin
   226 begin
   170     SDL_KillThread(callbackListenerThread);
   227     SDL_KillThread(callbackListenerThreadF);
       
   228     SDL_KillThread(callbackListenerThreadN);
   171     SDL_DestroyMutex(mutFrontend);
   229     SDL_DestroyMutex(mutFrontend);
   172     SDL_DestroyMutex(mutEngine);
   230     SDL_DestroyMutex(mutEngine);
       
   231     SDL_DestroyMutex(mutNet);
   173     SDL_DestroyCond(condFrontend);
   232     SDL_DestroyCond(condFrontend);
   174     SDL_DestroyCond(condEngine);
   233     SDL_DestroyCond(condEngine);
       
   234     SDL_DestroyCond(condNet);
   175 end;
   235 end;
   176 
   236 
   177 end.
   237 end.