forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteProcedureCall.pb
433 lines (345 loc) · 11.5 KB
/
RemoteProcedureCall.pb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
;
; Helper functions for RPC (remote procedure call)
; This is used by the Automation stuff only at the moment
;
; This basically encodes a function and a list of parameters into a single memory block and back.
;
Structure RPC_Parameter
Type.l ; #PB_Long etc, or -1 for memory buffer
Size.l
String$
StructureUnion
Long.l
Quad.l
Float.f
Double.d
*Memory
EndStructureUnion
EndStructure
Structure RPC_Call
Function$
IsResponse.l
ResponseID.l
ErrorFlag.l
NbParameters.l
Array Parameter.RPC_Parameter(0)
*Encoded
EncodedSize.l
EndStructure
Procedure RPC_ClearCall(*Call.RPC_Call)
If *Call\Encoded
FreeMemory(*Call\Encoded)
EndIf
*Call\Function$ = ""
*Call\ResponseID = 0
*Call\NbParameters = 0
*Call\Encoded = 0
*Call\IsResponse = 0
FreeArray(*Call\Parameter())
EndProcedure
Procedure RPC_InitCall(*Call.RPC_Call, Function$, NbParameters)
Static LastResponseID = 0
; clear old encoded data
;
If *Call\Encoded
FreeMemory(*Call\Encoded)
EndIf
*Call\Encoded = 0
LastResponseID + 1
*Call\Function$ = Function$
*Call\ResponseID = LastResponseID
*Call\NbParameters = NbParameters
*Call\ErrorFlag = 0
*Call\IsResponse = #False
If NbParameters > 0
Dim *Call\Parameter(NbParameters-1)
EndIf
EndProcedure
; Re-initialize an existing Call with values for the response:
; - keeps ResponseID and Function$ the same
;
Procedure RPC_InitResponse(*Call.RPC_Call, NbParameters, ErrorFlag = 0)
; clear old encoded data
;
If *Call\Encoded
FreeMemory(*Call\Encoded)
EndIf
*Call\Encoded = 0
*Call\NbParameters = NbParameters
*Call\ErrorFlag = ErrorFlag
*Call\IsResponse = #True
If NbParameters > 0
Dim *Call\Parameter(NbParameters-1)
EndIf
EndProcedure
; The format is:
; Long: TotalSize
; Long: IsResponse
; Long: ResponseID
; Long: ErrorFlag ; set to non-zero when returning from a call to indicate error
; Long: NbParameters
; Long: FunctionLength (+null)
; UTF: Function$
; Foreach Parameter
; Byte: Type
; <data>
; Next
;
Procedure RPC_Encode(*Call.RPC_Call)
Success = 0
; Calculate full size
;
NameLength = StringByteLength(*Call\Function$, #PB_UTF8) + 1
Size = 24 + NameLength
For i = 0 To *Call\NbParameters-1
Size + 1 + *Call\Parameter(i)\Size
If *Call\Parameter(i)\Type = -1 Or *Call\Parameter(i)\Type = #PB_String
Size + 4
EndIf
Next i
; allocate
If *Call\Encoded
FreeMemory(*Call\Encoded)
EndIf
*Call\Encoded = AllocateMemory(Size)
If *Call\Encoded
*Call\EncodedSize = Size
PokeL(*Call\Encoded, Size)
PokeL(*Call\Encoded+4, *Call\IsResponse)
PokeL(*Call\Encoded+8, *Call\ResponseID)
PokeL(*Call\Encoded+12, *Call\ErrorFlag)
PokeL(*Call\Encoded+16, *Call\NbParameters)
PokeL(*Call\Encoded+20, NameLength)
PokeS(*Call\Encoded+24, *Call\Function$, -1, #PB_UTF8)
*Pointer = *Call\Encoded + 24 + NameLength
For i = 0 To *Call\NbParameters-1
PokeB(*Pointer, *Call\Parameter(i)\Type)
*Pointer + 1
Select *Call\Parameter(i)\Type
Case #PB_Long:
PokeL(*Pointer, *Call\Parameter(i)\Long)
*Pointer + 4
Case #PB_Quad
PokeQ(*Pointer, *Call\Parameter(i)\Quad)
*Pointer + 8
Case #PB_Float
PokeF(*Pointer, *Call\Parameter(i)\Float)
*Pointer + 4
Case #PB_Double
PokeD(*Pointer, *Call\Parameter(i)\Double)
*Pointer + 8
Case #PB_String
PokeL(*Pointer, *Call\Parameter(i)\Size)
*Pointer + 4
PokeS(*Pointer, *Call\Parameter(i)\String$, -1, #PB_UTF8)
*Pointer + *Call\Parameter(i)\Size
Case -1
PokeL(*Pointer, *Call\Parameter(i)\Size)
*Pointer + 4
If *Call\Parameter(i)\Size > 0
CopyMemory(*Call\Parameter(i)\Memory, *Pointer, *Call\Parameter(i)\Size)
*Pointer + *Call\Parameter(i)\Size
EndIf
EndSelect
Next i
Success = 1
EndIf
ProcedureReturn Success
EndProcedure
; If CopyBuffer = #true, then the Call gets a copy of the buffer and the buffer can be freed
; If CopyBuffer = #false, then the Call assumes ownership of *Encoded and frees it (must be AllocateMemory() then)
;
Procedure RPC_Decode(*Call.RPC_Call, *Encoded, Size, CopyBuffer = #True)
RPC_ClearCall(*Call)
Success = 0
If *Encoded And Size And PeekL(*Encoded) <= Size ; make sure it is all complete
If CopyBuffer
*Call\Encoded = AllocateMemory(Size)
If *Call\Encoded
CopyMemory(*Encoded, *Call\Encoded, Size)
EndIf
Else
*Call\Encoded = *Encoded
EndIf
If *Call\Encoded
*Call\IsResponse = PeekL(*Encoded+4)
*Call\ResponseID = PeekL(*Encoded+8)
*Call\ErrorFlag = PeekL(*Encoded+12)
*Call\NbParameters = PeekL(*Encoded+16)
*Call\Function$ = PeekS(*Encoded+24, -1, #PB_UTF8)
*Pointer = *Encoded + 24 + PeekL(*Encoded+20)
If *Call\NbParameters > 0
Dim *Call\Parameter(*Call\NbParameters-1)
For i = 0 To *Call\NbParameters-1
*Call\Parameter(i)\Type = PeekB(*Pointer):
*Pointer + 1
Select *Call\Parameter(i)\Type
Case #PB_Long:
*Call\Parameter(i)\Size = 4
*Call\Parameter(i)\Long = PeekL(*Pointer)
*Pointer + 4
Case #PB_Quad
*Call\Parameter(i)\Size = 8
*Call\Parameter(i)\Quad = PeekQ(*Pointer)
*Pointer + 8
Case #PB_Float
*Call\Parameter(i)\Size = 4
*Call\Parameter(i)\Float = PeekF(*Pointer)
*Pointer + 4
Case #PB_Double
*Call\Parameter(i)\Size = 8
*Call\Parameter(i)\Double = PeekD(*Pointer)
*Pointer + 8
Case #PB_String
*Call\Parameter(i)\Size = PeekL(*Pointer)
*Pointer + 4
*Call\Parameter(i)\String$ = PeekS(*Pointer, -1, #PB_UTF8)
*Pointer + *Call\Parameter(i)\Size
Case -1
*Call\Parameter(i)\Size = PeekL(*Pointer)
*Pointer + 4
*Call\Parameter(i)\Memory = *Pointer
*Pointer + *Call\Parameter(i)\Size
EndSelect
Next i
EndIf
Success = 1
EndIf
EndIf
ProcedureReturn Success
EndProcedure
Procedure RPC_SetLong(*Call.RPC_Call, Index, Value)
If Index < *Call\NbParameters
*Call\Parameter(Index)\Type = #PB_Long
*Call\Parameter(Index)\Size = 4
*Call\Parameter(Index)\Long = Value
EndIf
EndProcedure
Procedure RPC_SetQuad(*Call.RPC_Call, Index, Value.q)
If Index < *Call\NbParameters
*Call\Parameter(Index)\Type = #PB_Quad
*Call\Parameter(Index)\Size = 8
*Call\Parameter(Index)\Quad = Value
EndIf
EndProcedure
Procedure RPC_SetFloat(*Call.RPC_Call, Index, Value.f)
If Index < *Call\NbParameters
*Call\Parameter(Index)\Type = #PB_Float
*Call\Parameter(Index)\Size = 4
*Call\Parameter(Index)\Float = Value
EndIf
EndProcedure
Procedure RPC_SetDouble(*Call.RPC_Call, Index, Value.d)
If Index < *Call\NbParameters
*Call\Parameter(Index)\Type = #PB_Double
*Call\Parameter(Index)\Size = 8
*Call\Parameter(Index)\Double = Value
EndIf
EndProcedure
Procedure RPC_SetString(*Call.RPC_Call, Index, Value$)
If Index < *Call\NbParameters
*Call\Parameter(Index)\Type = #PB_String
*Call\Parameter(Index)\Size = StringByteLength(Value$, #PB_UTF8) + 1
*Call\Parameter(Index)\String$ = Value$
EndIf
EndProcedure
; Buffer must remain valid until the Call has been encoded
;
Procedure RPC_SetMemory(*Call.RPC_Call, Index, *Buffer, Size)
If *Buffer = 0
Size = 0
EndIf
If Index < *Call\NbParameters
*Call\Parameter(Index)\Type = -1
*Call\Parameter(Index)\Size = Size
*Call\Parameter(Index)\Memory = *Buffer
EndIf
EndProcedure
Procedure RPC_GetLong(*Call.RPC_Call, Index)
If Index < *Call\NbParameters And *Call\Parameter(Index)\Type = #PB_Long
ProcedureReturn *Call\Parameter(Index)\Long
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure.q RPC_GetQuad(*Call.RPC_Call, Index)
If Index < *Call\NbParameters And *Call\Parameter(Index)\Type = #PB_Quad
ProcedureReturn *Call\Parameter(Index)\Quad
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure.f RPC_GetFloat(*Call.RPC_Call, Index)
If Index < *Call\NbParameters And *Call\Parameter(Index)\Type = #PB_Float
ProcedureReturn *Call\Parameter(Index)\Float
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure.d RPC_GetDouble(*Call.RPC_Call, Index)
If Index < *Call\NbParameters And *Call\Parameter(Index)\Type = #PB_Double
ProcedureReturn *Call\Parameter(Index)\Double
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure.s RPC_GetString(*Call.RPC_Call, Index)
If Index < *Call\NbParameters And *Call\Parameter(Index)\Type = #PB_String
ProcedureReturn *Call\Parameter(Index)\String$
Else
ProcedureReturn ""
EndIf
EndProcedure
Procedure RPC_GetMemorySize(*Call.RPC_Call, Index)
If Index < *Call\NbParameters And *Call\Parameter(Index)\Type = -1
ProcedureReturn *Call\Parameter(Index)\Size
Else
ProcedureReturn 0
EndIf
EndProcedure
; the memory is valid until the call is cleared
;
Procedure RPC_GetMemory(*Call.RPC_Call, Index)
If Index < *Call\NbParameters And *Call\Parameter(Index)\Type = -1
ProcedureReturn *Call\Parameter(Index)\Memory
Else
ProcedureReturn 0
EndIf
EndProcedure
CompilerIf #DEBUG
Procedure RPC_DebugCall(*Call.RPC_Call, Info$ = "")
Prefix$ = "[RPC] "
Debug Prefix$ + "------------------------------------------------------"
Debug Prefix$ + "RPC Call: " + Info$
Debug Prefix$ + "------------------------------------------------------"
Debug Prefix$ + "Function$ = " + *Call\Function$
Debug Prefix$ + "IsResponse = " + Str(*Call\IsResponse)
Debug Prefix$ + "ResponseID = " + Str(*Call\ResponseID)
Debug Prefix$ + "ErrorFlag = " + Str(*Call\ErrorFlag)
Debug Prefix$ + "NbParameters = " + Str(*Call\NbParameters)
For i = 0 To *Call\NbParameters-1
Line$ = Prefix$ + "Parameter("+Str(i)+") = "
Select *Call\Parameter(i)\Type
Case #PB_Long
Line$ + Str(*Call\Parameter(i)\Long) + " (Long)"
Case #PB_Quad
Line$ + Str(*Call\Parameter(i)\Quad) + " (Quad)"
Case #PB_Float
Line$ + StrF(*Call\Parameter(i)\Float) + " (Float)"
Case #PB_Double
Line$ + StrD(*Call\Parameter(i)\Double) + " (Double)"
Case #PB_String
Line$ + Chr(34) + *Call\Parameter(i)\String$ + Chr(34) + " (String)"
Case -1
Line$ + "<Buffer=" + Hex(*Call\Parameter(i)\Memory) + ", Size="+Str(*Call\Parameter(i)\Size)+"> (Memory)"
EndSelect
Debug Line$
Next i
Debug Prefix$ + "------------------------------------------------------"
EndProcedure
CompilerEndIf