/// <summary> /// 将mssage转换为bytes /// </summary> /// <param name="message"></param> /// <returns></returns> public byte[] Pack(IMessage message) { using (memoryStream = new MemoryStream()) { memoryStream.Position = 0; BinaryWriter writer = new BinaryWriter(memoryStream); int msglen = message.Length; //消息长度 writer.Write(msglen); byte cmd = (byte)(message.Id >> 8); byte act = (byte)(message.Id & 0x00ff); //消息ID writer.Write(cmd); writer.Write(act); //错误码 writer.Write((ushort)0); //消息内容 writer.Write(message.Bytes); writer.Flush(); byte[] payload = memoryStream.ToArray(); message.Clear(); messagePool.Push(message); return(payload); } }
public static void Push(object obj) { IObjectPool pool = null; Instance._objectPools.TryGetValue(obj.GetType(), out pool); if (null != pool) { pool.Push(obj); } }