Ejemplo n.º 1
0
        public virtual void Listen()
        {
            WorkerCollection lWorkers = null;

            if (Owner.CloseConnectionsOnShutdown)
            {
                lWorkers = new WorkerCollection();
            }

            try
            {
                Socket lSocket;
                do
                {
                    try
                    {
                        lSocket = fListeningSocket.Accept();
                    }
                    catch (ObjectDisposedException)
                    {
                        return;
                    }
                    catch (SocketException)
                    {
                        /* If Accept fails with a SocketException, the socket was ListeningSocket was probably
                         * closed, so we'll just exit and terminate the thread. */
                        return;
                    }

                    if (lSocket != null)
                    {
                                                #if echoes
                        Object lObject = Activator.CreateInstance(WorkerClass);
                                                #elif cooper
                        Object lObject = Class.getDeclaredConstructor(WorkerClass).newInstance();
                                                #elif island
                        Object lObject = WorkerClass.Instantiate();
                                                #elif toffee
                        Object lObject = WorkerClass.init();
                                                #endif
                        IWorker lWorker = lObject as IWorker;
                        lWorker.Owner = Owner;

                        if (Owner.ConnectionFactory != null)
                        {
                            lWorker.DataConnection = Owner.ConnectionFactory.CreateServerConnection(lSocket);
                        }
                                                #if ECHOES
                        else if (Owner.ConnectionClass != null)
                        {
                            lWorker.DataConnection = (Connection)Activator.CreateInstance(Owner.ConnectionClass);
                            lWorker.DataConnection.Init(lSocket);
                        }
                                                #endif
                                                #if FULLFRAMEWORK
                        else if (Owner.SslOptions.Enabled)
                        {
                            lWorker.DataConnection = Owner.SslOptions.CreateServerConnection(lSocket);
                        }
                                                #endif
                        else
                        {
                            lWorker.DataConnection = new Connection(lSocket);
                        }

                                                #if FULLFRAMEWORK
                        if (Owner.TimeoutEnabled)
                        {
                            lWorker.DataConnection.TimeoutEnabled = true;
                            lWorker.DataConnection.Timeout        = Owner.Timeout;
                        }
                                                #endif

                        if (Owner.MaxLineLengthEnabled)
                        {
                            lWorker.DataConnection.MaxLineLengthEnabled = true;
                            lWorker.DataConnection.MaxLineLength        = Owner.MaxLineLength;
                        }

                        try
                        {
                            lWorker.DataConnection.InitializeServerConnection();
                        }
                        catch (Exception)                         // nothing should escape this loop.
                        {
                            lWorker.DataConnection.Dispose();
                            continue;
                        }

                        lWorker.Thread = new Thread(() => { lWorker.Work(); });
                        try
                        {
                                                        #if FULLFRAMEWORK
                            lWorker.Thread.Name = String.Format("Internet Pack {0} for {1}", WorkerClass.Name, lSocket.RemoteEndPoint);
                                                        #endif
                        }
                        catch (SocketException)
                        {
                            // mono can fail in that code if the remote side has been disconnected. Since we already create a thread we have to run it (leak otherwise)
                        }

                        if (lWorkers != null)
                        {
                            lWorkers.Add(lWorker);
                        }

                        lWorker.Thread.Start();
                    }
                }while (lSocket != null);
            }
            finally
            {
                if (lWorkers != null)
                {
                    lWorkers.Close();
                }
            }
        }