This class is used to marshal simple types.
        /// <summary>
        /// Encode pack.
        /// </summary>
        /// <returns>Encoded bytes.</returns>
        public override byte[] Encode()
        {
            RESPONSE_MESSAGE responseMessage = new RESPONSE_MESSAGE();

            responseMessage.MESSAGEBODY   = this.msgBLKLIST;
            responseMessage.MESSAGEHEADER = this.MessageHeader;
            byte[] ret;

            List <byte> temp = new List <byte>();

            byte[] tempPayload;

            List <byte> listRet = new List <byte>();

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).SizeOfSegmentId, false));
            listRet.AddRange(((MSG_BLKLIST)responseMessage.MESSAGEBODY).SegmentId);
            while (listRet.Count % 4 != 0)
            {
                listRet.Add(byte.MinValue);
            }

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRangeCount, false));
            for (int i = 0; i < ((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRangeCount; i++)
            {
                listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRanges[i].Index, false));
                listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).BlockRanges[i].Count, false));
            }

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLKLIST)responseMessage.MESSAGEBODY).NextBlockIndex, false));
            tempPayload = listRet.ToArray();

            responseMessage.MESSAGEHEADER.MsgSize = (uint)tempPayload.Length + 16;

            List <byte> listRet1 = new List <byte>();

            listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MinorVersion, false));
            listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MajorVersion, false));
            listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.MsgType, false));
            listRet1.AddRange(MarshalHelper.GetBytes(responseMessage.MESSAGEHEADER.MsgSize, false));
            listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.CryptoAlgoId, false));

            temp.AddRange(listRet1.ToArray());
            temp.AddRange(tempPayload);
            temp.InsertRange(0, MarshalHelper.GetBytes((uint)temp.Count, false));
            ret = (byte[])temp.ToArray();

            return(ret);
        }
Example #2
0
        /// <summary>
        /// Encode pack.
        /// </summary>
        /// <returns>Encode bytes.</returns>
        public override byte[] Encode()
        {
            REQUEST_MESSAGE requestMessage = new REQUEST_MESSAGE();

            requestMessage.MESSAGEBODY   = this.msgGetBLKS;
            requestMessage.MESSAGEHEADER = this.MessageHeader;
            byte[] ret;

            List <byte> temp = new List <byte>();

            byte[] tempPayload;

            List <byte> listRet = new List <byte>();

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).SizeOfSegmentID, false));
            listRet.AddRange(((MSG_GETBLKS)requestMessage.MESSAGEBODY).SegmentID);
            while (listRet.Count % 4 != 0)
            {
                listRet.Add(byte.MinValue);
            }

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRangeCount, false));
            for (int i = 0; i < ((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRanges.Length; i++)
            {
                listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRanges[i].Index, false));
                listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).ReqBlockRanges[i].Count, false));
            }

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_GETBLKS)requestMessage.MESSAGEBODY).SizeOfDataForVrfBlock, false));
            tempPayload = listRet.ToArray();

            requestMessage.MESSAGEHEADER.MsgSize = (uint)tempPayload.Length + 16;

            List <byte> listRet1 = new List <byte>();

            listRet1.AddRange(MarshalHelper.GetBytes((ushort)requestMessage.MESSAGEHEADER.ProtVer.MinorVersion, false));
            listRet1.AddRange(MarshalHelper.GetBytes((ushort)requestMessage.MESSAGEHEADER.ProtVer.MajorVersion, false));
            listRet1.AddRange(MarshalHelper.GetBytes((uint)requestMessage.MESSAGEHEADER.MsgType, false));
            listRet1.AddRange(MarshalHelper.GetBytes(requestMessage.MESSAGEHEADER.MsgSize, false));
            listRet1.AddRange(MarshalHelper.GetBytes((uint)requestMessage.MESSAGEHEADER.CryptoAlgoId, false));

            temp.AddRange(listRet1.ToArray());
            temp.AddRange(tempPayload);
            ret = (byte[])temp.ToArray();

            return(ret);
        }
        /// <summary>
        /// Decode pack.
        /// </summary>
        /// <param name="rawdata">The rawdata.</param>
        /// <returns>The PccrrPacket.</returns>
        public override PccrrPacket Decode(byte[] rawdata)
        {
            if (rawdata == null)
            {
                throw new ArgumentNullException("rawdata");
            }

            if (rawdata.Length == 0)
            {
                throw new ArgumentException("The raw data should not be empty.");
            }

            PccrrNegoResponsePacket packet = new PccrrNegoResponsePacket();

            int messageLength = 0;

            messageLength = rawdata.Length;

            if (messageLength > 0)
            {
                int index = 0;

                byte[] data = rawdata;

                RESPONSE_MESSAGE ret = new RESPONSE_MESSAGE();
                ret.TRANSPORTRESPONSEHEADER.Size = MarshalHelper.GetUInt32(data, ref index, false);

                MESSAGE_HEADER ret1 = new MESSAGE_HEADER();
                ret1.ProtVer.MinorVersion = MarshalHelper.GetUInt16(data, ref index, false);
                ret1.ProtVer.MajorVersion = MarshalHelper.GetUInt16(data, ref index, false);
                ret1.MsgType      = (MsgType_Values)MarshalHelper.GetUInt32(data, ref index, false);
                ret1.MsgSize      = MarshalHelper.GetUInt32(data, ref index, false);
                ret1.CryptoAlgoId = (CryptoAlgoId_Values)MarshalHelper.GetUInt32(data, ref index, false);

                MSG_NEGO_RESP ret2 = new MSG_NEGO_RESP();
                ret2.MinSupporteProtocolVersion.MinorVersion = MarshalHelper.GetUInt16(data, ref index, false);
                ret2.MinSupporteProtocolVersion.MajorVersion = MarshalHelper.GetUInt16(data, ref index, false);
                ret2.MaxSupporteProtocolVersion.MinorVersion = MarshalHelper.GetUInt16(data, ref index, false);
                ret2.MaxSupporteProtocolVersion.MajorVersion = MarshalHelper.GetUInt16(data, ref index, false);

                packet.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                packet.MsgNegoResp             = ret2;
                packet.MessageHeader           = ret1;
            }

            return(packet);
        }
