Esempio n. 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;
 }
Esempio n. 2
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));
 }
Esempio n. 3
0
 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) );
 }
Esempio n. 4
0
	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));
	    }
	}
Esempio n. 5
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));
	}
Esempio n. 6
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));
     }
 }
Esempio n. 7
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(); });
 }
Esempio n. 8
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));
		}
	}
Esempio n. 9
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));
     }
 }
Esempio n. 10
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));
 }
Esempio n. 11
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));
    }
Esempio n. 12
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));
 }
Esempio n. 13
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();
 }
Esempio n. 14
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);
    });
}
Esempio n. 15
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(); }};
 }
Esempio n. 16
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();
		});
	}
void cia_server::handle_accept(cia_client::ptr client, const boost::system::error_code & err)
{
	if (err)
	{
		BOOST_LOG_SEV(cia_g_logger, Debug) << "服务端已停止接收新的客户端连接";
		return;
	}
	// BOOST_LOG_SEV(cia_g_logger, Debug) << "服务器接收到新的客户端连接";
	client->start();
	cia_client::ptr new_client = cia_client::new_(m_io_service, m_config_server);
	int queue_size_ = m_config_server->get_call_out_obj_queue_size();
	for (int x = 0; x < queue_size_; x++)
	{
		new_client->put_into_call_out_obj_queue(boost::make_shared<call_out_obj>(new_client, m_base_voice_card, m_io_service));
	}
	// BOOST_LOG_SEV(cia_g_logger, Debug) << "服务器开始准备接收新的连接";
	m_acceptor_.async_accept(new_client->sock(), boost::bind(&cia_server::handle_accept, this, new_client, _1));
}
Esempio n. 18
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(); }};
 }
    void do_accept2()
    {
        session_.reset(new asio_session(io_service_pool_.get_io_service(), buffer_size_, packet_size_, g_test_mode));
        acceptor_.async_accept(session_->socket(),
            [this](const boost::system::error_code & ec)
            {
                if (!ec) {
                    session_->start();
                }
                else {
                    // Accept error
                    std::cout << "async_asio_echo_serv_ex::handle_accept2() - Error: (code = " << ec.value() << ") "
                              << ec.message().c_str() << std::endl;
                    session_->stop();
                    session_.reset();
                }

                do_accept2();
            });
    }
Esempio n. 20
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;
 }
Esempio n. 21
0
	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));
	}
Esempio n. 22
0
 // Initiates an accept
 void do_accept()
 {
     acceptor_.async_accept(sock_, ep_,
         std::bind(&server::on_accept, this,
             std::placeholders::_1));
 }
Esempio n. 23
0
	//start()函数用于启动异步接受连接,需要调用acceptor的async_accept()函数。
	//为了能够让socket能够被异步调用后还能使用,我们必须使用shared_ptr来创建socket对象的智能指针,
	//它可以再程序的整个生命周期中存在,直到没有人使用它为止。
	void start()
	{
		sock_pt sock(new boost::asio::ip::tcp::socket(ios));//智能指针
		//启动异步监听服务
		acceptor.async_accept(*sock, bind(&server::accept_handler, this, boost::asio::placeholders::error, sock));
	}
 void do_accept()
 {
     asio_session * new_session = new asio_session(io_service_pool_.get_io_service(), buffer_size_, packet_size_, g_test_mode);
     acceptor_.async_accept(new_session->socket(), boost::bind(&async_asio_echo_serv_ex::handle_accept,
         this, boost::asio::placeholders::error, new_session));
 }