InternalShutdown() private méthode

private InternalShutdown ( SocketShutdown how ) : void
how SocketShutdown
Résultat void
Exemple #1
0
        // Disposes the Tcp connection.
        protected virtual void Dispose(bool disposing)
        {
            if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
            {
                if (disposing)
                {
                    IDisposable?dataStream = _dataStream;
                    if (dataStream != null)
                    {
                        dataStream.Dispose();
                    }
                    else
                    {
                        // If the NetworkStream wasn't created, the Socket might
                        // still be there and needs to be closed. In the case in which
                        // we are bound to a local IPEndPoint this will remove the
                        // binding and free up the IPEndPoint for later uses.
                        Socket chkClientSocket = Volatile.Read(ref _clientSocket);
                        if (chkClientSocket != null)
                        {
                            try
                            {
                                chkClientSocket.InternalShutdown(SocketShutdown.Both);
                            }
                            finally
                            {
                                chkClientSocket.Close();
                            }
                        }
                    }

                    GC.SuppressFinalize(this);
                }
            }
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(this);
                }

                // The only resource we need to free is the network stream, since this
                // is based on the client socket, closing the stream will cause us
                // to flush the data to the network, close the stream and (in the
                // NetoworkStream code) close the socket as well.
                if (_disposed)
                {
                    return;
                }

                Socket chkClientSocket = _clientSocket;
                if (chkClientSocket != null)
                {
                    // If the NetworkStream wasn't retrieved, the Socket might
                    // still be there and needs to be closed to release the effect
                    // of the Bind() call and free the bound IPEndPoint.
                    chkClientSocket.InternalShutdown(SocketShutdown.Both);
                    chkClientSocket.Dispose();
                    _clientSocket = null !;
                }

                _disposed = true;
                GC.SuppressFinalize(this);
            }
        }
Exemple #3
0
        protected override void Dispose(bool disposing)
        {
            if (Interlocked.Exchange(ref _disposed, 1) != 0)
            {
                return;
            }

            if (disposing)
            {
                // The only resource we need to free is the network stream, since this
                // is based on the client socket, closing the stream will cause us
                // to flush the data to the network, close the stream and (in the
                // NetoworkStream code) close the socket as well.
                _readable  = false;
                _writeable = false;
                if (_ownsSocket)
                {
                    // If we own the Socket (false by default), close it
                    // ignoring possible exceptions (eg: the user told us
                    // that we own the Socket but it closed at some point of time,
                    // here we would get an ObjectDisposedException)
                    _streamSocket.InternalShutdown(SocketShutdown.Both);
                    _streamSocket.Close(_closeTimeout);
                }
            }

            base.Dispose(disposing);
        }
Exemple #4
0
        private void FreeResources()
        {
            //
            // only resource we need to free is the network stream, since this
            // is based on the client socket, closing the stream will cause us
            // to flush the data to the network, close the stream and (in the
            // NetoworkStream code) close the socket as well.
            //
            if (m_CleanedUp)
            {
                return;
            }

            Socket chkClientSocket = Client;

            if (chkClientSocket != null)
            {
                //
                // if the NetworkStream wasn't retrieved, the Socket might
                // still be there and needs to be closed to release the effect
                // of the Bind() call and free the bound IPEndPoint.
                //
                chkClientSocket.InternalShutdown(SocketShutdown.Both);
                chkClientSocket.Close();
                Client = null;
            }
            m_CleanedUp = true;
        }
Exemple #5
0
        protected override void Dispose(bool disposing)
        {
#if DEBUG
            using (DebugThreadTracking.SetThreadKind(ThreadKinds.User))
            {
#endif
            // Mark this as disposed before changing anything else.
            bool cleanedUp = _cleanedUp;
            _cleanedUp = true;
            if (!cleanedUp && disposing)
            {
                // The only resource we need to free is the network stream, since this
                // is based on the client socket, closing the stream will cause us
                // to flush the data to the network, close the stream and (in the
                // NetoworkStream code) close the socket as well.
                _readable  = false;
                _writeable = false;
                if (_ownsSocket)
                {
                    // If we own the Socket (false by default), close it
                    // ignoring possible exceptions (eg: the user told us
                    // that we own the Socket but it closed at some point of time,
                    // here we would get an ObjectDisposedException)
                    _streamSocket.InternalShutdown(SocketShutdown.Both);
                    _streamSocket.Close(_closeTimeout);
                }
            }
#if DEBUG
        }
#endif
            base.Dispose(disposing);
        }
