hedgewars/uFLIPC.pas
branchqmlfrontend
changeset 10418 091d2c0216c3
parent 10416 1c301054694d
child 10420 02c573d19224
equal deleted inserted replaced
10416:1c301054694d 10418:091d2c0216c3
     1 unit uFLIPC;
     1 unit uFLIPC;
     2 interface
     2 interface
     3 uses SDLh;
     3 uses SDLh, uFLTypes;
     4 
       
     5 type TIPCMessage = record
       
     6                    str: shortstring;
       
     7                    len: Longword;
       
     8                    buf: Pointer
       
     9                end;
       
    10     TIPCCallback = procedure (p: pointer; s: shortstring);
       
    11 
     4 
    12 var msgFrontend, msgEngine: TIPCMessage;
     5 var msgFrontend, msgEngine: TIPCMessage;
    13     mutFrontend, mutEngine: PSDL_mutex;
     6     mutFrontend, mutEngine: PSDL_mutex;
    14     condFrontend, condEngine: PSDL_cond;
     7     condFrontend, condEngine: PSDL_cond;
    15 
     8 
    16 procedure initIPC;
     9 procedure initIPC;
    17 procedure freeIPC;
    10 procedure freeIPC;
    18 
    11 
    19 procedure ipcToEngine(s: shortstring); cdecl; export;
    12 procedure ipcToEngine(len: byte; msg: PChar); cdecl; export;
    20 function  ipcReadFromEngine: shortstring;
    13 function  ipcReadFromEngine: shortstring;
    21 function  ipcCheckFromEngine: boolean;
    14 function  ipcCheckFromEngine: boolean;
    22 
    15 
    23 procedure ipcToFrontend(s: shortstring);
    16 procedure ipcToFrontend(s: shortstring);
    24 function ipcReadFromFrontend: shortstring;
    17 function ipcReadFromFrontend: shortstring;
    33     callbackListenerThread: PSDL_Thread;
    26     callbackListenerThread: PSDL_Thread;
    34 
    27 
    35 procedure ipcSend(var s: shortstring; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
    28 procedure ipcSend(var s: shortstring; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
    36 begin
    29 begin
    37     SDL_LockMutex(mut);
    30     SDL_LockMutex(mut);
    38     writeln(stdout, 'ipc send', s);
    31 
    39     while (msg.str[0] > #0) or (msg.buf <> nil) do
    32     while (msg.str[0] > #0) or (msg.buf <> nil) do
    40         SDL_CondWait(cond, mut);
    33         SDL_CondWait(cond, mut);
    41 
    34 
    42     msg.str:= s;
    35     msg.str:= s;
    43     SDL_CondSignal(cond);
    36     SDL_CondSignal(cond);
    44     SDL_UnlockMutex(mut);
    37     SDL_UnlockMutex(mut);
    45     writeln(stdout, 'ipc sent', s[1])
       
    46 end;
    38 end;
    47 
    39 
    48 function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): shortstring;
    40 function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): shortstring;
    49 begin
    41 begin
    50     SDL_LockMutex(mut);
    42     SDL_LockMutex(mut);
    51     while (msg.str[0] = #0) and (msg.buf = nil) do
    43     while (msg.str[0] = #0) and (msg.buf = nil) do
    52         SDL_CondWait(cond, mut);
    44         SDL_CondWait(cond, mut);
    53 
    45 
    54     ipcRead:= msg.str;
    46     ipcRead:= msg.str;
    55     writeln(stdout, 'engine ipc received', msg.str[1]);
    47 
    56     msg.str[0]:= #0;
    48     msg.str[0]:= #0;
    57     if msg.buf <> nil then
    49     if msg.buf <> nil then
    58     begin
    50     begin
    59         FreeMem(msg.buf, msg.len);
    51         FreeMem(msg.buf, msg.len);
    60         msg.buf:= nil
    52         msg.buf:= nil
    69     SDL_LockMutex(mut);
    61     SDL_LockMutex(mut);
    70     ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
    62     ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
    71     SDL_UnlockMutex(mut)
    63     SDL_UnlockMutex(mut)
    72 end;
    64 end;
    73 
    65 
    74 procedure ipcToEngine(s: shortstring); cdecl; export;
    66 procedure ipcToEngine(len: byte; msg: PChar); cdecl; export;
       
    67 var s: shortstring;
    75 begin
    68 begin
       
    69     writeln(stderr, len);
       
    70     Move(msg^, s[1], len);
       
    71     s[0]:= char(len);
    76     ipcSend(s, msgEngine, mutEngine, condEngine)
    72     ipcSend(s, msgEngine, mutEngine, condEngine)
    77 end;
    73 end;
    78 
    74 
    79 procedure ipcToFrontend(s: shortstring);
    75 procedure ipcToFrontend(s: shortstring);
    80 begin
    76 begin
   100 begin
    96 begin
   101     ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine)
    97     ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine)
   102 end;
    98 end;
   103 
    99 
   104 function  listener(p: pointer): Longint; cdecl; export;
   100 function  listener(p: pointer): Longint; cdecl; export;
       
   101 var s: shortstring;
   105 begin
   102 begin
   106     listener:= 0;
   103     listener:= 0;
   107     repeat
   104     repeat
   108         callbackFunction(callbackPointer, ipcReadFromEngine())
   105         s:= ipcReadFromEngine();
       
   106         callbackFunction(callbackPointer, byte(s[0]), @s[1])
   109     until false
   107     until false
   110 end;
   108 end;
   111 
   109 
   112 procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
   110 procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
   113 begin
   111 begin