author | unc0rr |
Mon, 16 Nov 2015 22:57:24 +0300 | |
branch | qmlfrontend |
changeset 11408 | b894922d58cc |
parent 10935 | 3a65fcd7c335 |
child 11423 | 091149424aa4 |
permissions | -rw-r--r-- |
10406 | 1 |
unit uFLIPC; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
2 |
interface |
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
3 |
uses SDLh, uFLTypes; |
10406 | 4 |
|
10935 | 5 |
var msgFrontend, msgEngine, msgNet: TIPCMessage; |
6 |
mutFrontend, mutEngine, mutNet: PSDL_mutex; |
|
7 |
condFrontend, condEngine, condNet: PSDL_cond; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
8 |
|
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
9 |
procedure initIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
10 |
procedure freeIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
11 |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
12 |
procedure ipcToEngine(s: shortstring); |
10898 | 13 |
procedure ipcToEngineRaw(p: pointer; len: Longword); |
10420 | 14 |
//function ipcReadFromEngine: shortstring; |
15 |
//function ipcCheckFromEngine: boolean; |
|
10406 | 16 |
|
10935 | 17 |
procedure ipcToNet(s: shortstring); |
18 |
procedure ipcToNetRaw(p: pointer; len: Longword); |
|
19 |
||
10412 | 20 |
procedure ipcToFrontend(s: shortstring); |
10420 | 21 |
procedure ipcToFrontendRaw(p: pointer; len: Longword); |
10412 | 22 |
function ipcReadFromFrontend: shortstring; |
23 |
function ipcCheckFromFrontend: boolean; |
|
24 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
25 |
procedure registerIPCCallback(p: pointer; f: TIPCCallback); |
10935 | 26 |
procedure registerNetCallback(p: pointer; f: TIPCCallback); |
10416 | 27 |
|
10406 | 28 |
implementation |
29 |
||
10935 | 30 |
var callbackPointerF: pointer; |
31 |
callbackFunctionF: TIPCCallback; |
|
32 |
callbackListenerThreadF: PSDL_Thread; |
|
33 |
callbackPointerN: pointer; |
|
34 |
callbackFunctionN: TIPCCallback; |
|
35 |
callbackListenerThreadN: PSDL_Thread; |
|
10416 | 36 |
|
10420 | 37 |
procedure ipcSend(var s: TIPCMessage; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond); |
10412 | 38 |
begin |
39 |
SDL_LockMutex(mut); |
|
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
40 |
|
10412 | 41 |
while (msg.str[0] > #0) or (msg.buf <> nil) do |
42 |
SDL_CondWait(cond, mut); |
|
43 |
||
10420 | 44 |
msg:= s; |
10412 | 45 |
SDL_CondSignal(cond); |
10416 | 46 |
SDL_UnlockMutex(mut); |
10412 | 47 |
end; |
48 |
||
10420 | 49 |
function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): TIPCMessage; |
50 |
var tmp: pointer; |
|
10412 | 51 |
begin |
52 |
SDL_LockMutex(mut); |
|
53 |
while (msg.str[0] = #0) and (msg.buf = nil) do |
|
54 |
SDL_CondWait(cond, mut); |
|
55 |
||
10420 | 56 |
if msg.buf <> nil then |
57 |
begin |
|
58 |
tmp:= msg.buf; |
|
59 |
msg.buf:= GetMem(msg.len); |
|
60 |
Move(tmp^, msg.buf^, msg.len); |
|
61 |
FreeMem(tmp, msg.len) |
|
62 |
end; |
|
63 |
||
64 |
ipcRead:= msg; |
|
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
65 |
|
10412 | 66 |
msg.str[0]:= #0; |
10420 | 67 |
msg.buf:= nil; |
10412 | 68 |
|
69 |
SDL_CondSignal(cond); |
|
70 |
SDL_UnlockMutex(mut) |
|
71 |
end; |
|
72 |
||
73 |
function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean; |
|
74 |
begin |
|
75 |
SDL_LockMutex(mut); |
|
76 |
ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil); |
|
77 |
SDL_UnlockMutex(mut) |
|
78 |
end; |
|
79 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
80 |
procedure ipcToEngine(s: shortstring); |
10420 | 81 |
var msg: TIPCMessage; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
82 |
begin |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
83 |
msg.str:= s; |
10420 | 84 |
msg.buf:= nil; |
85 |
ipcSend(msg, msgEngine, mutEngine, condEngine) |
|
10412 | 86 |
end; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
87 |
|
10412 | 88 |
procedure ipcToFrontend(s: shortstring); |
10420 | 89 |
var msg: TIPCMessage; |
10412 | 90 |
begin |
10420 | 91 |
msg.str:= s; |
92 |
msg.buf:= nil; |
|
93 |
ipcSend(msg, msgFrontend, mutFrontend, condFrontend) |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
94 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
95 |
|
10935 | 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 |
||
10898 | 104 |
procedure ipcToEngineRaw(p: pointer; len: Longword); |
105 |
var msg: TIPCMessage; |
|
106 |
begin |
|
107 |
msg.str[0]:= #0; |
|
108 |
msg.len:= len; |
|
109 |
msg.buf:= GetMem(len); |
|
110 |
Move(p^, msg.buf^, len); |
|
111 |
ipcSend(msg, msgEngine, mutEngine, condEngine) |
|
112 |
end; |
|
113 |
||
10420 | 114 |
procedure ipcToFrontendRaw(p: pointer; len: Longword); |
115 |
var msg: TIPCMessage; |
|
116 |
begin |
|
117 |
msg.str[0]:= #0; |
|
118 |
msg.len:= len; |
|
119 |
msg.buf:= GetMem(len); |
|
120 |
Move(p^, msg.buf^, len); |
|
121 |
ipcSend(msg, msgFrontend, mutFrontend, condFrontend) |
|
122 |
end; |
|
123 |
||
10935 | 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 |
||
10420 | 134 |
function ipcReadFromEngine: TIPCMessage; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
135 |
begin |
10412 | 136 |
ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend) |
137 |
end; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
138 |
|
10412 | 139 |
function ipcReadFromFrontend: shortstring; |
140 |
begin |
|
10420 | 141 |
ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine).str |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
142 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
143 |
|
10935 | 144 |
function ipcReadToNet: TIPCMessage; |
145 |
begin |
|
146 |
ipcReadToNet:= ipcRead(msgNet, mutNet, condNet) |
|
147 |
end; |
|
148 |
||
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
149 |
function ipcCheckFromEngine: boolean; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
150 |
begin |
10412 | 151 |
ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend) |
152 |
end; |
|
153 |
||
154 |
function ipcCheckFromFrontend: boolean; |
|
155 |
begin |
|
156 |
ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine) |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
157 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
158 |
|
10933 | 159 |
function engineListener(p: pointer): Longint; cdecl; export; |
10420 | 160 |
var msg: TIPCMessage; |
10416 | 161 |
begin |
10933 | 162 |
engineListener:= 0; |
10416 | 163 |
repeat |
10420 | 164 |
msg:= ipcReadFromEngine(); |
165 |
if msg.buf = nil then |
|
10935 | 166 |
callbackFunctionF(callbackPointerF, @msg.str[1], byte(msg.str[0])) |
10420 | 167 |
else |
168 |
begin |
|
10935 | 169 |
callbackFunctionF(callbackPointerF, msg.buf, msg.len); |
170 |
FreeMem(msg.buf, msg.len) |
|
171 |
end |
|
172 |
until false |
|
173 |
end; |
|
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); |
|
10420 | 186 |
FreeMem(msg.buf, msg.len) |
187 |
end |
|
10416 | 188 |
until false |
189 |
end; |
|
190 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
191 |
procedure registerIPCCallback(p: pointer; f: TIPCCallback); |
10416 | 192 |
begin |
10935 | 193 |
callbackPointerF:= p; |
194 |
callbackFunctionF:= f; |
|
11408 | 195 |
callbackListenerThreadF:= SDL_CreateThread(@engineListener, 'engineListener', nil); |
10935 | 196 |
end; |
197 |
||
198 |
procedure registerNetCallback(p: pointer; f: TIPCCallback); |
|
199 |
begin |
|
200 |
callbackPointerN:= p; |
|
201 |
callbackFunctionN:= f; |
|
11408 | 202 |
callbackListenerThreadN:= SDL_CreateThread(@netListener, 'netListener', nil); |
10416 | 203 |
end; |
204 |
||
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
205 |
procedure initIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
206 |
begin |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
207 |
msgFrontend.str:= ''; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
208 |
msgFrontend.buf:= nil; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
209 |
msgEngine.str:= ''; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
210 |
msgEngine.buf:= nil; |
10935 | 211 |
msgNet.str:= ''; |
212 |
msgNet.buf:= nil; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
213 |
|
10935 | 214 |
callbackPointerF:= nil; |
215 |
callbackListenerThreadF:= nil; |
|
10416 | 216 |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
217 |
mutFrontend:= SDL_CreateMutex; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
218 |
mutEngine:= SDL_CreateMutex; |
10935 | 219 |
mutNet:= SDL_CreateMutex; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
220 |
condFrontend:= SDL_CreateCond; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
221 |
condEngine:= SDL_CreateCond; |
10935 | 222 |
condNet:= SDL_CreateCond; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
223 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
224 |
|
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
225 |
procedure freeIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
226 |
begin |
11408 | 227 |
//FIXME SDL_KillThread(callbackListenerThreadF); |
228 |
//FIXME SDL_KillThread(callbackListenerThreadN); |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
229 |
SDL_DestroyMutex(mutFrontend); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
230 |
SDL_DestroyMutex(mutEngine); |
10935 | 231 |
SDL_DestroyMutex(mutNet); |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
232 |
SDL_DestroyCond(condFrontend); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
233 |
SDL_DestroyCond(condEngine); |
10935 | 234 |
SDL_DestroyCond(condNet); |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
235 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
236 |
|
10406 | 237 |
end. |