Пример #1
0
        public void Close(bool keepAlive)
        {
            if (!keepAlive || !IsConnected())
            {
                stream.Close();
                if (stream != netStream)
                {
                    netStream.Close();
                }
                else if (false == netStream.OwnsSocket)
                {
                    try {
                        if (sock.Connected && !(sock.Poll(0, SelectMode.SelectRead) && sock.Available == 0))
                        {
                            sock.Shutdown(SocketShutdown.Both);
                        }
                    } catch {
                        // ignore
                    }

                    try {
                        server.UnregisterSocket(sock);
                    } catch {
                        // ignore
                    }

                    try {
                        sock.Close();
                    } catch {
                        // ignore
                    }
                }

                return;
            }

            netStream.EnableLingering = false;
            stream.Close();
            if (stream != netStream)
            {
                netStream.Close();
            }

            server.ReuseSocket(sock, reuses + 1);
        }