LOGIN_QUEUE_D = {}
setmetatable(LOGIN_QUEUE_D, {__index = _G})
local _ENV = LOGIN_QUEUE_D
local online_limit = 600
local cpu_useage_limit = 85
local login_size_limit = 30
local login_queue = {}
local login_list = {}
setmetatable(login_list, { __mode = "v" })
local function get_login_list_size()
local login_size = 0
for _, agent in pairs(login_list) do
if is_object(agent) then
login_size = login_size + 1
end
end
return login_size
end
local function _notify_queue_number()
local login_data = login_queue:get_first()
if login_data then
if not login_data["agent"] or not login_data["agent"]:is_valid() or
not login_data["agent"]:is_authed() then
login_queue:pop_first()
end
end
local agent
local invalid = 0
local index = 0
for _,value in pairs(login_queue:get_data()) do
if type(value) == "table" then
index = index + 1
agent = value["agent"]
if not agent or not agent:is_valid() or not agent:is_authed() then
invalid = invalid + 1
end
if agent and agent:is_valid() and agent:is_authed() then
agent:send_message(MSG_WAIT_QUEUE_NUMBER,
(k - invalid + 1) + get_login_list_size())
end
end
end
end
local function _respond_login()
if not is_server_load_limit() and get_login_list_size() <= login_size_limit then
local login_data = login_queue:pop_first()
if login_data then
if login_data["agent"]:is_valid() and login_data["agent"]:is_authed()
and type(login_data["func"]) == "function" then
login_list[login_data["agent"]:get_uni_port_no()] = agent
login_data["func"](login_data["agent"], login_data["rid"], login_data["login_info"])
end
end
end
end
function cache_login(agent, rid, login_info, func)
if agent:is_valid() and agent:is_authed() then
local login_data = {
agent = agent,
rid = rid,
login_info = login_info,
func = func,
}
local is_vip = login_info["is_vip"]
if is_vip == 1 then
if not is_server_load_limit() then
login_list[agent:get_uni_port_no()] = agent
func(agent, rid, login_info)
else
login_queue:push_front(login_data)
end
else
if get_login_list_size() <= login_size_limit then
if not is_server_load_limit() then
login_list[agent:get_uni_port_no()] = agent
func(agent, rid, login_info)
else
login_queue:push_back(login_data)
end
else
login_queue:push_back(login_data)
end
end
end
end
function remove_login_list_agent(port_no)
login_list[port_no] = nil
end
function is_server_load_limit()
local cpu = SYSTEM_D.get_cpu_ratio_avg() or 0
if cpu < cpu_useage_limit then
return false
end
return false
end
function respond_login()
_respond_login()
set_timer(500, _respond_login, nil, true)
end
function notify_queue_number()
_notify_queue_number()
set_timer(6000, _notify_queue_number, nil, true)
end
function get_online_limit()
return online_limit
end
function set_online_limit(num)
if not is_int(num) then
trace("%o不为数字", num)
return
end
online_limit = num
end
local function func_login_ok(user)
remove_login_list_agent(user:get_uni_port_no())
end
function create()
login_queue = clone_object(QUEUE_TDCLS)
register_post_init(respond_login)
register_post_init(notify_queue_number)
register_as_audience("LOGIN_QUEUE_D", {EVENT_USER_LOGIN = func_login_ok})
end
create()