Exemplo n.º 1
0
        public void Start()
        {
            stopped = false;
            mappingStrategy.Load();
            pool = new Util.ThreadPool("AGIServer", poolSize);
#if LOGGER
            logger.Info("Thread pool started.");
#endif
            try
            {
                IPAddress ipAddress = IPAddress.Parse(address);
                serverSocket = new IO.ServerSocket(port, ipAddress, this.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

            AGIConnectionHandler connectionHandler;
            IO.SocketConnection  socket;
            try
            {
                while ((socket = serverSocket.Accept()) != null)
                {
#if LOGGER
                    logger.Info("Received connection.");
#endif
                    connectionHandler = new AGIConnectionHandler(socket, mappingStrategy);
                    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
            }
        }