hedgewars/uFLIPC.pas
branchqmlfrontend
changeset 10420 02c573d19224
parent 10418 091d2c0216c3
child 10426 727a154cf784
equal deleted inserted replaced
10418:091d2c0216c3 10420:02c573d19224
     7     condFrontend, condEngine: PSDL_cond;
     7     condFrontend, condEngine: PSDL_cond;
     8 
     8 
     9 procedure initIPC;
     9 procedure initIPC;
    10 procedure freeIPC;
    10 procedure freeIPC;
    11 
    11 
    12 procedure ipcToEngine(len: byte; msg: PChar); cdecl; export;
    12 procedure ipcToEngine(p: PChar; len: byte); cdecl; export;
    13 function  ipcReadFromEngine: shortstring;
    13 //function  ipcReadFromEngine: shortstring;
    14 function  ipcCheckFromEngine: boolean;
    14 //function  ipcCheckFromEngine: boolean;
    15 
    15 
    16 procedure ipcToFrontend(s: shortstring);
    16 procedure ipcToFrontend(s: shortstring);
       
    17 procedure ipcToFrontendRaw(p: pointer; len: Longword);
    17 function ipcReadFromFrontend: shortstring;
    18 function ipcReadFromFrontend: shortstring;
    18 function ipcCheckFromFrontend: boolean;
    19 function ipcCheckFromFrontend: boolean;
    19 
    20 
    20 procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
    21 procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
    21 
    22 
    23 
    24 
    24 var callbackPointer: pointer;
    25 var callbackPointer: pointer;
    25     callbackFunction: TIPCCallback;
    26     callbackFunction: TIPCCallback;
    26     callbackListenerThread: PSDL_Thread;
    27     callbackListenerThread: PSDL_Thread;
    27 
    28 
    28 procedure ipcSend(var s: shortstring; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
    29 procedure ipcSend(var s: TIPCMessage; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
    29 begin
    30 begin
    30     SDL_LockMutex(mut);
    31     SDL_LockMutex(mut);
    31 
    32 
    32     while (msg.str[0] > #0) or (msg.buf <> nil) do
    33     while (msg.str[0] > #0) or (msg.buf <> nil) do
    33         SDL_CondWait(cond, mut);
    34         SDL_CondWait(cond, mut);
    34 
    35 
    35     msg.str:= s;
    36     msg:= s;
    36     SDL_CondSignal(cond);
    37     SDL_CondSignal(cond);
    37     SDL_UnlockMutex(mut);
    38     SDL_UnlockMutex(mut);
    38 end;
    39 end;
    39 
    40 
    40 function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): shortstring;
    41 function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): TIPCMessage;
       
    42 var tmp: pointer;
    41 begin
    43 begin
    42     SDL_LockMutex(mut);
    44     SDL_LockMutex(mut);
    43     while (msg.str[0] = #0) and (msg.buf = nil) do
    45     while (msg.str[0] = #0) and (msg.buf = nil) do
    44         SDL_CondWait(cond, mut);
    46         SDL_CondWait(cond, mut);
    45 
    47 
    46     ipcRead:= msg.str;
    48     if msg.buf <> nil then
       
    49     begin
       
    50         tmp:= msg.buf;
       
    51         msg.buf:= GetMem(msg.len);
       
    52         Move(tmp^, msg.buf^, msg.len);
       
    53         FreeMem(tmp, msg.len)
       
    54     end;
       
    55 
       
    56     ipcRead:= msg;
    47 
    57 
    48     msg.str[0]:= #0;
    58     msg.str[0]:= #0;
    49     if msg.buf <> nil then
    59     msg.buf:= nil;
    50     begin
       
    51         FreeMem(msg.buf, msg.len);
       
    52         msg.buf:= nil
       
    53     end;
       
    54 
    60 
    55     SDL_CondSignal(cond);
    61     SDL_CondSignal(cond);
    56     SDL_UnlockMutex(mut)
    62     SDL_UnlockMutex(mut)
    57 end;
    63 end;
    58 
    64 
    61     SDL_LockMutex(mut);
    67     SDL_LockMutex(mut);
    62     ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
    68     ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
    63     SDL_UnlockMutex(mut)
    69     SDL_UnlockMutex(mut)
    64 end;
    70 end;
    65 
    71 
    66 procedure ipcToEngine(len: byte; msg: PChar); cdecl; export;
    72 procedure ipcToEngine(p: PChar; len: byte); cdecl; export;
    67 var s: shortstring;
    73 var msg: TIPCMessage;
    68 begin
    74 begin
    69     writeln(stderr, len);
    75     writeln(stderr, len);
    70     Move(msg^, s[1], len);
    76     Move(p^, msg.str[1], len);
    71     s[0]:= char(len);
    77     msg.str[0]:= char(len);
    72     ipcSend(s, msgEngine, mutEngine, condEngine)
    78     msg.buf:= nil;
       
    79     ipcSend(msg, msgEngine, mutEngine, condEngine)
    73 end;
    80 end;
    74 
    81 
    75 procedure ipcToFrontend(s: shortstring);
    82 procedure ipcToFrontend(s: shortstring);
       
    83 var msg: TIPCMessage;
    76 begin
    84 begin
    77     ipcSend(s, msgFrontend, mutFrontend, condFrontend)
    85     msg.str:= s;
       
    86     msg.buf:= nil;
       
    87     ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
    78 end;
    88 end;
    79 
    89 
    80 function ipcReadFromEngine: shortstring;
    90 procedure ipcToFrontendRaw(p: pointer; len: Longword);
       
    91 var msg: TIPCMessage;
       
    92 begin
       
    93     msg.str[0]:= #0;
       
    94     msg.len:= len;
       
    95     msg.buf:= GetMem(len);
       
    96     Move(p^, msg.buf^, len);
       
    97     ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
       
    98 end;
       
    99 
       
   100 function ipcReadFromEngine: TIPCMessage;
    81 begin
   101 begin
    82     ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
   102     ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
    83 end;
   103 end;
    84 
   104 
    85 function ipcReadFromFrontend: shortstring;
   105 function ipcReadFromFrontend: shortstring;
    86 begin
   106 begin
    87     ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine)
   107     ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine).str
    88 end;
   108 end;
    89 
   109 
    90 function ipcCheckFromEngine: boolean;
   110 function ipcCheckFromEngine: boolean;
    91 begin
   111 begin
    92     ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
   112     ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
    96 begin
   116 begin
    97     ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine)
   117     ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine)
    98 end;
   118 end;
    99 
   119 
   100 function  listener(p: pointer): Longint; cdecl; export;
   120 function  listener(p: pointer): Longint; cdecl; export;
   101 var s: shortstring;
   121 var msg: TIPCMessage;
   102 begin
   122 begin
   103     listener:= 0;
   123     listener:= 0;
   104     repeat
   124     repeat
   105         s:= ipcReadFromEngine();
   125         msg:= ipcReadFromEngine();
   106         callbackFunction(callbackPointer, byte(s[0]), @s[1])
   126         if msg.buf = nil then
       
   127             callbackFunction(callbackPointer, @msg.str[1], byte(msg.str[0]))
       
   128         else
       
   129         begin
       
   130             callbackFunction(callbackPointer, msg.buf, msg.len);
       
   131             FreeMem(msg.buf, msg.len)
       
   132         end
   107     until false
   133     until false
   108 end;
   134 end;
   109 
   135 
   110 procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
   136 procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
   111 begin
   137 begin