boost::optional<BoostConnectionServer::Error> BoostConnectionServer::tryStart() {
	try {
		assert(!acceptor_);
		if (address_.isValid()) {
			acceptor_ = new boost::asio::ip::tcp::acceptor(
				*ioService_, 
				boost::asio::ip::tcp::endpoint(address_.getRawAddress(), boost::numeric_cast<unsigned short>(port_)));
		}
		else {
			acceptor_ = new boost::asio::ip::tcp::acceptor(
				*ioService_, 
				boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), boost::numeric_cast<unsigned short>(port_)));
		}
		acceptNextConnection();
	}
	catch (const boost::system::system_error& e) {
		if (e.code() == boost::asio::error::address_in_use) {
			return Conflict;
		}
		else {
			return UnknownError;
		}
	}
	return boost::optional<Error>();
}
void BoostConnectionServer::handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error) {
	if (error) {
		eventLoop->postEvent(
				boost::bind(
						&BoostConnectionServer::stop, shared_from_this(), UnknownError), 
				shared_from_this());
	}
	else {
		eventLoop->postEvent(
				boost::bind(boost::ref(onNewConnection), newConnection), 
				shared_from_this());
		newConnection->listen();
		acceptNextConnection();
	}
}
void BoostConnectionServer::start() {
	try {
		assert(!acceptor_);
		acceptor_ = new boost::asio::ip::tcp::acceptor(
				*ioService_, 
				boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port_));
		acceptNextConnection();
	}
	catch (const boost::system::system_error& e) {
		if (e.code() == boost::asio::error::address_in_use) {
			eventLoop->postEvent(boost::bind(boost::ref(onStopped), Conflict), shared_from_this());
		}
		else {
			eventLoop->postEvent(boost::bind(boost::ref(onStopped), UnknownError), shared_from_this());
		}
	}
}
Exemple #4
0
   Error runSingleThreaded()
   {

      // update state
      running_ = true;

      // get ready for next connection
      acceptNextConnection();

      // initialize scheduled command timer
      waitForScheduledCommandTimer();


      // run
      runServiceThread();


      return Success();
   }
Exemple #5
0
   Error run(std::size_t threadPoolSize = 1)
   {
      try
      {
         // update state
         running_ = true;

         // get ready for next connection
         acceptNextConnection();

         // initialize scheduled command timer
         waitForScheduledCommandTimer();
         
         // block all signals for the creation of the thread pool
         // (prevents signals from occurring on any of the handler threads)
         core::system::SignalBlocker signalBlocker;
         Error error = signalBlocker.blockAll();
         if (error)
            return error ;
      
         // create the threads
         for (std::size_t i=0; i < threadPoolSize; ++i)
         {
            // run the thread
            boost::shared_ptr<boost::thread> pThread(new boost::thread(
                              &AsyncServer<ProtocolType>::runServiceThread,
                              this));
            
            // add to list of threads
            threads_.push_back(pThread);            
         }
      }
      catch(const boost::thread_resource_error& e)
      {
         return Error(boost::thread_error::ec_from_exception(e),
                      ERROR_LOCATION);
      }
      
      return Success();
   }