Example #1
0
 void start_listening() {
     using boost::asio::ip::tcp;
     system::error_code error;
     tcp::resolver resolver(service_);
     tcp::resolver::query query(address_, port_);
     tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
     if (error) {
         NETWORK_MESSAGE("Error resolving address: " << address_ << ':' << port_);
         return;
     }
     tcp::endpoint endpoint = *endpoint_iterator;
     acceptor_.open(endpoint.protocol(), error);
     if (error) {
         NETWORK_MESSAGE("Error opening socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
         return;
     }
     socket_options_base::acceptor_options(acceptor_);
     acceptor_.bind(endpoint, error);
     if (error) {
         NETWORK_MESSAGE("Error binding to socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
         return;
     }
     acceptor_.listen(tcp::socket::max_connections, error);
     if (error) {
         NETWORK_MESSAGE("Error listening on socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
         return;
     }
     new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
     acceptor_.async_accept(new_connection->socket(),
         boost::bind(&sync_server_base<Tag,Handler>::handle_accept,
                     this, boost::asio::placeholders::error));
     listening_ = true;
 }
	~PrivateData()
	{
		if ( m_success )
		{
			m_acceptor.cancel();
			m_acceptor.close();
			m_thread.join();
		}
	}
void InitTCPServer(boost::asio::ip::tcp::acceptor& server, int server_port) {
  boost::system::error_code server_ec;
  boost::asio::ip::tcp::endpoint server_ep(boost::asio::ip::tcp::v4(),
                                           server_port);
  server.open(boost::asio::ip::tcp::v4(), server_ec);
  ASSERT_EQ(0, server_ec.value()) << "Could not open server acceptor";
  server.bind(server_ep, server_ec);
  ASSERT_EQ(0, server_ec.value()) << "Could not bind server acceptor";
  server.listen(boost::asio::socket_base::max_connections, server_ec);
  ASSERT_EQ(0, server_ec.value()) << "Server acceptor could not listen";
}
Example #4
0
 void
 on_accept(error_code ec)
 {
     if(! acceptor_.is_open())
         return;
     maybe_throw(ec, "accept");
     static int id_ = 0;
     std::thread{lambda{++id_, *this, std::move(sock_)}}.detach();
     acceptor_.async_accept(sock_,
         std::bind(&http_sync_server::on_accept, this,
             asio::placeholders::error));
 }
	PrivateData( int portNumber ) :
		m_success(false),
		m_endpoint(tcp::v4(), portNumber),
		m_service(),
		m_acceptor( m_service ),
		m_thread()
	{
		m_acceptor.open(  m_endpoint.protocol() );
		m_acceptor.set_option( boost::asio::ip::tcp::acceptor::reuse_address(true));
		m_acceptor.bind( m_endpoint );
		m_acceptor.listen();
		m_success = true;
	}
Example #6
0
 http_sync_server(endpoint_type const& ep,
         std::string const& root)
     : sock_(ios_)
     , acceptor_(ios_)
     , root_(root)
 {
     acceptor_.open(ep.protocol());
     acceptor_.bind(ep);
     acceptor_.listen(
         boost::asio::socket_base::max_connections);
     acceptor_.async_accept(sock_,
         std::bind(&http_sync_server::on_accept, this,
             beast::asio::placeholders::error));
     thread_ = std::thread{[&]{ ios_.run(); }};
 }
Example #7
0
 inline accepting_source::element_type *
 accepting_source::copy_next(iterator_range<element_type *> destination)
 {
     assert(m_acceptor);
     assert(m_yield);
     for (auto &client : destination)
     {
         assert(m_acceptor);
         client = std::make_shared<boost::asio::ip::tcp::socket>(
             m_acceptor->get_io_service());
         assert(m_yield);
         m_acceptor->async_accept(*client, *m_yield);
     }
     return destination.end();
 }
Example #8
0
 /// Handle a request to stop the server.
 virtual void handle_stop() {
     // The server is stopped by cancelling all outstanding asynchronous
     // operations. Once all operations have finished the io_service::run() call
     // will exit.
     m_acceptor.close();
     base_t::handle_stop();
 }
Example #9
0
 void StartAccept()
 {
   NewConnection = boost::make_shared<Private::Connection, boost::asio::io_service &>(IoService);
   Acceptor.async_accept(NewConnection->GetSocket(),
                         boost::bind(&EchoServer::HandleAccept, this,
                                     boost::asio::placeholders::error));
 }
Example #10
0
 ~http_sync_server()
 {
     error_code ec;
     ios_.dispatch(
         [&]{ acceptor_.close(ec); });
     thread_.join();
 }
Example #11
0
	void start_accept()
	{
		using namespace std;
		using namespace boost::asio::ip;

		auto connection = make_shared<tcp_connection>(_acceptor.get_io_service());
		_acceptor.async_accept(connection->socket(), [connection, this](const boost::system::error_code &ec)
		{
			if (!ec)
			{
				connection->send_string();
			}

			this->start_accept();
		});
	}
Example #12
0
 void Release() {
   acceptor->close();
   delete acceptor;
   (*socket_pptr)->close();
   delete *socket_pptr;
   delete socket_pptr;
 }
 void start()
 {
     session * new_session( new session( io_service_) );
     acceptor_.async_accept(
         new_session->socket(),
         boost::bind( & server::handle_accept_, this->shared_from_this(),
             new_session, boost::asio::placeholders::error) );
 }
    /** Open a listening port.

        @param ep The address and port to bind to.

        @param ec Set to the error, if any occurred.
    */
    void
    open(tcp::endpoint const& ep, error_code& ec)
    {
        acceptor_.open(ep.protocol(), ec);
        if(ec)
            return fail("open", ec);
        acceptor_.set_option(
            boost::asio::socket_base::reuse_address{true});
        acceptor_.bind(ep, ec);
        if(ec)
            return fail("bind", ec);
        acceptor_.listen(
            boost::asio::socket_base::max_connections, ec);
        if(ec)
            return fail("listen", ec);
        do_accept();
    }
	inline void server::handle_accept(const boost::system::error_code& e) {
	    if (!e) {
		new_connection_->start();
		new_connection_.reset(new connection(io_service_, request_handler_));
		acceptor_.async_accept(new_connection_->socket(),
				       boost::bind(&server::handle_accept, this,
						   boost::asio::placeholders::error));
	    }
	}
Example #16
0
	void start_accept()
	{
		boost::shared_ptr<session> new_session =
			boost::make_shared<session>(boost::ref(io_service_));
		acceptor_.async_accept(new_session->socket_,
			boost::bind(&server::handle_accept, this,
				boost::asio::placeholders::error,
				new_session));
	}
Example #17
0
 sync_echo_server(bool server, endpoint_type ep)
     : sock_(ios_)
     , acceptor_(ios_)
 {
     error_code ec;
     acceptor_.open(ep.protocol(), ec);
     maybe_throw(ec, "open");
     acceptor_.set_option(
         boost::asio::socket_base::reuse_address{true});
     acceptor_.bind(ep, ec);
     maybe_throw(ec, "bind");
     acceptor_.listen(
         boost::asio::socket_base::max_connections, ec);
     maybe_throw(ec, "listen");
     acceptor_.async_accept(sock_,
                            std::bind(&sync_echo_server::on_accept, this,
                                      beast::asio::placeholders::error));
     thread_ = std::thread{[&]{ ios_.run(); }};
 }
	sock_handle_t start_accept()
	{
		sock_handle_t handle = next_unused_tcp();
		if (!socket_handle_valid(handle))
			return handle;

		ip::tcp::socket& sock = tcp_from(handle);
		acceptor.accept(sock);
		return handle;
	}
Example #19
0
 void handle_accept(boost::system::error_code const & ec) {
     if (!ec) {
         socket_options_base::socket_options(new_connection->socket());
         new_connection->start();
         new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
         acceptor_.async_accept(new_connection->socket(),
             boost::bind(&sync_server_base<Tag,Handler>::handle_accept,
                         this, boost::asio::placeholders::error));
     }
 }
	sock_handle_t accept()
	{
		sock_handle_t handle = next_unused_tcp();
		if (!socket_handle_valid(handle))
			return handle;

		ip::tcp::socket& sock = tcp_from(handle);

		acceptor.accept(sock, ec);
		return !ec ? handle : socket_handle_invalid();
	}
Example #21
0
 void handle_accept(boost::system::error_code const& ec) {
   if (ec) {
   }
   socket_options_base::socket_options(new_connection->socket());
   new_connection->start();
   new_connection.reset(new sync_connection<Tag, Handler>(service_, handler_));
   auto self = this->shared_from_this();
   acceptor_.async_accept(
       new_connection->socket(),
       [=] (boost::system::error_code const &ec) { self->handle_accept(); });
 }
 /// Destructor.
 ~server()
 {
     work_ = boost::none;
     ios_.dispatch([&]
         {
             error_code ec;
             acceptor_.close(ec);
         });
     for(auto& t : thread_)
         t.join();
 }
Example #23
0
	void startAccept()
	{
		if (mAvailableConnections > 0) {
			--mAvailableConnections;
			SC_TcpConnection::pointer newConnection (new SC_TcpConnection(mWorld, ioService, this));

			acceptor.async_accept(newConnection->socket,
								  boost::bind(&SC_TcpInPort::handleAccept, this, newConnection,
											  boost::asio::placeholders::error));
		}
	}
Example #24
0
 /// Handle completion of an asynchronous accept operation.
 void handle_accept(const boost::system::error_code& e) {
     if (handle_accept_internal(e, m_new_channel)) {
         m_new_channel =
             tcp_channel<Handler>::create(this->m_io_service,
                     m_new_channel->handler(), channel<Handler>::TCP);
         m_acceptor.async_accept(
             static_cast<tcp_channel<Handler>&>(*m_new_channel).socket(),
             std::bind(&tcp_server<Handler>::handle_accept,
                         this, boost::asio::placeholders::error));
     }
 }
Example #25
0
 void start_listening() {
   using boost::asio::ip::tcp;
   system::error_code error;
   tcp::resolver resolver(service_);
   tcp::resolver::query query(address_, port_);
   tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error resolving address: " << address_ << ':'
                                                       << port_);
     boost::throw_exception(std::runtime_error("Error resolving address."));
   }
   tcp::endpoint endpoint = *endpoint_iterator;
   acceptor_.open(endpoint.protocol(), error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error opening socket: " << address_ << ':' << port_
                                                    << " -- reason: '" << error
                                                    << '\'');
     boost::throw_exception(std::runtime_error("Error opening socket."));
   }
   socket_options_base::acceptor_options(acceptor_);
   acceptor_.bind(endpoint, error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error binding to socket: "
                           << address_ << ':' << port_ << " -- reason: '"
                           << error << '\'');
     boost::throw_exception(std::runtime_error("Error binding to socket."));
   }
   acceptor_.listen(tcp::socket::max_connections, error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error listening on socket: "
                           << address_ << ':' << port_ << " -- reason: '"
                           << error << '\'');
     boost::throw_exception(std::runtime_error("Error listening on socket."));
   }
   new_connection.reset(new sync_connection<Tag, Handler>(service_, handler_));
   auto self = this->shared_from_this();
   acceptor_.async_accept(
       new_connection->socket(),
       [=] (boost::system::error_code const &ec) { self->handle_accept(ec); });
   listening_ = true;
 }
	inline server::server(const std::string& address, 
			      const std::string& port,
			      std::size_t thread_pool_size, 
			      request_handler& request_handler_)
	    : thread_pool_size_(thread_pool_size),
	      acceptor_(io_service_),
	      new_connection_(new connection(io_service_, request_handler_)),
	      request_handler_(request_handler_)
	{
	    // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
	    boost::asio::ip::tcp::resolver resolver(io_service_);
	    boost::asio::ip::tcp::resolver::query query(address, port);
	    boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
	    acceptor_.open(endpoint.protocol());
	    acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
	    acceptor_.bind(endpoint);
	    acceptor_.listen();
	    acceptor_.async_accept(new_connection_->socket(),
				   boost::bind(&server::handle_accept, this,
					       boost::asio::placeholders::error));
	}
