ngxd.lua 2.33 KB
skynet = require "skynet"
redisproxy = require "shared.redisproxy"
netpack = require "skynet.netpack"
datacenter = require "skynet.datacenter"
sharedata = require "skynet.sharedata"
mcast_util = require "services/mcast_util"
globalCsv = require "csvdata/GlobalDefine"

local socket = require "skynet.socket"
local harbor = require "skynet.harbor"

require "shared.init"
require "utils.init"
require "ProtocolCode"
require "skynet.manager"
require "RedisKeys"
require "GlobalVar"

local port = tonumber(...)
local SEP = "$end$"
local actions = {}

SendPacket = function(...) end
rpcAgent = function(...) end
rpcParter = function(...) end
rpcRole = function(...) end
rpcUnion = function(...) end
rpcOtherUnion = function(...) end

skynet.register_protocol {
	name = "role",
	id = 13,
	pack = skynet.pack,
	unpack = skynet.unpack,
}

local function process(request)
	local req = json.decode(request)
	if req["handle"] == "checkStatus" then
		return "success"
	end
	
	local modName, funcName = req["handle"]:match("([%w_]+)%.([%w_]+)")
	if not modName or not funcName then
		return table.concat({"handle格式错误", modName, funcName}, "\t")
	end
	local action = require("actions." .. modName .. "Action")
	return action[funcName](req)
end

local function main_loop(stdin, send)
	socket.lock(stdin)
	while true do
		local request = socket.readline(stdin, SEP)
		if not request then
			break
		end

		local response = process(request)
		if response then send(response) end
	end
	socket.unlock(stdin)
end

local function start()
	local listen_socket = socket.listen("0.0.0.0", port)
	print("Start nginx proxy at port: ", port)

	redisd = harbor.queryname("REDIS")
	
	csvdb = sharedata.query("csvdata")

	socket.start(listen_socket, function(id, addr)
		local function send(...)
			local t = { ... }
			socket.write(id, table.concat(t, "\t"))
			socket.write(id, SEP)
		end
		socket.start(id)
		skynet.fork(main_loop, id, send)
	end)

	-- 注册全服广播
	local channels = {}
	for i = 1, 10 do
		local channel = datacenter.get("MC_W_CHANNEL" .. i)
		if channel then
			table.insert(channels, channel)
		end
	end
	if #channels > 0 then
		mcast_util.sub_worlds(channels)
	end
end

local function __init__()
	skynet.dispatch("lua", function(_,_, command, ...)
		if command == "start" then
			skynet.ret(skynet.pack(start(...)))
		end
	end)

	skynet.register "NGXD"
end

skynet.start(__init__)