-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsrpc.lua
More file actions
100 lines (90 loc) · 2.24 KB
/
Copy pathlibsrpc.lua
File metadata and controls
100 lines (90 loc) · 2.24 KB
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
local skynet = require("skynet")
local profile = require("skynet.profile")
local cluster = require("skynet.cluster")
local cjson = require("cjson")
local msgpack = require("msgpack")
local function new_json_codec()
-- encode empty table as null is custom cjson api
-- fork from cloudwu/cjson
cjson.encode_empty_table_as_null(true)
local encoder = {
encode = cjson.encode,
decode = cjson.decode,
}
return encoder
end
local function new_msgpack_codec()
-- encode empty table as null is custom msgpack api
-- see ./skynet_example/msgpack.lua
msgpack.encode_empty_table_as_null(true)
local encoder = {
encode = msgpack.encode_one,
decode = msgpack.decode_one,
}
return encoder
end
local srpc = {
payload_codecs = {
["json"] = new_json_codec(),
["msgpack"] = new_msgpack_codec(),
},
codec = new_msgpack_codec(),
profile = false,
}
function srpc.register_codec(name, codec)
srpc.payload_codecs[name] = codec
end
function srpc.set_default_codec(name)
local c = srpc.payload_codecs[name]
if c then
srpc.codec = c
return true
end
return false
end
function srpc.send(node, sname, cmd, req)
if req then
req = srpc.codec.encode(req)
end
cluster.send(node, sname, cmd, req)
end
function srpc.call(node, sname, cmd, req)
if req then
req = srpc.codec.encode(req)
end
if srpc.profile then
profile.start()
end
local ok, ret = pcall(cluster.call, node, sname, cmd, req)
if not ok then
return ok, ret
end
if srpc.profile then
local cost = profile.stop()
skynet.error(node, sname, cmd, cost)
end
if ret then
ret = srpc.codec.decode(ret)
end
return ok, ret
end
function srpc.router(svc, cmd, callback)
svc[cmd] = function(req)
if req then
req = srpc.codec.decode(req)
end
if srpc.profile then
profile.start()
end
local ret = callback(req)
if ret then
ret = srpc.codec.encode(ret)
end
if srpc.profile then
local cost = profile.stop()
skynet.error(cmd, cost)
end
return ret
end
end
return srpc