Skip to content

Commit

Permalink
ns.RPC() function removed, just call IPC namespace as a function to c…
Browse files Browse the repository at this point in the history
…all a remote function
  • Loading branch information
untoxa committed Dec 11, 2019
1 parent 0bd3a35 commit c3462e4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lua/11_test_share_RPC.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ end
function main()
local ns = sh.GetIPCNameSpace("test_name_space")

local a, b, c = ns.RPC("testfunc", "a", {1, 2, {3, "b"}})
local a, b, c = ns("testfunc", "a", {1, 2, {3, "b"}}) -- just call IPC namespace as function
message("a = " .. tostring(a) .. " b = " .. table.tostring(b) .. " c = " .. tostring(c), 1)
end
78 changes: 40 additions & 38 deletions lua_share_main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ type tLuaShare = class(TLuaClass)
function __newindex(AContext: TLuaContext): integer;
function __IPC_index(AContext: TLuaContext): integer;
function __IPC_newindex(AContext: TLuaContext): integer;
function __IPC_call(AContext: TLuaContext): integer;

function DeepCopy(AContext: TLuaContext): integer;
function IPCDeepCopy(AContext: TLuaContext): integer;
function RPC(AContext: TLuaContext): integer;

function GetNameSpace(AContext: TLuaContext): integer;
function GetIPCNameSpace(AContext: TLuaContext): integer;

function ShowMessageBox(AContext: TLuaContext): integer;

function selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx: tLuaFunction): integer;
function selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx, acall: tLuaFunction): integer;
end;

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

function tLuaShare.__IPC_call(AContext: TLuaContext): integer;
var function_name : ansistring;
received_len : longint;
temp_buffer : array[0..max_single_value_size - 1] of ansichar;
ssize, i : longint;
begin
result:= 0;
EnterCriticalSection(lua_lock);
try
if IPCReady then begin
ssize:= AContext.StackSize; // __call(self, function_name, ...)
function_name:= AContext.Stack[2].AsString;
if (length(function_name) > 0) then begin
fCodec.startcodec(fDataBuffer, max_transmission_size);
fCodec.writestring(function_name);
fCodec.writenumber(max(0, ssize - 2));
for i:= 3 to ssize do
stack2buf(AContext.CurrentState, i, fCodec);
if fIPCClient.send_receive(fDataBuffer, fCodec.stopcodec, fDataBuffer, received_len, max_RPC_timeout) then begin
fCodec.startcodec(fDataBuffer, received_len);
result:= fCodec.readint(0);
for i:= 0 to result - 1 do
buf2stack(AContext.CurrentState, fCodec, @temp_buffer, sizeof(temp_buffer));
end;
end;
end;
finally LeaveCriticalSection(lua_lock); end;
end;

function tLuaShare.DeepCopy(AContext: TLuaContext): integer;
var namespace_name : ansistring;
begin
Expand Down Expand Up @@ -237,35 +266,6 @@ function tLuaShare.RPCCallNS(AContext: TLuaContext; const afuncname, ansname: an
finally LeaveCriticalSection(lua_lock); end;
end;

function tLuaShare.RPC(AContext: TLuaContext): integer;
var function_name : ansistring;
received_len : longint;
temp_buffer : array[0..max_single_value_size - 1] of ansichar;
ssize, i : longint;
begin
result:= 0;
EnterCriticalSection(lua_lock);
try
if IPCReady then begin
ssize:= AContext.StackSize;
function_name:= AContext.Stack[1].AsString;
if (length(function_name) > 0) then begin
fCodec.startcodec(fDataBuffer, max_transmission_size);
fCodec.writestring(function_name);
fCodec.writenumber(max(0, ssize - 1));
for i:= 2 to ssize do
stack2buf(AContext.CurrentState, i, fCodec);
if fIPCClient.send_receive(fDataBuffer, fCodec.stopcodec, fDataBuffer, received_len, max_RPC_timeout) then begin
fCodec.startcodec(fDataBuffer, received_len);
result:= fCodec.readint(0);
for i:= 0 to result - 1 do
buf2stack(AContext.CurrentState, fCodec, @temp_buffer, sizeof(temp_buffer));
end;
end;
end;
finally LeaveCriticalSection(lua_lock); end;
end;

function tLuaShare.ShowMessageBox(AContext: TLuaContext): integer;
begin
with AContext do
Expand All @@ -274,7 +274,7 @@ function tLuaShare.ShowMessageBox(AContext: TLuaContext): integer;
end;

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

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

function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx: tLuaFunction): integer;
function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; adeepcpy, aidx, anewidx, acall: tLuaFunction): integer;
begin
lua_newtable(ALuaState); // result table
lua_pushstring(ALuaState, 'DeepCopy');
Expand All @@ -298,9 +298,6 @@ function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; ade
lua_settable(ALuaState, -3);
lua_pushstring(ALuaState, 'GetIPCNameSpace');
PushMethod(ALuaState, GetIPCNameSpace);
lua_settable(ALuaState, -3);
lua_pushstring(ALuaState, 'RPC');
PushMethod(ALuaState, RPC);
lua_settable(ALuaState, -3);
lua_pushstring(ALuaState, namespace_item);
lua_pushstring(ALuaState, ANameSpace);
Expand All @@ -312,6 +309,11 @@ function tLuaShare.selfregister(ALuaState: TLuaState; ANameSpace: pAnsiChar; ade
lua_pushstring(ALuaState, '__newindex');
PushMethod(ALuaState, anewidx);
lua_settable(ALuaState, -3);
if assigned(acall) then begin
lua_pushstring(ALuaState, '__call');
PushMethod(ALuaState, acall);
lua_settable(ALuaState, -3);
end;
lua_setmetatable(ALuaState, -2);
result:= 1;
end;
Expand Down Expand Up @@ -383,7 +385,7 @@ function initialize_share(ALuaInstance: TLuaState): integer;
end else messagebox(0, pAnsiChar(format('Failed to find LUA library: %s', [lua_supported_libs[low(lua_supported_libs)]])), msgbox_err_title, MB_ICONERROR);
end;
if assigned(lua_share_instance) then begin
with lua_share_instance do result:= selfregister(ALuaInstance, datatable_name, DeepCopy, __index, __newindex);
with lua_share_instance do result:= selfregister(ALuaInstance, datatable_name, DeepCopy, __index, __newindex, nil);
// register result table as a global variable:
lua_pushvalue(ALuaInstance, -1);
lua_setglobal(ALuaInstance, package_name);
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ RPC:

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

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

0 comments on commit c3462e4

Please sign in to comment.