Example #27
0
    void start_accept()
    {
        session::pointer session(
            session::create(
                io_service_, game_));

        acceptor_.async_accept(
            session->socket(),
            boost::bind(
                &server::handle_accept, this, session,
                boost::asio::placeholders::error));
    }
Example #28
0
 void
 on_accept(error_code ec)
 {
     if(ec == boost::asio::error::operation_aborted)
         return;
     maybe_throw(ec, "accept");
     static int id_ = 0;
     std::thread{lambda{++id_, *this, std::move(sock_)}} .detach();
     acceptor_.async_accept(sock_,
                            std::bind(&sync_echo_server::on_accept, this,
                                      beast::asio::placeholders::error));
 }
    void start(const std::string & ip_addr, const std::string & port)
    {
        ip::tcp::resolver resolver(io_service_pool_.get_now_io_service());
        ip::tcp::resolver::query query(ip_addr, port);
        ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);

        boost::system::error_code ec;
        acceptor_.open(endpoint.protocol(), ec);
        if (ec) {
            // Open endpoint error
            std::cout << "async_asio_echo_serv_ex::start() - Error: (code = " << ec.value() << ") "
                      << ec.message().c_str() << std::endl;
            return;
        }

        boost::asio::socket_base::reuse_address option(true);
        acceptor_.set_option(option);
        acceptor_.bind(endpoint);
        acceptor_.listen();

        do_accept();
    }
Example #30
0
void accept(boost::asio::io_service &service,
        boost::asio::ip::tcp::acceptor &a,
        tell::db::ClientManager<void>& clientManager,
        int16_t numWarehouses) {
    auto conn = new tpcc::Connection(service, clientManager, numWarehouses);
    a.async_accept(conn->socket(), [conn, &service, &a, &clientManager, numWarehouses](const boost::system::error_code &err) {
        if (err) {
            delete conn;
            LOG_ERROR(err.message());
            return;
        }
        conn->run();
        accept(service, a, clientManager, numWarehouses);
    });
}