Exemple #1
0
    //解析protocol 协议
    public override void ParseData(string msgIndex, byte[] _data)
    {
        Debug.Log("解析了MDaccount");
        global::ProtoBuf.IExtensible retIE = null;
        switch (msgIndex)
        {
        case "Msg_2_2_":
        {
            //Msg_2_2_ cmsg = null;
            //if (_data != null) cmsg = ProtoBuf.Serializer.Deserialize<Msg_2_2_>(new MemoryStream(_data));
            //Fun_Msg_2_2_(cmsg);
            //retIE = cmsg;


            //Msg_2_2_ cmsg = (Msg_2_2_)GDDirector.getInstance().getCurrentFactory().createProtocolType1().Deserialize(_data);
            //Fun_Msg_2_2_(cmsg);
            //retIE = cmsg;
            IProtocolFactory factory = new ProtocolFactory();
            Msg_2_2_         cmsg    = (Msg_2_2_)factory.createProtocolType1().Deserialize(_data);
            Fun_Msg_2_2_(cmsg);
            retIE = cmsg;

            break;
        }
            //添加case 自动生成需要这行注释,不能删,不能改。
        }
        //return retIE;
    }
        public bool SendMessage(int type, global::ProtoBuf.IExtensible msg)
        {
            if (!_isConnected)
            {
                return(false);
            }

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            ProtoBuf.Serializer.Serialize(stream, msg);
            byte[] bMsg = stream.ToArray();

            int len = 4 + bMsg.Length;

            byte[] bLen = BitConverter.GetBytes(len);
            System.Array.Reverse(bLen);
            byte[] bType = BitConverter.GetBytes(type);
            System.Array.Reverse(bType);

            byte[] bSend = new byte[4 + len];
            Array.Copy(bLen, 0, bSend, 0, 4);
            Array.Copy(bType, 0, bSend, 4, 4);
            Array.Copy(bMsg, 0, bSend, 8, len - 4);
            _socketClient.Send(bSend);

            return(true);
        }
Exemple #3
0
    public static MessageStruct SerializeData(int dest, int cmd, global::ProtoBuf.IExtensible instance)
    {
        MemoryStream memStream = new MemoryStream();

        ProtoBuf.Serializer.Serialize(memStream, instance);

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

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

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

        smsg.dest = dest;
        smsg.cmd  = cmd;
        smsg.body = (byte[])Serialize(msg);

        return(smsg);
    }
Exemple #4
0
        public static string FormattedString(this global::ProtoBuf.IExtensible proto)
        {
            XmlSerializer serializer = new XmlSerializer(proto.GetType());

            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, proto);
                return(writer.ToString());
            }
        }
Exemple #5
0
    public object Deserialize(object data)
    {
        if (data == null)
        {
            return(null);
        }

        byte[] sourse = (byte[])data;
        global::ProtoBuf.IExtensible result = ProtoBuf.Serializer.Deserialize <Msg_2_2_>(new MemoryStream(sourse));
        return(result);
    }
Exemple #6
0
    //解析protocol 协议
    public override void ParseData(string msgIndex, byte[] _data)//public global::ProtoBuf.IExtensible ParseData(string msgIndex, byte[] _data)
    {
        Debug.Log("解析了MDaccount");
        global::ProtoBuf.IExtensible retIE = null;
        switch (msgIndex)
        {
        case "Msg_2_1_":
        {
            Msg_2_1_ cmsg = null;
            if (_data != null)
            {
                cmsg = ProtoBuf.Serializer.Deserialize <Msg_2_1_>(new MemoryStream(_data));
            }
            Fun_Msg_2_1_(cmsg);
            retIE = cmsg;
            break;
        }

        case "Msg_2_2_":
        {
            Msg_2_2_ cmsg = null;
            if (_data != null)
            {
                cmsg = ProtoBuf.Serializer.Deserialize <Msg_2_2_>(new MemoryStream(_data));
            }
            Fun_Msg_2_2_(cmsg);
            retIE = cmsg;
            break;
        }

        case "Msg_2_3_":
        {
            //Msg_2_3_ cmsg = null;
            //if(_data!=null) cmsg = ProtoBuf.Serializer.Deserialize<Msg_2_3_>(new MemoryStream(_data));
            //Fun_Msg_2_3_(cmsg);
            //retIE=cmsg;
            break;
        }

        case "Msg_2_6_":
        {
            Msg_2_6_ cmsg = null;
            if (_data != null)
            {
                cmsg = ProtoBuf.Serializer.Deserialize <Msg_2_6_>(new MemoryStream(_data));
            }
            Fun_Msg_2_6_(cmsg);
            retIE = cmsg;
            break;
        }
            //添加case 自动生成需要这行注释,不能删,不能改。
        }
        //return retIE;
    }
