dimanche 23 septembre 2018

Boost asio io_service::run doesn't block when mixing sync/async call?

I'm trying to use asio as a client to communicate with server.

Pseudo code:

socket_.connect(ip::tcp::endpoint(ip::address::from_string(m_server), m_port));

// 5 SYNC requests/responses for login and some initialization, I prefer sync read/write because the login requests/responses have different structure with normal requests/response.
boost::asio::write(socket_, 1stRequest ...
boost::asio::read(socket_, 1stResponse ...
...
boost::asio::write(socket_, 5thRequest ...
boost::asio::read(socket_, 5thResponse ...

// Then setup async_read and io_service::run
boost::asio::async_read(
    socket_,
    readPacket_.GetHeaderBuffer(),
    boost::bind(
        &my_client::handle_read_header,
        this,
        boost::asio::placeholders::error
    )
);

// Call io_service::run
ioService_.run();

// Send request, I hope this will trigger the previous async_read callback when server sending data to client quickly.
boost::asio::write(socket_, normalRequest ...

The ioService_.run() returns immediately without blocking, and my_client::handle_read_header never get called.

What's the problem is it?

Aucun commentaire:

Enregistrer un commentaire