int main() { SerialPort port( "/dev/tty.usbmodemfa1311", // "/dev/tty.usbmodemfa141", 57600, iosrv, [](std::vector<uint8_t>& data) { on_serial_data_receive(data); }); ba::deadline_timer timer(iosrv, boost::posix_time::seconds(1)); std::function<void(boost::system::error_code const&)> on_timeout; auto relauch_timer = [&]() { timer.async_wait(on_timeout); }; on_timeout = [&](boost::system::error_code const&) { on_timer(port); timer.expires_from_now(period); relauch_timer(); }; relauch_timer(); while(true) { iosrv.poll(); } return 0; }
void run_event_loop_until_connect() { while (!last_error && !is_connected) { service.poll(); service.reset(); check_if_timed_out(); } }
/** Handle all pending asynchonous events and return */ std::size_t poll() { try { return io_service_.poll(); } catch(const boost::system::system_error& err) { if(err.code() == boost::asio::error::operation_aborted) return 1; throw error(err.code()); } }
void run_event_loop_until_frame_received() { using boost::bind; have_frame = false; buffer.consume(buffer.size()); last_error = boost::system::error_code(); boost::asio::async_read_until(socket, buffer, "END\r\n", bind(&TimedSessionBase::handle_frame_reception, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); service.reset(); while (!last_error && !have_frame) { service.poll(); service.reset(); check_if_timed_out(); } }
// MsgWaitForMultipleObjectsEx 集成进去. static inline void avloop_run_gui(boost::asio::io_service& io_service) { using namespace ::detail; boost::asio::io_service::work work(io_service); if (!boost::asio::has_service<IdleService>(io_service)) boost::asio::add_service(io_service, new IdleService(io_service)); if (!boost::asio::has_service<Win32MsgLoopService>(io_service)) boost::asio::add_service(io_service, new Win32MsgLoopService(io_service)); while (!io_service.stopped()) { // 首先处理 asio 的消息. while (io_service.poll()) { // 然后执行 gui 循环,看有没有 gui 事件. if (boost::asio::use_service<Win32MsgLoopService>(io_service).has_message()) { // 执行以下. boost::asio::use_service<Win32MsgLoopService>(io_service).poll_one(); } } // 然后执行 gui 循环,看有没有 gui 事件. while(boost::asio::use_service<Win32MsgLoopService>(io_service).has_message()) { // 执行以下. boost::asio::use_service<Win32MsgLoopService>(io_service).poll_one(); } // 执行 idle handler! if (boost::asio::use_service<IdleService>(io_service).has_idle()) boost::asio::use_service<IdleService>(io_service).poll_one(); // 都没有事件了,执行 一次 1ms 的超时等待. auto ret = MsgWaitForMultipleObjectsEx(0, nullptr, 1, QS_ALLEVENTS, MWMO_WAITALL|MWMO_ALERTABLE | MWMO_INPUTAVAILABLE); // 可能是有 gui 消息了, 呵呵, 从头来吧. } }
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 }); }
//--------------------------------------------------------- void run_01(boost::asio::io_service& io_service, boost::thread::id id) { io_service.post(boost::bind(check_thread_id_01, id)); io_service.post(boost::bind(check_thread_id_00, boost::this_thread::get_id())); io_service.poll(); }
void poll() { io_service_.poll(); }
void Connection::Poll() { netservice.poll(); }
Connection::~Connection() { sock.close(); netservice.poll(); netservice.reset(); }