/// <summary> /// 将bytes转换为message /// </summary> /// <param name="length"></param> /// <param name="bytes"></param> /// <returns></returns> public IMessage Unpack(byte[] bytes) { using (memoryStream = new MemoryStream()) { try { BinaryReader reader = new BinaryReader(memoryStream); memoryStream.Seek(0, SeekOrigin.End); memoryStream.Write(bytes, 0, bytes.Length); memoryStream.Seek(0, SeekOrigin.Begin); int messageLength = reader.ReadInt32(); byte cmd = reader.ReadByte(); byte act = reader.ReadByte(); ushort errorCode = reader.ReadUInt16(); byte[] data = reader.ReadBytes(messageLength); int msgId = (cmd << 8) + act; IMessage message = messagePool.Pop(); message.Id = msgId; message.Length = messageLength; message.Bytes = data; reader.Close(); return(message); } catch (Exception ex) { Debug.LogErrorFormat("UnpackMessage Error:{0}", ex.ToString()); } return(null); } }