author | unc0rr |
Tue, 01 Dec 2015 23:40:38 +0300 | |
branch | qmlfrontend |
changeset 11443 | 1a6148b4de3b |
parent 11423 | 091149424aa4 |
child 11455 | 0c75fa9ce340 |
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 |
||
11423 | 56 |
if msg.buf <> nil then |
57 |
// FIXME is this copying really needed, the buffer is in another thread already anyway? |
|
10420 | 58 |
begin |
59 |
tmp:= msg.buf; |
|
60 |
msg.buf:= GetMem(msg.len); |
|
61 |
Move(tmp^, msg.buf^, msg.len); |
|
62 |
FreeMem(tmp, msg.len) |
|
63 |
end; |
|
64 |
||
65 |
ipcRead:= msg; |
|
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
66 |
|
10412 | 67 |
msg.str[0]:= #0; |
10420 | 68 |
msg.buf:= nil; |
10412 | 69 |
|
70 |
SDL_CondSignal(cond); |
|
71 |
SDL_UnlockMutex(mut) |
|
72 |
end; |
|
73 |
||
74 |
function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean; |
|
75 |
begin |
|
76 |
SDL_LockMutex(mut); |
|
77 |
ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil); |
|
78 |
SDL_UnlockMutex(mut) |
|
79 |
end; |
|
80 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
81 |
procedure ipcToEngine(s: shortstring); |
10420 | 82 |
var msg: TIPCMessage; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
83 |
begin |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
84 |
msg.str:= s; |
10420 | 85 |
msg.buf:= nil; |
86 |
ipcSend(msg, msgEngine, mutEngine, condEngine) |
|
10412 | 87 |
end; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
88 |
|
10412 | 89 |
procedure ipcToFrontend(s: shortstring); |
10420 | 90 |
var msg: TIPCMessage; |
10412 | 91 |
begin |
10420 | 92 |
msg.str:= s; |
93 |
msg.buf:= nil; |
|
94 |
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
|
95 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
96 |
|
10935 | 97 |
procedure ipcToNet(s: shortstring); |
98 |
var msg: TIPCMessage; |
|
99 |
begin |
|
100 |
msg.str:= s; |
|
101 |
msg.buf:= nil; |
|
102 |
ipcSend(msg, msgNet, mutNet, condNet) |
|
103 |
end; |
|
104 |
||
10898 | 105 |
procedure ipcToEngineRaw(p: pointer; len: Longword); |
106 |
var msg: TIPCMessage; |
|
107 |
begin |
|
108 |
msg.str[0]:= #0; |
|
109 |
msg.len:= len; |
|
110 |
msg.buf:= GetMem(len); |
|
111 |
Move(p^, msg.buf^, len); |
|
112 |
ipcSend(msg, msgEngine, mutEngine, condEngine) |
|
113 |
end; |
|
114 |
||
10420 | 115 |
procedure ipcToFrontendRaw(p: pointer; len: Longword); |
116 |
var msg: TIPCMessage; |
|
117 |
begin |
|
118 |
msg.str[0]:= #0; |
|
119 |
msg.len:= len; |
|
120 |
msg.buf:= GetMem(len); |
|
121 |
Move(p^, msg.buf^, len); |
|
122 |
ipcSend(msg, msgFrontend, mutFrontend, condFrontend) |
|
123 |
end; |
|
124 |
||
10935 | 125 |
procedure ipcToNetRaw(p: pointer; len: Longword); |
126 |
var msg: TIPCMessage; |
|
127 |
begin |
|
128 |
msg.str[0]:= #0; |
|
129 |
msg.len:= len; |
|
130 |
msg.buf:= GetMem(len); |
|
131 |
Move(p^, msg.buf^, len); |
|
132 |
ipcSend(msg, msgNet, mutNet, condNet) |
|
133 |
end; |
|
134 |
||
10420 | 135 |
function ipcReadFromEngine: TIPCMessage; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
136 |
begin |
10412 | 137 |
ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend) |
138 |
end; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
139 |
|
10412 | 140 |
function ipcReadFromFrontend: shortstring; |
141 |
begin |
|
10420 | 142 |
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
|
143 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
144 |
|
10935 | 145 |
function ipcReadToNet: TIPCMessage; |
146 |
begin |
|
147 |
ipcReadToNet:= ipcRead(msgNet, mutNet, condNet) |
|
148 |
end; |
|
149 |
||
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
150 |
function ipcCheckFromEngine: boolean; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
151 |
begin |
10412 | 152 |
ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend) |
153 |
end; |
|
154 |
||
155 |
function ipcCheckFromFrontend: boolean; |
|
156 |
begin |
|
157 |
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
|
158 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
159 |
|
10933 | 160 |
function engineListener(p: pointer): Longint; cdecl; export; |
10420 | 161 |
var msg: TIPCMessage; |
10416 | 162 |
begin |
10933 | 163 |
engineListener:= 0; |
10416 | 164 |
repeat |
10420 | 165 |
msg:= ipcReadFromEngine(); |
166 |
if msg.buf = nil then |
|
10935 | 167 |
callbackFunctionF(callbackPointerF, @msg.str[1], byte(msg.str[0])) |
10420 | 168 |
else |
169 |
begin |
|
10935 | 170 |
callbackFunctionF(callbackPointerF, msg.buf, msg.len); |
171 |
FreeMem(msg.buf, msg.len) |
|
172 |
end |
|
173 |
until false |
|
174 |
end; |
|
175 |
||
176 |
function netListener(p: pointer): Longint; cdecl; export; |
|
177 |
var msg: TIPCMessage; |
|
178 |
begin |
|
179 |
netListener:= 0; |
|
180 |
repeat |
|
181 |
msg:= ipcReadToNet(); |
|
182 |
if msg.buf = nil then |
|
183 |
callbackFunctionN(callbackPointerN, @msg.str[1], byte(msg.str[0])) |
|
184 |
else |
|
185 |
begin |
|
186 |
callbackFunctionN(callbackPointerN, msg.buf, msg.len); |
|
10420 | 187 |
FreeMem(msg.buf, msg.len) |
188 |
end |
|
10416 | 189 |
until false |
190 |
end; |
|
191 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
192 |
procedure registerIPCCallback(p: pointer; f: TIPCCallback); |
10416 | 193 |
begin |
10935 | 194 |
callbackPointerF:= p; |
195 |
callbackFunctionF:= f; |
|
11408 | 196 |
callbackListenerThreadF:= SDL_CreateThread(@engineListener, 'engineListener', nil); |
10935 | 197 |
end; |
198 |
||
199 |
procedure registerNetCallback(p: pointer; f: TIPCCallback); |
|
200 |
begin |
|
201 |
callbackPointerN:= p; |
|
202 |
callbackFunctionN:= f; |
|
11408 | 203 |
callbackListenerThreadN:= SDL_CreateThread(@netListener, 'netListener', nil); |
10416 | 204 |
end; |
205 |
||
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
206 |
procedure initIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
207 |
begin |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
208 |
msgFrontend.str:= ''; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
209 |
msgFrontend.buf:= nil; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
210 |
msgEngine.str:= ''; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
211 |
msgEngine.buf:= nil; |
10935 | 212 |
msgNet.str:= ''; |
213 |
msgNet.buf:= nil; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
214 |
|
10935 | 215 |
callbackPointerF:= nil; |
216 |
callbackListenerThreadF:= nil; |
|
10416 | 217 |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
218 |
mutFrontend:= SDL_CreateMutex; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
219 |
mutEngine:= SDL_CreateMutex; |
10935 | 220 |
mutNet:= SDL_CreateMutex; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
221 |
condFrontend:= SDL_CreateCond; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
222 |
condEngine:= SDL_CreateCond; |
10935 | 223 |
condNet:= SDL_CreateCond; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
224 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
225 |
|
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
226 |
procedure freeIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
227 |
begin |
11408 | 228 |
//FIXME SDL_KillThread(callbackListenerThreadF); |
229 |
//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
|
230 |
SDL_DestroyMutex(mutFrontend); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
231 |
SDL_DestroyMutex(mutEngine); |
10935 | 232 |
SDL_DestroyMutex(mutNet); |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
233 |
SDL_DestroyCond(condFrontend); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
234 |
SDL_DestroyCond(condEngine); |
10935 | 235 |
SDL_DestroyCond(condNet); |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
236 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
237 |
|
10406 | 238 |
end. |