local skynet = require "skynet" local queue = require "skynet.queue" local bson = require "bson" local socketdriver = require "skynet.socketdriver" local serverId = tonumber(skynet.getenv("servId")) require "shared.init" require "skynet.manager" local table_insert = table.insert local pairs = pairs local ipairs = ipairs local string_format = string.format local CMD, cs = {} local log_fd, connecting = nil , false local socket_message = {} -- read skynet_socket.h for these macro -- SKYNET_SOCKET_TYPE_DATA = 1 socket_message[1] = function(id, size, data) skynet.error(string.format("LOG SOCKET: data: ", skynet.tostring(data, size))) socketdriver.drop(data, size) end -- SKYNET_SOCKET_TYPE_CONNECT = 2 socket_message[2] = function(id, _ , addr) skynet.error("LOG SOCKET: connect: ", addr) connecting = false end -- SKYNET_SOCKET_TYPE_CLOSE = 3 socket_message[3] = function(id) skynet.error("LOG SOCKET: closed") connecting = false end -- SKYNET_SOCKET_TYPE_ERROR = 5 socket_message[5] = function(id, _, err) skynet.error("LOG SOCKET: error: ", err) connecting = false end skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 unpack = socketdriver.unpack, dispatch = function (_, _, t, ...) if socket_message[t] then socket_message[t](...) end end } function CMD.log(doc) if not socketdriver.send(log_fd, json.encode(doc) .. "\n") then if not connecting then CMD.open() -- 连一下试试 socketdriver.send(log_fd, json.encode(doc) .. "\n") end end end function CMD.open() log_fd = socketdriver.connect("127.0.0.1", 5170) connecting = true end local function __init__() skynet.dispatch("lua", function (session, address, command, ...) local f = CMD[command] if command == "open" then skynet.ret(skynet.pack(f(...))) else local logType, doc, index_suffix = ... cs(function() f(logType, doc, index_suffix) end) end end) cs = queue() skynet.register(".log") end skynet.start(__init__)