Exemple #6
0
        // Disposes the Tcp connection.
        protected virtual void Dispose(bool disposing)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, this, nameof(Dispose), "");
            }

            if (_cleanedUp)
            {
                if (NetEventSource.Log.IsEnabled())
                {
                    NetEventSource.Exit(NetEventSource.ComponentType.Socket, this, nameof(Dispose), "");
                }

                return;
            }

            if (disposing)
            {
                IDisposable dataStream = _dataStream;
                if (dataStream != null)
                {
                    dataStream.Dispose();
                }
                else
                {
                    // If the NetworkStream wasn't created, the Socket might
                    // still be there and needs to be closed. In the case in which
                    // we are bound to a local IPEndPoint this will remove the
                    // binding and free up the IPEndPoint for later uses.
                    Socket chkClientSocket = _clientSocket;
                    if (chkClientSocket != null)
                    {
                        try
                        {
                            chkClientSocket.InternalShutdown(SocketShutdown.Both);
                        }
                        finally
                        {
                            chkClientSocket.Dispose();
                            _clientSocket = null;
                        }
                    }
                }

                DisposeCore(); // platform-specific disposal work

                GC.SuppressFinalize(this);
            }

            _cleanedUp = true;
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, this, nameof(Dispose), "");
            }
        }
Exemple #7
0
        // Disposes the Tcp connection.
        protected virtual void Dispose(bool disposing)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.Sockets, this, "Dispose", "");
            }

            if (_cleanedUp)
            {
                if (Logging.On)
                {
                    Logging.Exit(Logging.Sockets, this, "Dispose", "");
                }

                return;
            }

            if (disposing)
            {
                IDisposable dataStream = _dataStream;
                if (dataStream != null)
                {
                    dataStream.Dispose();
                }
                else
                {
                    // If the NetworkStream wasn't created, the Socket might
                    // still be there and needs to be closed. In the case in which
                    // we are bound to a local IPEndPoint this will remove the
                    // binding and free up the IPEndPoint for later uses.
                    Socket chkClientSocket = Client;
                    if (chkClientSocket != null)
                    {
                        try
                        {
                            chkClientSocket.InternalShutdown(SocketShutdown.Both);
                        }
                        finally
                        {
                            chkClientSocket.Dispose();
                            Client = null;
                        }
                    }
                }

                GC.SuppressFinalize(this);
            }

            _cleanedUp = true;
            if (Logging.On)
            {
                Logging.Exit(Logging.Sockets, this, "Dispose", "");
            }
        }
Exemple #8
0
 private void FreeResources()
 {
     if (!this.m_CleanedUp)
     {
         Socket client = this.Client;
         if (client != null)
         {
             client.InternalShutdown(SocketShutdown.Both);
             client.Close();
             this.Client = null;
         }
         this.m_CleanedUp = true;
     }
 }
Exemple #9
0
 protected virtual void Dispose(bool disposing)
 {
     if (Logging.On)
     {
         Logging.Enter(Logging.Sockets, this, "Dispose", "");
     }
     if (this.m_CleanedUp)
     {
         if (Logging.On)
         {
             Logging.Exit(Logging.Sockets, this, "Dispose", "");
         }
     }
     else
     {
         if (disposing)
         {
             IDisposable dataStream = this.m_DataStream;
             if (dataStream != null)
             {
                 dataStream.Dispose();
             }
             else
             {
                 Socket client = this.Client;
                 if (client != null)
                 {
                     try
                     {
                         client.InternalShutdown(SocketShutdown.Both);
                     }
                     finally
                     {
                         client.Close();
                         this.Client = null;
                     }
                 }
             }
             GC.SuppressFinalize(this);
         }
         this.m_CleanedUp = true;
         if (Logging.On)
         {
             Logging.Exit(Logging.Sockets, this, "Dispose", "");
         }
     }
 }
