Example #1
0
 public ThreadTask(ThreadPool enclosingInstance, string name)
     : base(name)
 {
     this.threadPool = enclosingInstance;
 }
        public void Start()
        {
            stopped = false;
            mappingStrategy.Load();
            pool = new ThreadPool("AGIServer", poolSize);
#if LOGGER
            logger.Info("Thread pool started.");
#endif
            try
            {
                var ipAddress = IPAddress.Parse(address);
                serverSocket = new ServerSocket(port, ipAddress, SocketEncoding);
            }
            catch (IOException ex)
            {
#if LOGGER
                logger.Error("Unable start AGI Server: cannot to bind to " + address + ":" + port + ".", ex);
#endif
                throw ex;
            }
#if LOGGER
            logger.Info("Listening on " + address + ":" + port + ".");
#endif

            try
            {
                SocketConnection socket;
                while ((socket = serverSocket.Accept()) != null)
                {
#if LOGGER
                    logger.Info("Received connection.");
#endif
                    var connectionHandler = new AGIConnectionHandler(socket, mappingStrategy, SC511_CAUSES_EXCEPTION,
                        SCHANGUP_CAUSES_EXCEPTION);
                    pool.AddJob(connectionHandler);
                }
            }
            catch (IOException ex)
            {
                if (!stopped)
                {
#if LOGGER
                    logger.Error("IOException while waiting for connections (1).", ex);
#endif
                    throw ex;
                }
            }
            finally
            {
                if (serverSocket != null)
                {
                    try
                    {
                        serverSocket.Close();
                    }
#if LOGGER
                    catch (IOException ex)
                    {
                        logger.Error("IOException while waiting for connections (2).", ex);
                    }
#else
					catch { }
#endif
                }
                serverSocket = null;
                pool.Shutdown();
#if LOGGER
                logger.Info("AGIServer shut down.");
#endif
            }
        }