/// <summary>
        /// Initializes a new instance of the <see cref="TDSPreLoginOptionToken"/> class.
        /// </summary>
        /// <param name="type">PreLogin Option Token Type</param>
        public TDSPreLoginOptionToken(TDSPreLoginOptionTokenType type)
        {
            this.Type = type;
            switch (this.Type)
            {
            case TDSPreLoginOptionTokenType.Encryption:
            {
                this.Length = 1;
                break;
            }

            case TDSPreLoginOptionTokenType.FedAuthRequired:
            {
                this.Length = 1;
                break;
            }

            case TDSPreLoginOptionTokenType.InstOpt:
            {
                throw new NotSupportedException();
            }

            case TDSPreLoginOptionTokenType.MARS:
            {
                this.Length = 1;
                break;
            }

            case TDSPreLoginOptionTokenType.NonceOpt:
            {
                this.Length = 32;
                break;
            }

            case TDSPreLoginOptionTokenType.Terminator:
            {
                this.Length = 0;
                break;
            }

            case TDSPreLoginOptionTokenType.ThreadID:
            {
                this.Length = 4;
                break;
            }

            case TDSPreLoginOptionTokenType.TraceID:
            {
                this.Length = 36;
                break;
            }

            case TDSPreLoginOptionTokenType.Version:
            {
                this.Length = 6;
                break;
            }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds PreLogin option to the PreLogin packet
        /// </summary>
        /// <param name="type">Option type</param>
        /// <param name="data">Option data</param>
        public void AddOption(TDSPreLoginOptionTokenType type, object data)
        {
            if (this.Terminated)
            {
                throw new InvalidOperationException();
            }

            switch (type)
            {
            case TDSPreLoginOptionTokenType.Version:
            {
                if (data is TDSClientVersion && this.ClientVersion == null)
                {
                    this.ClientVersion = (TDSClientVersion)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type}.");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.Encryption:
            {
                if (data is TDSEncryptionOption)
                {
                    this.Encryption = (TDSEncryptionOption)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type} [{this.Encryption}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.FedAuthRequired:
            {
                if (data is bool)
                {
                    this.FedAuthRequired = (bool)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type} [{(bool)data}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.MARS:
            {
                if (data is bool)
                {
                    this.MARS = (bool)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type} [{(bool)data}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.ThreadID:
            {
                if (data is uint)
                {
                    this.ThreadID = (uint)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type} [{this.ThreadID}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.TraceID:
            {
                if (data is TDSClientTraceID)
                {
                    this.TraceID = (TDSClientTraceID)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type}.");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.NonceOpt:
            {
                if (data is byte[])
                {
                    this.Nonce = (byte[])data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type}.");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            default:
            {
                throw new NotSupportedException();
            }
            }

            this.Options.Add(new TDSPreLoginOptionToken(type));
        }
        public void AddOption(TDSPreLoginOptionTokenType type, Object data)
        {
            if (Terminated)
            {
                throw new InvalidOperationException();
            }

            switch (type)
            {
            case TDSPreLoginOptionTokenType.Version:
            {
                if (data is TDSClientVersion && ClientVersion == null)
                {
                    ClientVersion = (TDSClientVersion)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type.ToString()}.");
                }
                else
                {
                    throw new ArgumentException();
                }
                break;
            }

            case TDSPreLoginOptionTokenType.Encryption:
            {
                if (data is TDSEncryptionOption)
                {
                    Encryption = (TDSEncryptionOption)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type.ToString()} [{Encryption.ToString()}].");
                }
                else
                {
                    throw new ArgumentException();
                }
                break;
            }

            case TDSPreLoginOptionTokenType.FedAuthRequired | TDSPreLoginOptionTokenType.MARS:
            {
                if (data is bool)
                {
                    if (type == TDSPreLoginOptionTokenType.FedAuthRequired)
                    {
                        FedAuthRequired = (bool)data;
                    }
                    else
                    {
                        MARS = (bool)data;
                    }

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type.ToString()} [{(bool)data}].");
                }
                else
                {
                    throw new ArgumentException();
                }
                break;
            }

            case TDSPreLoginOptionTokenType.ThreadID:
            {
                if (data is ulong)
                {
                    ThreadID = (ulong)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type.ToString()} [{ThreadID}].");
                }
                else
                {
                    throw new ArgumentException();
                }
                break;
            }

            case TDSPreLoginOptionTokenType.TraceID:
            {
                if (data is TDSClientTraceID)
                {
                    TraceID = (TDSClientTraceID)data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type.ToString()}.");
                }
                else
                {
                    throw new ArgumentException();
                }
                break;
            }

            case TDSPreLoginOptionTokenType.NonceOpt:
            {
                if (data is byte[])
                {
                    Nonce = (byte[])data;

                    LoggingUtilities.WriteLogVerboseOnly($" Adding PreLogin option {type.ToString()}.");
                }
                else
                {
                    throw new ArgumentException();
                }
                break;
            }

            default:
            {
                throw new NotSupportedException();
            }
            }

            Options.Add(new TDSPreLoginOptionToken(type));
        }
        /// <summary>
        /// Adds PreLogin option to the PreLogin packet
        /// </summary>
        /// <param name="type">Option type</param>
        /// <param name="data">Option data</param>
        public void AddOption(TDSPreLoginOptionTokenType type, object data)
        {
            if (this.Terminated)
            {
                throw new InvalidOperationException();
            }

            switch (type)
            {
            case TDSPreLoginOptionTokenType.Version:
            {
                if (data is TDSClientVersion && this.ClientVersion == null)
                {
                    this.ClientVersion = (TDSClientVersion)data;
                    //LoggingUtilities.WriteLog($"    Adding PreLogin option {type}.");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.Encryption:
            {
                if (data is TDSEncryptionOption)
                {
                    this.Encryption = (TDSEncryptionOption)data;

                    LoggingUtilities.WriteLog($"  Adding PreLogin option {type} [{this.Encryption}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.FedAuthRequired:
            {
                if (data is bool)
                {
                    this.FedAuthRequired = (bool)data;

                    LoggingUtilities.WriteLog($"  Adding PreLogin option {type} [{(bool)data}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.MARS:
            {
                if (data is bool)
                {
                    this.MARS = (bool)data;

                    LoggingUtilities.WriteLog($"  Adding PreLogin option {type} [{(bool)data}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.ThreadID:
            {
                if (data is uint)
                {
                    this.ThreadID = (uint)data;

                    LoggingUtilities.WriteLog($"  Adding PreLogin option {type} [{this.ThreadID}].");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.TraceID:
            {
                if (data is TDSClientTraceID)
                {
                    this.TraceID = (TDSClientTraceID)data;

                    LoggingUtilities.WriteLog($"  Adding PreLogin option {type}");
                    LoggingUtilities.WriteLog($"   ConnectionID: {new Guid(this.TraceID.TraceID).ToString().ToUpper()}", writeToSummaryLog: true);
                    LoggingUtilities.WriteLog($"   ActivityID: {new Guid(this.TraceID.ActivityID).ToString().ToUpper()}");
                    LoggingUtilities.WriteLog($"   ActivitySequence: {this.TraceID.ActivitySequence}");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            case TDSPreLoginOptionTokenType.NonceOpt:
            {
                if (data is byte[])
                {
                    this.Nonce = (byte[])data;

                    LoggingUtilities.WriteLog($"  Adding PreLogin option {type}.");
                }
                else
                {
                    throw new ArgumentException();
                }

                break;
            }

            default:
            {
                throw new NotSupportedException();
            }
            }

            this.Options.Add(new TDSPreLoginOptionToken(type));
        }