Skip to content

Commit c3462e4

Browse files
committed
ns.RPC() function removed, just call IPC namespace as a function to call a remote function
1 parent 0bd3a35 commit c3462e4

3 files changed

Lines changed: 42 additions & 40 deletions

File tree

lua/11_test_share_RPC.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ end
3535
function main()
3636
local ns = sh.GetIPCNameSpace("test_name_space")
3737

38-
local a, b, c = ns.RPC("testfunc", "a", {1, 2, {3, "b"}})
38+
local a, b, c = ns("testfunc", "a", {1, 2, {3, "b"}}) -- just call IPC namespace as function
3939
message("a = " .. tostring(a) .. " b = " .. table.tostring(b) .. " c = " .. tostring(c), 1)
4040
end

lua_share_main.pas

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ type tLuaShare = class(TLuaClass)
4646
function __newindex(AContext: TLuaContext): integer;
4747
function __IPC_index(AContext: TLuaContext): integer;
4848
function __IPC_newindex(AContext: TLuaContext): integer;
49+
function __IPC_call(AContext: TLuaContext): integer;
4950

5051
function DeepCopy(AContext: TLuaContext): integer;
5152
function IPCDeepCopy(AContext: TLuaContext): integer;
52-
function RPC(AContext: TLuaContext): integer;
5353

5454
function GetNameSpace(AContext: TLuaContext): integer;
5555
function GetIPCNameSpace(AContext: TLuaContext): integer;
5656

5757
function ShowMessageBox(AContext: TLuaContext): integer;
5858

59-
function selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx: tLuaFunction): integer;
59+
function selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx, acall: tLuaFunction): integer;
6060
end;
6161

6262
function initialize_share(ALuaInstance: TLuaState): integer;
@@ -183,6 +183,35 @@ function tLuaShare.__IPC_index(AContext: TLuaContext): integer;
183183
function tLuaShare.__IPC_newindex(AContext: TLuaContext): integer;
184184
begin result:= RPCCallNS(AContext, 'SetIPC', AContext.Stack[1].AsTable[namespace_item].AsString(datatable_name), [2, 3], AContext.Stack[4].AsInteger(max_RPC_timeout)); end;
185185