Exemple #7
0
 //有待重新实现 20150914
 //获取当前传过来的协议信息
 public T getCurrMsg <T>(int dest, int cmd) where T : global::ProtoBuf.IExtensible
 {
     global::ProtoBuf.IExtensible pbInstance = null;
     //if (pbnoCurr.msg == null)
     //{
     //    return (T)pbInstance;
     //}
     //if (pbnoCurr.msg.cmd == cmd && pbnoCurr.msg.dest == dest && pbnoCurr.msg != null)
     //{
     //    return (T)pbnoCurr;
     //}
     return((T)pbInstance);
 }
Exemple #8
0
    //
    // To bytes
    //
    public static byte[] ToArray(short message, global::ProtoBuf.IExtensible proto)
    {
        MemoryStream memStream = new MemoryStream();

        ProtoBuf.Serializer.Serialize(memStream, proto);

        byte[] msg    = BitConverter.GetBytes(message);
        byte[] buffer = new byte[msg.Length + memStream.Length];
        Array.Copy(msg, 0, buffer, 0, msg.Length);
        Array.Copy(memStream.ToArray(), 0, buffer, msg.Length, memStream.Length);

        return(buffer);
    }
Exemple #9
0
 //获取当前传过来的协议信息
 public T getCurrMsg <T>(int dest, int cmd) where T : global::ProtoBuf.IExtensible
 {
     global::ProtoBuf.IExtensible pbInstance = null;
     if (pbnoCurr.pbInstance == null)
     {
         return((T)pbInstance);
     }
     if (pbnoCurr.msg.cmd == cmd && pbnoCurr.msg.dest == dest && pbnoCurr.pbInstance != null)
     {
         return((T)pbnoCurr.pbInstance);
     }
     return((T)pbInstance);
 }
        /*
         * 向服务端发送数据
         * **/
        public void sendToServer(global::ProtoBuf.IExtensible netMsg)
        {
            Type msgType = netMsg.GetType();
            uint msgId   = NetMsgMap.instance.getMsgIdByType(msgType);

            byte[] byteArr
                = Parse <global::ProtoBuf.IExtensible>(netMsg, msgId);

            if (null == _clientNet)
            {
                _clientNet = ClientNet.Singleton;
            }

            _clientNet.SendPacket(byteArr);
        }
Exemple #11
0
 protected void SendProto(ProtoGameClient.REQUEST_MSG msg, global::ProtoBuf.IExtensible proto)
 {
     try
     {
         if (IsConnected())
         {
             Package package = new Package();
             package.buffer = ProtoHelper.ToArray((short)msg, proto);
             package.size   = (short)package.buffer.Length;
             Send(package);
         }
     }
     catch (Exception)
     {
     }
 }
Exemple #12
0
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //public void ParseData(int dest, int cmd, byte[] _data)
    //{

    //    string msgindex = "Msg_";
    //    msgindex += dest + "_";
    //    msgindex += cmd + "_";
    //    if (dictPBParseBasic.ContainsKey(msgindex))
    //    {
    //        dictPBParseBasic[msgindex].ParseData(msgindex, _data);
    //    }
    //    else
    //    {
    //        Debug.LogError(msgindex + " no add!!!!!!!!!!!!!!!");
    //    }
    //}

    //解析协议
    public global::ProtoBuf.IExtensible ParseData(int dest, int cmd, byte[] _data)
    {
        string msgindex = "Msg_";

        msgindex += dest + "_";
        msgindex += cmd + "_";
        if (dictPBParseBasic.ContainsKey(msgindex))
        {
            global::ProtoBuf.IExtensible ret = dictPBParseBasic[msgindex].ParseData(msgindex, _data);
            return(ret);
        }
        else
        {
            Debug.LogError(msgindex + " no add!!!!!!!!!!!!!!!");
        }

        return(null);
    }
Exemple #13
0
 public SendDataStruct(int d, int c, global::ProtoBuf.IExtensible i)
 {
     dest     = d;
     cmd      = c;
     instance = i;
 }
Exemple #14
0
 public void setNull()
 {
     msg        = null;
     pbInstance = null;
 }