public void PutReceiveMsgToPool(byte[] recvMsg) { //Debug.Log(Encoding.UTF8.GetString(recvMsg)); //ushort id = BitConverter.ToUInt16(recvMsg, 0); //Debug.Log(id); //byte[] result = new byte[recvMsg.Length - 2]; //Buffer.BlockCopy(recvMsg, 2, result, 0, recvMsg.Length-2); //Debug.Log(Encoding.UTF8.GetString(result)); //NetMsgBase tmpNet = new NetMsgBase(id, result); //Debug.Log(string.Format("收到消息 msgid{0} 消息:{1}", id, Encoding.UTF8.GetString(tmpNet.buffer))); NetModel model = ProtoSerialize.DeSerialize(recvMsg); recvMsgPool.Enqueue(new NetMsg(model)); }
public static NetModel DeSerialize(byte[] msg) { try { using (MemoryStream ms = new MemoryStream()) { ms.Write(msg, 0, msg.Length); ms.Position = 0; NetModel model = ProtoBuf.Serializer.Deserialize <NetModel>(ms); return(model); } } catch (System.Exception e) { Debug.Log("DeSerialize Failed: " + e.ToString()); return(null); } }
public static byte[] Serialize(NetModel model) { try { using (MemoryStream ms = new MemoryStream()) { ProtoBuf.Serializer.Serialize <NetModel>(ms, model); byte[] result = new byte[ms.Length]; ms.Position = 0; ms.Read(result, 0, result.Length); return(result); } } catch (System.Exception e) { Debug.Log("Serialize Failed: " + e.ToString()); return(null); } }
public NetMsg(NetModel model) : base(model.MsgID) { this.model = model; }