示例#1
0
        public TdsComm(string dataSource, int port, int packetSize, int timeout, TdsVersion tdsVersion)
        {
            this.packetSize = packetSize;
            this.tdsVersion = tdsVersion;
            this.dataSource = dataSource;

            outBuffer = new byte[packetSize];
            inBuffer  = new byte[packetSize];

            outBufferLength = packetSize;
            inBufferLength  = packetSize;

            lsb = true;

            IPEndPoint endPoint;
            bool       have_exception = false;

            try {
#if NET_2_0
                IPAddress ip;
                if (IPAddress.TryParse(this.dataSource, out ip))
                {
                    endPoint = new IPEndPoint(ip, port);
                }
                else
                {
                    IPHostEntry hostEntry = Dns.GetHostEntry(this.dataSource);
                    endPoint = new IPEndPoint(hostEntry.AddressList [0], port);
                }
#else
                IPHostEntry hostEntry = Dns.Resolve(this.dataSource);
                endPoint = new IPEndPoint(hostEntry.AddressList [0], port);
#endif
            } catch (SocketException e) {
                throw new TdsInternalException("Server does not exist or connection refused.", e);
            }

            try {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IAsyncResult ares       = socket.BeginConnect(endPoint, null, null);
                int          timeout_ms = timeout * 1000;
                if (timeout > 0 && !ares.IsCompleted && !ares.AsyncWaitHandle.WaitOne(timeout_ms, false))
                {
                    throw Tds.CreateTimeoutException(dataSource, "Open()");
                }
                socket.EndConnect(ares);
                try {
                    // MS sets these socket option
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
                } catch (SocketException) {
                    // Some platform may throw an exception, so
                    // eat all socket exception, yeaowww!
                }

                try {
#if NET_2_0
                    socket.NoDelay = true;
#endif
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, timeout_ms);
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout_ms);
                } catch {
                    // Ignore exceptions here for systems that do not support these options.
                }
                // Let the stream own the socket and take the pleasure of closing it
                stream = new NetworkStream(socket, true);
            } catch (SocketException e) {
                have_exception = true;
                throw new TdsInternalException("Server does not exist or connection refused.", e);
            } catch (Exception) {
                have_exception = true;
                throw;
            } finally {
                if (have_exception && socket != null)
                {
                    try {
                        Socket s = socket;
                        socket = null;
                        s.Close();
                    } catch {}
                }
            }
            if (!socket.Connected)
            {
                throw new TdsInternalException("Server does not exist or connection refused.", null);
            }
            packetsSent = 1;
        }