--[[
内部通信协议模板规则如下( world <-->logic通信)
1.  在每个模块的Proto.lua文件中定义[ WL(world->logic)或 LW(logic->world) ]开头的协议
2.  LW协议在每个模块的HandlerW协议中处理
3.  {"v1",  "int"},
    {"v2",  "string"},
    {"v3",  "table"},
--]]

proto_id = proto_id or {}
proto_handler = proto_handler or {}
proto_name = proto_name or {}
proto_template = proto_template or {}

wl = wl or {} --world -> logic
lw = lw or {} --logic -> world

local msg_parse = _G.msg_inner_parse 
local Config = require("Config")
local Log = require("common.Log")

function register(name, id, template, handler)
    if not proto_name[id] then
        if string.sub(name, 1, 2) == "WL" then
            if _G.is_middle then
                wl[name] = {[1] = id}
            else
                wl[id] = {[1] = id}
            end     
            
        elseif string.sub(name, 1, 2) == "LW" then
            if _G.is_middle then
                lw[id] = {[1] = id}
            else
                lw[name] = {[1] = id}
            end        
        end
    end
    proto_handler[id] = handler
    proto_name[id] = name
    proto_template[id] = template
    for i = 1, #template do
        if template[i][2] == "int" then
            template[i][3] = 0
        elseif template[i][2] == "string" then
            template[i][3] = 1
        elseif template[i][2] == "table" then
            template[i][3] = 2
		elseif template[i][2] == "chunk" then--发送时压缩，读取时不解压
            template[i][3] = 3
		elseif template[i][2] == "binary" then
            template[i][3] = 4
        elseif template[i][2] == "zip" then--发送时压缩，读取时解压
            template[i][3] = 5
        end
		
    end
end


function readMsg(id, msg)
    if not msg then
        -- Log.write(Log.LOGID_DEBUG, "[readMsg] msg参数为nil, id="..(id or "nil"))
        return
    end
    local template = proto_template[id]
    if not template then
        assert(nil, "readMsg no template: id:"..id)
    end
    
    local ret
    for i = 1, #template do
        if template[i][3] == 0 then
            ret, msg[template[i][1] ] = msg_parse.readint()
            if not ret then
                assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
            end
        elseif template[i][3] == 1 then
            ret, msg[template[i][1] ] = msg_parse.readstring()
            if not ret then
                assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
            end
        elseif template[i][3] == 2 then
            msg[template[i][1] ] = {}
            ret = msg_parse.readtable(msg[template[i][1]])
            if not ret then
                assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
            end
        elseif template[i][3] == 3 then
            ret, msg[template[i][1]] = msg_parse.readchunk()
            if not ret then
                assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
            end
        elseif template[i][3] == 4 then
			msg[template[i][1]] = {}
            ret = msg_parse.readbinary( msg[template[i][1]])
            if not ret then
                assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
            end
        elseif template[i][3] == 5 then
			msg[template[i][1]] = {}
            ret = msg_parse.readzip( msg[template[i][1]])
            if not ret then
                assert(nil, "inner msg:"..proto_name[id].." "..template[i][1])
            end
        end
    end
end


function sendMsg(fd, msg)
    local id = msg[1]
    local template = proto_template[id]
    if not template then
        assert(nil, "sendMsg no template: id:"..id)
    end
	
	--Log.write(Log.LOGID_INNER_CLOSE, "inner socket close...,drop inner msg id:", id)
    if not _G.is_middle and not msg_parse.check_connect() then --链接断了，写日志
        print("inner socket close...,drop inner msg id--"..id)
        return 
    end
    msg_parse.writebegin(fd, id)
    for i = 1, #template do
        if template[i][3] == 0 then
            msg_parse.writeint(msg[template[i][1]])
        elseif template[i][3] == 1 then
			msg_parse.writestring(msg[template[i][1]])
        elseif template[i][3] == 2 then
			msg_parse.writetable(msg[template[i][1]])
        elseif template[i][3] == 3 then
			msg_parse.writechunk(msg[template[i][1]])
		elseif template[i][3] == 4 then
			msg_parse.writebinary(msg[template[i][1]], string.len(msg[template[i][1]]))
        elseif template[i][3] == 5 then
			msg_parse.writezip(msg[template[i][1]])
        end         
    end
    msg_parse.writeend()
end



function unbinary(binary, len , out)
   return msg_parse.unbinary(binary, len , out)
end