Exemple #10
0
        /// <include file='doc\NetworkStream.uex' path='docs/doc[@for="NetworkStream.Dispose"]/*' />
        protected virtual void Dispose(bool disposing)
        {
            if (m_CleanedUp)
            {
                return;
            }

            if (disposing)
            {
                //no managed objects to cleanup
            }
            //
            // only resource we need to free is the network stream, since this
            // is based on the client socket, closing the stream will cause us
            // to flush the data to the network, close the stream and (in the
            // NetoworkStream code) close the socket as well.
            //
            if (m_StreamSocket != null)
            {
                m_Readable  = false;
                m_Writeable = false;
                if (m_OwnsSocket)
                {
                    //
                    // if we own the Socket (false by default), close it
                    // swallowing possible exceptions (eg: the user told us
                    // that we own the Socket but it closed at some point of time,
                    // here we would get an ObjectDisposedException)
                    //
                    Socket chkStreamSocket = m_StreamSocket;
                    if (chkStreamSocket != null)
                    {
                        chkStreamSocket.InternalShutdown(SocketShutdown.Both);
                        chkStreamSocket.Close();
                    }
                }
                //
                // at this point dereference the Socket anyway
                //
                m_StreamSocket = null;
            }
            m_CleanedUp = true;
        }
Exemple #11
0
        protected override void Dispose(bool disposing)
        {
#if DEBUG
            using (GlobalLog.SetThreadKind(ThreadKinds.User)) {
#endif
            if (!m_CleanedUp && disposing)
            {
                //
                // only resource we need to free is the network stream, since this
                // is based on the client socket, closing the stream will cause us
                // to flush the data to the network, close the stream and (in the
                // NetoworkStream code) close the socket as well.
                //
                if (m_StreamSocket != null)
                {
                    m_Readable  = false;
                    m_Writeable = false;
                    if (m_OwnsSocket)
                    {
                        //
                        // if we own the Socket (false by default), close it
                        // swallowing possible exceptions (eg: the user told us
                        // that we own the Socket but it closed at some point of time,
                        // here we would get an ObjectDisposedException)
                        //
                        Socket chkStreamSocket = m_StreamSocket;
                        if (chkStreamSocket != null)
                        {
                            chkStreamSocket.InternalShutdown(SocketShutdown.Both);
                            chkStreamSocket.Close(m_CloseTimeout);
                        }
                    }
                }
            }
            m_CleanedUp = true;
#if DEBUG
        }
#endif
            base.Dispose(disposing);
        }
        /// <devdoc>
        ///    <para>
        ///       Establishes a connection to the specified port on the
        ///       specified host.
        ///    </para>
        /// </devdoc>
        public void Connect(string hostname, int port)
        {
            //
            // parameter validation
            //
            if (m_CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (!ValidationHelper.ValidateTcpPort(port))
            {
                throw new ArgumentOutOfRangeException("port");
            }

            //
            // IPv6 Changes: instead of just using the first address in the list,
            //               we must now look for addresses that use a compatible
            //               address family to the client socket.
            //               However, in the case of the <hostname,port> constructor
            //               we will have deferred creating the socket and will
            //               do that here instead.
            //               In addition, the redundant CheckForBroadcast call was
            //               removed here since it is called from Connect().
            //
            IPAddress[] addresses = Dns.GetHostAddresses(hostname);

            foreach (IPAddress address in addresses)
            {
                if (m_ClientSocket == null)
                {
                    //
                    // We came via the <hostname,port> constructor. Set the
                    // address family appropriately, create the socket and
                    // try to connect.
                    //
                    m_Family = address.AddressFamily;

                    createClientSocket();

                    try
                    {
                        //
                        // Attempt to connect to the host
                        //
                        Connect(new IPEndPoint(address, port));

                        break;
                    }
                    catch (SocketException)
                    {
                        //
                        // Destroy the client socket here for retry with
                        // the next address.
                        //
                        try{
                            m_ClientSocket.InternalShutdown(SocketShutdown.Both);
                        }
                        finally{
                            m_ClientSocket.Close();
                        }
                    }
                }
                else if (address.AddressFamily == m_Family)
                {
                    //
                    // Only use addresses with a matching family
                    //
                    try
                    {
                        //
                        // Attempt to connect to the host
                        //
                        Connect(new IPEndPoint(address, port));

                        break;
                    }
                    catch (SocketException)
                    {
                        //
                        // Intentionally empty - all set to retry the next address
                        //
                    }
                }
            }
            //
            // m_Active tells us whether we managed to connect
            //
            if (!m_Active)
            {
                throw new SocketException(SocketError.NotConnected);
            }
        }