diff --git a/bin/proto/pbschema.json b/bin/proto/pbschema.json new file mode 100644 index 00000000..c8a93e47 --- /dev/null +++ b/bin/proto/pbschema.json @@ -0,0 +1,1784 @@ +{ + "files": [ + { + "filename": "robot.proto", + "package": "ncmd_cs", + "syntax": 3, + "dependency": [], + "enums": [ + { + "name": "BtNodeId", + "full_name": "ncmd_cs.BtNodeId", + "extend": { + "desc": [ + "BtNodeId" + ] + }, + "alias": "BtNodeId", + "values": [ + { + "name": "BT_ZERO", + "full_name": "ncmd_cs.BT_ZERO", + "number": 0, + "extend": {}, + "alias": "BT_ZERO" + }, + { + "name": "BT_LOGIN", + "full_name": "ncmd_cs.BT_LOGIN", + "number": 1001, + "extend": { + "desc": [ + "login", + "登陆" + ] + }, + "alias": "BT_LOGIN" + }, + { + "name": "BT_LOGIN_UNIT", + "full_name": "ncmd_cs.BT_LOGIN_UNIT", + "number": 1002, + "extend": { + "desc": [ + "登陆单元测试" + ] + }, + "alias": "BT_LOGIN_UNIT" + }, + { + "name": "BT_LOGOUT", + "full_name": "ncmd_cs.BT_LOGOUT", + "number": 1003, + "extend": { + "desc": [ + "登出" + ] + }, + "alias": "BT_LOGOUT" + }, + { + "name": "BT_LOGIN_WAIT", + "full_name": "ncmd_cs.BT_LOGIN_WAIT", + "number": 1004, + "extend": { + "desc": [ + "登陆" + ] + }, + "alias": "BT_LOGIN_WAIT" + } + ] + } + ], + "messages": [] + }, + { + "filename": "ncmd_cs.proto", + "package": "ncmd_cs", + "syntax": 3, + "dependency": [], + "enums": [ + { + "name": "NCmdId", + "full_name": "ncmd_cs.NCmdId", + "extend": { + "desc": [ + "NCmdId" + ] + }, + "alias": "NCmdId", + "values": [ + { + "name": "NID_QUANTA_ZERO", + "full_name": "ncmd_cs.NID_QUANTA_ZERO", + "number": 0, + "extend": {}, + "alias": "NID_QUANTA_ZERO" + }, + { + "name": "NID_HEARTBEAT_REQ", + "full_name": "ncmd_cs.NID_HEARTBEAT_REQ", + "number": 1001, + "extend": {}, + "alias": "NID_HEARTBEAT_REQ" + }, + { + "name": "NID_HEARTBEAT_RES", + "full_name": "ncmd_cs.NID_HEARTBEAT_RES", + "number": 1002, + "extend": {}, + "alias": "NID_HEARTBEAT_RES" + }, + { + "name": "NID_LOGIN_ACCOUNT_LOGIN_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ACCOUNT_LOGIN_REQ", + "number": 10001, + "extend": { + "desc": [ + "登陆系统10001-10100", + "登陆账号请求" + ] + }, + "alias": "NID_LOGIN_ACCOUNT_LOGIN_REQ" + }, + { + "name": "NID_LOGIN_ACCOUNT_LOGIN_RES", + "full_name": "ncmd_cs.NID_LOGIN_ACCOUNT_LOGIN_RES", + "number": 10002, + "extend": {}, + "alias": "NID_LOGIN_ACCOUNT_LOGIN_RES" + }, + { + "name": "NID_LOGIN_ACCOUNT_RELOAD_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ACCOUNT_RELOAD_REQ", + "number": 10003, + "extend": { + "desc": [ + "重登账号请求" + ] + }, + "alias": "NID_LOGIN_ACCOUNT_RELOAD_REQ" + }, + { + "name": "NID_LOGIN_ACCOUNT_RELOAD_RES", + "full_name": "ncmd_cs.NID_LOGIN_ACCOUNT_RELOAD_RES", + "number": 10004, + "extend": {}, + "alias": "NID_LOGIN_ACCOUNT_RELOAD_RES" + }, + { + "name": "NID_LOGIN_RANDOM_NAME_REQ", + "full_name": "ncmd_cs.NID_LOGIN_RANDOM_NAME_REQ", + "number": 10005, + "extend": { + "desc": [ + "随机名字请求" + ] + }, + "alias": "NID_LOGIN_RANDOM_NAME_REQ" + }, + { + "name": "NID_LOGIN_RANDOM_NAME_RES", + "full_name": "ncmd_cs.NID_LOGIN_RANDOM_NAME_RES", + "number": 10006, + "extend": {}, + "alias": "NID_LOGIN_RANDOM_NAME_RES" + }, + { + "name": "NID_LOGIN_ROLE_CREATE_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_CREATE_REQ", + "number": 10007, + "extend": { + "desc": [ + "创建角色请求" + ] + }, + "alias": "NID_LOGIN_ROLE_CREATE_REQ" + }, + { + "name": "NID_LOGIN_ROLE_CREATE_RES", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_CREATE_RES", + "number": 10008, + "extend": {}, + "alias": "NID_LOGIN_ROLE_CREATE_RES" + }, + { + "name": "NID_LOGIN_ROLE_CHOOSE_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_CHOOSE_REQ", + "number": 10009, + "extend": { + "desc": [ + "选择角色请求" + ] + }, + "alias": "NID_LOGIN_ROLE_CHOOSE_REQ" + }, + { + "name": "NID_LOGIN_ROLE_CHOOSE_RES", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_CHOOSE_RES", + "number": 10010, + "extend": {}, + "alias": "NID_LOGIN_ROLE_CHOOSE_RES" + }, + { + "name": "NID_LOGIN_ROLE_DELETE_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_DELETE_REQ", + "number": 10011, + "extend": { + "desc": [ + "删除角色请求" + ] + }, + "alias": "NID_LOGIN_ROLE_DELETE_REQ" + }, + { + "name": "NID_LOGIN_ROLE_DELETE_RES", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_DELETE_RES", + "number": 10012, + "extend": {}, + "alias": "NID_LOGIN_ROLE_DELETE_RES" + }, + { + "name": "NID_LOGIN_ROLE_LOGIN_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_LOGIN_REQ", + "number": 10013, + "extend": { + "desc": [ + "登陆角色请求" + ] + }, + "alias": "NID_LOGIN_ROLE_LOGIN_REQ" + }, + { + "name": "NID_LOGIN_ROLE_LOGIN_RES", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_LOGIN_RES", + "number": 10014, + "extend": {}, + "alias": "NID_LOGIN_ROLE_LOGIN_RES" + }, + { + "name": "NID_LOGIN_ROLE_LOGOUT_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_LOGOUT_REQ", + "number": 10015, + "extend": { + "desc": [ + "登出角色请求" + ] + }, + "alias": "NID_LOGIN_ROLE_LOGOUT_REQ" + }, + { + "name": "NID_LOGIN_ROLE_LOGOUT_RES", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_LOGOUT_RES", + "number": 10016, + "extend": {}, + "alias": "NID_LOGIN_ROLE_LOGOUT_RES" + }, + { + "name": "NID_LOGIN_ROLE_RELOAD_REQ", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_RELOAD_REQ", + "number": 10017, + "extend": { + "desc": [ + "重登角色请求" + ] + }, + "alias": "NID_LOGIN_ROLE_RELOAD_REQ" + }, + { + "name": "NID_LOGIN_ROLE_RELOAD_RES", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_RELOAD_RES", + "number": 10018, + "extend": {}, + "alias": "NID_LOGIN_ROLE_RELOAD_RES" + }, + { + "name": "NID_LOGIN_ROLE_KICKOUT_NTF", + "full_name": "ncmd_cs.NID_LOGIN_ROLE_KICKOUT_NTF", + "number": 10019, + "extend": { + "desc": [ + "踢玩家下线通知" + ] + }, + "alias": "NID_LOGIN_ROLE_KICKOUT_NTF" + } + ] + } + ], + "messages": [] + }, + { + "filename": "login.proto", + "package": "ncmd_cs", + "syntax": 3, + "dependency": [], + "enums": [ + { + "name": "platform_type", + "full_name": "ncmd_cs.platform_type", + "extend": { + "desc": [ + "平台定义" + ] + }, + "alias": "platform_type", + "values": [ + { + "name": "PLATFORM_GUEST", + "full_name": "ncmd_cs.PLATFORM_GUEST", + "number": 0, + "extend": { + "desc": [ + "游客" + ] + }, + "alias": "PLATFORM_GUEST" + }, + { + "name": "PLATFORM_PASSWORD", + "full_name": "ncmd_cs.PLATFORM_PASSWORD", + "number": 1, + "extend": { + "desc": [ + "PASSWORD" + ] + }, + "alias": "PLATFORM_PASSWORD" + } + ] + } + ], + "messages": [ + { + "name": "role_info", + "full_name": "ncmd_cs.role_info", + "extend": { + "desc": [ + "角色信息" + ] + }, + "alias": "role_info", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "role_id", + "full_name": "ncmd_cs.role_info.role_id", + "number": 1, + "extend": { + "desc": [ + "角色id" + ] + }, + "alias": "role_id" + }, + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "name", + "full_name": "ncmd_cs.role_info.name", + "number": 2, + "extend": { + "desc": [ + "角色名字" + ] + }, + "alias": "name" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "gender", + "full_name": "ncmd_cs.role_info.gender", + "number": 3, + "extend": { + "desc": [ + "角色性别" + ] + }, + "alias": "gender" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "model", + "full_name": "ncmd_cs.role_info.model", + "number": 4, + "extend": { + "desc": [ + "模型ID" + ] + }, + "alias": "model" + } + ] + }, + { + "name": "login_account_login_req", + "full_name": "ncmd_cs.login_account_login_req", + "extend": { + "desc": [ + "NID_LOGIN_ACCOUNT_LOGIN_REQ" + ] + }, + "alias": "login_account_login_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "openid", + "full_name": "ncmd_cs.login_account_login_req.openid", + "number": 1, + "extend": { + "desc": [ + "第三方账号openid" + ] + }, + "alias": "openid" + }, + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "session", + "full_name": "ncmd_cs.login_account_login_req.session", + "number": 2, + "extend": { + "desc": [ + "第三方账号登陆验证session" + ] + }, + "alias": "session" + }, + { + "is_repeated": false, + "type": "TYPE_ENUM", + "type_name": "ncmd_cs.platform_type", + "name": "platform", + "full_name": "ncmd_cs.login_account_login_req.platform", + "number": 3, + "extend": { + "desc": [ + "平台类型" + ] + }, + "alias": "platform" + }, + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "device_id", + "full_name": "ncmd_cs.login_account_login_req.device_id", + "number": 9, + "extend": { + "desc": [ + "设备id" + ] + }, + "alias": "device_id" + } + ] + }, + { + "name": "login_account_login_res", + "full_name": "ncmd_cs.login_account_login_res", + "extend": { + "desc": [ + "NID_LOGIN_ACCOUNT_LOGIN_RES" + ] + }, + "alias": "login_account_login_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_account_login_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "user_id", + "full_name": "ncmd_cs.login_account_login_res.user_id", + "number": 2, + "extend": { + "desc": [ + "第三方账号openid" + ] + }, + "alias": "user_id" + }, + { + "is_repeated": true, + "type": "TYPE_MESSAGE", + "type_name": "ncmd_cs.role_info", + "name": "roles", + "full_name": "ncmd_cs.login_account_login_res.roles", + "number": 3, + "extend": { + "desc": [ + "账号角色列表" + ] + }, + "alias": "roles" + } + ] + }, + { + "name": "login_account_reload_req", + "full_name": "ncmd_cs.login_account_reload_req", + "extend": { + "desc": [ + "NID_LOGIN_ACCOUNT_RELOAD_REQ" + ] + }, + "alias": "login_account_reload_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "openid", + "full_name": "ncmd_cs.login_account_reload_req.openid", + "number": 1, + "extend": { + "desc": [ + "第三方账号openid" + ] + }, + "alias": "openid" + }, + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "session", + "full_name": "ncmd_cs.login_account_reload_req.session", + "number": 2, + "extend": { + "desc": [ + "第三方账号登陆验证session" + ] + }, + "alias": "session" + }, + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "device_id", + "full_name": "ncmd_cs.login_account_reload_req.device_id", + "number": 3, + "extend": { + "desc": [ + "设备id" + ] + }, + "alias": "device_id" + } + ] + }, + { + "name": "login_account_reload_res", + "full_name": "ncmd_cs.login_account_reload_res", + "extend": { + "desc": [ + "NID_LOGIN_ACCOUNT_RELOAD_RES" + ] + }, + "alias": "login_account_reload_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_account_reload_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "user_id", + "full_name": "ncmd_cs.login_account_reload_res.user_id", + "number": 2, + "extend": { + "desc": [ + "角色账号ID" + ] + }, + "alias": "user_id" + }, + { + "is_repeated": true, + "type": "TYPE_MESSAGE", + "type_name": "ncmd_cs.role_info", + "name": "roles", + "full_name": "ncmd_cs.login_account_reload_res.roles", + "number": 3, + "extend": { + "desc": [ + "账号角色列表" + ] + }, + "alias": "roles" + } + ] + }, + { + "name": "login_random_name_req", + "full_name": "ncmd_cs.login_random_name_req", + "extend": { + "desc": [ + "NID_LOGIN_RANDOM_NAME_REQ" + ] + }, + "alias": "login_random_name_req", + "fields": [] + }, + { + "name": "login_random_name_res", + "full_name": "ncmd_cs.login_random_name_res", + "extend": { + "desc": [ + "NID_LOGIN_RANDOM_NAME_RES" + ] + }, + "alias": "login_random_name_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_random_name_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + }, + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "name", + "full_name": "ncmd_cs.login_random_name_res.name", + "number": 2, + "extend": { + "desc": [ + "随机名字" + ] + }, + "alias": "name" + } + ] + }, + { + "name": "rolemodel", + "full_name": "ncmd_cs.rolemodel", + "extend": { + "desc": [ + "捏脸数据定义" + ] + }, + "alias": "rolemodel", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "model", + "full_name": "ncmd_cs.rolemodel.model", + "number": 1, + "extend": { + "desc": [ + "模型ID" + ] + }, + "alias": "model" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "color", + "full_name": "ncmd_cs.rolemodel.color", + "number": 2, + "extend": { + "desc": [ + "肤色" + ] + }, + "alias": "color" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "head", + "full_name": "ncmd_cs.rolemodel.head", + "number": 3, + "extend": { + "desc": [ + "头模ID" + ] + }, + "alias": "head" + } + ] + }, + { + "name": "login_role_create_req", + "full_name": "ncmd_cs.login_role_create_req", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_CREATE_REQ" + ] + }, + "alias": "login_role_create_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "user_id", + "full_name": "ncmd_cs.login_role_create_req.user_id", + "number": 1, + "extend": { + "desc": [ + "账号id" + ] + }, + "alias": "user_id" + }, + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "name", + "full_name": "ncmd_cs.login_role_create_req.name", + "number": 2, + "extend": { + "desc": [ + "名字" + ] + }, + "alias": "name" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "gender", + "full_name": "ncmd_cs.login_role_create_req.gender", + "number": 3, + "extend": { + "desc": [ + "性别" + ] + }, + "alias": "gender" + }, + { + "is_repeated": false, + "type": "TYPE_BYTES", + "type_name": "bytes", + "name": "custom", + "full_name": "ncmd_cs.login_role_create_req.custom", + "number": 4, + "extend": { + "desc": [ + "捏脸数据,rolemodel" + ] + }, + "alias": "custom" + } + ] + }, + { + "name": "login_role_create_res", + "full_name": "ncmd_cs.login_role_create_res", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_CREATE_RES" + ] + }, + "alias": "login_role_create_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_role_create_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + }, + { + "is_repeated": false, + "type": "TYPE_MESSAGE", + "type_name": "ncmd_cs.role_info", + "name": "role", + "full_name": "ncmd_cs.login_role_create_res.role", + "number": 2, + "extend": { + "desc": [ + "新角色信息" + ] + }, + "alias": "role" + } + ] + }, + { + "name": "login_role_choose_req", + "full_name": "ncmd_cs.login_role_choose_req", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_CHOOSE_REQ" + ] + }, + "alias": "login_role_choose_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "user_id", + "full_name": "ncmd_cs.login_role_choose_req.user_id", + "number": 1, + "extend": { + "desc": [ + "账号id" + ] + }, + "alias": "user_id" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "role_id", + "full_name": "ncmd_cs.login_role_choose_req.role_id", + "number": 2, + "extend": { + "desc": [ + "角色id" + ] + }, + "alias": "role_id" + } + ] + }, + { + "name": "login_role_choose_res", + "full_name": "ncmd_cs.login_role_choose_res", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_CHOOSE_RES" + ] + }, + "alias": "login_role_choose_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_role_choose_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + }, + { + "is_repeated": true, + "type": "TYPE_STRING", + "type_name": "string", + "name": "addrs", + "full_name": "ncmd_cs.login_role_choose_res.addrs", + "number": 2, + "extend": { + "desc": [ + "gateway ips" + ] + }, + "alias": "addrs" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "port", + "full_name": "ncmd_cs.login_role_choose_res.port", + "number": 3, + "extend": { + "desc": [ + "gateway port" + ] + }, + "alias": "port" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "lobby", + "full_name": "ncmd_cs.login_role_choose_res.lobby", + "number": 4, + "extend": { + "desc": [ + "lobby id" + ] + }, + "alias": "lobby" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "token", + "full_name": "ncmd_cs.login_role_choose_res.token", + "number": 5, + "extend": { + "desc": [ + "验证token" + ] + }, + "alias": "token" + } + ] + }, + { + "name": "login_role_delete_req", + "full_name": "ncmd_cs.login_role_delete_req", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_DELETE_REQ" + ] + }, + "alias": "login_role_delete_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "user_id", + "full_name": "ncmd_cs.login_role_delete_req.user_id", + "number": 1, + "extend": { + "desc": [ + "账号id" + ] + }, + "alias": "user_id" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "role_id", + "full_name": "ncmd_cs.login_role_delete_req.role_id", + "number": 2, + "extend": { + "desc": [ + "角色id" + ] + }, + "alias": "role_id" + } + ] + }, + { + "name": "login_role_delete_res", + "full_name": "ncmd_cs.login_role_delete_res", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_DELETE_RES" + ] + }, + "alias": "login_role_delete_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_role_delete_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + } + ] + }, + { + "name": "login_role_login_req", + "full_name": "ncmd_cs.login_role_login_req", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_LOGIN_REQ" + ] + }, + "alias": "login_role_login_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "open_id", + "full_name": "ncmd_cs.login_role_login_req.open_id", + "number": 1, + "extend": { + "desc": [ + "open_id" + ] + }, + "alias": "open_id" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "role_id", + "full_name": "ncmd_cs.login_role_login_req.role_id", + "number": 2, + "extend": { + "desc": [ + "角色id" + ] + }, + "alias": "role_id" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "lobby", + "full_name": "ncmd_cs.login_role_login_req.lobby", + "number": 3, + "extend": { + "desc": [ + "lobby id" + ] + }, + "alias": "lobby" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "token", + "full_name": "ncmd_cs.login_role_login_req.token", + "number": 4, + "extend": { + "desc": [ + "验证token" + ] + }, + "alias": "token" + } + ] + }, + { + "name": "login_role_login_res", + "full_name": "ncmd_cs.login_role_login_res", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_LOGIN_RES" + ] + }, + "alias": "login_role_login_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_role_login_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "token", + "full_name": "ncmd_cs.login_role_login_res.token", + "number": 2, + "extend": { + "desc": [ + "重连token" + ] + }, + "alias": "token" + } + ] + }, + { + "name": "login_role_logout_req", + "full_name": "ncmd_cs.login_role_logout_req", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_LOGOUT_REQ" + ] + }, + "alias": "login_role_logout_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "role_id", + "full_name": "ncmd_cs.login_role_logout_req.role_id", + "number": 1, + "extend": { + "desc": [ + "角色id" + ] + }, + "alias": "role_id" + } + ] + }, + { + "name": "login_role_logout_res", + "full_name": "ncmd_cs.login_role_logout_res", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_LOGOUT_RES" + ] + }, + "alias": "login_role_logout_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_role_logout_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + } + ] + }, + { + "name": "login_role_reload_req", + "full_name": "ncmd_cs.login_role_reload_req", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_RELOAD_REQ" + ] + }, + "alias": "login_role_reload_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_STRING", + "type_name": "string", + "name": "open_id", + "full_name": "ncmd_cs.login_role_reload_req.open_id", + "number": 1, + "extend": { + "desc": [ + "open_id" + ] + }, + "alias": "open_id" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "role_id", + "full_name": "ncmd_cs.login_role_reload_req.role_id", + "number": 2, + "extend": { + "desc": [ + "角色id" + ] + }, + "alias": "role_id" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "lobby", + "full_name": "ncmd_cs.login_role_reload_req.lobby", + "number": 3, + "extend": { + "desc": [ + "lobby id" + ] + }, + "alias": "lobby" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "token", + "full_name": "ncmd_cs.login_role_reload_req.token", + "number": 4, + "extend": { + "desc": [ + "验证token" + ] + }, + "alias": "token" + } + ] + }, + { + "name": "login_role_reload_res", + "full_name": "ncmd_cs.login_role_reload_res", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_RELOAD_RES" + ] + }, + "alias": "login_role_reload_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "error_code", + "full_name": "ncmd_cs.login_role_reload_res.error_code", + "number": 1, + "extend": { + "desc": [ + "错误码" + ] + }, + "alias": "error_code" + }, + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "token", + "full_name": "ncmd_cs.login_role_reload_res.token", + "number": 2, + "extend": { + "desc": [ + "重连token" + ] + }, + "alias": "token" + } + ] + }, + { + "name": "login_role_kickout_ntf", + "full_name": "ncmd_cs.login_role_kickout_ntf", + "extend": { + "desc": [ + "NID_LOGIN_ROLE_KICKOUT_NTF" + ] + }, + "alias": "login_role_kickout_ntf", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "reason", + "full_name": "ncmd_cs.login_role_kickout_ntf.reason", + "number": 1, + "extend": { + "desc": [ + "原因" + ] + }, + "alias": "reason" + } + ] + } + ] + }, + { + "filename": "errorcode.proto", + "package": "ncmd_cs", + "syntax": 3, + "dependency": [], + "enums": [ + { + "name": "ErrorCode", + "full_name": "ncmd_cs.ErrorCode", + "extend": { + "desc": [ + "ErrorCode" + ] + }, + "alias": "ErrorCode", + "values": [ + { + "name": "FRAME_SUCCESS", + "full_name": "ncmd_cs.FRAME_SUCCESS", + "number": 0, + "extend": { + "desc": [ + "框架错误码", + "成功" + ] + }, + "alias": "FRAME_SUCCESS" + }, + { + "name": "FRAME_FAILED", + "full_name": "ncmd_cs.FRAME_FAILED", + "number": 1, + "extend": { + "desc": [ + "系统错误,请重试" + ] + }, + "alias": "FRAME_FAILED" + }, + { + "name": "FRAME_TOOFAST", + "full_name": "ncmd_cs.FRAME_TOOFAST", + "number": 2, + "extend": { + "desc": [ + "请求太频繁" + ] + }, + "alias": "FRAME_TOOFAST" + }, + { + "name": "FRAME_PARAMS", + "full_name": "ncmd_cs.FRAME_PARAMS", + "number": 3, + "extend": { + "desc": [ + "参数错误,请重试" + ] + }, + "alias": "FRAME_PARAMS" + }, + { + "name": "FRAME_UPHOLD", + "full_name": "ncmd_cs.FRAME_UPHOLD", + "number": 4, + "extend": { + "desc": [ + "服务维护" + ] + }, + "alias": "FRAME_UPHOLD" + }, + { + "name": "LOGIN_PLATFORM_ERROR", + "full_name": "ncmd_cs.LOGIN_PLATFORM_ERROR", + "number": 1001, + "extend": { + "desc": [ + "登陆错误码", + "平台错误" + ] + }, + "alias": "LOGIN_PLATFORM_ERROR" + }, + { + "name": "LOGIN_VERIFY_FAILED", + "full_name": "ncmd_cs.LOGIN_VERIFY_FAILED", + "number": 1002, + "extend": { + "desc": [ + "验证失败" + ] + }, + "alias": "LOGIN_VERIFY_FAILED" + }, + { + "name": "LOGIN_SERVER_UPHOLD", + "full_name": "ncmd_cs.LOGIN_SERVER_UPHOLD", + "number": 1003, + "extend": { + "desc": [ + "服务器维护" + ] + }, + "alias": "LOGIN_SERVER_UPHOLD" + }, + { + "name": "LOGIN_ACCOUTN_BANS", + "full_name": "ncmd_cs.LOGIN_ACCOUTN_BANS", + "number": 1004, + "extend": { + "desc": [ + "账号被封" + ] + }, + "alias": "LOGIN_ACCOUTN_BANS" + }, + { + "name": "LOGIN_ACCOUTN_INLINE", + "full_name": "ncmd_cs.LOGIN_ACCOUTN_INLINE", + "number": 1005, + "extend": { + "desc": [ + "账号已经登陆" + ] + }, + "alias": "LOGIN_ACCOUTN_INLINE" + }, + { + "name": "LOGIN_ACCOUTN_OFFLINE", + "full_name": "ncmd_cs.LOGIN_ACCOUTN_OFFLINE", + "number": 1006, + "extend": { + "desc": [ + "账号未登陆" + ] + }, + "alias": "LOGIN_ACCOUTN_OFFLINE" + }, + { + "name": "LOGIN_ROLE_NOT_EXIST", + "full_name": "ncmd_cs.LOGIN_ROLE_NOT_EXIST", + "number": 1007, + "extend": { + "desc": [ + "角色不存在" + ] + }, + "alias": "LOGIN_ROLE_NOT_EXIST" + }, + { + "name": "LOGIN_ROLE_NUM_LIMIT", + "full_name": "ncmd_cs.LOGIN_ROLE_NUM_LIMIT", + "number": 1008, + "extend": { + "desc": [ + "角色数量超过上限" + ] + }, + "alias": "LOGIN_ROLE_NUM_LIMIT" + }, + { + "name": "LOGIN_ROLE_NAME_EXIST", + "full_name": "ncmd_cs.LOGIN_ROLE_NAME_EXIST", + "number": 1009, + "extend": { + "desc": [ + "角色名字重复" + ] + }, + "alias": "LOGIN_ROLE_NAME_EXIST" + }, + { + "name": "LOGIN_ROLE_IS_INLINE", + "full_name": "ncmd_cs.LOGIN_ROLE_IS_INLINE", + "number": 1020, + "extend": { + "desc": [ + "角色已经登陆" + ] + }, + "alias": "LOGIN_ROLE_IS_INLINE" + }, + { + "name": "LOGIN_ROLE_IS_OFFLINE", + "full_name": "ncmd_cs.LOGIN_ROLE_IS_OFFLINE", + "number": 1021, + "extend": { + "desc": [ + "角色未登陆" + ] + }, + "alias": "LOGIN_ROLE_IS_OFFLINE" + }, + { + "name": "LOGIN_ROLE_TOKEN_ERR", + "full_name": "ncmd_cs.LOGIN_ROLE_TOKEN_ERR", + "number": 1022, + "extend": { + "desc": [ + "TOKEN验证失败" + ] + }, + "alias": "LOGIN_ROLE_TOKEN_ERR" + }, + { + "name": "KICK_DEVICE_REPLACE", + "full_name": "ncmd_cs.KICK_DEVICE_REPLACE", + "number": 1051, + "extend": { + "desc": [ + "踢出错误码", + "其他设备登陆" + ] + }, + "alias": "KICK_DEVICE_REPLACE" + }, + { + "name": "KICK_SERVER_UPHOLD", + "full_name": "ncmd_cs.KICK_SERVER_UPHOLD", + "number": 1052, + "extend": { + "desc": [ + "服务器维护" + ] + }, + "alias": "KICK_SERVER_UPHOLD" + }, + { + "name": "KICK_ACCOUTN_BANS", + "full_name": "ncmd_cs.KICK_ACCOUTN_BANS", + "number": 1053, + "extend": { + "desc": [ + "账号被封" + ] + }, + "alias": "KICK_ACCOUTN_BANS" + } + ] + } + ], + "messages": [] + }, + { + "filename": "common.proto", + "package": "ncmd_cs", + "syntax": 3, + "dependency": [], + "enums": [], + "messages": [ + { + "name": "heartbeat_req", + "full_name": "ncmd_cs.heartbeat_req", + "extend": { + "desc": [ + "NID_HEARTBEAT_REQ" + ] + }, + "alias": "heartbeat_req", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "serial", + "full_name": "ncmd_cs.heartbeat_req.serial", + "number": 1, + "extend": { + "desc": [ + "心跳序列号" + ] + }, + "alias": "serial" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "time", + "full_name": "ncmd_cs.heartbeat_req.time", + "number": 2, + "extend": { + "desc": [ + "客户端时间" + ] + }, + "alias": "time" + } + ] + }, + { + "name": "heartbeat_res", + "full_name": "ncmd_cs.heartbeat_res", + "extend": { + "desc": [ + "NID_HEARTBEAT_RES" + ] + }, + "alias": "heartbeat_res", + "fields": [ + { + "is_repeated": false, + "type": "TYPE_UINT32", + "type_name": "uint32", + "name": "serial", + "full_name": "ncmd_cs.heartbeat_res.serial", + "number": 1, + "extend": { + "desc": [ + "心跳序列号" + ] + }, + "alias": "serial" + }, + { + "is_repeated": false, + "type": "TYPE_UINT64", + "type_name": "uint64", + "name": "time", + "full_name": "ncmd_cs.heartbeat_res.time", + "number": 2, + "extend": { + "desc": [ + "服务器时间" + ] + }, + "alias": "time" + } + ] + } + ] + } + ], + "indexs": { + "file_idxs": { + "common.proto": 4, + "errorcode.proto": 3, + "login.proto": 2, + "ncmd_cs.proto": 1, + "robot.proto": 0 + }, + "enum_idxs": { + "ncmd_cs.BtNodeId": [ + 0, + 0 + ], + "ncmd_cs.ErrorCode": [ + 3, + 0 + ], + "ncmd_cs.NCmdId": [ + 1, + 0 + ], + "ncmd_cs.platform_type": [ + 2, + 0 + ] + }, + "message_idxs": { + "ncmd_cs.heartbeat_req": [ + 4, + 0 + ], + "ncmd_cs.heartbeat_res": [ + 4, + 1 + ], + "ncmd_cs.login_account_login_req": [ + 2, + 1 + ], + "ncmd_cs.login_account_login_res": [ + 2, + 2 + ], + "ncmd_cs.login_account_reload_req": [ + 2, + 3 + ], + "ncmd_cs.login_account_reload_res": [ + 2, + 4 + ], + "ncmd_cs.login_random_name_req": [ + 2, + 5 + ], + "ncmd_cs.login_random_name_res": [ + 2, + 6 + ], + "ncmd_cs.login_role_choose_req": [ + 2, + 10 + ], + "ncmd_cs.login_role_choose_res": [ + 2, + 11 + ], + "ncmd_cs.login_role_create_req": [ + 2, + 8 + ], + "ncmd_cs.login_role_create_res": [ + 2, + 9 + ], + "ncmd_cs.login_role_delete_req": [ + 2, + 12 + ], + "ncmd_cs.login_role_delete_res": [ + 2, + 13 + ], + "ncmd_cs.login_role_kickout_ntf": [ + 2, + 20 + ], + "ncmd_cs.login_role_login_req": [ + 2, + 14 + ], + "ncmd_cs.login_role_login_res": [ + 2, + 15 + ], + "ncmd_cs.login_role_logout_req": [ + 2, + 16 + ], + "ncmd_cs.login_role_logout_res": [ + 2, + 17 + ], + "ncmd_cs.login_role_reload_req": [ + 2, + 18 + ], + "ncmd_cs.login_role_reload_res": [ + 2, + 19 + ], + "ncmd_cs.role_info": [ + 2, + 0 + ], + "ncmd_cs.rolemodel": [ + 2, + 7 + ] + } + } +} \ No newline at end of file diff --git a/core/luabus/src/lua_socket_mgr.cpp b/core/luabus/src/lua_socket_mgr.cpp index ae4df70a..ea1e8c8d 100644 --- a/core/luabus/src/lua_socket_mgr.cpp +++ b/core/luabus/src/lua_socket_mgr.cpp @@ -16,13 +16,12 @@ int lua_socket_mgr::listen(lua_State* L, const char* ip, int port) { } std::string err; - eproto_type proto_type = (eproto_type)luaL_optinteger(L, 3, 0); - int token = m_mgr->listen(err, ip, port, proto_type); + int token = m_mgr->listen(err, ip, port); if (token == 0) { return luakit::variadic_return(L, nullptr, err); } - auto listener = new lua_socket_node(token, m_lvm, m_mgr, m_router, true, proto_type); + auto listener = new lua_socket_node(token, m_lvm, m_mgr, m_router, true); listener->set_codec(m_codec.get()); return luakit::variadic_return(L, listener, "ok"); } @@ -33,13 +32,12 @@ int lua_socket_mgr::connect(lua_State* L, const char* ip, const char* port, int } std::string err; - eproto_type proto_type = (eproto_type)luaL_optinteger(L, 4, 0); - int token = m_mgr->connect(err, ip, port, timeout, proto_type); + int token = m_mgr->connect(err, ip, port, timeout); if (token == 0) { return luakit::variadic_return(L, nullptr, err); } - auto socket_node = new lua_socket_node(token, m_lvm, m_mgr, m_router, false, proto_type); + auto socket_node = new lua_socket_node(token, m_lvm, m_mgr, m_router, false); socket_node->set_codec(m_codec.get()); return luakit::variadic_return(L, socket_node, "ok"); } @@ -52,7 +50,10 @@ int lua_socket_mgr::get_recvbuf_size(uint32_t token) { return m_mgr->get_recvbuf_size(token); } +void lua_socket_mgr::set_proto_type(uint32_t token, eproto_type type) { + return m_mgr->set_proto_type(token, type); +} + int lua_socket_mgr::map_token(uint32_t node_id, uint32_t token) { return m_router->map_token(node_id, token); } - diff --git a/core/luabus/src/lua_socket_mgr.h b/core/luabus/src/lua_socket_mgr.h index 0556b13d..72d78f16 100644 --- a/core/luabus/src/lua_socket_mgr.h +++ b/core/luabus/src/lua_socket_mgr.h @@ -16,6 +16,7 @@ struct lua_socket_mgr final int map_token(uint32_t node_id, uint32_t token); int listen(lua_State* L, const char* ip, int port); int connect(lua_State* L, const char* ip, const char* port, int timeout); + void set_proto_type(uint32_t token, eproto_type type); std::shared_ptr get_router() { return m_router; } diff --git a/core/luabus/src/lua_socket_node.cpp b/core/luabus/src/lua_socket_node.cpp index e06f7961..9564150f 100644 --- a/core/luabus/src/lua_socket_node.cpp +++ b/core/luabus/src/lua_socket_node.cpp @@ -1,14 +1,14 @@ #include "stdafx.h" #include "lua_socket_node.h" -lua_socket_node::lua_socket_node(uint32_t token, lua_State* L, std::shared_ptr& mgr, std::shared_ptr& router - , bool blisten, eproto_type proto_type) : m_token(token), m_mgr(mgr), m_router(router), m_proto_type(proto_type) { +lua_socket_node::lua_socket_node(uint32_t token, lua_State* L, std::shared_ptr& mgr, std::shared_ptr& router, bool blisten) + : m_token(token), m_mgr(mgr), m_router(router) { m_stoken = (m_token & 0xffff) << 16; m_luakit = std::make_shared(L); m_mgr->get_remote_ip(m_token, m_ip); if (blisten) { - m_mgr->set_accept_callback(token, [=](uint32_t steam_token, eproto_type proto_type) { - auto node = new lua_socket_node(steam_token, m_luakit->L(), m_mgr, m_router, false, proto_type); + m_mgr->set_accept_callback(token, [=](uint32_t steam_token) { + auto node = new lua_socket_node(steam_token, m_luakit->L(), m_mgr, m_router, false); node->set_codec(m_codec); m_luakit->object_call(this, "on_accept", nullptr, std::tie(), node); }); @@ -23,8 +23,8 @@ lua_socket_node::lua_socket_node(uint32_t token, lua_State* L, std::shared_ptrobject_call(this, "on_error", nullptr, std::tie(), token, err); }); - m_mgr->set_package_callback(token, [=](slice* data){ - return on_recv(data); + m_mgr->set_package_callback(token, [=](slice* data, eproto_type proto_type){ + return on_recv(data, proto_type); }); } @@ -199,14 +199,14 @@ int lua_socket_node::transfer_hash(lua_State* L, uint32_t session_id, uint16_t s return 0; } -int lua_socket_node::on_recv(slice* slice) { - if (eproto_type::proto_head == m_proto_type) { +int lua_socket_node::on_recv(slice* slice, eproto_type proto_type) { + if (eproto_type::proto_head == proto_type) { return on_call_head(slice); } - if (eproto_type::proto_text == m_proto_type) { + if (eproto_type::proto_text == proto_type) { return on_call_text(slice); } - if (eproto_type::proto_rpc != m_proto_type) { + if (eproto_type::proto_rpc != proto_type) { return on_call_data(slice); } diff --git a/core/luabus/src/lua_socket_node.h b/core/luabus/src/lua_socket_node.h index 35df5957..22baf93f 100644 --- a/core/luabus/src/lua_socket_node.h +++ b/core/luabus/src/lua_socket_node.h @@ -8,17 +8,17 @@ class lua_socket_node { public: - lua_socket_node(uint32_t token, lua_State* L, std::shared_ptr& mgr, std::shared_ptr& router - , bool blisten = false, eproto_type proto_type = eproto_type::proto_rpc); + lua_socket_node(uint32_t token, lua_State* L, std::shared_ptr& mgr, std::shared_ptr& router, bool blisten = false); ~lua_socket_node(); void close(); uint32_t build_session_id() { return m_stoken | m_sindex++; } uint32_t get_route_count() { return m_router->get_route_count(); } + void set_codec(codec_base* codec) { m_codec = codec; } void set_timeout(int ms) { m_mgr->set_timeout(m_token, ms); } void set_nodelay(bool flag) { m_mgr->set_nodelay(m_token, flag); } - void set_codec(codec_base* codec) { m_codec = codec; } + void set_proto_type(eproto_type proto_type) { m_mgr->set_proto_type(m_token, proto_type); } int call_data(lua_State* L); int call(lua_State* L, uint32_t session_id, uint8_t flag); @@ -59,10 +59,10 @@ class lua_socket_node uint16_t m_sindex = 1; private: - int on_recv(slice* slice); int on_call_head(slice* slice); int on_call_text(slice* slice); int on_call_data(slice* slice); + int on_recv(slice* slice, eproto_type proto_type); void on_call(router_header* header, slice* slice); void on_transfer(transfer_header* header, slice* slice); void on_forward_broadcast(router_header* header, size_t target_size); @@ -72,5 +72,4 @@ class lua_socket_node std::shared_ptr m_mgr; std::shared_ptr m_luakit; std::shared_ptr m_router; - eproto_type m_proto_type; }; diff --git a/core/luabus/src/luabus.cpp b/core/luabus/src/luabus.cpp index 6895e8aa..b00a70b6 100644 --- a/core/luabus/src/luabus.cpp +++ b/core/luabus/src/luabus.cpp @@ -43,6 +43,7 @@ namespace luabus { lluabus.set_function("create_socket_mgr", create_socket_mgr); lluabus.new_enum("eproto_type", "rpc", eproto_type::proto_rpc, + "wss", eproto_type::proto_wss, "head", eproto_type::proto_head, "text", eproto_type::proto_text, "mongo", eproto_type::proto_mongo, @@ -85,6 +86,7 @@ namespace luabus { "transfer_call", &lua_socket_node::transfer_call, "transfer_hash", &lua_socket_node::transfer_hash, "forward_target", &lua_socket_node::forward_target, + "set_proto_type", &lua_socket_node::set_proto_type, "get_route_count", &lua_socket_node::get_route_count, "build_session_id", &lua_socket_node::build_session_id, "forward_transfer", &lua_socket_node::forward_transfer, diff --git a/core/luabus/src/socket_listener.cpp b/core/luabus/src/socket_listener.cpp index 868a2aa3..d126957f 100644 --- a/core/luabus/src/socket_listener.cpp +++ b/core/luabus/src/socket_listener.cpp @@ -4,7 +4,7 @@ #ifdef _MSC_VER socket_listener::socket_listener(socket_mgr* mgr, - LPFN_ACCEPTEX accept_func, LPFN_GETACCEPTEXSOCKADDRS addrs_func, eproto_type proto_type) : m_proto_type(proto_type) { + LPFN_ACCEPTEX accept_func, LPFN_GETACCEPTEXSOCKADDRS addrs_func) { mgr->increase_count(); m_mgr = mgr; m_accept_func = accept_func; @@ -17,8 +17,7 @@ socket_listener::socket_listener(socket_mgr* mgr, #endif #if defined(__linux) || defined(__APPLE__) -socket_listener::socket_listener(socket_mgr* mgr, eproto_type proto_type) : - m_proto_type(proto_type) { +socket_listener::socket_listener(socket_mgr* mgr) { mgr->increase_count(); m_mgr = mgr; } @@ -103,12 +102,12 @@ void socket_listener::on_complete(WSAOVERLAPPED* ovl) { set_no_block(node->fd); - auto token = m_mgr->accept_stream(node->fd, ip, m_accept_cb, m_proto_type); + auto token = m_mgr->accept_stream(node->fd, ip, m_proto_type); if (token == 0) { closesocket(node->fd); } else { - m_accept_cb(token, m_proto_type); + m_accept_cb(token); } node->fd = INVALID_SOCKET; @@ -164,7 +163,7 @@ void socket_listener::queue_accept(WSAOVERLAPPED* ovl) { (*m_addrs_func)(node->buffer, 0, sizeof(node->buffer[0]), sizeof(node->buffer[2]), &local_addr, &local_addr_len, &remote_addr, &remote_addr_len); get_ip_string(ip, sizeof(ip), remote_addr, (size_t)remote_addr_len); - auto token = m_mgr->accept_stream(node->fd, ip, m_accept_cb, m_proto_type); + auto token = m_mgr->accept_stream(node->fd, ip, m_proto_type); if (token == 0) { closesocket(node->fd); node->fd = INVALID_SOCKET; @@ -174,7 +173,7 @@ void socket_listener::queue_accept(WSAOVERLAPPED* ovl) { } node->fd = INVALID_SOCKET; - m_accept_cb(token, m_proto_type); + m_accept_cb(token); } } #endif @@ -202,7 +201,7 @@ void socket_listener::on_can_recv(size_t max_len, bool is_eof) { set_no_delay(fd, 1); set_close_on_exec(fd); - auto token = m_mgr->accept_stream(fd, ip, m_accept_cb, m_proto_type); + auto token = m_mgr->accept_stream(fd, ip); if (token != 0) { m_accept_cb(token, m_proto_type); } diff --git a/core/luabus/src/socket_listener.h b/core/luabus/src/socket_listener.h index eda8dd2d..9c16b353 100644 --- a/core/luabus/src/socket_listener.h +++ b/core/luabus/src/socket_listener.h @@ -13,19 +13,18 @@ struct socket_listener : public socket_object #ifdef _MSC_VER socket_listener(socket_mgr* mgr, LPFN_ACCEPTEX accept_func, - LPFN_GETACCEPTEXSOCKADDRS addrs_func, - eproto_type proto_type = eproto_type::proto_rpc); + LPFN_GETACCEPTEXSOCKADDRS addrs_func); #endif #if defined(__linux) || defined(__APPLE__) - socket_listener(socket_mgr* mgr, eproto_type proto_type = eproto_type::proto_rpc); + socket_listener(socket_mgr* mgr); #endif ~socket_listener(); bool setup(socket_t fd); bool get_remote_ip(std::string& ip) override { return false; } bool update(int64_t now) override; - void set_accept_callback(const std::function& cb) override { m_accept_cb = cb; } + void set_accept_callback(const std::function& cb) override { m_accept_cb = cb; } void set_error_callback(const std::function& cb) override { m_error_cb = cb; } #ifdef _MSC_VER @@ -39,10 +38,9 @@ struct socket_listener : public socket_object private: socket_mgr* m_mgr = nullptr; - eproto_type m_proto_type; socket_t m_socket = INVALID_SOCKET; + std::function m_accept_cb; std::function m_error_cb; - std::function m_accept_cb; #ifdef _MSC_VER struct listen_node diff --git a/core/luabus/src/socket_mgr.cpp b/core/luabus/src/socket_mgr.cpp index bd32f423..f60a9697 100644 --- a/core/luabus/src/socket_mgr.cpp +++ b/core/luabus/src/socket_mgr.cpp @@ -159,18 +159,18 @@ int socket_mgr::wait(int64_t now, int timeout) { return (int)event_count; } -int socket_mgr::listen(std::string& err, const char ip[], int port, eproto_type proto_type) { +int socket_mgr::listen(std::string& err, const char ip[], int port) { int ret = false; socket_t fd = INVALID_SOCKET; sockaddr_storage addr; size_t addr_len = 0; #ifdef _MSC_VER - auto* listener = new socket_listener(this, m_accept_func, m_addrs_func, proto_type); + auto* listener = new socket_listener(this, m_accept_func, m_addrs_func); #endif #if defined(__linux) || defined(__APPLE__) - auto* listener = new socket_listener(this, proto_type); + auto* listener = new socket_listener(this); #endif ret = make_ip_addr(&addr, &addr_len, ip, port); @@ -206,18 +206,18 @@ int socket_mgr::listen(std::string& err, const char ip[], int port, eproto_type return 0; } -int socket_mgr::connect(std::string& err, const char node_name[], const char service_name[], int timeout, eproto_type proto_type) { +int socket_mgr::connect(std::string& err, const char node_name[], const char service_name[], int timeout) { if (is_full()) { err = "too-many-connection"; return 0; } #ifdef _MSC_VER - socket_stream* stm = new socket_stream(this, m_connect_func, proto_type); + socket_stream* stm = new socket_stream(this, m_connect_func); #endif #if defined(__linux) || defined(__APPLE__) - socket_stream* stm = new socket_stream(this, proto_type); + socket_stream* stm = new socket_stream(this); #endif stm->connect(node_name, service_name, timeout); @@ -241,6 +241,13 @@ void socket_mgr::set_nodelay(uint32_t token, int flag) { } } +void socket_mgr::set_proto_type(uint32_t token, eproto_type type) { + auto node = get_object(token); + if (node) { + node->set_proto_type(type); + } +} + void socket_mgr::send(uint32_t token, const void* data, size_t data_len) { auto node = get_object(token); if (node) { @@ -286,7 +293,7 @@ int socket_mgr::get_recvbuf_size(uint32_t token){ return 0; } -void socket_mgr::set_accept_callback(uint32_t token, const std::function& cb) { +void socket_mgr::set_accept_callback(uint32_t token, const std::function& cb) { auto node = get_object(token); if (node) { node->set_accept_callback(cb); @@ -300,7 +307,7 @@ void socket_mgr::set_connect_callback(uint32_t token, const std::function& cb) { +void socket_mgr::set_package_callback(uint32_t token, const std::function& cb) { auto node = get_object(token); if (node) { node->set_package_callback(cb); @@ -411,10 +418,11 @@ bool socket_mgr::watch_send(socket_t fd, socket_object* object, bool enable) { #endif } -int socket_mgr::accept_stream(socket_t fd, const char ip[], const std::function& cb, eproto_type proto_type) { - auto* stm = new socket_stream(this, proto_type); +int socket_mgr::accept_stream(socket_t fd, const char ip[], eproto_type proto_type) { + auto* stm = new socket_stream(this); if (watch_accepted(fd, stm) && stm->accept_socket(fd, ip)) { auto token = new_token(); + stm->set_proto_type(proto_type); m_objects[token] = stm; return token; } diff --git a/core/luabus/src/socket_mgr.h b/core/luabus/src/socket_mgr.h index 596cdca8..8e48edf9 100644 --- a/core/luabus/src/socket_mgr.h +++ b/core/luabus/src/socket_mgr.h @@ -23,11 +23,12 @@ enum class elink_status : int enum class eproto_type : int { proto_rpc = 0, // rpc协议,根据协议头解析 - proto_head = 1, // head协议,根据协议头解析 - proto_text = 2, // text协议,文本协议 - proto_mongo = 3, // mongo协议,协议前4个字节为长度 - proto_mysql = 4, // mysql协议,协议前3个字节为长度 - proto_max = 5, // max + proto_wss = 1, // wss协议,协议前n个字节带长度 + proto_head = 2, // head协议,根据协议头解析 + proto_text = 3, // text协议,文本协议 + proto_mongo = 4, // mongo协议,协议前4个字节为长度 + proto_mysql = 5, // mysql协议,协议前3个字节为长度 + proto_max = 6, // max }; struct sendv_item @@ -49,10 +50,11 @@ struct socket_object virtual void set_nodelay(int flag) { } virtual void send(const void* data, size_t data_len) { } virtual void sendv(const sendv_item items[], int count) { }; - virtual void set_accept_callback(const std::function& cb) { } + virtual void set_proto_type(eproto_type type) { m_proto_type = type; } + virtual void set_accept_callback(const std::function& cb) { } virtual void set_connect_callback(const std::function& cb) { } virtual void set_error_callback(const std::function& cb) { } - virtual void set_package_callback(const std::function& cb) { } + virtual void set_package_callback(const std::function& cb) { } #ifdef _MSC_VER virtual void on_complete(WSAOVERLAPPED* ovl) = 0; @@ -64,6 +66,7 @@ struct socket_object #endif protected: + eproto_type m_proto_type = eproto_type::proto_rpc; elink_status m_link_status = elink_status::link_init; }; @@ -81,8 +84,8 @@ class socket_mgr int wait(int64_t now, int timeout); - int listen(std::string& err, const char ip[], int port, eproto_type proto_type); - int connect(std::string& err, const char node_name[], const char service_name[], int timeout, eproto_type proto_type); + int listen(std::string& err, const char ip[], int port); + int connect(std::string& err, const char node_name[], const char service_name[], int timeout); int get_sendbuf_size(uint32_t token); int get_recvbuf_size(uint32_t token); @@ -92,10 +95,10 @@ class socket_mgr void sendv(uint32_t token, const sendv_item items[], int count); void close(uint32_t token); bool get_remote_ip(uint32_t token, std::string& ip); - - void set_accept_callback(uint32_t token, const std::function& cb); + void set_proto_type(uint32_t token, eproto_type type); + void set_accept_callback(uint32_t token, const std::function& cb); void set_connect_callback(uint32_t token, const std::function& cb); - void set_package_callback(uint32_t token, const std::function& cb); + void set_package_callback(uint32_t token, const std::function& cb); void set_error_callback(uint32_t token, const std::function& cb); bool watch_listen(socket_t fd, socket_object* object); @@ -103,7 +106,7 @@ class socket_mgr bool watch_connecting(socket_t fd, socket_object* object); bool watch_connected(socket_t fd, socket_object* object); bool watch_send(socket_t fd, socket_object* object, bool enable); - int accept_stream(socket_t fd, const char ip[], const std::function& cb, eproto_type proto_type = eproto_type::proto_rpc); + int accept_stream(socket_t fd, const char ip[], eproto_type proto_type); void increase_count() { m_count++; } void decrease_count() { m_count--; } diff --git a/core/luabus/src/socket_stream.cpp b/core/luabus/src/socket_stream.cpp index 3897471a..ffefbe3e 100644 --- a/core/luabus/src/socket_stream.cpp +++ b/core/luabus/src/socket_stream.cpp @@ -5,8 +5,7 @@ #include "socket_router.h" #ifdef _MSC_VER -socket_stream::socket_stream(socket_mgr* mgr, LPFN_CONNECTEX connect_func, eproto_type proto_type) : - m_proto_type(proto_type) { +socket_stream::socket_stream(socket_mgr* mgr, LPFN_CONNECTEX connect_func) { mgr->increase_count(); m_mgr = mgr; m_connect_func = connect_func; @@ -14,10 +13,8 @@ socket_stream::socket_stream(socket_mgr* mgr, LPFN_CONNECTEX connect_func, eprot } #endif -socket_stream::socket_stream(socket_mgr* mgr, eproto_type proto_type) : - m_proto_type(proto_type) { +socket_stream::socket_stream(socket_mgr* mgr) { mgr->increase_count(); - m_proto_type = proto_type; m_mgr = mgr; m_ip[0] = 0; } @@ -394,11 +391,9 @@ void socket_stream::do_send(size_t max_len, bool is_eof) { break; } #endif - #if defined(__linux) || defined(__APPLE__) if (err == EINTR) continue; - if (err == EAGAIN) break; #endif @@ -440,9 +435,8 @@ void socket_stream::do_recv(size_t max_len, bool is_eof) } #endif #if defined(__linux) || defined(__APPLE__) - if (err == EINTR) + if (err == EINTR) continue; - if (err == EAGAIN) break; #endif @@ -467,57 +461,65 @@ void socket_stream::dispatch_package() { while (m_link_status == elink_status::link_connected) { size_t data_len = 0, package_size = 0; auto* data = m_recv_buffer->data(&data_len); - if (eproto_type::proto_rpc == m_proto_type) { - size_t header_len = sizeof(router_header); - auto data = m_recv_buffer->peek_data(header_len); - if (!data) { - break; - } - router_header* header = (router_header*)data; - // 当前包长小于headlen, 关闭连接 - if (header->len < header_len) { - on_error("package-length-err"); - break; + if (data_len == 0) break; + switch (m_proto_type) { + case eproto_type::proto_rpc: { + size_t header_len = sizeof(router_header); + if (!m_recv_buffer->peek_data(header_len)) return; + // 当前包长小于headlen, 关闭连接 + router_header* header = (router_header*)data; + if (header->len < header_len) { + on_error("package-length-err"); + break; + } + package_size = header->len; } - package_size = header->len; - } - else if (eproto_type::proto_head == m_proto_type) { - // pack模式获取socket_header - size_t header_len = sizeof(socket_header); - auto data = m_recv_buffer->peek_data(header_len); - if (!data) { - break; + break; + case eproto_type::proto_head: { + size_t header_len = sizeof(socket_header); + if (!m_recv_buffer->peek_data(header_len)) return; + // 当前包长小于headlen, 关闭连接 + socket_header* header = (socket_header*)data; + if (header->len < header_len) { + on_error("package-length-err"); + return; + } + package_size = header->len; } - socket_header* header = (socket_header*)data; - // 当前包长小于headlen, 关闭连接 - if (header->len < header_len) { - on_error("package-length-err"); - break; + break; + case eproto_type::proto_mongo: { + uint32_t* length = (uint32_t*)m_recv_buffer->peek_data(sizeof(uint32_t)); + if (!length) return; + //package_size = length + contents + package_size = *length; } - package_size = header->len; - } - else if (eproto_type::proto_mongo == m_proto_type) { - uint32_t* length = (uint32_t*)m_recv_buffer->peek_data(sizeof(uint32_t)); - if (!length) { - break; + break; + case eproto_type::proto_mysql: { + uint32_t* length = (uint32_t*)m_recv_buffer->peek_data(sizeof(uint32_t)); + if (!length) return; + //package_size = length + serialize_id + contents + package_size = ((*length) >> 8) + sizeof(uint32_t); } - //package_size = length + contents - package_size = *length; - } - else if (eproto_type::proto_mysql == m_proto_type) { - uint32_t* length = (uint32_t*)m_recv_buffer->peek_data(sizeof(uint32_t)); - if (!length) { - break; + break; + case eproto_type::proto_wss: { + uint16_t* length = (uint16_t*)m_recv_buffer->peek_data(sizeof(uint16_t)); + if (!length) return; + uint16_t payload = (*length) & 0x7f; + if (payload < 0x7e) { + package_size = payload + sizeof(uint16_t); + } else { + size_t* length = (size_t*)m_recv_buffer->peek_data((payload == 0x7f) ? 8 : 2, sizeof(uint16_t)); + if (!length) return; + package_size = (*length) + sizeof(uint16_t); + } } - //package_size = length + serialize_id + contents - package_size = ((*length) >> 8) + sizeof(uint32_t); - } - else if (eproto_type::proto_text == m_proto_type) { - package_size = m_recv_buffer->size(); - if (data_len == 0) break; - } else { - on_error("proto-type-not-suppert!"); break; + case eproto_type::proto_text: + package_size = data_len; + break; + default: + on_error("proto-type-not-suppert!"); + return; } //当前包头标识的数据超过最大长度, 关闭连接 if (package_size > SOCKET_PACKET_MAX) { @@ -526,7 +528,7 @@ void socket_stream::dispatch_package() { } // 数据包还没有收完整 if (data_len < package_size) break; - int read_size = m_package_cb(m_recv_buffer->get_slice(package_size)); + int read_size = m_package_cb(m_recv_buffer->get_slice(package_size), m_proto_type); // 数据包还没有收完整 if (read_size == 0) { break; diff --git a/core/luabus/src/socket_stream.h b/core/luabus/src/socket_stream.h index 848134ab..791adc44 100644 --- a/core/luabus/src/socket_stream.h +++ b/core/luabus/src/socket_stream.h @@ -6,9 +6,9 @@ struct socket_stream : public socket_object { #ifdef _MSC_VER - socket_stream(socket_mgr* mgr, LPFN_CONNECTEX connect_func, eproto_type proto_type = eproto_type::proto_rpc); + socket_stream(socket_mgr* mgr, LPFN_CONNECTEX connect_func); #endif - socket_stream(socket_mgr* mgr, eproto_type proto_type = eproto_type::proto_rpc); + socket_stream(socket_mgr* mgr); ~socket_stream(); bool get_remote_ip(std::string& ip) override; @@ -18,10 +18,10 @@ struct socket_stream : public socket_object bool do_connect(); void try_connect(); void close() override; - void set_accept_callback(const std::function& cb) override { m_accept_cb = cb; } - void set_package_callback(const std::function& cb) override { m_package_cb = cb; } + void set_accept_callback(const std::function& cb) override { m_accept_cb = cb; } void set_error_callback(const std::function& cb) override { m_error_cb = cb; } void set_connect_callback(const std::function& cb) override { m_connect_cb = cb; } + void set_package_callback(const std::function& cb) override { m_package_cb = cb; } void set_timeout(int duration) override { m_timeout = duration; } void set_nodelay(int flag) override { set_no_delay(m_socket, flag); } @@ -51,7 +51,6 @@ struct socket_stream : public socket_object int token = 0; socket_mgr* m_mgr = nullptr; socket_t m_socket = INVALID_SOCKET; - eproto_type m_proto_type = eproto_type::proto_rpc; std::shared_ptr m_recv_buffer = std::make_shared(); std::shared_ptr m_send_buffer = std::make_shared(); @@ -72,8 +71,8 @@ struct socket_stream : public socket_object int m_ovl_ref = 0; #endif - std::function m_package_cb = nullptr; + std::function m_accept_cb = nullptr; std::function m_error_cb = nullptr; - std::function m_accept_cb = nullptr; std::function m_connect_cb = nullptr; + std::function m_package_cb = nullptr; }; diff --git a/extend/lcodec/lcodec.vcxproj b/extend/lcodec/lcodec.vcxproj index 8a378106..b6f1b79d 100644 --- a/extend/lcodec/lcodec.vcxproj +++ b/extend/lcodec/lcodec.vcxproj @@ -13,7 +13,9 @@ + + diff --git a/extend/lcodec/lcodec.vcxproj.filters b/extend/lcodec/lcodec.vcxproj.filters index d203b5db..3f0b2162 100644 --- a/extend/lcodec/lcodec.vcxproj.filters +++ b/extend/lcodec/lcodec.vcxproj.filters @@ -19,9 +19,15 @@ inc + + inc + inc + + inc + diff --git a/extend/lcodec/src/http.h b/extend/lcodec/src/http.h index 8d66f0e5..a0122a3c 100644 --- a/extend/lcodec/src/http.h +++ b/extend/lcodec/src/http.h @@ -20,6 +20,7 @@ namespace lcodec { inline const char* CRLF2 = "\r\n\r\n"; #define SC_UNKNOWN 0 + #define SC_PROTOCOL 101 #define SC_OK 200 #define SC_NOCONTENT 204 #define SC_PARTIAL 206 @@ -84,6 +85,7 @@ namespace lcodec { case SC_OBJMOVED: m_buf->write("HTTP/1.1 302 Moved Temporarily\r\n"); break; case SC_NOTFOUND: m_buf->write("HTTP/1.1 404 Not Found\r\n"); break; case SC_BADMETHOD: m_buf->write("HTTP/1.1 405 Method Not Allowed\r\n"); break; + case SC_PROTOCOL: m_buf->write("HTTP/1.1 101 Switching Protocols\r\n"); break; default: m_buf->write("HTTP/1.1 500 Internal Server Error\r\n"); break; } } @@ -142,21 +144,26 @@ namespace lcodec { void http_parse_body(lua_State* L, string_view header, string_view& buf) { m_buf->clean(); bool jsonable = false; + bool contentlenable = false; slice* mslice = nullptr; vector headers; split(header, CRLF, headers); + lua_createtable(L, 0, 4); for (auto header : headers) { size_t pos = header.find(":"); if (pos != string_view::npos) { string_view key = header.substr(0, pos); header.remove_prefix(pos + 1); + header.remove_prefix(header.find_first_not_of(" ")); if (!strncasecmp(key.data(), "Content-Length", key.size())) { + contentlenable = true; mslice = m_buf->get_slice(); size_t content_size = atol(header.data()); mslice->attach((uint8_t*)buf.data(), content_size); buf.remove_prefix(content_size); } else if (!strncasecmp(key.data(), "Transfer-Encoding", key.size()) && !strncasecmp(header.data(), "chunked", header.size())) { + contentlenable = true; size_t pos = buf.find(CRLF2); string_view chunk_data = buf.substr(0, pos); buf.remove_prefix(pos + LCRLF2); @@ -167,19 +174,28 @@ namespace lcodec { m_buf->push_data((const uint8_t*)chunks[i].data(), chunks[i].size()); } } + mslice = m_buf->get_slice(); } - else if (!strncasecmp(key.data(), "Content-Type", key.size()) && !strncasecmp(header.data(), "application/json", header.size())) { + else if (!strncasecmp(key.data(), "Content-Type", key.size()) && !strncasecmp(header.data(), "application/json", strlen("application/json"))) { jsonable = true; } + //压栈 + lua_pushlstring(L, key.data(), key.size()); + lua_pushlstring(L, header.data(), header.size()); + lua_settable(L, -3); } } + if (!contentlenable) { + mslice = m_buf->get_slice(); + mslice->attach((uint8_t*)buf.data(), buf.size()); + buf.remove_prefix(buf.size()); + } if (jsonable) { - if (!mslice) mslice = m_buf->get_slice(); m_jcodec->set_slice(mslice); m_jcodec->decode(L); return; } - lua_pushlstring(L, (char*)m_buf->head(), m_buf->size()); + lua_pushlstring(L, (char*)mslice->head(), mslice->size()); } void parse_http_packet(lua_State* L, string_view& buf) { diff --git a/extend/lcodec/src/lcodec.cpp b/extend/lcodec/src/lcodec.cpp index 72da346d..bd880a5a 100644 --- a/extend/lcodec/src/lcodec.cpp +++ b/extend/lcodec/src/lcodec.cpp @@ -6,6 +6,7 @@ namespace lcodec { thread_local ketama thread_ketama; thread_local rdscodec thread_rds; + thread_local wsscodec thread_wss; thread_local httpcodec thread_http; thread_local luakit::luabuf thread_buff; @@ -15,6 +16,12 @@ namespace lcodec { return &thread_rds; } + static wsscodec* wss_codec(codec_base* codec) { + thread_wss.set_codec(codec); + thread_wss.set_buff(&thread_buff); + return &thread_wss; + } + static httpcodec* http_codec(codec_base* codec) { thread_http.set_codec(codec); thread_http.set_buff(&thread_buff); @@ -84,6 +91,7 @@ namespace lcodec { llcodec.set_function("ketama_map", ketama_map); llcodec.set_function("rediscodec", rds_codec); llcodec.set_function("httpcodec", http_codec); + llcodec.set_function("wsscodec", wss_codec); kit_state.new_class( "flip", &bitarray::flip, diff --git a/extend/lcodec/src/lcodec.h b/extend/lcodec/src/lcodec.h index dc05b05a..495ea2a0 100644 --- a/extend/lcodec/src/lcodec.h +++ b/extend/lcodec/src/lcodec.h @@ -8,4 +8,5 @@ #include "hash.h" #include "redis.h" #include "ketama.h" +#include "websocket.h" #include "bitarray.h" diff --git a/extend/lcodec/src/mysql.h b/extend/lcodec/src/mysql.h new file mode 100644 index 00000000..5ea58f21 --- /dev/null +++ b/extend/lcodec/src/mysql.h @@ -0,0 +1,43 @@ +#pragma once + +#include "lua_kit.h" + +using namespace std; +using namespace luakit; + +namespace lcodec { + + class mysqlscodec : public codec_base { + public: + virtual uint8_t* encode(lua_State* L, int index, size_t* len) { + m_buf->clean(); + return m_buf->data(len); + } + + virtual size_t decode(lua_State* L) { + if (!m_slice) return 0; + int top = lua_gettop(L); + return lua_gettop(L) - top; + } + + void set_codec(codec_base* codec) { + m_jcodec = codec; + } + + void set_buff(luabuf* buf) { + m_buf = buf; + } + + protected: + char* xor_byte(char* buffer, char* mask, size_t blen, size_t mlen) { + for (int i = 0; i < blen; i++) { + buffer[i] = buffer[i] ^ mask[i % mlen]; + } + return buffer; + } + + protected: + luabuf* m_buf = nullptr; + codec_base* m_jcodec = nullptr; + }; +} diff --git a/extend/lcodec/src/websocket.h b/extend/lcodec/src/websocket.h new file mode 100644 index 00000000..2f5cc4f4 --- /dev/null +++ b/extend/lcodec/src/websocket.h @@ -0,0 +1,87 @@ +#pragma once + +#include "lua_kit.h" + +using namespace std; +using namespace luakit; + +namespace lcodec { + + class wsscodec : public codec_base { + public: + virtual uint8_t* encode(lua_State* L, int index, size_t* len) { + m_buf->clean(); + uint8_t* body = nullptr; + size_t opcode = lua_tointeger(L, index); + if (lua_type(L, index + 1) == LUA_TTABLE) { + body = m_jcodec->encode(L, index + 1, len); + } else { + body = (uint8_t*)lua_tolstring(L, index + 1, len); + } + m_buf->write((0x80 | opcode)); + if (*len < 126) { + m_buf->write(*len); + } else if (*len < 0xffff) { + m_buf->write(126); + m_buf->write(*len); + } else { + m_buf->write(127); + m_buf->write(*len); + } + m_buf->push_data(body, *len); + return m_buf->data(len); + } + + virtual size_t decode(lua_State* L) { + if (!m_slice) return 0; + uint8_t head = *(uint8_t*)m_slice->read(); + if ((head & 0x80) != 0x80) throw length_error("shared packet not suppert!"); + uint8_t payload = *(uint8_t*)m_slice->read(); + uint8_t opcode = head & 0xf; + bool mask = ((payload & 0x80) == 0x80); + payload = payload & 0x7f; + if (payload >= 0x7e) { + m_slice->erase((payload == 0x7f) ? 8 : 2); + } + int top = lua_gettop(L); + lua_pushstring(L, "WSS"); + lua_pushinteger(L, opcode); + if (mask) { + size_t data_len; + char* maskkey = (char*)m_slice->peek(4); + m_slice->erase(4); + char* data = (char*)m_slice->data(&data_len); + xor_byte(data, maskkey, data_len, 4); + } + size_t osize = m_slice->size(); + if (opcode == 0x02) { + m_jcodec->set_slice(m_slice); + m_jcodec->decode(L); + } else { + lua_pushlstring(L, (char*)m_slice->head(), osize); + } + m_slice->erase(osize); + return lua_gettop(L) - top; + } + + void set_codec(codec_base* codec) { + m_jcodec = codec; + } + + void set_buff(luabuf* buf) { + m_buf = buf; + } + + protected: + char* xor_byte(char* buffer, char* mask, size_t blen, size_t mlen) { + for (int i = 0; i < blen; i++) { + buffer[i] = buffer[i] ^ mask[i % mlen]; + } + return buffer; + } + + protected: + luabuf* m_buf = nullptr; + codec_base* m_jcodec = nullptr; + }; +} diff --git a/extend/luakit/include/lua_buff.h b/extend/luakit/include/lua_buff.h index 28274a11..3d3f1dee 100644 --- a/extend/luakit/include/lua_buff.h +++ b/extend/luakit/include/lua_buff.h @@ -87,10 +87,10 @@ namespace luakit { return 0; } - uint8_t* peek_data(size_t peek_len) { - size_t data_len = m_tail - m_head; + uint8_t* peek_data(size_t peek_len, size_t offset = 0) { + size_t data_len = m_tail - m_head - offset; if (peek_len > 0 && data_len >= peek_len) { - return m_head; + return m_head + offset; } return nullptr; } diff --git a/proto/build.bat b/proto/build.bat index 180d2b23..0cb19dab 100644 --- a/proto/build.bat +++ b/proto/build.bat @@ -14,6 +14,7 @@ for %%i in (*.proto) do ( call set "Files=%%i %%Files%%" ) protoc.exe --descriptor_set_out=%ProtoDir%\ncmd_cs.pb %Files% +protoc.exe --plugin=protoc-gen-json=./pbjson.exe --json_out=%ProtoDir%\ %Files% pause diff --git a/proto/pbjson.exe b/proto/pbjson.exe new file mode 100644 index 00000000..f614ba32 Binary files /dev/null and b/proto/pbjson.exe differ diff --git a/proto/protoc.exe b/proto/protoc.exe index e69179c0..789cc2a4 100644 Binary files a/proto/protoc.exe and b/proto/protoc.exe differ diff --git a/script/driver/socket.lua b/script/driver/socket.lua index 3367082b..20da12d8 100644 --- a/script/driver/socket.lua +++ b/script/driver/socket.lua @@ -19,7 +19,6 @@ prop:reader("port", 0) prop:reader("host", nil) prop:reader("token", nil) prop:reader("alive", false) -prop:reader("proto_type", eproto_type.text) prop:reader("session", nil) --连接成功对象 prop:reader("listener", nil) prop:accessor("id", 0) @@ -47,16 +46,14 @@ function Socket:listen(ip, port, ptype) if self.listener then return true end - if ptype then - self.proto_type = ptype - end - self.listener = socket_mgr.listen(ip, port, self.proto_type) + self.listener = socket_mgr.listen(ip, port) if not self.listener then - log_err("[Socket][listen] failed to listen: %s:%d type=%d", ip, port, self.proto_type) + log_err("[Socket][listen] failed to listen: %s:%d", ip, port) return false end self.ip, self.port = ip, port - log_info("[Socket][listen] start listen at: %s:%d type=%d", ip, port, self.proto_type) + self.listener.set_proto_type(ptype or eproto_type.text) + log_info("[Socket][listen] start listen at: %s:%d", ip, port) self.listener.on_accept = function(session) qxpcall(self.on_socket_accept, "on_socket_accept: %s", self, session, ip, port) end @@ -76,14 +73,12 @@ function Socket:connect(ip, port, ptype) end return false, "socket in connecting" end - if ptype then - self.proto_type = ptype - end - local session, cerr = socket_mgr.connect(ip, port, CONNECT_TIMEOUT, self.proto_type) + local session, cerr = socket_mgr.connect(ip, port, CONNECT_TIMEOUT) if not session then - log_err("[Socket][connect] failed to connect: %s:%d type=%d, err=%s", ip, port, self.proto_type, cerr) + log_err("[Socket][connect] failed to connect: %s:%d err=%s", ip, port, cerr) return false, cerr end + session.set_proto_type(ptype or eproto_type.text) --设置阻塞id local token = session.token local block_id = thread_mgr:build_session_id() diff --git a/script/driver/websocket.lua b/script/driver/websocket.lua index 80c28caa..2a51bfc1 100644 --- a/script/driver/websocket.lua +++ b/script/driver/websocket.lua @@ -1,64 +1,62 @@ --websocket.lua -local ssub = string.sub -local spack = string.pack -local sformat = string.format -local sunpack = string.unpack local log_err = logger.err local log_info = logger.info +local log_debug = logger.debug local lsha1 = crypt.sha1 -local lxor_byte = crypt.xor_byte local lb64encode = crypt.b64_encode local qxpcall = quanta.xpcall -local socket_mgr = quanta.get("socket_mgr") -local thread_mgr = quanta.get("thread_mgr") +local eproto_type = luabus.eproto_type + +local socket_mgr = quanta.get("socket_mgr") +local thread_mgr = quanta.get("thread_mgr") local NETWORK_TIMEOUT = quanta.enum("NetwkTime", "NETWORK_TIMEOUT") -local HTTP_CALL_TIMEOUT = quanta.enum("NetwkTime", "HTTP_CALL_TIMEOUT") local WebSocket = class() local prop = property(WebSocket) prop:reader("ip", nil) prop:reader("host", nil) prop:reader("token", nil) +prop:reader("wcodec", nil) --codec +prop:reader("hcodec", nil) --codec prop:reader("alive", false) prop:reader("session", nil) --连接成功对象 prop:reader("listener", nil) -prop:reader("recvbuf", "") -prop:reader("context", nil) --context -prop:reader("url", "") prop:reader("port", 0) function WebSocket:__init(host) self.host = host + local jcodec = json.jsoncodec() + self.wcodec = codec.wsscodec(jcodec) + self.hcodec = codec.httpcodec(jcodec) end function WebSocket:close() if self.session then if self.alive then - self:send_frame(true, 0x8, "") + self:send_data(0x8, "") end self.session.close() - self.context = nil self.alive = false self.session = nil self.token = nil end end -function WebSocket:listen(ip, port) +function WebSocket:listen(ip, port, ptype) if self.listener then return true end - local proto_type = 2 - self.listener = socket_mgr.listen(ip, port, proto_type) + self.listener = socket_mgr.listen(ip, port) if not self.listener then - log_err("[WebSocket][listen] failed to listen: %s:%d type=%d", ip, port, proto_type) + log_err("[WebSocket][listen] failed to listen: %s:%d", ip, port) return false end self.ip, self.port = ip, port - log_info("[WebSocket][listen] start listen at: %s:%d type=%d", ip, port, proto_type) + self.listener.set_proto_type(ptype or eproto_type.text) + log_info("[WebSocket][listen] start listen at: %s:%d", ip, port) self.listener.on_accept = function(session) qxpcall(self.on_socket_accept, "on_socket_accept: %s", self, session, ip, port) end @@ -71,236 +69,101 @@ function WebSocket:on_socket_accept(session) end function WebSocket:on_socket_error(token, err) - if self.session then - self.session = nil - self.alive = false - log_err("[WebSocket][on_socket_error] err: %s - %s!", err, token) - self.host:on_socket_error(self, token, err) - self.token = nil - end + thread_mgr:fork(function() + if self.session then + self.token = nil + self.session = nil + self.alive = false + log_err("[WebSocket][on_socket_error] err: %s - %s!", err, token) + self.host:on_socket_error(self, token, err) + end + end) end -function WebSocket:on_socket_recv(session, data) - local token = session.token - if self.alive then - self.recvbuf = self.recvbuf .. data - while true do - local frame = self:recv_frame() - if not frame then - break - end - if frame.error and frame.opcode == 0x8 then -- close/error - self:close() - self.host:on_socket_error(self, token, frame.data) - end - if frame.opcode == 0x9 then --Ping - self:send_frame(true, 0xA, data) - goto continue - end - if frame.opcode == 0xA then --Pong - goto continue - end - thread_mgr:fork(function() - local context = self.context - if context then - thread_mgr:response(context.session_id, frame) - return - end - local message = self:combine_frame(frame) - self.host:on_socket_recv(self, token, message) - self.context = nil - end) - :: continue :: +function WebSocket:on_socket_recv(session, token, opcode, message) + thread_mgr:fork(function() + if opcode == 0x8 then -- close/error + self:close() + self.host:on_socket_error(self, token, message) + return end - return - end - self.alive = true - self.token = token - self.session = session - if self:on_accept_handshake(session, token, data) then - self.host:on_socket_accept(self, token) - end + if opcode == 0x9 then --Ping + self:send_frame(0xA, "PONG") + return + end + if opcode <= 0X02 then + self.host:on_socket_recv(self, token, message) + end + end) end --accept function WebSocket:accept(session, ip, port) - self.ip, self.port = ip, port + log_debug("[WebSocket][accept] ip, port: %s, %s", ip, port) + local token = session.token session.set_timeout(NETWORK_TIMEOUT) - session.on_call_text = function(recv_len, data) - qxpcall(self.on_socket_recv, "on_socket_recv: %s", self, session, data) + session.on_call_data = function(recv_len, method, ...) + if method == "WSS" then + self:on_socket_recv(session, token, ...) + else + self:on_handshake(session, token, ...) + end end - session.on_error = function(token, err) - thread_mgr:fork(function() - self:on_socket_error(token, err) - end) + session.on_error = function(stoken, err) + self:on_socket_error(stoken, err) end + session.set_codec(self.hcodec) + self.ip, self.port = ip, port end --握手协议 -function WebSocket:on_accept_handshake(session, token, data) - local request = http.create_request() - if not request:append(data) then - log_err("[WebSocket][on_accept_handshake] http request append failed, close client(token:%s)!", token) - return self:response(400, request, "this http request parse error!") - end - request:process() - local state = request:state() - local HTTP_REQUEST_ERROR = 2 - if state == HTTP_REQUEST_ERROR then - log_err("[WebSocket][on_accept_handshake] http request process failed, close client(token:%s)!", token) - return self:response(400, request, "this http request parse error!") - end - local headers = request:headers() +function WebSocket:on_handshake(session, token, url, params, headers, body) + log_debug("[WebSocket][on_handshake] recv: %s, %s, %s, %s!", url, params, headers, body) local upgrade = headers["Upgrade"] if not upgrade or upgrade ~= "websocket" then - return self:response(400, request, "can upgrade only to websocket!") + return self:send_data(400, nil, "can upgrade only to websocket!") end local connection = headers["Connection"] if not connection or connection ~= "Upgrade" then - return self:response(400, request, "connection must be upgrade!") + return self:send_data(400, nil, "connection must be upgrade!") end local version = headers["Sec-WebSocket-Version"] if not version or version ~= "13" then - return self:response(400, request, "HTTP/1.1 Upgrade Required\r\nSec-WebSocket-Version: 13\r\n\r\n") + return self:send_data(400, nil, "Upgrade Required Sec-WebSocket-Version: 13") end local key = headers["Sec-WebSocket-Key"] if not key then - return self:response(400, request, "Sec-WebSocket-Key must not be nil!") + return self:send_data(400, nil, "Sec-WebSocket-Key must not be nil!") end - self.url = request:url() - local protocol = headers["Sec-WebSocket-Protocol"] - if protocol then - local i = protocol:find(",", 1, true) - protocol = "Sec-WebSocket-Protocol: " .. protocol:sub(1, i and i-1) + local cbheaders = { + ["Upgrade"] = "websocket", + ["Connection"] = "Upgrade", + ["Sec-WebSocket-Accept"] = lb64encode(lsha1(key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")) + } + if headers["Sec-WebSocket-Protocol"] then + cbheaders["Sec-WebSocket-Protocol"] = "mqtt" end - local accept = lb64encode(lsha1(key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")) - local fmt_text = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n%s\r\n" - self:send(sformat(fmt_text, accept, protocol or "")) + self.alive = true + --handshake 完成 + self.token = token + self.session = session + self:send_data(101, cbheaders, "") + self.host:on_socket_accept(self, token) + self.session.set_codec(self.wcodec) return true end ---回执 -function WebSocket:response(status, request, response) - local text = request:response(status, "text/plain", response or "") - self:send(text) - self:close() -end - ---释放数据 -function WebSocket:pop(len) - if len > 0 then - if #self.recvbuf > len then - self.recvbuf = ssub(self.recvbuf, len + 1) - else - self.recvbuf = "" - end - end -end - ---peek -function WebSocket:peek(len, offset) - offset = offset or 0 - if offset + len <= #self.recvbuf then - return ssub(self.recvbuf, offset + 1, offset + len) - end -end - ---发送数据 -function WebSocket:send(data) - if self.alive and data then - local send_len = self.session.call_text(data) +function WebSocket:send_data(...) + if self.alive then + local send_len = self.session.call_data(...) return send_len > 0 end - log_err("[WebSocket][send] the socket not alive, can't send") - return false + return false, "socket not alive" end --发送帧 -function WebSocket:send_frame(fin, opcode, data) - local len = #data - local finbit = fin and 0x80 or 0 - --服务器回包不需要掩码格式化 - if len < 126 then - self:send(spack("BB", finbit | opcode, len) .. data) - elseif len < 0xFFFF then - self:send(spack(">BBI2", finbit | opcode, 126, len) .. data) - else - self:send(spack(">BBI8", finbit | opcode, 127, len) .. data) - end -end - -function WebSocket:send_text(data) - self:send_frame(true, 0x1, data) -end - -function WebSocket:send_binary(data) - self:send_frame(true, 0x2, data) -end - ---接收ws帧 -function WebSocket:recv_frame() - local offset = 2 - local data = self.recvbuf - if #data < offset then - return - end - local header, payloadlen = sunpack("BB", data) - if header & 0x70 ~= 0 then - return { error = true, data = "Reserved_bits show using undefined extensions" } - end - local masklen, packlen = 0, 0 - local fmtlen = payloadlen & 0x7f - if payloadlen & 0x80 ~= 0 then - masklen = 4 - end - if fmtlen == 126 then - packlen = 2 - elseif fmtlen == 127 then - packlen = 8 - end - if #data < offset + packlen + masklen then - return - end - local data_len = fmtlen - local frame = {opcode = header & 0xf, final = header & 0x80 ~= 0 } - if packlen == 2 then - data_len = sunpack(">H", self:peek(packlen, offset)) - elseif packlen == 8 then - data_len = sunpack(">I8", self:peek(packlen, offset)) - end - offset = offset + packlen - if masklen > 0 then - frame.mask = self:peek(masklen, offset) - offset = offset + masklen - end - if #data < offset + data_len then - return - end - if data_len > 0 then - frame.data = self:peek(data_len, offset) - if masklen > 0 then - frame.data = lxor_byte(frame.data, frame.mask) - end - end - self:pop(data_len + offset) - return frame -end - ---组合帧数据 -function WebSocket:combine_frame(frame) - if frame.final then - return frame.data - end - while true do - local session_id = thread_mgr:build_session_id() - self.context = { session_id = session_id } - local next_frame = thread_mgr:yield(session_id, "combine_frame", HTTP_CALL_TIMEOUT) - frame.data = frame.data .. next_frame.data - if next_frame.final then - break - end - end - return frame.data +function WebSocket:send_frame(data) + return self:send_data(0x01, data) end return WebSocket diff --git a/script/network/http_server.lua b/script/network/http_server.lua index 536c7f93..9c3d88db 100644 --- a/script/network/http_server.lua +++ b/script/network/http_server.lua @@ -62,19 +62,19 @@ function HttpServer:on_socket_accept(socket, token) socket:set_codec(self.codec) end -function HttpServer:on_socket_recv(socket, method, url, params, body) - log_debug("[HttpServer][on_socket_recv] recv: %s, %s, %s, %s!", method, url, params, body) +function HttpServer:on_socket_recv(socket, method, url, params, headers, body) + log_debug("[HttpServer][on_socket_recv] recv: %s, %s, %s, %s, %s!", method, url, params, headers, body) if method == "GET" then - return self:on_http_request(self.get_handlers, socket, url, params) + return self:on_http_request(self.get_handlers, socket, url, params, headers) end if method == "POST" then - return self:on_http_request(self.post_handlers, socket, url, body, params) + return self:on_http_request(self.post_handlers, socket, url, body, params, headers) end if method == "PUT" then - return self:on_http_request(self.put_handlers, socket, url, body, params) + return self:on_http_request(self.put_handlers, socket, url, body, params, headers) end if method == "DELETE" then - return self:on_http_request(self.del_handlers, socket, url, params) + return self:on_http_request(self.del_handlers, socket, url, params, headers) end end diff --git a/script/network/net_client.lua b/script/network/net_client.lua index 404c5492..1472ab15 100644 --- a/script/network/net_client.lua +++ b/script/network/net_client.lua @@ -48,13 +48,13 @@ function NetClient:connect(block) if self.socket then return true end - local proto_type = 1 - local socket, cerr = socket_mgr.connect(self.ip, self.port, CONNECT_TIMEOUT, proto_type) + local socket, cerr = socket_mgr.connect(self.ip, self.port, CONNECT_TIMEOUT) if not socket then - log_err("[NetClient][connect] failed to connect: %s:%s type=%s, err=%s", self.ip, self.port, proto_type, cerr) + log_err("[NetClient][connect] failed to connect: %s:%s err=%s", self.ip, self.port, cerr) return false, cerr end --设置阻塞id + socket.set_proto_type(luabus.eproto_type.head); local block_id = block and thread_mgr:build_session_id() -- 调用成功,开始安装回调函数 socket.on_connect = function(res) diff --git a/script/network/net_server.lua b/script/network/net_server.lua index 02aafaf7..1907b2b3 100644 --- a/script/network/net_server.lua +++ b/script/network/net_server.lua @@ -57,17 +57,17 @@ function NetServer:setup(ip, port, induce) signalquit() return end - local listen_proto_type = 1 local real_port = induce and (port + quanta.order - 1) or port - self.listener = socket_mgr.listen(ip, real_port, listen_proto_type) + self.listener = socket_mgr.listen(ip, real_port) if not self.listener then - log_err("[NetServer][setup] failed to listen: %s:%d type=%d", ip, real_port, listen_proto_type) + log_err("[NetServer][setup] failed to listen: %s:%d", ip, real_port) signalquit() return end self.ip, self.port = ip, real_port - log_info("[NetServer][setup] start listen at: %s:%d type=%d", ip, real_port, listen_proto_type) + log_info("[NetServer][setup] start listen at: %s:%d", ip, real_port) -- 安装回调 + self.listener.set_proto_type(luabus.eproto_type.head) self.listener.on_accept = function(session) qxpcall(self.on_socket_accept, "on_socket_accept: %s", self, session) end diff --git a/script/network/ws_server.lua b/script/network/ws_server.lua index 45c6f671..758ecb71 100644 --- a/script/network/ws_server.lua +++ b/script/network/ws_server.lua @@ -1,12 +1,8 @@ --ws_server.lua local WebSocket = import("driver/websocket.lua") -local type = type -local log_err = logger.err local log_info = logger.info local log_debug = logger.debug -local json_encode = json.encode -local tunpack = table.unpack local signalquit = signal.quit local saddr = qstring.addr @@ -15,9 +11,7 @@ local prop = property(WSServer) prop:reader("listener", nil) --网络连接对象 prop:reader("ip", nil) --WS server地址 prop:reader("port", 8191) --WS server端口 -prop:reader("mode", "text") --发送类型(text/binary) prop:reader("clients", {}) --clients -prop:reader("handlers", {}) --get_handlers function WSServer:__init(ws_addr) self:setup(ws_addr) @@ -50,56 +44,10 @@ function WSServer:on_socket_accept(socket, token) self.clients[token] = socket end ---注册回调 -function WSServer:register_handler(url, handler, target) - log_debug("[WSServer][register_handler] url: %s", url) - self.handlers[url] = { handler, target } -end - --回调 function WSServer:on_socket_recv(socket, token, message) - local url = socket:get_url() - local handler_info = self.handlers[url] or self.handlers["*"] - if handler_info then - local handler, target = tunpack(handler_info) - if not target then - if type(handler) == "function" then - local ok, response = pcall(handler, url, message) - if not ok then - response = { code = 1, msg = response } - end - self:response(socket, response) - return - end - else - if type(handler) == "string" then - handler = target[handler] - end - if type(handler) == "function" then - local ok, response = pcall(handler, target, url, message) - if not ok then - response = { code = 1, msg = response } - end - self:response(socket, response) - return - end - end - end - log_err("[WSServer][on_socket_recv] request %s hasn't process!", url) -end - -function WSServer:response(socket, ws_res) - if type(ws_res) == "table" then - ws_res = json_encode(ws_res) - end - if type(ws_res) ~= "string" then - ws_res = tostring(ws_res) - end - if self.mode == "text" then - socket:send_text(ws_res) - return - end - socket:send_binary(ws_res) + log_debug("[WSServer][on_socket_recv] client(token:%s) msg:%s!", token, message) + socket:send_frame(message) end return WSServer diff --git a/server/center/gm_mgr.lua b/server/center/gm_mgr.lua index 66f0e179..6fb85734 100644 --- a/server/center/gm_mgr.lua +++ b/server/center/gm_mgr.lua @@ -160,7 +160,7 @@ end ---------------------------------------------------------------------- --gm_page function GM_Mgr:on_gm_page(url, params) - return self.gm_page, {["Access-Control-Allow-Origin"] = "*"} + return self.gm_page, {["Access-Control-Allow-Origin"] = "*", ["X-Frame-Options"]= "ALLOW_FROM"} end --gm列表 diff --git a/server/center/gm_page.lua b/server/center/gm_page.lua index 8677739c..9ad5e84e 100644 --- a/server/center/gm_page.lua +++ b/server/center/gm_page.lua @@ -75,7 +75,24 @@ return [[
-
+ +
+ 服务器: + + + + + + + + + +
+
+
    +
  • 请选择服务器
  • +
+
@@ -100,7 +117,20 @@ return [[