mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
remove feature: remote object. Issue #58
This commit is contained in:
@@ -481,87 +481,6 @@ function skynet.harbor(addr)
|
||||
return c.harbor(addr)
|
||||
end
|
||||
|
||||
------ remote object --------
|
||||
|
||||
do
|
||||
-- proto is 11
|
||||
|
||||
local remote_query, remote_alloc, remote_bind = c.remote_init(skynet.self())
|
||||
local weak_meta = { __mode = "kv" }
|
||||
local meta = getmetatable(c.unpack(c.pack({ __remote = 0 })))
|
||||
local remote_call_func = setmetatable({}, weak_meta)
|
||||
|
||||
local _send = assert(c.send)
|
||||
local _pack = assert(c.pack)
|
||||
local _unpack = assert(c.unpack)
|
||||
local _local = skynet.self()
|
||||
|
||||
function meta__index(t, method)
|
||||
local f = remote_call_func[method]
|
||||
if f == nil then
|
||||
f = function(...)
|
||||
local addr = remote_query(t.__remote)
|
||||
-- the proto is 11 (lua is 10)
|
||||
local session = assert(_send(addr, 11 , nil, _pack(t,method,...)), "call to invalid address")
|
||||
local succ, msg, sz = coroutine_yield("CALL", session)
|
||||
if succ then
|
||||
return select(2,assert(_unpack(msg,sz)))
|
||||
else
|
||||
error "call to dead remote object"
|
||||
end
|
||||
end
|
||||
remote_call_func[method] = f
|
||||
end
|
||||
rawset(t,method,f)
|
||||
return f
|
||||
end
|
||||
|
||||
-- prevent gc
|
||||
meta.__index = meta__index
|
||||
|
||||
meta.__newindex = error
|
||||
|
||||
function skynet.remote_create(t, handle)
|
||||
t = t or {}
|
||||
if handle then
|
||||
remote_bind(handle)
|
||||
else
|
||||
handle = remote_alloc()
|
||||
end
|
||||
rawset(t, "__remote" , handle)
|
||||
rawset(meta, handle, t)
|
||||
return t
|
||||
end
|
||||
|
||||
function skynet.remote_bind(handle)
|
||||
return setmetatable( { __remote = handle } , meta)
|
||||
end
|
||||
|
||||
local function remote_call(obj, method, ...)
|
||||
if type(obj) ~= "table" or type(method) ~= "string" then
|
||||
return coroutine_yield("RETURN", _pack(false, "Invalid call"))
|
||||
end
|
||||
local f = obj[method]
|
||||
if type(f) ~= "function" then
|
||||
return coroutine_yield("RETURN", _pack(false, "Object has not method " .. method))
|
||||
end
|
||||
return coroutine_yield("RETURN", _pack(pcall(f,...)))
|
||||
end
|
||||
|
||||
function skynet.remote_root()
|
||||
return skynet.remote_bind(0)
|
||||
end
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "remoteobj",
|
||||
id = 11,
|
||||
unpack = c.unpack,
|
||||
dispatch = function (session, source, ...)
|
||||
remote_call(...)
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
----- debug
|
||||
|
||||
local internal_info_func
|
||||
|
||||
Reference in New Issue
Block a user