diff options
-rw-r--r-- | server-beta/src/define/ErrorCodes.lua | 16 | ||||
-rw-r--r-- | server-beta/src/service/init.lua | 9 | ||||
-rw-r--r-- | server-beta/src/service/proto.lua | 80 | ||||
-rw-r--r-- | server-beta/src/service/service.lua (renamed from server-beta/src/service.lua) | 18 |
4 files changed, 107 insertions, 16 deletions
diff --git a/server-beta/src/define/ErrorCodes.lua b/server-beta/src/define/ErrorCodes.lua new file mode 100644 index 00000000..befb5630 --- /dev/null +++ b/server-beta/src/define/ErrorCodes.lua @@ -0,0 +1,16 @@ + +return { + -- Defined by JSON RPC + ParseError = -32700, + InvalidRequest = -32600, + MethodNotFound = -32601, + InvalidParams = -32602, + InternalError = -32603, + serverErrorStart = -32099, + serverErrorEnd = -32000, + ServerNotInitialized = -32002, + UnknownErrorCode = -32001, + + -- Defined by the protocol. + RequestCancelled = -32800, +} diff --git a/server-beta/src/service/init.lua b/server-beta/src/service/init.lua new file mode 100644 index 00000000..d5a45b19 --- /dev/null +++ b/server-beta/src/service/init.lua @@ -0,0 +1,9 @@ +local service = require 'service.service' +local proto = require 'service.proto' +local util = require 'utility' + +proto.on('initialize', function (params) + log.debug(util.dump(params)) +end) + +return service diff --git a/server-beta/src/service/proto.lua b/server-beta/src/service/proto.lua new file mode 100644 index 00000000..d35d76ba --- /dev/null +++ b/server-beta/src/service/proto.lua @@ -0,0 +1,80 @@ +local subprocess = require 'bee.subprocess' +local util = require 'utility' +local task = require 'task' +local pub = require 'pub' +local jsonrpc = require 'jsonrpc' +local ErrorCodes = require 'define.ErrorCodes' + +local m = {} + +m.ability = {} + +local function isOptionalMethod(method) + return method:sub(1, 2) == '$/' +end + +function m.on(method, callback) + m.ability[method] = callback +end + +function m.response(id, res) + -- res 可能是nil,为了转成json时保留nil,使用 container 容器 + local data = util.container() + data.id = id + data.result = res + local buf = jsonrpc.encode(data) + io.stdout:write(buf) +end + +function m.responseErr(id, code, message) + local buf = jsonrpc.encode { + id = id, + error = { + code = code, + message = message, + } + } + io.stdout:write(buf) +end + +function m.doProto(proto) + local method = proto.method + local abil = m.ability[method] + if not abil then + if not isOptionalMethod(method) then + log.warn('Recieved unknown proto: ' .. method) + end + if proto.id then + m.responseErr(proto.id, ErrorCodes.MethodNotFound, method) + end + return + end + task.create(function () + local clock = os.clock() + local ok, res = xpcall(abil, log.error, proto.params) + local passed = os.clock() - clock + if passed > 0.2 then + log.debug(('Method [%s] takes [%.3f]sec.'):format(method, passed)) + end + if ok then + m.response(proto.id, res) + else + m.responseErr(proto.id, ErrorCodes.InternalError, res) + end + end) +end + +function m.listen() + subprocess.filemode(io.stdin, 'b') + subprocess.filemode(io.stdout, 'b') + io.stdin:setvbuf 'no' + io.stdout:setvbuf 'no' + task.create(function () + while true do + local proto = pub.task('loadProto') + m.doProto(proto) + end + end) +end + +return m diff --git a/server-beta/src/service.lua b/server-beta/src/service/service.lua index 114ca6d0..2f7e31b8 100644 --- a/server-beta/src/service.lua +++ b/server-beta/src/service/service.lua @@ -1,26 +1,12 @@ local pub = require 'pub' -local subprocess = require 'bee.subprocess' local thread = require 'bee.thread' local task = require 'task' -local utility = require 'utility' local timer = require 'timer' +local proto = require 'service.proto' local m = {} m.type = 'service' -function m.listenProto() - subprocess.filemode(io.stdin, 'b') - subprocess.filemode(io.stdout, 'b') - io.stdin:setvbuf 'no' - io.stdout:setvbuf 'no' - task.create(function () - while true do - local proto = pub.task('loadProto') - log.debug('proto:', utility.dump(proto)) - end - end) -end - function m.listenPub() task.create(function () while true do @@ -45,7 +31,7 @@ end function m.start() pub.recruitBraves(4) task.setErrorHandle(log.error) - m.listenProto() + proto.listen() m.listenPub() m.startTimer() |