Пример #1
0
        /// <summary>
        /// Close the socket.  This is NOT async.  .Net doesn't have
        /// async closes.  But, it can be *called* async, particularly
        /// from GotData.  Attempts to do a shutdown() first.
        /// </summary>
        public override void Close()
        {
            Debug.WriteLine("Close");
            lock (this)
            {
                /*
                 * switch (State)
                 * {
                 * case State.Closed:
                 *  throw new InvalidOperationException("Socket already closed");
                 * case State.Closing:
                 *  throw new InvalidOperationException("Socket already closing");
                 * }
                 */

                SocketState oldState = State;

                if (m_sock.Connected)
                {
                    State = SocketState.Closing;
                }

                if (m_stream != null)
                {
                    m_stream.Close();
                }
                else
                {
                    try
                    {
                        m_sock.Close();
                    }
                    catch { }
                }

                if (oldState <= SocketState.Connected)
                {
                    m_listener.OnClose(this);
                }

                if (m_watcher != null)
                {
                    m_watcher.CleanupSocket(this);
                }

                State = SocketState.Closed;
            }
        }