Пример #1
0
        //发送消息到服务器
        public void SendMsg(ProtoDefine protoType, IExtensible protoData)
        {
            //byte[] temp = Encoding.UTF8.GetBytes(msg);
            NetMsgData msg = NetMsgData.GetMsgData(protoType, protoData);

            byte[] data = NetUtilcs.PackNetMsg(msg);
            stream.Write(data, 0, data.Length);
            Debug.Log("发送消息 -> " + msg.ProtoId + data);
        }
Пример #2
0
 public void SendMsg1(ProtoDefine protoType, IExtensible protoData)
 {
     if (!this.mIsRunning)
     {
         return;
     }
     lock (this.mSendLock)
     {
         mSendWaitingMsgQueue.Enqueue(NetMsgData.GetMsgData(protoType, protoData));
         Monitor.Pulse(this.mSendLock);
     }
 }
Пример #3
0
        public static NetMsgData UnpackNetMsg(byte[] msgData)
        {
            MemoryStream ms = null;

            using (ms = new MemoryStream(msgData))
            {
                BinaryReader reader  = new BinaryReader(ms);
                ushort       msgLen  = reader.ReadUInt16();
                ushort       protoId = reader.ReadUInt16();

                if (msgLen <= msgData.Length - 4)
                {
                    IExtensible protoData = CreateProtoBuf.GetProtoData((ProtoDefine)protoId, reader.ReadBytes(msgLen));
                    return(NetMsgData.GetMsgData((ProtoDefine)protoId, protoData, msgLen));
                }
                else
                {
                    Debug.LogError("协议长度错误");
                }
            }

            return(null);
        }