local debug_flag = 1
local send_debug_flag = 1
local new_connection_callback = {}
local socket_cookie_map = {}
local msg_filter = {}
local max_online_num = 1000
local function socket_connect_callback(cookie, fd, client_ip)
local cookie_info = socket_cookie_map[cookie]
if not cookie_info then
close_fd(fd)
return
end
socket_cookie_map[cookie] = nil
unregister_new_connection_callback(cookie)
local agent = fd
if fd ~= -1 then
agent = CLONE_OBJECT(AGENT_TDCLS)
agent:set_authed(true)
agent:set_port_no(fd)
end
local f, arg = cookie_info["callback"], cookie_info["arg"]
if type(f) == "function" then
f(agent, arg)
end
end
local function update_operation_time(agent)
if agent:is_user() then
agent:set_temp("last_operation_time", os.time())
end
end
function register_new_connection_callback(cookie, f)
new_connection_callback[cookie] = f
end
function unregister_new_connection_callback(cookie)
new_connection_callback[cookie] = nil
end
function cmd_connection_lost(port_no)
TRACE("断开连接(%d)", port_no)
local agent = find_agent_by_port(port_no)
if agent == nil then
do return end
end
agent:connection_lost()
end
function get_server_type(client_ip, server_port)
if SERVER_TYPE == "logic" then
return SERVER_TYPE_GATE
elseif SERVER_TYPE == "gate" and server_port == tonumber(GATE_LOGIC_PORT) then
return SERVER_TYPE_LOGIC
end
return SERVER_TYPE_CLIENT
end
function cmd_new_connection(cookie, fd, client_ip, server_port, websocket)
TRACE("cmd_new_connection 收到新连接(%d)。端口(%d), 客户端地址(%s), new connect info", fd, server_port, client_ip)
local f = new_connection_callback[cookie]
if type(f) == "function" then
f(cookie, fd, client_ip)
return
end
local agent = CLONE_OBJECT(AGENT_TDCLS)
agent:set_port_no(fd)
agent:set_client_ip(client_ip)
agent:set_websocket(websocket)
if server_port ~= GATE_LOGIC_PORT then
if get_real_agent_count() > max_online_num then
TRACE("555555555555")
agent:connection_lost()
return
end
end
end
function debug_on(flag, rid)
if not rid or type(rid) ~= "string" then
if not flag or flag == 0 then
debug_flag = 0
else
debug_flag = 1
end
else
if not flag or flag == 0 then
if type(debug_flag) == "table" then
debug_flag[rid] = nil
if SIZEOF(debug_flag) <= 0 then
debug_flag = 0
end
end
else
if type(debug_flag) ~= "table" then
debug_flag = {}
end
debug_flag[rid] = true
end
end
end
function send_debug_on(flag, rid)
if not rid or type(rid) ~= "string" then
if not flag or flag == 0 then
send_debug_flag = 0
else
send_debug_flag = 1
end
else
if not flag or flag == 0 then
if type(send_debug_flag) == "table" then
send_debug_flag[rid] = nil
if SIZEOF(send_debug_flag) <= 0 then
send_debug_flag = 0
end
end
else
if type(send_debug_flag) ~= "table" then
send_debug_flag = {}
end
send_debug_flag[rid] = true
end
end
end
function get_send_debug_flag()
return send_debug_flag
end
function get_debug_flag()
return debug_flag
end
function get_message_manage_type(message, server_type)
local msg_type = get_message_type(message)
if msg_type == "" then
return MESSAGE_DISCARD
end
if SERVER_TYPE == SERVER_CLIENT then
return MESSAGE_MANAGE
end
if server_type == SERVER_TYPE_CLIENT then
if msg_type == MESSAGE_GATE or msg_type == MESSAGE_SERVER then
return MESSAGE_MANAGE
else
return MESSAGE_FORWARD
end
elseif server_type == SERVER_TYPE_GATE then
return MESSAGE_MANAGE
elseif server_type == SERVER_TYPE_LOGIC then
if msg_type == MESSAGE_LOGIC or msg_type == MESSAGE_SERVER then
return MESSAGE_MANAGE
else
return MESSAGE_FORWARD
end
else
ASSERT(false, "unknow message type")
end
end
function oper_message(agent, message, msg_buf)
local name, args = MSG_TO_TABLE(msg_buf)
local flag = get_debug_flag()
if (type(flag) == "number" and flag == 1) or
(type(flag) == "table" and agent:is_user() and flag[agent:GET_RID()]) then
TRACE("------------- msg : %s -------------\n%o", message, args)
end
local message_handler = _G[message]
if not message_handler then
TRACE("global_dispatch_command message_handler : %o 未定义消息处理函数!", message)
return
end
message_handler(agent, unpack(args or {}))
end
function websocket_recalc_name(message, buffer)
if message == "web_socket_text" then
local name, args = buffer:msg_to_table()
if type(args) ~= "string" then
return nil
end
TRACE("args is %o", args)
message = READ_MSG_NAME(args)
TRACE("message is %o", message)
end
return message
end
function global_dispatch_command(port_no, message, buffer)
local agent = find_agent_by_port(port_no)
TRACE("global_dispatch_command message is %o", message)
if not message then
TRACE("非法连接(%d)\n 传送非法消息(源消息为%o)", port_no, message)
if IS_OBJECT(agent) then
agent:print_fd_info()
DESTRUCT_OBJECT(agent)
else
close_fd(port_no)
end
do return end
end
TRACE("------- my agent = %o ---------", agent)
if not agent or
(not agent:is_authed() and (message ~= "cmd_internal_auth" and message ~= "cmd_agent_identity")) then
TRACE("非法连接(%d)\n 消息为(%o)", port_no, message)
if not agent then
TRACE("端口绑定的对象不存在")
end
if IS_OBJECT(agent) then
agent:print_fd_info()
DESTRUCT_OBJECT(agent)
else
close_fd(port_no)
end
do return end
end
TRACE("agent == %o", agent)
if not agent:get_code_type() then
TRACE("未知连接身份(%d)\n 发送消息为(%o)", port_no, message)
agent:connection_lost()
return
end
if agent:get_server_type() == SERVER_TYPE_CLIENT and SERVER_TYPE == SERVER_TYPE_GATE and not agent:check_next_client(buffer:get_seq_fd()) then
TRACE("package check failed %o kick the socket", agent:get_ob_id())
TRACE("agent:get_server_type() = %o ", agent:get_server_type())
agent:connection_lost()
del_message(buffer)
return
end
local to_type, to_id, msg_flag = buffer:get_to_svr_type(), buffer:get_to_svr_id(), buffer:get_msg_flag()
TRACE("11111111111111 %o %o, %o, fd= %o", message, to_type, to_id, buffer:get_seq_fd())
if to_type == SERVER_TYPE_CLIENT then
local clientAgent = find_agent_by_port(buffer:get_seq_fd())
TRACE("2121212121 %o clientAgent = %o", message, clientAgent)
if clientAgent then
clientAgent:forward_client_message(buffer)
end
del_message(buffer)
do return end
TRACE("2222222222222222 %o", message)
else
TRACE("3333333333333333 %o, msg_flag = %o", message, msg_flag)
if msg_flag == MSG_FLAG_FORWARD then
if SERVER_TYPE ~= SERVER_NAMES[to_type] then
local port_agent = find_agent_by_port(buffer:get_seq_fd() + 0x10000)
if port_agent then
port_agent:connection_lost()
end
LOG.warn("非法消息, 消息无法处理, 却在服务器接收 %o 消息为 %o", SERVER_TYPE, message)
del_message(buffer)
return
end
else
TRACE("aaaaaaaaaaaaaaa %o", to_type)
if SERVER_TYPE_GATE ~= to_type and (SERVER_TYPE == SERVER_GATE) then
local agent = find_port_by_code(to_type, to_id)
if not IS_OBJECT(agent) then
return
end
agent:forward_server_message(buffer, port_no)
del_message(buffer)
return
end
end
end
TRACE("44444444444444444 %o", message)
if not is_msg_can_deal(message) then
agent:connection_lost()
LOG.warn("发送非法消息, 该服务器无法处理 %o 消息为 %o", SERVER_TYPE, message)
del_message(buffer)
return
end
if IS_FUNCTION(msg_filter[message]) then
msg_filter[message](agent, buffer)
del_message(buffer)
return
end
oper_message(agent, message, buffer)
del_message(buffer)
if agent:is_user() then
update_operation_time(agent)
if PACKAGE_STATD then
PACKAGE_STATD.on_user_recv_package(agent)
end
end
end
function msg_db_result(cookie, ret, result_list)
DB_D.notify_operation_result(cookie, ret, result_list)
end
function msg_redis_result(cookie, value)
REDIS_D.notify_operation_result(cookie, value)
end
function register_msg_filter(msg, f)
msg_filter[msg] = f
end
function socket_connect(ip, port, timeout, callback, arg)
local cookie = new_cookie()
socket_cookie_map[cookie] = {
ip = ip,
port = port,
timeout = timeout,
callback = callback,
arg = arg,
}
register_new_connection_callback(cookie, socket_connect_callback)
return (new_connect(ip, port, timeout, cookie))
end
function http_server_msg_recv(cookie, url, body, remote_host)
cookie = tonumber(cookie)
TRACE("http_server_msg_recv args is %o", {cookie, route, query, body, remote_host})
http_server_respone(cookie, "hello world from lua")
end
function http_client_msg_respone(cookie, success, body)
cookie = tonumber(cookie)
success = success == "true" or success == "1"
TRACE("http_client_msg_respone args is %o", {cookie, success, body})
end
function set_max_online_num(num)
max_online_num = num
end
function get_max_online_num()
return max_online_num
end