From b342a165b1d5ecbdf48f1fcf90079750f4e8e93f Mon Sep 17 00:00:00 2001 From: xiyoo0812 Date: Mon, 9 Oct 2023 06:24:08 +0000 Subject: [PATCH] =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E6=B5=8B=E8=AF=95=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E4=BC=98=E5=8C=96=201=E3=80=81=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=B7=A5=E5=85=B7=E4=BC=98=E5=8C=96=202?= =?UTF-8?q?=E3=80=81=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9BBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/accord.conf | 3 + extend/lbson/src/bson.h | 4 + extend/lbson/src/lbson.cpp | 8 - extend/lcodec/src/websocket.h | 3 +- extend/ljson/src/ljson.h | 4 + extend/luakit/include/lua_codec.h | 4 + script/driver/redis.lua | 2 +- script/driver/websocket.lua | 5 +- script/kernel/protobuf_mgr.lua | 2 +- script/network/net_client.lua | 2 +- server/accord.lua | 1 + server/cache/cache_gm.lua | 2 +- server/center/gm_mgr.lua | 14 +- server/robot/accord/accord_conf.lua | 5 - server/robot/accord/index.lua | 2684 ++++++++++++++++++ server/robot/accord/page/data_utils.js | 90 + server/robot/accord/page/format_utils.js | 37 + server/robot/accord/page/index.html | 1512 ++++++++++ server/robot/accord/page/net_utils.js | 38 + server/robot/accord/page/page.html | 1332 --------- server/robot/accord/page/{css => }/style.css | 54 +- server/robot/accord_mgr.lua | 325 +-- server/robot/dao/accord_dao.lua | 73 +- server/robot/module/login.lua | 2 +- server/robot/module/session.lua | 14 +- server/robot/msg_mgr.lua | 113 + server/robot/robot.lua | 4 +- tools/accordbuild.bat | 13 + 28 files changed, 4762 insertions(+), 1588 deletions(-) delete mode 100644 server/robot/accord/accord_conf.lua create mode 100644 server/robot/accord/index.lua create mode 100644 server/robot/accord/page/data_utils.js create mode 100644 server/robot/accord/page/format_utils.js create mode 100644 server/robot/accord/page/index.html create mode 100644 server/robot/accord/page/net_utils.js delete mode 100644 server/robot/accord/page/page.html rename server/robot/accord/page/{css => }/style.css (95%) create mode 100644 server/robot/msg_mgr.lua create mode 100644 tools/accordbuild.bat diff --git a/bin/accord.conf b/bin/accord.conf index 775edc55..398d4931 100644 --- a/bin/accord.conf +++ b/bin/accord.conf @@ -28,3 +28,6 @@ set_env("QUANTA_MODE", "3") --协议测试 set_env("QUANTA_ACCORD_HTTP", "127.0.0.1:10401") + +--服务器列表API +set_env("QUANTA_SRVLIST_API", "http://10.98.8.100:18080/server_mgr/query") diff --git a/extend/lbson/src/bson.h b/extend/lbson/src/bson.h index 4249159a..d55f9485 100644 --- a/extend/lbson/src/bson.h +++ b/extend/lbson/src/bson.h @@ -205,6 +205,10 @@ namespace lbson { if (!lua_isinteger(L, -2)) { return bson_type::BSON_DOCUMENT; } + size_t key = lua_tointeger(L, -2); + if (key <= 0 || key > raw_len) { + return bson_type::BSON_DOCUMENT; + } cur_len++; lua_pop(L, 1); } diff --git a/extend/lbson/src/lbson.cpp b/extend/lbson/src/lbson.cpp index a133cedc..8a1fcd6f 100644 --- a/extend/lbson/src/lbson.cpp +++ b/extend/lbson/src/lbson.cpp @@ -12,12 +12,6 @@ namespace lbson { static int decode(lua_State* L) { return thread_bson.decode(L); } - static slice* encode_slice(lua_State* L) { - return thread_bson.encode_slice(L); - } - static int decode_slice(lua_State* L, slice* slice) { - return thread_bson.decode_slice(L, slice); - } static bson_value* int32(int32_t value) { return new bson_value(bson_type::BSON_INT64, value); } @@ -50,8 +44,6 @@ namespace lbson { auto llbson = kit_state.new_table(); llbson.set_function("encode", encode); llbson.set_function("decode", decode); - llbson.set_function("encode_slice", encode_slice); - llbson.set_function("decode_slice", decode_slice); llbson.set_function("mongocodec", mongo_codec); llbson.set_function("timestamp", timestamp); llbson.set_function("int32", int32); diff --git a/extend/lcodec/src/websocket.h b/extend/lcodec/src/websocket.h index cbec2ab7..19770981 100644 --- a/extend/lcodec/src/websocket.h +++ b/extend/lcodec/src/websocket.h @@ -16,7 +16,8 @@ namespace lcodec { uint8_t masklen = (((*payload) & 0x80) == 0x80) ? 4 : 0; uint8_t payloadlen = (*payload) & 0x7f; if (payloadlen < 0x7e) { - return masklen + payloadlen + sizeof(uint16_t); + m_packet_len = masklen + payloadlen + sizeof(uint16_t); + return m_packet_len; } size_t* length = (size_t*)m_slice->peek((payloadlen == 0x7f) ? 8 : 2, sizeof(uint16_t)); if (!length) return 0; diff --git a/extend/ljson/src/ljson.h b/extend/ljson/src/ljson.h index 4ad41863..771b4f57 100644 --- a/extend/ljson/src/ljson.h +++ b/extend/ljson/src/ljson.h @@ -62,6 +62,10 @@ namespace ljson { if (!lua_isinteger(L, -2)) { return false; } + size_t key = lua_tointeger(L, -2); + if (key <= 0 || key > raw_len) { + return false; + } lua_pop(L, 1); cur_len++; } diff --git a/extend/luakit/include/lua_codec.h b/extend/luakit/include/lua_codec.h index 78017b8f..39723e0e 100644 --- a/extend/luakit/include/lua_codec.h +++ b/extend/luakit/include/lua_codec.h @@ -56,6 +56,10 @@ namespace luakit { if (!lua_isinteger(L, -2)) { return false; } + size_t key = lua_tointeger(L, -2); + if (key <= 0 || key > raw_len) { + return false; + } cur_len++; lua_pop(L, 1); } diff --git a/script/driver/redis.lua b/script/driver/redis.lua index 62451172..199c469f 100644 --- a/script/driver/redis.lua +++ b/script/driver/redis.lua @@ -199,7 +199,7 @@ function RedisDB:check_clusters() end local ok, res = self:commit(socket, "cluster", "slots") if not ok then - log_err("[RedisDB][check_clusters] load cluster slots failed! because: %s", res) + log_err("[RedisDB][check_clusters] load cluster slots failed! because: {}", res) return end for i, info in pairs(res) do diff --git a/script/driver/websocket.lua b/script/driver/websocket.lua index 3ff76917..436cea43 100644 --- a/script/driver/websocket.lua +++ b/script/driver/websocket.lua @@ -12,6 +12,7 @@ local qxpcall = quanta.xpcall local proto_text = luabus.eproto_type.text +local event_mgr = quanta.get("event_mgr") local socket_mgr = quanta.get("socket_mgr") local thread_mgr = quanta.get("thread_mgr") @@ -152,7 +153,9 @@ function WebSocket:on_handshake(session, token, url, params, headers, body) self.session = session self:send_data(101, cbheaders, "") self.host:on_socket_accept(self, token) - session.set_codec(self.wcodec) + event_mgr:fire_frame(function() + session.set_codec(self.wcodec) + end) return true end diff --git a/script/kernel/protobuf_mgr.lua b/script/kernel/protobuf_mgr.lua index e8968630..c47ef45f 100644 --- a/script/kernel/protobuf_mgr.lua +++ b/script/kernel/protobuf_mgr.lua @@ -67,7 +67,7 @@ function ProtobufMgr:enum(ename, ekey) local value = emun[ekey] if not value then local info = dgetinfo(2, "S") - log_warn("[ProtobufMgr][enum] %s.%s not defined! source({}:{})", ename, ekey, info.short_src, info.linedefined) + log_warn("[ProtobufMgr][enum] {}.{} not defined! source({}:{})", ename, ekey, info.short_src, info.linedefined) return end return value diff --git a/script/network/net_client.lua b/script/network/net_client.lua index 70b8ebb3..61a10eef 100644 --- a/script/network/net_client.lua +++ b/script/network/net_client.lua @@ -117,7 +117,7 @@ function NetClient:write(cmd_id, data, type, session_id, flag) -- call lbus local send_len = self.socket.call_pb(session_id, cmd_id, flag, type, 0, data) if send_len < 0 then - log_err("[NetClient][write] call_pb failed! code:%s", send_len) + log_err("[NetClient][write] call_pb failed! code:{}", send_len) return false end --proxy_agent:statistics("on_proto_send", cmd_id, send_len) diff --git a/server/accord.lua b/server/accord.lua index 7da3ff70..e45b05f2 100644 --- a/server/accord.lua +++ b/server/accord.lua @@ -4,6 +4,7 @@ import("kernel.lua") quanta.startup(function() import("kernel/protobuf_mgr.lua") import("robot/node_factory.lua") + import("robot/msg_mgr.lua") import("robot/robot_mgr.lua") import("store/mongo_mgr.lua") import("robot/dao/accord_dao.lua") diff --git a/server/cache/cache_gm.lua b/server/cache/cache_gm.lua index a5f162be..9de6245a 100644 --- a/server/cache/cache_gm.lua +++ b/server/cache/cache_gm.lua @@ -92,7 +92,7 @@ function CacheGM:signed(player_id) --加载open_id local code, doc = cache_mgr:load_document("player", player_id) if qfailed(code) then - log_err("[CacheGM][signed] load_document failed! player_id=%s", player_id) + log_err("[CacheGM][signed] load_document failed! player_id={}", player_id) return "failed" end local open_id = doc:get_datas().open_id diff --git a/server/center/gm_mgr.lua b/server/center/gm_mgr.lua index 615af598..9e1a600d 100644 --- a/server/center/gm_mgr.lua +++ b/server/center/gm_mgr.lua @@ -134,25 +134,25 @@ end --执行gm, command:string function GM_Mgr:rpc_execute_command(command) - log_debug("[GM_Mgr][rpc_execute_command] command: %s", command) + log_debug("[GM_Mgr][rpc_execute_command] command: {}", command) local res = self:exec_command(command) return SUCCESS, res end --执行gm, message: table function GM_Mgr:rpc_execute_message(message) - log_debug("[GM_Mgr][rpc_execute_message] message: %s", message) + log_debug("[GM_Mgr][rpc_execute_message] message: {}", message) local res = self:exec_message(message) return SUCCESS, res end function GM_Mgr:on_service_close(id, name) - log_debug("[GM_Mgr][on_service_close] node: %s-%s", name, id) + log_debug("[GM_Mgr][on_service_close] node: {}-{}", name, id) self.monitors[id] = nil end function GM_Mgr:on_service_ready(id, name, info) - log_debug("[GM_Mgr][on_service_ready] node: %s-%s, info: %s", name, id, info) + log_debug("[GM_Mgr][on_service_ready] node: {}-{}, info: {}", name, id, info) self.monitors[id] = sformat("%s:%s", info.ip, info.port) end @@ -170,7 +170,7 @@ end --monitor拉取 function GM_Mgr:on_monitors(url, params) - log_debug("[GM_Mgr][on_monitors] body: %s", params) + log_debug("[GM_Mgr][on_monitors] body: {}", params) local nodes = {} for _, addr in pairs(self.monitors) do tinsert(nodes, { text = addr, tag = "log" }) @@ -180,13 +180,13 @@ end --后台GM调用,字符串格式 function GM_Mgr:on_command(url, body) - log_debug("[GM_Mgr][on_command] body: %s", body) + log_debug("[GM_Mgr][on_command] body: {}", body) return self:exec_command(body.data) end --后台GM调用,table格式 function GM_Mgr:on_message(url, body) - log_debug("[GM_Mgr][on_message] body: %s", body) + log_debug("[GM_Mgr][on_message] body: {}", body) return self:exec_message(body.data) end diff --git a/server/robot/accord/accord_conf.lua b/server/robot/accord/accord_conf.lua deleted file mode 100644 index 9717999d..00000000 --- a/server/robot/accord/accord_conf.lua +++ /dev/null @@ -1,5 +0,0 @@ ---luacheck: ignore 631 -return { - lua = {servers={[1]='127.0.0.1:20013'},cases={login={protocols={[1]={id=10009,name='NID_LOGIN_ROLE_CHOOSE_REQ',args={role_id=0,user_id=0}},[2]={id=10011,name='NID_LOGIN_ROLE_DELETE_REQ',args={role_id=0,user_id=0}}},name='login',openid='test001',password='123456',server='127.0.0.1:20013'}},accord={NID_LOGIN_ROLE_LOGOUT_REQ={msg_id=10015,name='NID_LOGIN_ROLE_LOGOUT_REQ',fields={role_id=0}},NID_LOGIN_ROLE_RELOAD_REQ={msg_id=10017,name='NID_LOGIN_ROLE_RELOAD_REQ',fields={role_id=0,token=0,lobby=0,user_id=0}},NID_HEARTBEAT_REQ={msg_id=1001,name='NID_HEARTBEAT_REQ',fields={time=0,serial=0}},NID_LOGIN_RANDOM_NAME_REQ={msg_id=10005,name='NID_LOGIN_RANDOM_NAME_REQ',fields={}},NID_LOGIN_ACCOUNT_RELOAD_REQ={msg_id=10003,name='NID_LOGIN_ACCOUNT_RELOAD_REQ',fields={openid='xxx',account_token=0}},NID_LOGIN_ROLE_CHOOSE_REQ={msg_id=10009,name='NID_LOGIN_ROLE_CHOOSE_REQ',fields={role_id=0,user_id=0}},NID_LOGIN_ROLE_CREATE_REQ={msg_id=10007,name='NID_LOGIN_ROLE_CREATE_REQ',fields={model=0,gender=0,name='xxx',user_id=0}},NID_LOGIN_ACCOUNT_LOGIN_REQ={msg_id=10001,name='NID_LOGIN_ACCOUNT_LOGIN_REQ',fields={platform=0,openid='xxx',session='xxx'}},NID_LOGIN_ROLE_LOGIN_REQ={msg_id=10013,name='NID_LOGIN_ROLE_LOGIN_REQ',fields={role_id=0,token=0,lobby=0,user_id=0}},NID_LOGIN_ROLE_DELETE_REQ={msg_id=10011,name='NID_LOGIN_ROLE_DELETE_REQ',fields={role_id=0,user_id=0}}}}, - json = [[{"servers":["127.0.0.1:20013"],"cases":{"login":{"protocols":[{"id":10009,"name":"NID_LOGIN_ROLE_CHOOSE_REQ","args":{"role_id":0,"user_id":0}},{"id":10011,"name":"NID_LOGIN_ROLE_DELETE_REQ","args":{"role_id":0,"user_id":0}}],"name":"login","openid":"test001","password":"123456","server":"127.0.0.1:20013"}},"accord":{"NID_LOGIN_ROLE_LOGOUT_REQ":{"msg_id":10015,"name":"NID_LOGIN_ROLE_LOGOUT_REQ","fields":{"role_id":0}},"NID_LOGIN_ROLE_RELOAD_REQ":{"msg_id":10017,"name":"NID_LOGIN_ROLE_RELOAD_REQ","fields":{"role_id":0,"token":0,"lobby":0,"user_id":0}},"NID_HEARTBEAT_REQ":{"msg_id":1001,"name":"NID_HEARTBEAT_REQ","fields":{"time":0,"serial":0}},"NID_LOGIN_RANDOM_NAME_REQ":{"msg_id":10005,"name":"NID_LOGIN_RANDOM_NAME_REQ","fields":{}},"NID_LOGIN_ACCOUNT_RELOAD_REQ":{"msg_id":10003,"name":"NID_LOGIN_ACCOUNT_RELOAD_REQ","fields":{"openid":"xxx","account_token":0}},"NID_LOGIN_ROLE_CHOOSE_REQ":{"msg_id":10009,"name":"NID_LOGIN_ROLE_CHOOSE_REQ","fields":{"role_id":0,"user_id":0}},"NID_LOGIN_ROLE_CREATE_REQ":{"msg_id":10007,"name":"NID_LOGIN_ROLE_CREATE_REQ","fields":{"model":0,"gender":0,"name":"xxx","user_id":0}},"NID_LOGIN_ACCOUNT_LOGIN_REQ":{"msg_id":10001,"name":"NID_LOGIN_ACCOUNT_LOGIN_REQ","fields":{"platform":0,"openid":"xxx","session":"xxx"}},"NID_LOGIN_ROLE_LOGIN_REQ":{"msg_id":10013,"name":"NID_LOGIN_ROLE_LOGIN_REQ","fields":{"role_id":0,"token":0,"lobby":0,"user_id":0}},"NID_LOGIN_ROLE_DELETE_REQ":{"msg_id":10011,"name":"NID_LOGIN_ROLE_DELETE_REQ","fields":{"role_id":0,"user_id":0}}}}]] -} diff --git a/server/robot/accord/index.lua b/server/robot/accord/index.lua new file mode 100644 index 00000000..de192c91 --- /dev/null +++ b/server/robot/accord/index.lua @@ -0,0 +1,2684 @@ +-- luacheck: ignore +return [[ + + + + 协议测试 + + + + + + + + +
+
+
+ +
+

服务器地址: {{serverAddr}}

+

OpenID: {{openid}}

+

roleId: {{roleId}}

+
+
+
+
+ +
    +
    +

    + 启动 +

    启动Robot

    +

    + +
    +
    +
  • +
    + + + + + + +

    + {{cmd.name}} +

    + + +
    +
  • + +
    +
    +

    + 停止 +

    停止Robot

    +

    +
    +
+
+
+ +
+
+
+ + + + + +
+
+ +
+ +
+
+
+
  {{messages}}
+
+
+
+
+ + + +
+
+
+

{{curProtocol ? curProtocol.name : ""}}

+
+
+
+
+
+
+
+
+
+
+
+ + + + + + + + + + + + + +
+ +

+
+ 回执关注(默认关注RES协议)(示例:1000,1001): +

+
+ + +
+
+
+
+
+
+
+
+
+
+ + +
+
+
+ + + +
+
+
+
+
+
+
+
+ + +
+ +

+
+ +
+
+
+
+
+
+
+
+
+
+ + +
+
+
+ + + +
+
+
+ +
+
+
+
+
+
+
+
    +
  • + {{ group.name }} + + +
  • +
+
+
+
+
+
+
+
+
+
+ + + +
+
+
+ + + +
+
+
+
+
+
+
+
+
+ + +

+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+ + + +
+
+
+

配置测试用例

+
+
+
+
+
+
+
+
+
+
+ + +
+ +
+ + + + + + + + + + + + + + + +
+ + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+
+
+ + + + + + +]] \ No newline at end of file diff --git a/server/robot/accord/page/data_utils.js b/server/robot/accord/page/data_utils.js new file mode 100644 index 00000000..863746d0 --- /dev/null +++ b/server/robot/accord/page/data_utils.js @@ -0,0 +1,90 @@ +//全局uuid自增 +var g_uuid_index = 0; +var DataUtils = { + //获取当前时间戳(毫秒) + getNowTime:function(){ + return Date.now(); + }, + //获取当前时间戳(毫秒) + getNowMilliSecond:function(){ + return Date.now(); + }, + //获取当前时间戳(秒) + getNowSecond:function(){ + var result = Math.floor(this.getNowTime() / 1000); + return result; + }, + //深拷贝 + deepCopy:function (source) { + var sourceCopy = null; + if (source instanceof Array) { + sourceCopy = []; + } + else { + if (source) { + sourceCopy = {}; + } + else { + sourceCopy = source; + } + } + + for (var item in source) { + if (typeof source[item] === 'object') { + sourceCopy[item] = this.deepCopy(source[item]); + } + else { + sourceCopy[item] = source[item]; + } + } + return sourceCopy; + }, + generateUUID:function(maxCount=10000){ + var uuid = "" + Date.now() + g_uuid_index; + g_uuid_index++; + if (g_uuid_index >= maxCount) { + //一毫米内超过maxCount个uuid有问题 + g_uuid_index = 0; + } + return uuid; + }, + getMapCount:function(map){ + var count = 0 + for (var key in map) { + count++ + } + return count + }, + isValidIP:function(address){ + const ipPattern = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/; + return ipPattern.test(address); + }, + copyToClipboard:function(str){ + if (navigator.clipboard && window.isSecureContext) { + return navigator.clipboard.writeText(str); + } else { + let textArea = document.createElement("textarea"); + textArea.value = str; + textArea.style.position = "absolute"; + textArea.style.opacity = 0; + textArea.style.left = "-999999px"; + textArea.style.top = "-999999px"; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + return new Promise((res, rej) => { + document.execCommand('copy') ? res() : rej(); + textArea.remove(); + }); + } + }, + arrObjIndexOf(array, key, val){ + for(var i=0; i/g, '>'); + return str.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { + var cls = 'number'; + if (/^"/.test(match)) { + if (/:$/.test(match)) { + cls = 'key'; + } else { + cls = 'string'; + } + } else if (/true|false/.test(match)) { + cls = 'boolean'; + } else if (/null/.test(match)) { + cls = 'null'; + } + return '' + match + ''; + }); + } catch (e) { + alert("异常信息:" + e); + } + }, + formatJson:function(text){ + var result = text.replace(/\\n/g, '\n').replace(/\\/g, '').replace(/^\"|\"$/g, ''); + return result + }, +} \ No newline at end of file diff --git a/server/robot/accord/page/index.html b/server/robot/accord/page/index.html new file mode 100644 index 00000000..b328446a --- /dev/null +++ b/server/robot/accord/page/index.html @@ -0,0 +1,1512 @@ +-- luacheck: ignore +return [[ + + + + 协议测试 + + + + + + + + +
+
+
+ +
+

服务器地址: {{serverAddr}}

+

OpenID: {{openid}}

+

roleId: {{roleId}}

+
+
+
+
+ +
    +
    +

    + 启动 +

    启动Robot

    +

    + +
    +
    +
  • +
    + + + + + + +

    + {{cmd.name}} +

    + + +
    +
  • + +
    +
    +

    + 停止 +

    停止Robot

    +

    +
    +
+
+
+ +
+
+
+ + + + + +
+
+ +
+ +
+
+
+
  {{messages}}
+
+
+
+
+ + + +
+
+
+

{{curProtocol ? curProtocol.name : ""}}

+
+
+
+
+
+
+
+
+
+
+
+ + + + + + + + + + + + + +
+ +

+
+ 回执关注(默认关注RES协议)(示例:1000,1001): +

+
+ + +
+
+
+
+
+
+
+
+
+
+ + +
+
+
+ + + +
+
+
+
+
+
+
+
+ + +
+ +

+
+ +
+
+
+
+
+
+
+
+
+
+ + +
+
+
+ + + +
+
+
+ +
+
+
+
+
+
+
+
    +
  • + {{ group.name }} + + +
  • +
+
+
+
+
+
+
+
+
+
+ + + +
+
+
+ + + +
+
+
+
+
+
+
+
+
+ + +

+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+ + + +
+
+
+

配置测试用例

+
+
+
+
+
+
+
+
+
+
+ + +
+ +
+ + + + + + + + + + + + + + + +
+ + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+
+
+ + + + + + +]] \ No newline at end of file diff --git a/server/robot/accord/page/net_utils.js b/server/robot/accord/page/net_utils.js new file mode 100644 index 00000000..dba4edd4 --- /dev/null +++ b/server/robot/accord/page/net_utils.js @@ -0,0 +1,38 @@ +var NetUtils = { + http: null, + init: function(http){ + this.http = http + }, + post: function (name, args, callback) { + this.http.post(name, args).then(function (response) { + var data = response.data || {} + if(response.status == 200){ + if(callback){ + callback(true, data) + } + }else { + callback(false, data) + console.error("[Request][post] fail name(%s) args(%s) status(%s)", name, args, response.status) + } + }, function (response) { + callback(false, null) + console.error("[Request][post] fail name(%s) args(%s) status(%s)", name, args, response.status) + }); + }, + get: function(name, args, callback){ + this.http.get(name, args).then(function(response){ + var data = response.data || {} + if(response.status == 200){ + if(callback){ + callback(true, data) + } + }else { + callback(false, data) + console.error("[Request][get] fail name(%s) args(%s) status(%s)", name, args, response.status) + } + }, function (response) { + callback(false, null) + console.error("[Request][get] fail name(%s) args(%s) status(%s)", name, args, response.status) + }); + } +} \ No newline at end of file diff --git a/server/robot/accord/page/page.html b/server/robot/accord/page/page.html deleted file mode 100644 index 5b081409..00000000 --- a/server/robot/accord/page/page.html +++ /dev/null @@ -1,1332 +0,0 @@ - - - - 协议测试 - - - - - - - - -
-
-
- -
-

服务器地址: {{serverAddr}}

-

OpenID: {{openid}}

-

roleId: {{roleId}}

-
-
-
-
- -
    -
    -

    - 启动 -

    启动Robot

    -

    - -
    -
    -
  • -
    - - - - - - -

    - {{cmd.name}} -

    - - -
    -
  • - -
    -
    -

    - 停止 -

    停止Robot

    -

    -
    -
-
-
- -
-
-
- - - - -
- -
-
  {{messages}}
-
-
-
-
- - - -
-
-
-

{{curProtocolName}}

-
-
-
-
-
-
-
-
-
-
-
- - - - - - -
- -

-
- 回执关注(默认关注RES协议)(示例:1000,1001): -

-
- - -
-
-
-
-
-
-
-
-
-
- - > -
-
-
- - - -
-
-
-
-
-
-
-
- - -
- -

-
- -
-
-
-
-
-
-
-
-
-
- - > -
-
-
- - - -
-
-
- -
-
-
-
-
-
-
-
    -
  • - {{ server.name }} - {{ server.address }}:{{ server.port }} - - -
  • -
-
-
-
-
-
-
-
-
-
- - -
-
-
- - - -
-
-
-
-
-
-
-
-
- - - - - - -

-
- -
-
- -
-
-
-
-
-
-
-
-
-
- -
-
-
- - - -
-
-
-

配置测试用例

-
-
-
-
-
-
-
-
-
-
- - -
- - - - - - - -
- - - - - - - -
- - -
-
- -
-
- -
-
- -
-
- -
-
- -
-
-
-
-
-
-
-
-
-
- - - - \ No newline at end of file diff --git a/server/robot/accord/page/css/style.css b/server/robot/accord/page/style.css similarity index 95% rename from server/robot/accord/page/css/style.css rename to server/robot/accord/page/style.css index c5253f79..2619eecc 100644 --- a/server/robot/accord/page/css/style.css +++ b/server/robot/accord/page/style.css @@ -497,7 +497,7 @@ body { } .log-half { - width: calc(100% - 440px); + width: calc(100% - 450px); height: calc(100% - 20px); overflow-y: auto; float: left; @@ -926,7 +926,7 @@ body { } .checkbox { - display: flex; + /* display: flex; */ flex-direction: row; } @@ -958,4 +958,54 @@ body { border: 1px solid #ccc; border-radius: 5px; cursor: pointer; +} + +.checke{ + position: relative; + -webkit-appearance: none; + width:40px; + height: 20px; + line-height: 10px; + background: #eee; + border-radius: 30px; + outline: none; +} +.checke:before{ + position: absolute; + left: 0; + content: ''; + width: 20px; + height: 20px; + border-radius: 50%; + background: white; + box-shadow: 0px 0px 5px #ddd; + transition: all 0.2s linear; +} +.checke:checked{ + background: rgb(39, 217, 159); +} +.checke:checked:before{ + left: 20px; + transition: all 0.2s linear; +} + +.log-fast-btn { + height: 50px; + width: 55px; + padding: 0px 10px; + margin-left: 50px; + background-color: rgb(39, 217, 159); + border: 1px solid #ccc; + cursor: pointer; + opacity: .3; + transition: opacity 0.5s; + border-radius: 50px; +} +.log-fast-btn:hover { + opacity: 1; + cursor: pointer; +} + +.pre-text { + white-space: pre-wrap; } \ No newline at end of file diff --git a/server/robot/accord_mgr.lua b/server/robot/accord_mgr.lua index 3a747b45..76f7786c 100644 --- a/server/robot/accord_mgr.lua +++ b/server/robot/accord_mgr.lua @@ -1,58 +1,55 @@ -- accord_mgr.lua -local log_warn = logger.warn -local log_debug = logger.debug -local jdecode = json.decode +local log_warn = logger.warn +local log_debug = logger.debug +local jdecode = json.decode +local json_pretty = json.pretty -local HttpServer = import("network/http_server.lua") +local HttpServer = import("network/http_server.lua") -local robot_mgr = quanta.get("robot_mgr") -local update_mgr = quanta.get("update_mgr") -local thread_mgr = quanta.get("thread_mgr") -local accord_dao = quanta.get("accord_dao") +local robot_mgr = quanta.get("robot_mgr") +local update_mgr = quanta.get("update_mgr") +local thread_mgr = quanta.get("thread_mgr") +local accord_dao = quanta.get("accord_dao") +local msg_mgr = quanta.get("msg_mgr") -- 时间单位 -local SECOND_3_MS = quanta.enum("PeriodTime", "SECOND_3_MS") +local SECOND_3_MS = quanta.enum("PeriodTime", "SECOND_3_MS") -local AccordMgr = singleton() -local prop = property(AccordMgr) +local AccordMgr = singleton() +local prop = property(AccordMgr) prop:reader("http_server", nil) -prop:reader("accord_html", "") -prop:reader("accord_css", "") -prop:reader("server_list", {}) -prop:reader("accord_list", {}) +prop:reader("accord_list", {}) -- 协议列表(添加的数据) +prop:reader("case_group", {}) -- 用例分组 prop:reader("load_db_status", false) +prop:reader("srvlist_api", environ.get("QUANTA_SRVLIST_API")) -- 服务器列表api function AccordMgr:__init() -- 创建HTTP服务器 local server = HttpServer(environ.get("QUANTA_ACCORD_HTTP")) server:register_get("/", "on_accord_page", self) - server:register_get("/style", "on_accord_css", self) server:register_get("/message", "on_message", self) server:register_post("/create", "on_create", self) server:register_post("/destory", "on_destory", self) - server:register_post("/runall", "on_runall", self) server:register_post("/run", "on_run", self) - - -- 服务器操作 - server:register_post("/server_list", "on_server_list", self) - server:register_post("/server_edit", "on_server_edit", self) - server:register_post("/server_del", "on_server_del", self) + server:register_post("/get_config", "on_get_config", self) -- 协议操作 + server:register_post("/case_group", "on_case_group", self) + server:register_post("/case_group_edit", "on_case_group_edit", self) + server:register_post("/case_group_del", "on_case_group_del", self) + server:register_post("/accord_group", "on_accord_group", self) server:register_post("/accord_list", "on_accord_list", self) server:register_post("/accord_edit", "on_accord_edit", self) server:register_post("/accord_del", "on_accord_del", self) - server:register_post("/upload", "on_upload", self) server:register_post("/proto_edit", "on_proto_edit", self) server:register_post("/proto_del", "on_proto_del", self) service.make_node(server:get_port()) self.http_server = server -- 定时更新 - update_mgr:attach_second(self) - -- 加载数据 + update_mgr:attach_second5(self) + self:on_second5() self:load_db_data() - self:on_second() end -- 加载db数据 @@ -61,84 +58,55 @@ function AccordMgr:load_db_data() return end thread_mgr:success_call(SECOND_3_MS, function() - local svr_ok, srv_dbdata = accord_dao.load_server_list() - if svr_ok then - for _,server in pairs(srv_dbdata) do - self.server_list[tostring(server.name)] = server + -- 用例分组 + local cgp_ok, cgp_dbdata = accord_dao:load_data("case_group") + if cgp_ok then + for _, group in pairs(cgp_dbdata) do + self.case_group[group.id] = group end else - log_warn("[AccordMgr][load_db_data] load_server_list fail ok({}})", svr_ok) + log_warn("[AccordMgr][load_db_data] case_group fail ok:{}", cgp_ok) end - local cf_ok, cf_dbdata = accord_dao.load_accord_conf() + -- 协议配置 + local cf_ok, cf_dbdata = accord_dao:load_data("accord_conf") if cf_ok then - for _,conf in pairs(cf_dbdata) do - local name = tostring(conf.name) - self.accord_list[name] = { - name = name, + for _, conf in pairs(cf_dbdata) do + self.accord_list[conf.id] = { + id = conf.id, + name = tostring(conf.name), openid = tostring(conf.openid), passwd = tostring(conf.passwd), server = conf.server or "", rpt_att = conf.rpt_att, - protocols = {} + protocols = {}, + time = conf.time or 0, + casegroup = conf.casegroup or "" } -- 解析proto - for _,proto in pairs(conf.protocols) do - self.accord_list[name].protocols[tostring(proto.name)] = proto + for _, proto in pairs(conf.protocols) do + self.accord_list[conf.id].protocols[tonumber(proto.id)] = proto end end else - log_warn("[AccordMgr][load_db_data] load_accord_conf fail ok({}})", cf_ok) + log_warn("[AccordMgr][load_db_data] accord_conf fail ok:{}", cf_ok) end - if svr_ok and cf_ok then + if cgp_ok and cf_ok then self.load_db_status = true end - return svr_ok and cf_ok + return cgp_ok and cf_ok end) end --- 加载页面 +-- 加载资源 function AccordMgr:load_html() - self.accord_html = [[ - - - - -

Failed to load the html file. Please check the html file(filePath:bin\accord\page.html)

- - - ]] - local currentDir = debug.getinfo(1, "S").source:sub(2):match("(.*/)") - local file_path = currentDir .. "accord/page/page.html" - local file = io.open(file_path, "r") - if file then - self.accord_html = file:read("*a") - file:close() - else - log_warn("Unable to open file(file_path=bin/{}})", file_path) - end -end - --- 加载页面 -function AccordMgr:load_css() - self.accord_css = [[ - ]] - local currentDir = debug.getinfo(1, "S").source:sub(2):match("(.*/)") - local file_path = currentDir .. "accord/page/css/style.css" - local file = io.open(file_path, "r") - if file then - self.accord_css = file:read("*a") - file:close() - else - log_warn("Unable to open file(file_path=bin/{}})", file_path) - end + self.accord_html = import("../server/robot/accord/index.lua") end -- 定时更新 -function AccordMgr:on_second() +function AccordMgr:on_second5() self:load_html() - self:load_css() end -- http 回调 @@ -163,170 +131,189 @@ function AccordMgr:on_accord_page(url, body) } end --- accord_css -function AccordMgr:on_accord_css(url, body) - return self.accord_css, { - ["Access-Control-Allow-Origin"] = "*" - } +-- 获取资源名称 +function AccordMgr:get_src_name(src_path) + local match = string.match(src_path, "[^/\\]+$") + local src_name = string.sub(match, 1, -5) + return src_name end -- 拉取日志 function AccordMgr:on_message(url, params) - --log_debug("[AccordMgr][on_message] open_id: {}}", open_id) + -- log_debug("[AccordMgr][on_message] open_id: {}", params.open_id) return robot_mgr:get_accord_message(params.open_id) end -- monitor拉取 -function AccordMgr:on_create(url, body) - log_debug("[AccordMgr][on_create] body: {}}", body) +function AccordMgr:on_create(url, body, params) + log_debug("[AccordMgr][on_create] params:{}", params) return robot_mgr:create_robot(body.ip, body.port, body.open_id, body.passwd) end -- 后台GM调用,字符串格式 -function AccordMgr:on_destory(url, body) - log_debug("[AccordMgr][on_destory] body: {}}", body) +function AccordMgr:on_destory(url, body, params) + log_debug("[AccordMgr][on_destory] body:{}", body) return robot_mgr:destory_robot(body.open_id) end --- 服务器列表 -function AccordMgr:on_server_list(url, body) - log_debug("[AccordMgr][on_server_list] body: {}}", body) - return { code = 0, server_list = self.server_list} +-- 协议分组 +function AccordMgr:on_accord_group(url, body) + log_debug("[AccordMgr][on_accord_group] body:{}", body) + return { + code = 0, + accord_group = msg_mgr.accord_group + }, { + ["Access-Control-Allow-Origin"] = "*" + } +end + +-- 分组列表 +function AccordMgr:on_case_group(url, body) + log_debug("[AccordMgr][on_case_group] body:{}", body) + return { code = 0, case_group = self.case_group } end --- 编辑服务器 -function AccordMgr:on_server_edit(url, body) - log_debug("[AccordMgr][on_server_edit] body: {}}", body) +-- 编辑用例分组 +function AccordMgr:on_case_group_edit(url, body) + log_debug("[AccordMgr][on_case_group_edit] body:{}", body) if body.data then - local data = body.data - if self.server_list[data.name] then - accord_dao:save_server(data) + local data = body.data.case_group + local id = data.id + if self.case_group[id] then + accord_dao:update("case_group", data) else - accord_dao:add_server(data) + accord_dao:insert("case_group", data) end - self.server_list[data.name] = data - return { code = 0} + self.case_group[id] = data + return { code = 0 } end - return { code = -1, msg = "数据格式不正确!"} + return { code = -1, msg = "数据格式不正确!" } end --- 删除服务器 -function AccordMgr:on_server_del(url, body) - log_debug("[AccordMgr][on_server_del] body: {}}", body) +-- 删除用例分组 +function AccordMgr:on_case_group_del(url, body) + log_debug("[AccordMgr][on_case_group_del] body:{}", body) if body.data then local data = body.data - self.server_list[data.name] = nil - accord_dao:del_server(data.name) - return { code = 0} + local id = data.id + local group = self.case_group[id] + if not group then + return { code = 0 } + end + for aid, accord in pairs(self.accord_list) do + if group.name == accord.casegroup then + accord_dao:delete("accord_conf", aid) + end + end + self.case_group[id] = nil + accord_dao:delete("case_group", id) + return { code = 0 } end - return { code = -1, msg = "数据格式不正确!"} + return { code = -1, msg = "数据格式不正确!" } end -- 协议列表 function AccordMgr:on_accord_list(url, body) - log_debug("[AccordMgr][on_accord_list] body: {}}", body) - return { code = 0, accord_list = self.accord_list} + log_debug("[AccordMgr][on_accord_list] body:{}", body) + return { code = 0, accord_list = self.accord_list } end -- 编辑协议 function AccordMgr:on_accord_edit(url, body) - log_debug("[AccordMgr][on_accord_edit] body: {}}", body) + log_debug("[AccordMgr][on_accord_edit] body:{}", body) if body.data then local data = body.data - local accords = data.accords - local low_name = data.low_name - local clone = data.clone - -- 删除原数据 - if not clone and low_name ~= accords.name then - self.accord_list[low_name] = nil - accord_dao:del_accord_conf(low_name) + local id = data.id + local new = false + local accord = self.accord_list[id] + if not accord then + new = true + accord = {} + accord.protocols = {} + end + accord.id = data.id + accord.name = data.name + accord.openid = data.openid + accord.passwd = data.passwd + accord.server = data.server + accord.rpt_att = data.rpt_att + accord.casegroup = data.casegroup + if data.protocols then + accord.protocols = data.protocols end - if self.accord_list[accords.name] then - accord_dao:save_accord_conf(accords) + accord.time = data.time + + if new == false then + accord_dao:update("accord_conf", accord) else - accord_dao:add_accord_conf(accords) + accord_dao:insert("accord_conf", accord) end - self.accord_list[accords.name] = accords - return { code = 0} + self.accord_list[id] = accord + return { code = 0 } end - return { code = -1, msg = "数据格式不正确!"} + return { code = -1, msg = "数据格式不正确!" } end -- 删除协议 function AccordMgr:on_accord_del(url, body) - log_debug("[AccordMgr][on_accord_del] body: {}}", body) + log_debug("[AccordMgr][on_accord_del] body:{}", body) if body.data then local data = body.data - self.accord_list[data.name] = nil - accord_dao:del_accord_conf(data.name) - return { code = 0} - end - return { code = -1, msg = "数据格式不正确!"} -end - --- 客户端上传用例 -function AccordMgr:on_upload(url, body) - log_debug("[AccordMgr][on_upload] body: {}}", body) - if body.data then - local data = jdecode(body.data); - local add = false - if not self.accord_list[data.name] then - add = true - end - self.accord_list[data.name] = data - if add then - accord_dao:add_accord_conf(data) - else - accord_dao:save_accord_conf(data) - end - return { code = 0, data=data} + self.accord_list[data.id] = nil + accord_dao:delete("accord_conf", data.id) + return { code = 0 } end - return { code = -1, msg = "数据格式不正确!"} + return { code = -1, msg = "数据格式不正确!" } end -- 编辑协议选项 function AccordMgr:on_proto_edit(url, body) - log_debug("[AccordMgr][on_proto_edit] body: {}}", body) - if body.data then + log_debug("[AccordMgr][on_proto_edit] body:{}", body) +if body and body.data then local data = body.data - local accord = self.accord_list[data.name] + local accord = self.accord_list[data.id] if not accord then - return { code = -1, msg = "不存在的协议配置,请重新创建!"} + return { code = -1, msg = "不存在的协议配置,请重新创建!" } end local proto = data.data - accord.protocols[proto.name] = proto - accord_dao:save_accord_conf(accord) + -- 解析json + proto.args = jdecode(proto.args); + -- 格式json字符串 + proto.args = json_pretty(proto.args) + accord.protocols[tonumber(proto.id)] = proto + accord_dao:update("accord_conf", accord) end - return { code = -1, msg = "数据格式不正确!"} + return { code = 0} end -- 删除协议选项 function AccordMgr:on_proto_del(url, body) - log_debug("[AccordMgr][on_proto_del] body: {}}", body) + log_debug("[AccordMgr][on_proto_del] body:{}", body) if body.data then local data = body.data - local accord = self.accord_list[data.accord_name] + local accord = self.accord_list[data.id] if not accord then - return { code = -1, msg = "不存在的协议配置!"} + return { code = -1, msg = "不存在的协议配置!" } end - accord.protocols[data.proto_name] = nil - accord_dao:save_accord_conf(accord) + accord.protocols[tonumber(data.proto_id)] = nil + accord_dao:update("accord_conf", accord) end - return { code = -1, msg = "数据格式不正确!"} + return { code = 0} end -- 后台GM调用,table格式 function AccordMgr:on_run(url, body) + local data = body.data if body.cmd_id ~= 1001 then - log_debug("[AccordMgr][on_run] body: {}}", body) + data = jdecode(body.data) + log_debug("[AccordMgr][on_run] body:{}", body) end - return robot_mgr:run_accord_message(body.open_id, body.cmd_id, body.data) + return robot_mgr:run_accord_message(body.open_id, body.cmd_id, data) end --- 后台GM调用,table格式 -function AccordMgr:on_runall(url, body) - log_debug("[AccordMgr][on_runall] body: {}}", body) - return robot_mgr:run_accord_messages(body.open_id, body.data) +-- 获取配置 +function AccordMgr:on_get_config(url, body) + return { code = 0, srvlist_api=self.srvlist_api} end quanta.accord_mgr = AccordMgr() diff --git a/server/robot/dao/accord_dao.lua b/server/robot/dao/accord_dao.lua index 87667c3d..7344c0fa 100644 --- a/server/robot/dao/accord_dao.lua +++ b/server/robot/dao/accord_dao.lua @@ -1,5 +1,5 @@ -local log_err = logger.err +local log_debug = logger.debug local mrandom = qmath.random local SUCCESS = quanta.enum("KernCode", "SUCCESS") @@ -10,81 +10,42 @@ local AccordDao = singleton() function AccordDao:__init() end --- 加载协议配置 -function AccordDao:load_accord_conf() - local code, data = mongo_mgr:find(1, mrandom(), "accord_conf", {}) +-- 加载数据 +function AccordDao:load_data(document) + local code, data = mongo_mgr:find(1, mrandom(), document, {}) if code ~= SUCCESS then + log_debug("[AccordDao][load_data] document:{} code:{}", document, code) return false, code end return true, data end --- 添加协议配置 -function AccordDao:add_accord_conf(data) - local code, res = mongo_mgr:insert(1, mrandom(), "accord_conf", data,nil) +-- 插入数据 +function AccordDao:insert(document, data) + local code = mongo_mgr:insert(1, mrandom(), document, data) if code ~= SUCCESS then - log_err("[AccordDao][add_accord_conf] name:{}", data.name, code, res) + log_debug("[AccordDao][insert] document:{} code:{}", document, code) return false end return true end --- 存储协议配置 -function AccordDao:save_accord_conf(data) +-- 更新数据 +function AccordDao:update(document, data) local udata = { ["$set"] = data } - local code, res = mongo_mgr:update(1, mrandom(), "accord_conf", udata, { name = data.name }) + local code = mongo_mgr:update(1, mrandom(), document, udata, { id = data.id }) if code ~= SUCCESS then - log_err("[AccordDao][save_accord_conf] name:{}", data.name, code, res) + log_debug("[AccordDao][update] document:{} code:{}", document, code) return false end return true end --- 删除协议配置 -function AccordDao:del_accord_conf(name) - local code, res = mongo_mgr:delete(1, mrandom(), "accord_conf", { name = name }) +-- 删除数据 +function AccordDao:delete(document, id) + local code = mongo_mgr:delete(1, mrandom(), document, {id=id}) if code ~= SUCCESS then - log_err("[AccordDao][del_accord_conf] name:{}", name, code, res) - return false - end - return true -end - --- 加载服务器配置 -function AccordDao:load_server_list() - local code, data = mongo_mgr:find(1, mrandom(), "accord_server", {}) - if code ~= SUCCESS then - return false, code - end - return true, data -end - --- 添加服务配置 -function AccordDao:add_server(data) - local code, res = mongo_mgr:insert(1, mrandom(), "accord_server", data) - if code ~= SUCCESS then - log_err("[AccordDao][add_server] name:{}", data.name, code, res) - return false - end - return true -end - --- 保存服务配置 -function AccordDao:save_server(data) - local udata = { ["$set"] = data } - local code, res = mongo_mgr:update(1, mrandom(), "accord_server", udata, { name = data.name }) - if code ~= SUCCESS then - log_err("[AccordDao][save_server] name:{}", data.name, code, res) - return false - end - return true -end - --- 删除服务配置 -function AccordDao:del_server(name) - local code, res = mongo_mgr:delete(1, mrandom(), "accord_server", { name = name }) - if code ~= SUCCESS then - log_err("[AccordDao][del_server] name:{}", name, code, res) + log_debug("[AccordDao][delete] document:{} code:{}", document, code) return false end return true diff --git a/server/robot/module/login.lua b/server/robot/module/login.lua index 3c73be33..896ba4a8 100644 --- a/server/robot/module/login.lua +++ b/server/robot/module/login.lua @@ -170,7 +170,7 @@ function LoginModule:choose_role_req() } local ok, res = self:call("NID_LOGIN_ROLE_CHOOSE_REQ", req_data) if self:check_callback(ok, res) then - log_warn("[LoginModule][choose_role_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) + log_warn("[LoginModule][choose_role_req] robot:{}, ok={}, res={} req_data={}", self:get_title(), ok, res, req_data) return false end self.lobby = res.lobby diff --git a/server/robot/module/session.lua b/server/robot/module/session.lua index d72e7c1c..628b803d 100644 --- a/server/robot/module/session.lua +++ b/server/robot/module/session.lua @@ -50,6 +50,7 @@ end function SessionModule:on_socket_rpc(client, cmd_id, body) local doer = self.cmd_doers[cmd_id] if not doer then + self:push_message(cmd_id, body) log_warn("[SessionModule][on_socket_rpc] cmd {} hasn't register doer!, msg={}", cmd_id, body) return end @@ -81,8 +82,19 @@ function SessionModule:send(cmdid, data) end function SessionModule:call(cmdid, data) + if type(cmdid) == "string" then + cmdid = protobuf_mgr:msg_id(cmdid) + end if self.client then - local ok, resp = self.client:call(cmdid, data, self:conv_type(cmdid)) + local srv_type = self:conv_type(cmdid) + local ok, resp = self.client:call(cmdid, data, srv_type) + log_debug("call cmdid:{} data:{} srv_type:{} ok:{} resp:{}",cmdid, data, srv_type, ok, resp) + if srv_type == 0 and cmdid ~= 1001 then + if resp then + resp.req_cmd_id = cmdid + end + self:push_message(cmdid+1, resp) + end return ok, resp end return false diff --git a/server/robot/msg_mgr.lua b/server/robot/msg_mgr.lua new file mode 100644 index 00000000..e0977785 --- /dev/null +++ b/server/robot/msg_mgr.lua @@ -0,0 +1,113 @@ +-- robot_mgr.lua + +local pb_enum_id = protobuf.enum +local supper = string.upper +local tunpack = table.unpack +local tcopy = qtable.copy + +local protobuf_mgr = quanta.get("protobuf_mgr") + +local MsgMgr = singleton() +local prop = property(MsgMgr) +prop:reader("accord_req_group", {}) -- 协议分组(解析proto) +prop:reader("accord_res_group", {}) -- 协议分组(解析proto) +prop:reader("accord_ntf_group", {}) -- 协议分组(解析proto) +prop:reader("accord_group", {}) -- 协议分组(解析proto) + +function MsgMgr:__init() + self:init_accord() +end + +function MsgMgr:ssplit(str, token) + local t = {} + while #str > 0 do + local pos = str:find(token) + if not pos then + t[#t + 1] = str + break + end + if pos > 1 then + t[#t + 1] = str:sub(1, pos - 1) + end + str = str:sub(pos + 1, #str) + end + return t +end + +function MsgMgr:sends_with(str, ending) + return str:sub(- #ending) == ending +end + +-- 初始化协议数据 +function MsgMgr:init_accord() + for full_name, proto_name, type in protobuf.types() do + if type == "message" then + local package_name = tunpack(self:ssplit(full_name, "%.")) + local enum_type = package_name .. ".NCmdId" + local msg_name = "NID_" .. supper(proto_name) + local msg_id = pb_enum_id(enum_type, msg_name) + if msg_id then + local fields = {} + for name, _, typ in protobuf.fields(full_name) do + fields[name] = (typ == "string") and "xxx" or 0 + end + local group = string.sub(msg_name, 1, string.find(msg_name, "_", string.find(msg_name, "_") + 1) - 1) + if group then + if not self.accord_group[group] then + self.accord_group[group] = {} + end + self.accord_group[group][msg_name] = { + msg_id = msg_id, + name = msg_name, + fields = fields, + type = string.sub(proto_name, -3) + } + end + end + end + end +end + +-- 转换字段 +function MsgMgr:convert_fields(struct, data) + if not data then + return + end + for key, field_type in pairs(struct) do + local value = data[key] + if type(field_type) == "table" then + self:convert_fields(field_type, value) + elseif field_type == "uint64" then + data[key] = tostring(value) + end + end +end + +-- 返回协议结构 +function MsgMgr:msg_struct(cmd_id) + local struct = {} + local name = protobuf_mgr:msg_name(cmd_id) + for key, _, type in protobuf.fields(name) do + struct[key] = type + end + return struct +end + +-- 参数转换 +function MsgMgr:args_convert(cmd_id, data) + if cmd_id == 1001 then + return + end + local c_data = tcopy(data) + -- 获取协议结构 + local struct = self:msg_struct(cmd_id) + -- 转换参数字段 + if struct then + self:convert_fields(struct, c_data) + end + return c_data +end + +quanta.msg_mgr = MsgMgr() + +return MsgMgr diff --git a/server/robot/robot.lua b/server/robot/robot.lua index 1dcd26e4..c8529f74 100644 --- a/server/robot/robot.lua +++ b/server/robot/robot.lua @@ -7,6 +7,7 @@ local LoginModule = import("robot/module/login.lua") local SessionModule = import("robot/module/session.lua") local update_mgr = quanta.get("update_mgr") +local msg_mgr = quanta.get("msg_mgr") local Robot = class(nil, SessionModule, LoginModule) local prop = property(Robot) @@ -47,7 +48,8 @@ end function Robot:push_message(cmd_id, msg) log_debug("recv server msg:{} {}",cmd_id, msg) msg.cmd_id = cmd_id - self.messages:push(msg) + local c_msg = msg_mgr:args_convert(cmd_id, msg) + self.messages:push(c_msg) end function Robot:get_messages() diff --git a/tools/accordbuild.bat b/tools/accordbuild.bat new file mode 100644 index 00000000..5b244d36 --- /dev/null +++ b/tools/accordbuild.bat @@ -0,0 +1,13 @@ +@echo off +set DATA_UTILS_JS=../server/robot/accord/page/data_utils.js +set FORMAT_UTILS_JS=../server/robot/accord/page/format_utils.js +set NET_UTILS_JS=../server/robot/accord/page/net_utils.js +set STYLE_CSS=../server/robot/accord/page/style.css +set TPL=../server/robot/accord/page/index.html +set OUT=../server/robot/accord/index.lua +set SCRIPT=../extend/lmake/ltemplate.lua + +..\bin\lua.exe %SCRIPT% %TPL% %OUT% DATA_UTILS_JS %DATA_UTILS_JS% FORMAT_UTILS_JS %FORMAT_UTILS_JS% NET_UTILS_JS %NET_UTILS_JS% STYLE_CSS %STYLE_CSS% + +pause +