The complete layout of a Response type Download Protocol Message sent over HTTP is:
        /// <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);
        }
        /// <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);
        }
        /// <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);
        }
Пример #4
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);
        }
        /// <summary>
        /// Send message MSG_NEGO_REQ
        /// </summary>
        public void SendMsgNegoReq()
        {
            PccrrNegoRequestPacket packet = this.pccrrStack.CreateMsgNegoRequest(this.minSupV, this.maxSupV, this.cryptoAlgo, MsgType_Values.MSG_NEGO_REQ, this.protoVer);

            this.pccrrStack.SendPacket(packet, new TimeSpan(0, 0, this.timeout));

            PccrrPacket respMSG = this.pccrrStack.ExpectPacket();

            PccrrNegoResponsePacket pccrrNegoResponsePacket = respMSG as PccrrNegoResponsePacket;

            MSG_NEGO_RESP msgNegoResp = pccrrNegoResponsePacket.MsgNegoResp;
            MESSAGE_HEADER messageHeader = pccrrNegoResponsePacket.MessageHeader;
            TRANSPORT_RESPONSE_HEADER transportRespH = pccrrNegoResponsePacket.TransportResponseHeader;
            RESPONSE_MESSAGE respMessage = new RESPONSE_MESSAGE();
            respMessage.MESSAGEBODY = msgNegoResp;
            respMessage.MESSAGEHEADER = messageHeader;
            respMessage.TRANSPORTRESPONSEHEADER = transportRespH;
            this.uiPayload = Marshal.SizeOf(messageHeader) + Marshal.SizeOf(msgNegoResp);

            this.VerifyHttpResponse();
            PccrrBothRoleCaptureCode.CaptureHttpRequirements();
            this.VerifyMsgNegoResp(msgNegoResp);
            this.VerifyMessageHeader(messageHeader);
            PccrrBothRoleCaptureCode.CaptureMessageHeaderRequirements(messageHeader, this.uiPayload);
            this.VerifyTransportResponseHeader(transportRespH);
            PccrrBothRoleCaptureCode.CaptureMessageRequirements();
            this.VerifyResponseMessage(respMessage);

            this.ReceiveMsgNegoResp();
        }
        /// <summary>
        /// Send message MSG_GETBLKS
        /// </summary>
        /// <param name="sid">segment id.</param>
        /// <param name="blockRang">Block ranges client wants to get</param>
        /// <param name="isVersionSupported">The version in this message is supported in server.</param>
        public void SendMsgGetBlks(byte[] sid, BLOCKRANGE[] blockRang, bool isVersionSupported)
        {
            PccrrGETBLKSRequestPacket packet;

            BLOCK_RANGE[] blockRanges = Helper.ConvertToStackBLOCKRANGEArray(blockRang);

            if (!isVersionSupported)
            {
                packet = this.pccrrStack.CreateMsgGetBlksRequest(sid, this.cryptoAlgo, MsgType_Values.MSG_GETBLKS, this.protoErrorVer);
            }
            else
            {
                packet = this.pccrrStack.CreateMsgGetBlksRequest(sid, this.cryptoAlgo, MsgType_Values.MSG_GETBLKS, this.protoVer);
            }

            for (int i = 0; i < blockRanges.Length; i++)
            {
                if (blockRanges.Length == 1)
                {
                    packet.MsgGetBLKS.ReqBlockRanges[i].Index = blockRanges[i].Index;
                    packet.MsgGetBLKS.ReqBlockRanges[i].Count = blockRanges[i].Count;
                }
                else
                {
                    MSG_GETBLKS msgGETBLKS = packet.MsgGetBLKS;
                    msgGETBLKS.ReqBlockRanges = new BLOCK_RANGE[blockRanges.Length];
                    msgGETBLKS.ReqBlockRangeCount = (uint)blockRanges.Length;
                    packet.MsgGetBLKS = msgGETBLKS;

                    packet.MsgGetBLKS.ReqBlockRanges[i].Index = blockRanges[i].Index;
                    packet.MsgGetBLKS.ReqBlockRanges[i].Count = blockRanges[i].Count;
                }
            }

            this.pccrrStack.SendPacket(packet, new TimeSpan(0, 0, this.timeout));

            PccrrPacket respMSG = this.pccrrStack.ExpectPacket();

            if (!isVersionSupported)
            {
                PccrrNegoResponsePacket pccrrNegoResponsePacket = respMSG as PccrrNegoResponsePacket;

                if (pccrrNegoResponsePacket != null)
                {
                    this.ReceiveMsgNegoResp();
                }
            }
            else
            {
                PccrrBLKResponsePacket pccrrBLKResponsePacket = respMSG as PccrrBLKResponsePacket;

                MSG_BLK msgBLK = pccrrBLKResponsePacket.MsgBLK;
                MESSAGE_HEADER messageHeader = pccrrBLKResponsePacket.MessageHeader;
                TRANSPORT_RESPONSE_HEADER transportRespH = pccrrBLKResponsePacket.TransportResponseHeader;
                RESPONSE_MESSAGE respMessage = new RESPONSE_MESSAGE();
                respMessage.MESSAGEBODY = msgBLK;
                respMessage.MESSAGEHEADER = messageHeader;
                respMessage.TRANSPORTRESPONSEHEADER = transportRespH;

                this.VerifyMsgBlk(msgBLK);
                PccrrBothRoleCaptureCode.CaptureSegmentIdRequirements(msgBLK.SegmentId);
                this.VerifyResponseMessage(respMessage);

                bool isSizeOfBlockZero = false;

                if (msgBLK.SizeOfBlock == 0)
                {
                    isSizeOfBlockZero = true;
                }
                else
                {
                    isSizeOfBlockZero = false;
                }

                bool isBlockEmpty = false;

                if (msgBLK.Block.Length == 0)
                {
                    isBlockEmpty = true;
                }
                else
                {
                    isBlockEmpty = false;
                }

                CryptoAlgoIdValues crypAlgoId = Helper.ConvertFromStackCryptoAlgoIdValues(pccrrBLKResponsePacket.MessageHeader.CryptoAlgoId);
                this.ReceiveMsgBlk(msgBLK.BlockIndex, msgBLK.NextBlockIndex, isSizeOfBlockZero, isBlockEmpty, crypAlgoId);
            }
        }
        /// <summary>
        /// Send message MSG_GETBLKLIST.
        /// </summary>
        /// <param name="sid">segment id.</param>
        /// <param name="blockRang">Block ranges client wants to get.</param>
        /// <param name="isVersionSupported">The version in message is supported by server or not.</param>
        public void SendMsgGetBlkList(byte[] sid, BLOCKRANGE[] blockRang, bool isVersionSupported)
        {
            PccrrGETBLKLISTRequestPacket packet;

            BLOCK_RANGE[] blockRanges = Helper.ConvertToStackBLOCKRANGEArray(blockRang);

            if (!isVersionSupported)
            {
                packet = this.pccrrStack.CreateMsgGetBlkListRequest(sid, blockRanges, this.cryptoAlgo, MsgType_Values.MSG_GETBLKLIST, this.protoErrorVer);
            }
            else
            {
                packet = this.pccrrStack.CreateMsgGetBlkListRequest(sid, blockRanges, this.cryptoAlgo, MsgType_Values.MSG_GETBLKLIST, this.protoVer);
            }

            this.pccrrStack.SendPacket(packet, new TimeSpan(0, 0, this.timeout));

            PccrrPacket respMSG = this.pccrrStack.ExpectPacket();

            if (!isVersionSupported)
            {
                PccrrNegoResponsePacket pccrrNegoResponsePacket = respMSG as PccrrNegoResponsePacket;

                if (pccrrNegoResponsePacket != null)
                {
                    this.ReceiveMsgNegoResp();
                }
            }
            else
            {
                PccrrBLKLISTResponsePacket pccrrBLKLISTResponsePacket = respMSG as PccrrBLKLISTResponsePacket;

                MSG_BLKLIST msgBLKLIST = pccrrBLKLISTResponsePacket.MsgBLKLIST;
                MESSAGE_HEADER messageHeader = pccrrBLKLISTResponsePacket.MessageHeader;
                TRANSPORT_RESPONSE_HEADER transportRespH = pccrrBLKLISTResponsePacket.TransportResponseHeader;
                RESPONSE_MESSAGE respMessage = new RESPONSE_MESSAGE();
                respMessage.MESSAGEBODY = msgBLKLIST;
                respMessage.MESSAGEHEADER = messageHeader;
                respMessage.TRANSPORTRESPONSEHEADER = transportRespH;

                if (msgBLKLIST.BlockRanges.Length != 0)
                {
                    BLOCK_RANGE blockRange = pccrrBLKLISTResponsePacket.MsgBLKLIST.BlockRanges[0];
                    PccrrBothRoleCaptureCode.CaptureBlockRangeRequirements(blockRange);
                }

                this.VerifyMsgBlkList(msgBLKLIST);
                PccrrBothRoleCaptureCode.CaptureCommonDataTypesRequirements(msgBLKLIST);
                this.VerifyResponseMessage(respMessage);

                BLOCKRANGE[] blkRanges = Helper.ConvertFromStackBLOCKRANGEArray(msgBLKLIST.BlockRanges);
                this.ReceiveMsgBlkList(msgBLKLIST.BlockRangeCount, blkRanges, msgBLKLIST.NextBlockIndex);
            }
        }
        /// <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;
        }
        /// <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;
        }
        /// <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;
        }
        /// <summary>
        /// Encode pack.
        /// </summary>
        /// <returns>Encode bytes.</returns>
        public override byte[] Encode()
        {
            RESPONSE_MESSAGE responseMessage = new RESPONSE_MESSAGE();
            responseMessage.MESSAGEBODY = this.msgBLK;
            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_BLK)responseMessage.MESSAGEBODY).SizeOfSegmentId, false));
            listRet.AddRange(((MSG_BLK)responseMessage.MESSAGEBODY).SegmentId);
            while (listRet.Count % 4 != 0)
            {
                listRet.Add(byte.MinValue);
            }

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLK)responseMessage.MESSAGEBODY).BlockIndex, false));
            listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLK)responseMessage.MESSAGEBODY).NextBlockIndex, false));
            listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLK)responseMessage.MESSAGEBODY).SizeOfBlock, false));
            if (((MSG_BLK)responseMessage.MESSAGEBODY).Block != null)
            {
                listRet.AddRange(((MSG_BLK)responseMessage.MESSAGEBODY).Block);
            }

            while (listRet.Count % 4 != 0)
            {
                listRet.Add(byte.MinValue);
            }

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

            listRet.AddRange(MarshalHelper.GetBytes(((MSG_BLK)responseMessage.MESSAGEBODY).SizeOfIVBlock, false));
            if (((MSG_BLK)responseMessage.MESSAGEBODY).IVBlock != null)
            {
                listRet.AddRange(((MSG_BLK)responseMessage.MESSAGEBODY).IVBlock);
            }

            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;
        }