Ejemplo n.º 1
0
        static byte[] Encode(object message)
        {
            if (message == null)
            {
                return(TransferPacket.EmptyByteArray);
            }
            var type     = message.GetType();
            var typeCode = BinarySerializer.GetTypeCode(type);

            byte[] body;
            try
            {
                body = BinarySerializer.Serialize(message);
            }
            catch (System.Exception ex)
            {
                //序列化报文异常
                SystemRunErrorPorvider.CatchException(ex);
                return(TransferPacket.EmptyByteArray);
            }
            var data = new byte[2 + body.Length];

            unsafe
            {
                // 消息主码
                data[0] = (byte)(typeCode / 256);
                //消息辅码
                data[1] = (byte)typeCode;
                // 2.包体。
                Buffer.BlockCopy(body, 0, data, 2, body.Length);

                return(TransferPacket.UnsafePacketData(TransferPacketType.Binnary, data));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="meeage"></param>
 public TransferMessage(string meeage)
 {
     this.UPacketData = TransferPacket.UnsafePacketData(TransferPacketType.Binnary, meeage.GetBytes());
     this._message    = meeage;
 }