TCP connection handle
Inheritance: System.Data.SqlClient.SNI.SNIHandle
Example #1
0
        /// <summary>
        /// Creates an SNITCPHandle object
        /// </summary>
        /// <param name="fullServerName">Server string. May contain a comma delimited port number.</param>
        /// <param name="timerExpire">Timer expiration</param>
        /// <param name="callbackObject">Asynchronous I/O callback object</param>
        /// <param name="parallel">Should MultiSubnetFailover be used</param>
        /// <returns>SNITCPHandle</returns>
        private SNITCPHandle CreateTcpHandle(string fullServerName, long timerExpire, object callbackObject, bool parallel, ref byte[] spnBuffer, bool isIntegratedSecurity)
        {
            // TCP Format:
            // tcp:<host name>\<instance name>
            // tcp:<host name>,<TCP/IP port number>

            string    hostName  = null;
            int       port      = -1;
            Exception exception = null;

            if (string.IsNullOrWhiteSpace(fullServerName)) // when fullServerName is empty
            {
                hostName = DefaultHostName;
                port     = DefaultSqlServerPort;
            }
            else
            {
                string[] serverNamePartsByComma     = fullServerName.Split(CommaSeparator);
                string[] serverNamePartsByBackSlash = fullServerName.Split(BackSlashSeparator);

                // when no port or instance name provided
                if (serverNamePartsByComma.Length < 2 && serverNamePartsByBackSlash.Length < 2)
                {
                    hostName = fullServerName;
                    port     = DefaultSqlServerPort;
                }
                // when port is provided, and no instance name
                else if (serverNamePartsByComma.Length == 2 && serverNamePartsByBackSlash.Length < 2)
                {
                    hostName = serverNamePartsByComma[0];
                    string portString = serverNamePartsByComma[1];
                    try
                    {
                        port = ushort.Parse(portString);
                    }
                    catch (Exception e)
                    {
                        exception = e;
                    }
                }
                // when instance name is provided, and no port
                else if (serverNamePartsByComma.Length < 2 && serverNamePartsByBackSlash.Length == 2)
                {
                    hostName = serverNamePartsByBackSlash[0];
                    string instanceName = serverNamePartsByBackSlash[1];
                    try
                    {
                        port = GetPortByInstanceName(hostName, instanceName);
                    }
                    catch (Exception e)
                    {
                        exception = e;
                    }
                }
            }

            if (hostName != null && port > 0 && exception == null && isIntegratedSecurity)
            {
                try
                {
                    hostName  = GetFullyQualifiedDomainName(hostName);
                    spnBuffer = MakeMsSqlServerSPN(hostName, port);
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }

            SNITCPHandle sniTcpHandle = null;

            if (hostName != null && port > 0 && exception == null)
            {
                sniTcpHandle = new SNITCPHandle(hostName, port, timerExpire, callbackObject, parallel);
            }
            else if (exception != null)
            {
                SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InvalidConnStringError, exception);
            }
            else
            {
                SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.InvalidConnStringError, string.Empty);
            }

            return(sniTcpHandle);
        }