void AsyncConnection::Send(const MessagePtr msg) { if (is_open()) { async_send(boost::asio::buffer(msg->Data(), msg->Length()), m_strand.wrap(boost::bind(&AsyncConnection::HandleSend, this, msg, boost::asio::placeholders::error, true))); } else { boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(m_ip), m_port); async_connect(endpoint, m_strand.wrap(boost::bind(&AsyncConnection::HandleConnect, this, msg, boost::asio::placeholders::error))); } }
void AsyncConnection::HandleSend(const MessagePtr msg, const boost::system::error_code& e, bool resend) { if (e) { if (resend) { boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(m_ip), m_port); async_connect(endpoint, m_strand.wrap(boost::bind(&AsyncConnection::HandleConnect, this, msg, boost::asio::placeholders::error))); } else { m_app->HandleSendError(Connection::shared_from_this(), msg, e); } } }
int async_net(const char* host, short port) { int sockfd = async_socket(); if (sockfd < 0) { return sockfd; } int ret = async_connect(sockfd, host, port); if (ret < 0) { close(sockfd); return ret; } return sockfd; }
void server::handle_reverse_connection(const boost::system::error_code& err) { if (!err) { new_connection_.reset(new connection(std::move(socket_), io_service_)); new_connection_->start(); } else { // Construct a timer without setting an expiry time. boost::asio::deadline_timer timer(io_service_); timer.expires_from_now(boost::posix_time::seconds(5)); timer.wait(); async_connect(); } }
void ClientConnection::connect(std::string host, std::string username) { this->host = host; this->username = username; auto endpoint_iterator = resolver.resolve({this->host, std::to_string(GameState::tcpPort)}); async_connect(socket, endpoint_iterator, [&](boost::system::error_code ec, ip::tcp::resolver::iterator it) { std::cout << "Managed to connect to " << this->host << std::endl; auto mh = MessageHeader{ MessageType::CONNECT, this->username.length()+1 }; std::vector<uint8_t> sendBuffer; sendBuffer.resize( sizeof(MessageHeader) + mh.size ); memcpy(sendBuffer.data(), &mh, sizeof(MessageHeader)); memcpy(sendBuffer.data() + sizeof(MessageHeader), this->username.data(), mh.size); send(sendBuffer); receiveHeader(); }); }
//----------------------------------------------------------------------------- TEST_F(FiberTest, multipleConnectDisconnectFiber) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info); Wait(); const uint32_t number_of_connections = 1000; std::atomic<uint32_t> number_of_connected(0); std::atomic<uint32_t> number_of_accepted(0); std::promise<bool> accepted_all; std::promise<bool> connected_all; fiber_acceptor fib_acceptor(io_service_server_); std::function<void(std::shared_ptr<fiber> p_fiber, const boost::system::error_code&)> async_accept_h1; std::function<void()> async_accept_h2; async_accept_h2 = [this, &async_accept_h1, &fib_acceptor]() { auto p_fiber = std::make_shared<fiber>(io_service_server_); fib_acceptor.async_accept(*p_fiber, boost::bind(async_accept_h1, p_fiber, _1)); }; async_accept_h1 = [this, &async_accept_h2, &number_of_accepted, &number_of_connections, &accepted_all]( std::shared_ptr<fiber> p_fiber, const boost::system::error_code& accept_ec) { if (!accept_ec) { p_fiber->close(); ++number_of_accepted; if (number_of_accepted == number_of_connections) { accepted_all.set_value(true); return; } this->io_service_server_.dispatch(async_accept_h2); } else { ASSERT_EQ(accept_ec.value(), 0) << "Accept handler should not be in error: " << accept_ec.value(); } }; auto async_connect_h1 = [this, &number_of_connected, &number_of_connections, &connected_all]( std::shared_ptr<fiber> p_fiber, const boost::system::error_code& connect_ec) { if (!connect_ec) { p_fiber->close(); ++number_of_connected; if (number_of_connected == number_of_connections) { connected_all.set_value(true); } } else { ASSERT_EQ(connect_ec.value(), 0) << "Connect handler should not be in error"; } }; boost::system::error_code ec_server; fiber_endpoint fib_server_endpoint( boost::asio::fiber::stream_fiber<socket>::v1(), demux_server_, 1); fib_acceptor.open(fib_server_endpoint.protocol()); fib_acceptor.bind(fib_server_endpoint, ec_server); fib_acceptor.listen(); async_accept_h2(); fiber_endpoint fib_client_endpoint( boost::asio::fiber::stream_fiber<socket>::v1(), demux_client_, 1); for (std::size_t i = 0; i < number_of_connections; ++i) { auto p_fiber = std::make_shared<fiber>(io_service_client_); p_fiber->async_connect(fib_client_endpoint, boost::bind<void>(async_connect_h1, p_fiber, _1)); } connected_all.get_future().wait(); accepted_all.get_future().wait(); boost::system::error_code close_fib_acceptor_ec; fib_acceptor.close(close_fib_acceptor_ec); }
socket1.bind(ip::udp::endpoint(ip::udp::v4(), 0)); socket1.bind(ip::udp::endpoint(ip::udp::v6(), 0)); socket1.bind(ip::udp::endpoint(ip::udp::v4(), 0), ec); socket1.bind(ip::udp::endpoint(ip::udp::v6(), 0), ec); socket1.connect(ip::udp::endpoint(ip::udp::v4(), 0)); socket1.connect(ip::udp::endpoint(ip::udp::v6(), 0)); socket1.connect(ip::udp::endpoint(ip::udp::v4(), 0), ec); socket1.connect(ip::udp::endpoint(ip::udp::v6(), 0), ec); socket1.async_connect(ip::udp::endpoint(ip::udp::v4(), 0), connect_handler()); socket1.async_connect(ip::udp::endpoint(ip::udp::v6(), 0), connect_handler()); int i1 = socket1.async_connect(ip::udp::endpoint(ip::udp::v4(), 0), lazy); (void)i1; int i2 = socket1.async_connect(ip::udp::endpoint(ip::udp::v6(), 0), lazy); (void)i2; socket1.set_option(settable_socket_option1); socket1.set_option(settable_socket_option1, ec); socket1.set_option(settable_socket_option2); socket1.set_option(settable_socket_option2, ec); socket1.set_option(settable_socket_option3); socket1.set_option(settable_socket_option3, ec); socket1.get_option(gettable_socket_option1); socket1.get_option(gettable_socket_option1, ec); socket1.get_option(gettable_socket_option2); socket1.get_option(gettable_socket_option2, ec);
template <class Mutable_Buffers, class Handler> void async_read_some(Mutable_Buffers const& buffers, Handler const& handler) { TORRENT_SOCKTYPE_FORWARD(async_read_some(buffers, handler)) } template <class Const_Buffers> std::size_t write_some(Const_Buffers const& buffers, error_code& ec) { TORRENT_SOCKTYPE_FORWARD_RET(write_some(buffers, ec), 0) } template <class Const_Buffers, class Handler> void async_write_some(Const_Buffers const& buffers, Handler const& handler) { TORRENT_SOCKTYPE_FORWARD(async_write_some(buffers, handler)) } template <class Handler> void async_connect(endpoint_type const& endpoint, Handler const& handler) { TORRENT_SOCKTYPE_FORWARD(async_connect(endpoint, handler)) } #ifndef BOOST_NO_EXCEPTIONS template <class IO_Control_Command> void io_control(IO_Control_Command& ioc) { TORRENT_SOCKTYPE_FORWARD(io_control(ioc)) } template <class Mutable_Buffers> std::size_t read_some(Mutable_Buffers const& buffers) { TORRENT_SOCKTYPE_FORWARD_RET(read_some(buffers), 0) } #endif template <class IO_Control_Command> void io_control(IO_Control_Command& ioc, error_code& ec) { TORRENT_SOCKTYPE_FORWARD(io_control(ioc, ec)) }
void tcp_connector::async_connect( int port, std::string const& hostname , FactoryFunc fact_func, ConnectHandler const& handler, options const& opts /*= options()*/ ) { async_connect(port, hostname, fact_func, handler, options_ptr(new options(opts))); }
void WorldSession::Connect(std::string IP, std::string Port) { async_connect(Socket, Resolver.resolve(TCPResolver::query(IP, Port)), boost::bind(&WorldSession::OnConnect, boost::static_pointer_cast<WorldSession>(shared_from_this()), placeholders::error)); }
int main(int argc, char** argv) { try { auto parameters = get_parameters(argc, argv); boost::asio::io_service io; auto transport = std::make_shared<autobahn::wamp_tcp_transport>( io, parameters->rawsocket_endpoint()); bool debug = parameters->debug(); auto session = std::make_shared<autobahn::wamp_session>( io, transport, transport, debug); // Make sure the continuation futures we use do not run out of scope prematurely. // Since we are only using one thread here this can cause the io service to block // as a future generated by a continuation will block waiting for its promise to be // fulfilled when it goes out of scope. This would prevent the session from receiving // responses from the router. boost::future<void> start_future; boost::future<void> join_future; boost::future<void> leave_future; boost::future<void> stop_future; transport->async_connect([&](boost::system::error_code ec) { if (!ec) { std::cerr << "connected to server" << std::endl; start_future = session->start().then([&](boost::future<bool> started) { if (started.get()) { std::cerr << "session started" << std::endl; join_future = session->join(parameters->realm()).then([&](boost::future<uint64_t> joined) { std::cerr << "joined realm: " << joined.get() << std::endl; std::tuple<std::string> arguments(std::string("hello")); session->publish("com.examples.subscriptions.topic1", arguments); std::cerr << "event published" << std::endl; leave_future = session->leave().then([&](boost::future<std::string> reason) { std::cerr << "left session (" << reason.get() << ")" << std::endl; stop_future = session->stop().then([&](boost::future<void> stopped) { std::cerr << "stopped session" << std::endl; io.stop(); }); }); }); } else { std::cerr << "failed to start session" << std::endl; io.stop(); } }); } else { std::cerr << "connect failed: " << ec.message() << std::endl; io.stop(); } } ); std::cerr << "starting io service" << std::endl; io.run(); std::cerr << "stopped io service" << std::endl; } catch (const std::exception& e) { std::cerr << e.what() << std::endl; return 1; } return 0; }