186+
function tLuaShare.__IPC_call(AContext: TLuaContext): integer;
187+
var function_name : ansistring;
188+
received_len : longint;
189+
temp_buffer : array[0..max_single_value_size - 1] of ansichar;
190+
ssize, i : longint;
191+
begin
192+
result:= 0;
193+
EnterCriticalSection(lua_lock);
194+
try
195+
if IPCReady then begin
196+
ssize:= AContext.StackSize; // __call(self, function_name, ...)
197+
function_name:= AContext.Stack[2].AsString;
198+
if (length(function_name) > 0) then begin
199+
fCodec.startcodec(fDataBuffer, max_transmission_size);
200+
fCodec.writestring(function_name);
201+
fCodec.writenumber(max(0, ssize - 2));
202+
for i:= 3 to ssize do
203+
stack2buf(AContext.CurrentState, i, fCodec);
204+
if fIPCClient.send_receive(fDataBuffer, fCodec.stopcodec, fDataBuffer, received_len, max_RPC_timeout) then begin
205+
fCodec.startcodec(fDataBuffer, received_len);
206+
result:= fCodec.readint(0);
207+
for i:= 0 to result - 1 do
208+
buf2stack(AContext.CurrentState, fCodec, @temp_buffer, sizeof(temp_buffer));
209+
end;
210+
end;
211+
end;
212+
finally LeaveCriticalSection(lua_lock); end;
213+
end;
214+
186215
function tLuaShare.DeepCopy(AContext: TLuaContext): integer;
187216
var namespace_name : ansistring;
188217
begin
@@ -237,35 +266,6 @@ function tLuaShare.RPCCallNS(AContext: TLuaContext; const afuncname, ansname: an
237266
finally LeaveCriticalSection(lua_lock); end;
238267
end;
239268

240-
function tLuaShare.RPC(AContext: TLuaContext): integer;
241-
var function_name : ansistring;
242-
received_len : longint;
243-
temp_buffer : array[0..max_single_value_size - 1] of ansichar;
244-
ssize, i : longint;
245-
begin
246-
result:= 0;
247-
EnterCriticalSection(lua_lock);
248-
try
249-
if IPCReady then begin
250-
ssize:= AContext.StackSize;
251-
function_name:= AContext.Stack[1].AsString;
252-
if (length(function_name) > 0) then begin
253-
fCodec.startcodec(fDataBuffer, max_transmission_size);
254-
fCodec.writestring(function_name);
255-
fCodec.writenumber(max(0, ssize - 1));
256-
for i:= 2 to ssize do
257-
stack2buf(AContext.CurrentState, i, fCodec);
258-
if fIPCClient.send_receive(fDataBuffer, fCodec.stopcodec, fDataBuffer, received_len, max_RPC_timeout) then begin
259-
fCodec.startcodec(fDataBuffer, received_len);
260-
result:= fCodec.readint(0);
261-
for i:= 0 to result - 1 do
262-
buf2stack(AContext.CurrentState, fCodec, @temp_buffer, sizeof(temp_buffer));
263-
end;
264-
end;
265-
end;
266-
finally LeaveCriticalSection(lua_lock); end;
267-
end;
268-
269269
function tLuaShare.ShowMessageBox(AContext: TLuaContext): integer;
270270
begin
271271
with AContext do
@@ -274,7 +274,7 @@ function tLuaShare.ShowMessageBox(AContext: TLuaContext): integer;
274274
end;
275275

276276
function tLuaShare.GetNameSpace(AContext: TLuaContext): integer;
277-
begin with AContext do result:= selfregister(CurrentState, pAnsiChar(Stack[1].AsString(datatable_name)), DeepCopy, __index, __newindex); end;
277+
begin with AContext do result:= selfregister(CurrentState, pAnsiChar(Stack[1].AsString(datatable_name)), DeepCopy, __index, __newindex, nil); end;
278278

279279
function tLuaShare.GetIPCNameSpace(AContext: TLuaContext): integer;
280280
begin
@@ -284,10 +284,10 @@ function tLuaShare.GetIPCNameSpace(AContext: TLuaContext): integer;
284284
if not assigned(fCodec) then fCodec:= tLuaCodec.Create;
285285
if not assigned(fDataBuffer) then fDataBuffer:= allocmem(transmission_buffer_size);
286286
finally LeaveCriticalSection(lua_lock); end;
287-
with AContext do result:= selfregister(CurrentState, pAnsiChar(Stack[1].AsString(datatable_name)), IPCDeepCopy, __IPC_index, __IPC_newindex);
287+
with AContext do result:= selfregister(CurrentState, pAnsiChar(Stack[1].AsString(datatable_name)), IPCDeepCopy, __IPC_index, __IPC_newindex, __IPC_call);
288288
end;
289289

290-
function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx: tLuaFunction): integer;
290+
function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx, acall: tLuaFunction): integer;
291291
begin
292292
lua_newtable(ALuaState); // result table
293293
lua_pushstring(ALuaState, 'DeepCopy');
@@ -298,9 +298,6 @@ function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; ade
298298
lua_settable(ALuaState, -3);
299299
lua_pushstring(ALuaState, 'GetIPCNameSpace');
300300
PushMethod(ALuaState, GetIPCNameSpace);
301-
lua_settable(ALuaState, -3);
302-
lua_pushstring(ALuaState, 'RPC');
303-
PushMethod(ALuaState, RPC);
304301
lua_settable(ALuaState, -3);
305302
lua_pushstring(ALuaState, namespace_item);
306303
lua_pushstring(ALuaState, ANameSpace);
@@ -312,6 +309,11 @@ function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; ade
312309
lua_pushstring(ALuaState, '__newindex');
313310
PushMethod(ALuaState, anewidx);
314311
lua_settable(ALuaState, -3);
312+
if assigned(acall) then begin
313+
lua_pushstring(ALuaState, '__call');
314+
PushMethod(ALuaState, acall);
315+
lua_settable(ALuaState, -3);
316+
end;
315317
lua_setmetatable(ALuaState, -2);
316318
result:= 1;
317319
end;
@@ -383,7 +385,7 @@ function initialize_share(ALuaInstance: TLuaState): integer;
383385
end else messagebox(0, pAnsiChar(format('Failed to find LUA library: %s', [lua_supported_libs[low(lua_supported_libs)]])), msgbox_err_title, MB_ICONERROR);
384386
end;
385387
if assigned(lua_share_instance) then begin
386-
with lua_share_instance do result:= selfregister(ALuaInstance, datatable_name, DeepCopy, __index, __newindex);
388+
with lua_share_instance do result:= selfregister(ALuaInstance, datatable_name, DeepCopy, __index, __newindex, nil);
387389
// register result table as a global variable:
388390
lua_pushvalue(ALuaInstance, -1);
389391
lua_setglobal(ALuaInstance, package_name);

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ RPC:
9191

9292
`
9393
local ns = sh.GetIPCNameSpace("test_name_space")
94-
a, b, c = ns.RPC("testfunc", "a", {1, 2, {3, "b"}})
94+
a, b, c = ns("testfunc", "a", {1, 2, {3, "b"}}) -- просто вызываем IPC неймспейс как функцию
9595
`
9696

9797
pre-defined пространства:

0 commit comments

Comments
 (0)