Пример #1
0
    /// <summary>
    /// 消息解码
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static SocketModel MDecode(byte[] value)
    {
        ByteArray   ba    = new ByteArray(value);
        SocketModel model = new SocketModel();
        byte        type;
        int         area;
        int         command;

        //从数据中读取 三层协议  读取数据顺序必须和写入顺序保持一致
        ba.read(out type);
        ba.read(out area);
        ba.read(out command);
        model.Type    = type;
        model.Area    = area;
        model.Command = command;
        //判断读取完协议后 是否还有数据需要读取 是则说明有消息体 进行消息体读取
        if (ba.Readnable)
        {
            byte[] message;
            //将剩余数据全部读取出来
            ba.read(out message, ba.Length - ba.Position);
            //反序列化剩余数据为消息体
            model.Message = SerializeUtil.Decode(message);
        }
        ba.Close();
        return(model);
    }
Пример #2
0
        /// <summary>
        /// 消息体的反序列化
        /// </summary>
        /// <param name="value">序列化的二进制码</param>
        /// <returns></returns>
        public static object MessageDecode(byte[] value)
        {
            ByteArray   ba    = new ByteArray(value);
            SocketModel model = new SocketModel();
            byte        type;
            int         area;
            int         command;

            ba.Read(out type);
            ba.Read(out area);
            ba.Read(out command);
            model.Type    = type;
            model.Area    = area;
            model.Command = command;

            if (ba.ReadEnable)
            {
                byte[] message;
                ba.Read(out message, ba.Length - ba.Position);
                model.Message = SerializeUtil.Decode(message);
            }
            ba.Close();
            return(model);
        }