/// <summary>
        /// Create TransNamedRap request for client to send a rap request to server. 
        /// </summary>
        /// <param name = "messageId">the id of message, used to identity the request and the server response. </param>
        /// <param name = "sessionUid">the valid session id, must be response by server of the session setup request. </param>
        /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
        /// <param name = "flags">
        /// The Flags field contains individual flags, as specified in [CIFS] sections 2.4.2 and 3.1.1. 
        /// </param>
        /// <param name = "flags2">
        /// The Flags2 field contains individual bit flags that, depending on the negotiated SMB dialect, indicate   
        /// various client and server capabilities. 
        /// </param>
        /// <param name = "transactOptions">
        /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
        /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
        /// client MAY set either or both of the following bit flags 
        /// </param>
        /// <param name="rapOPCode">
        /// The operation code for the particular operation. For more information on valid operation codes, see 2.5.4.
        /// </param>
        /// <param name="paramDesc">
        /// This value MUST be a null-terminated ASCII descriptor string. The server SHOULD validate that the ParamDesc
        /// value passed by the client matches what is specified by the RAPOpcode. The following table specifies the
        /// descriptor character and the notation for each data type.
        /// </param>
        /// <param name="dataDesc">
        /// (Optional) If this value is specified, it MUST be a null-terminated ASCII descriptor string that describes
        /// the contents of the data returned to the client. Certain RAPOpcodes specify a DataDesc field; for a list
        /// of Remote Administration Protocol commands that specify a DataDesc field, see section 2.5.5.
        /// </param>
        /// <param name="rapParamsAndAuxDesc">
        /// This field combines the following fields, because each of their length is unknown:<para/>
        /// RAPParams: Remote Administration Protocol command-specific parameters, as specified in sections 2.5.5, 2.5.6, 2.5.7,
        /// 2.5.8, and 2.5.9.<para/>
        /// AuxDesc: (Optional) If this value is specified, it MUST be a null-terminated ASCII descriptor string that describes
        /// auxiliary data returned to the client. If no AuxDesc field is specified for the Remote Administration
        /// Protocol command, this field MUST NOT be present. For the origin of the descriptor string values, see
        /// section 4.2.
        /// </param>
        /// <param name="rapInData">
        /// Additional data for the Remote Administration Protocol request. This field MUST be present in the
        /// NetPrintJobSetInfoRequest command. This field cannot be present in any other command.
        /// </param>
        /// <param name = "timeOut">
        /// The maximum amount of time in milliseconds to wait for the operation to complete. The client SHOULD set  
        /// this to 0 to indicate that no time-out is given. If the operation does not complete within the specified  
        /// time, the server MAY abort the request and send a failure response. 
        /// </param>
        /// <returns>a named rap request packet </returns>
        private SmbTransRapRequestPacket CreateTransNamedRapRequest(
            ushort messageId,
            ushort sessionUid,
            ushort treeId,
            SmbHeader_Flags_Values flags,
            SmbHeader_Flags2_Values flags2,
            TransSmbParametersFlags transactOptions,
            ushort rapOPCode,
            byte[] paramDesc,
            byte[] dataDesc,
            byte[] rapParamsAndAuxDesc,
            byte[] rapInData,
            uint timeOut)
        {
            SmbTransRapRequestPacket packet = new SmbTransRapRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_TRANSACTION,
                messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2);

            // Set Smb Parameters
            SMB_COM_TRANSACTION_Request_SMB_Parameters smbParameters =
                new SMB_COM_TRANSACTION_Request_SMB_Parameters();
            smbParameters.MaxParameterCount = this.capability.MaxParameterCount;
            smbParameters.MaxDataCount = this.capability.MaxDataCount;
            smbParameters.MaxSetupCount = this.capability.MaxSetupCount;
            smbParameters.Flags = transactOptions;
            smbParameters.Timeout = timeOut;
            smbParameters.SetupCount = 0; // the correct count in word of the Setup is always 0.
            smbParameters.Setup = new ushort[0];
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION_Request_SMB_Parameters>(
                smbParameters) / SmbCapability.NUM_BYTES_OF_WORD);

            // Set Smb Data
            SMB_COM_TRANSACTION_Request_SMB_Data smbData = new SMB_COM_TRANSACTION_Request_SMB_Data();
            smbData.Name = CifsMessageUtils.ToSmbStringBytes("", this.capability.IsUnicode);

            // Set Transaction Parameters
            TRANSACTION_Rap_Request_Trans_Parameters transParameters = new TRANSACTION_Rap_Request_Trans_Parameters();
            transParameters.RapOPCode = rapOPCode;
            transParameters.ParamDesc = paramDesc;
            transParameters.DataDesc = dataDesc;
            transParameters.RAPParamsAndAuxDesc = rapParamsAndAuxDesc;

            // Set Transaction Data
            TRANSACTION_Rap_Request_Trans_Data transData = new TRANSACTION_Rap_Request_Trans_Data();
            transData.RAPInData = rapInData;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.TransParameters = transParameters;
            packet.TransData = transData;
            packet.UpdateCountAndOffset();

            // update TransactionSubCommand in SmbGlobalContext, record the transact action
            this.capability.TransactionSubCommand = TransSubCommandExtended.TRANS_EXT_RAP;

            return packet;
        }
        /// <summary>
        /// to decode the smb data: from the general SmbDada to the concrete Smb Data.
        /// </summary>
        protected override void DecodeData()
        {
            this.smbData = new SMB_COM_TRANSACTION_Request_SMB_Data();
            using (MemoryStream memoryStream = new MemoryStream(CifsMessageUtils.ToBytes<SmbData>(this.smbDataBlock)))
            {
                using (Channel channel = new Channel(null, memoryStream))
                {
                    this.smbData.ByteCount = channel.Read<ushort>();

                    bool isUnicode = (this.smbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE;
                    if (isUnicode)
                    {
                        byte padLength = 1;
                        channel.ReadBytes(padLength);
                        List<ushort> array = new List<ushort>();
                        ushort letter;

                        do
                        {
                            letter = channel.Read<ushort>();
                            array.Add(letter);
                        }
                        while (letter != new ushort());
                        this.smbData.Name = CifsMessageUtils.ToBytesArray(array.ToArray());
                    }
                    else
                    {
                        List<byte> array = new List<byte>();
                        byte letter;

                        do
                        {
                            letter = channel.Read<byte>();
                            array.Add(letter);
                        }
                        while (letter != new byte());

                        this.smbData.Name = array.ToArray();
                    }

                    // the padding length of Pad1.
                    int pad1Length = this.smbParameters.ParameterOffset - this.HeaderSize
                        - this.smbParameters.WordCount * 2 - SmbComTransactionPacket.SmbParametersWordCountLength
                        - SmbComTransactionPacket.SmbDataByteCountLength - this.smbData.Name.Length;

                    // sub the padding bytes for Name.
                    if (isUnicode)
                    {
                        pad1Length -= 1;
                    }

                    // read Pad1 from channel.
                    if (pad1Length > 0)
                    {
                        this.smbData.Pad1 = channel.ReadBytes(pad1Length);
                    }

                    this.smbData.Trans_Parameters = channel.ReadBytes(this.smbParameters.ParameterCount);
                    if (this.smbParameters.DataOffset > 0)
                    {
                        this.smbData.Pad2 = channel.ReadBytes(this.smbParameters.DataOffset
                            - this.smbParameters.ParameterOffset - this.smbParameters.ParameterCount);
                        this.smbData.Trans_Data = channel.ReadBytes(this.smbParameters.DataCount);
                    }
                    else
                    {
                        this.smbData.Pad2 = new byte[0];
                        this.smbData.Trans_Data = new byte[0];
                    }
                }
            }
            this.DecodeTransParameters();
            this.DecodeTransData();
        }
        /// <summary>
        /// Create TransMailslotWrite request for client to write data to mailslot on server. 
        /// </summary>
        /// <param name = "messageId">the id of message, used to identity the request and the server response. </param>
        /// <param name = "sessionUid">the valid session id, must be response by server of the session setup request. </param>
        /// <param name = "treeId">the valid tree connect id, must be response by server of the tree connect. </param>
        /// <param name = "flags">
        /// The Flags field contains individual flags, as specified in [CIFS] sections 2.4.2 and 3.1.1. 
        /// </param>
        /// <param name = "flags2">
        /// The Flags2 field contains individual bit flags that, depending on the negotiated SMB dialect, indicate   
        /// various client and server capabilities. 
        /// </param>
        /// <param name = "mailslotName">The name of maislot to write to. </param>
        /// <param name = "transactOptions">
        /// A set of bit flags that alter the behavior of the requested operation. Unused bit fields MUST be set to  
        /// zero by the client sending the request, and MUST be ignored by the server receiving the request. The 
        /// client MAY set either or both of the following bit flags 
        /// </param>
        /// <param name = "timeOut">
        /// The maximum amount of time in milliseconds to wait for the operation to complete. The client SHOULD set  
        /// this to 0 to indicate that no time-out is given. If the operation does not complete within the specified  
        /// time, the server MAY abort the request and send a failure response. 
        /// </param>
        /// <param name = "writeData">The data to write to mailslot. </param>
        /// <param name = "priority">
        /// This field MUST be in the range of 0 to 9. The larger value being the higher priority. 
        /// </param>
        /// <param name = "className">
        /// The third setup word and the class of the mailslot request. This value MUST be set to one of the following 
        /// values. 
        /// </param>
        /// <returns>a write mailslot request packet </returns>
        private SmbTransMailslotWriteRequestPacket CreateTransMailslotWriteRequest(
            ushort messageId,
            ushort sessionUid,
            ushort treeId,
            SmbHeader_Flags_Values flags,
            SmbHeader_Flags2_Values flags2,
            string mailslotName,
            TransSmbParametersFlags transactOptions,
            uint timeOut,
            byte[] writeData,
            ushort priority,
            SmbTransMailslotClass className)
        {
            if (mailslotName == null)
            {
                mailslotName = string.Empty;
            }

            SmbTransMailslotWriteRequestPacket packet = new SmbTransMailslotWriteRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_TRANSACTION,
                messageId, sessionUid, treeId, (SmbFlags)flags, (SmbFlags2)flags2);

            // Set Smb Parameters
            SMB_COM_TRANSACTION_Request_SMB_Parameters smbParameters =
                new SMB_COM_TRANSACTION_Request_SMB_Parameters();
            smbParameters.MaxParameterCount = this.capability.MaxParameterCount;
            smbParameters.MaxDataCount = this.capability.MaxDataCount;
            smbParameters.MaxSetupCount = this.capability.MaxSetupCount;
            smbParameters.Flags = transactOptions;
            smbParameters.Timeout = timeOut;
            smbParameters.SetupCount = 3; // the correct count in word of the Setup is always 3.
            smbParameters.Setup = new ushort[3];
            smbParameters.Setup[0] = (ushort)TransSubCommand.TRANS_MAILSLOT_WRITE;
            smbParameters.Setup[1] = (ushort)priority;
            smbParameters.Setup[2] = (ushort)className;
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION_Request_SMB_Parameters>(
                smbParameters) / SmbCapability.NUM_BYTES_OF_WORD);

            // Set Smb Data
            SMB_COM_TRANSACTION_Request_SMB_Data smbData = new SMB_COM_TRANSACTION_Request_SMB_Data();
            smbData.Name = CifsMessageUtils.ToSmbStringBytes(mailslotName, this.capability.IsUnicode);
            smbData.Trans_Data = writeData;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.UpdateCountAndOffset();

            // update TransactionSubCommand in SmbGlobalContext, record the transact action
            this.capability.TransactionSubCommand = TransSubCommandExtended.TRANS_EXT_MAILSLOT_WRITE;

            return packet;
        }
        /// <summary>
        /// to create a TransWriteNmpipe request packet.
        /// </summary>
        /// <param name="messageId">This field SHOULD be the multiplex ID that is used to associate a response with a
        /// request.</param>
        /// <param name="uid">This field SHOULD identify the authenticated instance of the user.</param>
        /// <param name="treeId">This field identifies the subdirectory (or tree) on the server that the client is
        /// accessing.</param>
        /// <param name="flags">An 8-bit field of 1-bit flags describing various features in effect for the
        /// message</param>
        /// <param name="flags2">A 16-bit field of 1-bit flags that represent various features in effect for the
        /// message. Unspecified bits are reserved and MUST be zero.</param>
        /// <param name="maxParameterCount">The maximum number of parameter bytes that the client will accept in the
        /// transaction reply. The server MUST NOT return more than this number of parameter bytes.</param>
        /// <param name="maxDataCount">The maximum number of data bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of data bytes.</param>
        /// <param name="maxSetupCount">Maximum number of setup bytes that the client will accept in the transaction
        /// reply. The server MUST NOT return more than this number of setup bytes</param>
        /// <param name="smbParametersflags">A set of bit flags that alter the behavior of the requested
        /// operation</param>
        /// <param name="timeout">The value of this field MUST be the maximum number of milliseconds the server SHOULD
        /// wait for completion of the transaction before generating a timeout and returning a response to the
        /// client. </param>
        /// <param name="fid">MUST contain a valid FID obtained from a previously successful SMB open command.</param>
        /// <param name="writeData">This field MUST contain the bytes to be written to the named pipe as part of the
        /// transacted operation.</param>
        /// <param name="name">The pathname of the mailslot or named pipe to which the transaction subcommand applies
        /// or a client supplied identifier that provides a name for the transaction.</param>
        /// <returns>a TransWriteNmpipe request packet</returns>
        public SmbTransWriteNmpipeRequestPacket CreateTransWriteNmpipeRequest(
            ushort messageId,
            ushort uid,
            ushort treeId,
            SmbFlags flags,
            SmbFlags2 flags2,
            ushort maxParameterCount,
            ushort maxDataCount,
            byte maxSetupCount,
            TransSmbParametersFlags smbParametersflags,
            uint timeout,
            ushort fid,
            byte[] writeData,
            string name)
        {
            if (writeData == null)
            {
                writeData = new byte[0];
            }
            if (name == null)
            {
                name = string.Empty;
            }

            SmbTransWriteNmpipeRequestPacket packet = new SmbTransWriteNmpipeRequestPacket();
            packet.SmbHeader = CifsMessageUtils.CreateSmbHeader(SmbCommand.SMB_COM_TRANSACTION,
                messageId, uid, treeId, flags, flags2);

            // Set Smb Parameters
            SMB_COM_TRANSACTION_Request_SMB_Parameters smbParameters =
                new SMB_COM_TRANSACTION_Request_SMB_Parameters();
            smbParameters.MaxParameterCount = maxParameterCount;
            smbParameters.MaxDataCount = maxDataCount;
            smbParameters.MaxSetupCount = maxSetupCount;
            smbParameters.Flags = smbParametersflags;
            smbParameters.Timeout = timeout;
            smbParameters.SetupCount = 2; // the correct count in word of the Setup is always 2.
            smbParameters.Setup = new ushort[2];
            smbParameters.Setup[0] = (ushort)TransSubCommand.TRANS_WRITE_NMPIPE;
            smbParameters.Setup[1] = fid;
            smbParameters.WordCount = (byte)(CifsMessageUtils.GetSize<SMB_COM_TRANSACTION_Request_SMB_Parameters>(
                smbParameters) / NumBytesOfWord);

            // Set Smb Data
            SMB_COM_TRANSACTION_Request_SMB_Data smbData = new SMB_COM_TRANSACTION_Request_SMB_Data();
            smbData.Name = CifsMessageUtils.ToSmbStringBytes(name,
                (flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE);

            // Set Trans_Data
            TRANS_WRITE_NMPIPE_Request_Trans_Data transData = new TRANS_WRITE_NMPIPE_Request_Trans_Data();
            transData.WriteData = writeData;

            packet.SmbParameters = smbParameters;
            packet.SmbData = smbData;
            packet.TransData = transData;
            packet.UpdateCountAndOffset();

            return packet;
        }