void thread_fun() { for (;;) { error_code ec; tcp::endpoint from; tcp::socket socket(m_ios); condition_variable cond; bool done = false; m_acceptor.async_accept(socket, from, boost::bind(&new_connection, _1, &ec, &done)); while (!done) { m_ios.run_one(); m_ios.reset(); } if (ec == boost::asio::error::operation_aborted || ec == boost::asio::error::bad_descriptor) return; if (ec) { fprintf(stderr, "Error accepting connection on peer socket: %s\n", ec.message().c_str()); return; } fprintf(stderr, "%s: incoming peer connection\n", time_now_string()); ++m_peer_requests; socket.close(ec); } }
inline void run_service( boost::asio::io_service & io_service) { h_timer timer( io_service, boost::chrono::seconds(0) ); timer.async_wait( boost::bind( timer_handler, boost::ref( timer) ) ); while (true) { boost::system::error_code ec; std::size_t num = io_service.run_one(ec); if (num == 0) { return; } boost::this_fiber::yield(); } }
void thread_fun() { char buffer[2000]; for (;;) { error_code ec; udp::endpoint from; size_t bytes_transferred; bool done = false; m_socket.async_receive_from( asio::buffer(buffer, sizeof(buffer)), from, 0 , boost::bind(&incoming_packet, _1, _2, &bytes_transferred, &ec, &done)); while (!done) { m_ios.run_one(); m_ios.reset(); } if (ec == boost::asio::error::operation_aborted || ec == boost::asio::error::bad_descriptor) return; if (ec) { fprintf(stderr, "Error receiving on DHT socket: %s\n", ec.message().c_str()); return; } try { entry msg = bdecode(buffer, buffer + bytes_transferred); #if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM std::cerr << msg << std::endl; #endif ++m_dht_requests; } catch (std::exception& e) { fprintf(stderr, "failed to decode DHT message: %s\n", e.what()); } } }
static inline void avloop_run(boost::asio::io_service& io_service) { using namespace ::detail; if (!boost::asio::has_service<IdleService>(io_service)) boost::asio::add_service(io_service, new IdleService(io_service)); while (!io_service.stopped()) { if(!boost::asio::use_service<IdleService>(io_service).has_idle()) { if (!io_service.run_one()) break; } else { while (io_service.poll()); // 执行 idle handler! boost::asio::use_service<IdleService>(io_service).poll_one(); } } }
service( boost::asio::io_service & io_svc) : boost::asio::io_service::service( io_svc), work_{ new boost::asio::io_service::work( io_svc) } { io_svc.post([&io_svc](){ //] //[asio_rr_service_lambda while ( ! io_svc.stopped() ) { if ( boost::fibers::has_ready_fibers() ) { // run all pending handlers in round_robin while ( io_svc.poll() ); // run pending (ready) fibers this_fiber::yield(); } else { // run one handler inside io_service // if no handler available, block this thread if ( ! io_svc.run_one() ) { break; } } } //] //[asio_rr_service_bottom }); }