Example #1
0
    public void SerializeData(SendDataStruct data)
    {
        MemoryStream memStream = new MemoryStream();

        ProtoBuf.Serializer.Serialize(memStream, data.instance);

        byte[] body = memStream.ToArray();
        CMsg   _nnn = new CMsg();

        _nnn.dest = data.dest;
        _nnn.cmd  = data.cmd;
        if (body != null && body.Length > 0)
        {
            _nnn.body = body;
        }

        //放在消息队列里面发
        messageStruct smsg = new messageStruct();

        smsg.dest = data.dest;
        smsg.cmd  = data.cmd;
        smsg.body = PBParseManage.getSerialize <CMsg>(_nnn);

        NetWorkManage.getInstance().PushSendDataQueue(smsg);
    }
Example #2
0
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 100, 50), "连接测试"))
        {
            Debug.Log("开始连接");

            string ip   = "192.168.0.43";
            int    port = 6000;

            NetWorkManage s_NetWorkManage = NetWorkManage.getInstance();
            s_NetWorkManage.setHostPort(ip, port);
            if (!s_NetWorkManage.Connect())
            {
                Debug.Log("服务器连接失败,请检查网络!");
                return;
            }
        }

        if (GUI.Button(new Rect(150, 10, 100, 50), "登陆"))
        {
            //登录请求
            Debug.Log("登录请求");

            Msg_2_2 msg = new Msg_2_2();
            msg.aid    = "Test02"; //先写死角色名
            msg.zoneId = 1;        //先写死1

            SendDataStruct data = new SendDataStruct();
            data.cmd      = 2;
            data.dest     = 2;
            data.instance = msg;

            //NetWorkManage.SendData(2, 2, msg);
            NetWorkManage.SendData(data);
        }

        if (GUI.Button(new Rect(350, 10, 100, 50), "login"))
        {
            //登录请求
            Debug.Log("login界面");
            GameObject obj = UILayerManage.getInstance().CreateRoot("UILogin", "UILogin");
        }

        if (GUI.Button(new Rect(500, 10, 100, 50), "抽象工厂模式"))
        {
            IProtocolFactory factory = new ProtocolFactory();

            factory.createProtocolType1().DeserializeData(null);
            factory.createProtocolType2().DeserializeData(null);
        }
    }
Example #3
0
    public object DeserializeData(object source)
    {
        Debug.Log("DeserializeData");

        if (source == null)
        {
            return(null);
        }

        Stream stream = (Stream)source;

        CMsg cmsg       = ProtoBuf.Serializer.Deserialize <CMsg>(stream);
        int  cmd        = cmsg.cmd;
        int  dest       = cmsg.dest;
        long serverTime = cmsg.serverTime;

        byte[] body = cmsg.body;


        //消息号保存进队列
        PBNO _pb = new PBNO();

        _pb.msg = cmsg;

        //分发协议去解析
        PBParseManage.getInstance().ParseData(dest, cmd, body);

        if (NetWorkManage.getInstance().IsOftenData(dest, cmd))
        {
            NetWorkManage.getInstance().PushReceDataScene(_pb);
        }
        else
        {
            NetWorkManage.getInstance().PushReceDataQueue(_pb);
        }

        Debug.Log("收到消息 dest=" + dest + ",cmd=" + cmd);

        return(null);
    }
Example #4
0
    public void DeserializeData(ReceDataStruct source)
    {
        if (source == null)
        {
            return;
        }

        Stream stream = new MemoryStream(source.data, 0, source.datalen);

        CMsg cmsg       = ProtoBuf.Serializer.Deserialize <CMsg>(stream);
        int  cmd        = cmsg.cmd;
        int  dest       = cmsg.dest;
        long serverTime = cmsg.serverTime;

        byte[] body = cmsg.body;

        //消息号保存进队列
        messageStruct _pb = new messageStruct();

        _pb.cmd        = cmsg.cmd;
        _pb.dest       = cmsg.cmd;
        _pb.body       = cmsg.body;
        _pb.serverTime = cmsg.serverTime;

        //分发协议去解析
        PBParseManage.getInstance().ParseData(dest, cmd, body);

        if (NetWorkManage.getInstance().IsOftenData(dest, cmd))
        {
            NetWorkManage.getInstance().PushReceDataScene(_pb);
        }
        else
        {
            NetWorkManage.getInstance().PushReceDataQueue(_pb);
        }

        Debug.Log("收到消息 dest=" + dest + ",cmd=" + cmd);
    }