示例#1
0
        /// <summary>
        /// Stops listening to the specified end point. Does not close any connections.
        /// </summary>
        /// <param name="endPoint">The end point</param>
        public override void StopListening(object endPoint)
        {
            lock (this)
            {
                if (this._socket == null)
                {
                    return;
                }

                // LOG:
                BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "UdpConnectionManager.StopListening",
                                               LogMessageType.ListeningStopped, null, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, this.DbgConnectionId, 0, 0, 0, null, null, null, null,
                                               "The listening socket is not longer associated with the {0} local end point.", endPoint == null ? string.Empty : endPoint.ToString());
                }

                SocketUtility.CloseSocket(this._socket);
                this._socket = null;

                this._closing = true;
            }

            if (!this._receivingThreadClosed.WaitOne(TimeSpan.FromMinutes(2), false))
            {
                throw GenuineExceptions.Get_Processing_LogicError("Receiving thread didn't exit within 2 minutes.");
            }
        }
        /// <summary>
        /// Accepts incoming connections.
        /// </summary>
        public void AcceptConnections()
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;
            Exception       exception       = null;
            int             numberOfFailure = 0;

            try
            {
                for ( ; ;)
                {
                    try
                    {
                        if (this.IAcceptConnectionConsumer.IsDisposed())
                        {
                            return;
                        }
                        if (this.StopListening.WaitOne(0, false))
                        {
                            return;
                        }

                        Socket clientSocket = this.Socket.Accept();
                        GenuineThreadPool.QueueUserWorkItem(new WaitCallback(this.AcceptConnection), clientSocket, true);
                    }
                    catch (Exception ex)
                    {
                        numberOfFailure++;

                        // LOG:
                        if (binaryLogWriter != null && binaryLogWriter[LogCategory.AcceptingConnection] > 0)
                        {
                            binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "GenuineTcp.AcceptConnectionClosure.AcceptConnections",
                                                       LogMessageType.ConnectionAccepting, ex, null, null, null,
                                                       GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                       null, null, -1, 0, 0, 0, null, null, null, null,
                                                       "An inbound TCP connection has not been accepted. Number of failure: {0}.", numberOfFailure);
                        }

                        this.ITransportContext.IGenuineEventProvider.Fire(new GenuineEventArgs(GenuineEventType.GeneralListenerFailure, ex, null, ListeningEndPoint));
                    }
                }
            }
            catch (Exception ex)
            {
                exception = ex;

                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.AcceptingConnection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "GenuineTcp.AcceptConnectionClosure.AcceptConnections",
                                               LogMessageType.ListeningStopped, ex, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, -1, 0, 0, 0, null, null, null, null,
                                               "Fatal Error. The socket does not accept inbound connections any longer.");
                }

                this.ITransportContext.IGenuineEventProvider.Fire(new GenuineEventArgs(GenuineEventType.GeneralListenerFailure, ex, null, this.ListeningEndPoint));
            }
            finally
            {
                SocketUtility.CloseSocket(this.Socket);
            }
        }