美文网首页Quick-Cocos2dx-Community
使用SimpleTcp创建网络连接

使用SimpleTcp创建网络连接

作者: Kael_ | 来源:发表于2018-10-10 11:23 被阅读69次
--网络连接管理器
require("network.PacketBuffer");
require("network.socketProcesser");
require("network.socketCmd");

local SimpleTCP = require("framework.SimpleTCP")


SocketManager = class("SocketManager")

SocketManager.getInstance = function()
    printf("action", "SocketManager.getInstance ")
    if not SocketManager.s_instance then 
        SocketManager.s_instance = SocketManager.new();
    end
    return SocketManager.s_instance;
end

SocketManager.releaseInstance = function()
    printf("action", "SocketManager.releaseInstance ")
    if SocketManager.s_instance then
        SocketManager.s_instance:dtor();
    end
    SocketManager.s_instance = nil;
end

function SocketManager:ctor()
    printLog("action", "SocketManager:ctor ")
    self.m_socketProcessers = {};
    self.buf = PacketBuffer.new();
end

function SocketManager:dtor()
    printLog("action", "SocketManager:dtor ")
    
    self.m_commonProcesser = nil;
    if self._socket then
        self._socket:close();
        self._socket = nil;
    end
end
--开始连接socket
function SocketManager:openSocket()
    local host = SERVER_IP;
    local port = SERVIER_PORT;
    printLog("action SocketManager:openSocket", "------host ="..host);
    printLog("action SocketManager:openSocket", "------port ="..port);
    if not self._socket then
        self._socket = SimpleTCP.new(host, port,  handler(self, self.onTCPEvent));
        self._socket:connect();
    else
        kLoginInfo:login();
    end
end
function SocketManager:onTCPEvent( even, data )
    if even == SimpleTCP.EVENT_DATA then
        self:socket_data(data);
    elseif even == SimpleTCP.EVENT_CONNECTING then
        self:socket_connenting();
    elseif even == SimpleTCP.EVENT_CONNECTED then
        self:socket_connected();
        -- self.stcp:send("Hello server, i'm SimpleTCP")
    elseif even == SimpleTCP.EVENT_CLOSED then
        self:socket_close();
    elseif even == SimpleTCP.EVENT_FAILED then
        self:socket_failed();
    else
        printLog("action", "SocketManager:onTCPEvent 类型匹配错误!!!!")
    end
end
--socket 连接中
function SocketManager:socket_connenting()
    printLog("action", "socket_connenting-------")
end

--socket 连接失败
function SocketManager:socket_failed()
    printLog("action", "socket_failed-------")
end

--socket 连接成功
function SocketManager:socket_connected()
    printLog("action", "scoket_connected-------")
    --登录
    kLoginInfo:login();
end
--socket 关闭
function SocketManager:socket_close()
    printLog("action", "socket_close-------")
end
--socket发送消息
function SocketManager:send(code, subcode, msgContent)
    if not self._socket then
        return;
    end 
    local data = (msgContent ~= nil) and json.encode(msgContent) or nil;
    printLog("action", "客户端发送消息:".."code="..code.." subcode="..subcode);
    if json.encode(msgContent) then
        printLog("action", "客户端发送消息内容:"..json.encode(msgContent))
    end
    local buf = PacketBuffer.createMessage(code, subcode, data);
    self._socket:send(buf:getPack());
end
--socket 接收到数据
function SocketManager:socket_data(data)
    local msgs = self.buf:parseMessage(data); 
    for i=1,#msgs do
        local msg = msgs[i];
        for k, v in ipairs(self.m_socketProcessers) do
            local info = json.decode(msg.content);
            if v:onReceivePacket(msg.subcode, info) then
                break;
            end
        end
    end 
end
--添加监听
function SocketManager:addSocketProcesser(socketProcesser)
    if not self:checkExist(self.m_socketProcessers, socketProcesser) then
        table.insert(self.m_socketProcessers, 1, socketProcesser);
    end
end
--移除监听
function SocketManager:removeSocketProcesser(socketProcesser)
    local index = self:getIndex(self.m_socketProcessers,socketProcesser);
    if index ~= -1 then
        table.remove(self.m_socketProcessers,index);
    end
end
--监听index
function SocketManager:getIndex(vtable, value)
    for k,v in pairs(vtable or {}) do 
        if v == value then
            return k;
        end
    end

    return -1;
end

function SocketManager:checkExist(vtable, value)
    return self:getIndex(vtable,value) ~= -1;
end

相关文章

网友评论

    本文标题:使用SimpleTcp创建网络连接

    本文链接:https://www.haomeiwen.com/subject/rskpaftx.html