--- 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;