Example #4
0
        /// <summary>
        /// Decode MSG_BLKLIST message.
        /// </summary>
        /// <param name="data">The data to be decoded.</param>
        /// <param name="index">Started index.</param>
        /// <returns>The MSG_BLKLIST struct.</returns>
        private MSG_BLKLIST DecodeMSG_BLKLIST(byte[] data, ref int index)
        {
            MSG_BLKLIST ret = new MSG_BLKLIST();

            ret.SizeOfSegmentId = MarshalHelper.GetUInt32(data, ref index, false);
            ret.SegmentId       = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId);
            ret.ZeroPad         = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId % 4);
            if (ret.ZeroPad == null)
            {
                ret.ZeroPad = new byte[0];
            }

            ret.BlockRangeCount = MarshalHelper.GetUInt32(data, ref index, false);
            ret.BlockRanges     = new BLOCK_RANGE[ret.BlockRangeCount];
            for (int i = 0; i < ret.BlockRangeCount; i++)
            {
                ret.BlockRanges[i].Index = MarshalHelper.GetUInt32(data, ref index, false);
                ret.BlockRanges[i].Count = MarshalHelper.GetUInt32(data, ref index, false);
            }

            ret.NextBlockIndex = MarshalHelper.GetUInt32(data, ref index, false);
            return(ret);
        }
        /// <summary>
        /// Encode pack.
        /// </summary>
        /// <returns>Encode bytes.</returns>
        public override byte[] Encode()
        {
            RESPONSE_MESSAGE responseMessage = new RESPONSE_MESSAGE();

            responseMessage.MESSAGEBODY   = this.msgNegoResp;
            responseMessage.MESSAGEHEADER = this.MessageHeader;
            byte[] ret;

            List <byte> temp = new List <byte>();

            byte[] tempPayload;

            List <byte> listRet = new List <byte>();

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MinSupporteProtocolVersion.MinorVersion, false));
            listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MinSupporteProtocolVersion.MajorVersion, false));
            listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MaxSupporteProtocolVersion.MinorVersion, false));
            listRet.AddRange(MarshalHelper.GetBytes(((MSG_NEGO_RESP)responseMessage.MESSAGEBODY).MaxSupporteProtocolVersion.MajorVersion, false));
            tempPayload = listRet.ToArray();

            responseMessage.MESSAGEHEADER.MsgSize = (uint)tempPayload.Length + 16;

            List <byte> listRet1 = new List <byte>();

            listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MinorVersion, false));
            listRet1.AddRange(MarshalHelper.GetBytes((ushort)responseMessage.MESSAGEHEADER.ProtVer.MajorVersion, false));
            listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.MsgType, false));
            listRet1.AddRange(MarshalHelper.GetBytes(responseMessage.MESSAGEHEADER.MsgSize, false));
            listRet1.AddRange(MarshalHelper.GetBytes((uint)responseMessage.MESSAGEHEADER.CryptoAlgoId, false));

            temp.AddRange(listRet1.ToArray());
            temp.AddRange(tempPayload);
            temp.InsertRange(0, MarshalHelper.GetBytes((uint)temp.Count, false));
            ret = (byte[])temp.ToArray();

            return(ret);
        }
Example #6
0
        /// <summary>
        /// Decode response message.
        /// </summary>
        /// <param name="rawdata">The raw data.</param>
        /// <returns>The PccrrPacket.</returns>
        public PccrrPacket DecodeResponseMessage(byte[] rawdata)
        {
            if (rawdata == null)
            {
                throw new ArgumentNullException("rawdata");
            }

            if (rawdata.Length == 0)
            {
                throw new ArgumentException("The raw data should not be empty.");
            }

            int messageLength = 0;

            messageLength = rawdata.Length;

            PccrrPacket packet = null;

            if (messageLength > 0)
            {
                int index = 0;

                RESPONSE_MESSAGE ret = new RESPONSE_MESSAGE();
                ret.TRANSPORTRESPONSEHEADER.Size = MarshalHelper.GetUInt32(rawdata, ref index, false);
                ret.MESSAGEHEADER = this.DecodeMessageHeader(rawdata, ref index);

                switch (ret.MESSAGEHEADER.MsgType)
                {
                case MsgType_Values.MSG_BLKLIST:
                    PccrrBLKLISTResponsePacket pccrrBLKLISTResponsePacket = new PccrrBLKLISTResponsePacket();

                    MSG_BLKLIST msgBLKLIST = this.DecodeMSG_BLKLIST(rawdata, ref index);
                    pccrrBLKLISTResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrBLKLISTResponsePacket.MsgBLKLIST    = msgBLKLIST;
                    pccrrBLKLISTResponsePacket.MessageHeader = ret.MESSAGEHEADER;
                    packet = pccrrBLKLISTResponsePacket;
                    break;

                case MsgType_Values.MSG_BLK:
                    PccrrBLKResponsePacket pccrrBLKResponsePacket = new PccrrBLKResponsePacket();

                    MSG_BLK msgBLK = this.DecodeMSG_BLK(rawdata, ref index);
                    pccrrBLKResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrBLKResponsePacket.MsgBLK        = msgBLK;
                    pccrrBLKResponsePacket.MessageHeader = ret.MESSAGEHEADER;
                    packet = pccrrBLKResponsePacket;
                    break;

                case MsgType_Values.MSG_NEGO_RESP:
                    PccrrNegoResponsePacket pccrrNegoResponsePacket = new PccrrNegoResponsePacket();

                    MSG_NEGO_RESP msgNEGORESP = this.DecodeMSG_NEGO_RESP(rawdata, ref index);
                    pccrrNegoResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrNegoResponsePacket.MsgNegoResp             = msgNEGORESP;
                    pccrrNegoResponsePacket.MessageHeader           = ret.MESSAGEHEADER;
                    packet = pccrrNegoResponsePacket;
                    break;

                case MsgType_Values.MSG_SEGLIST:
                    PccrrSegListResponsePacket pccrrSegListResponsePacket = new PccrrSegListResponsePacket();
                    pccrrSegListResponsePacket.MsgSegList = TypeMarshal.ToStruct <MSG_SEGLIST>(rawdata, ref index);
                    pccrrSegListResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrSegListResponsePacket.MessageHeader           = ret.MESSAGEHEADER;
                    packet = pccrrSegListResponsePacket;
                    break;
                }
            }

            return(packet);
        }