I'm learning Boost.Asio. I have defined a class that has the role of client and server:
I want to be able to make requests and receive responses after a subscription, but there is a problem when I get the response: at that moment the socket waiting at the same time receive a subscription and a response.
Sometimes there is deadlock, sometimes I get a subscription when it was waiting for a response, ...
To read, I use the following functions:
void async_receive_subscription(const socket& the_socket, read_handler_signature handler)
{
auto self( this->shared_from_this() );
boost::asio::async_read(
the_socket.get(),
boost::asio::buffer( my_buffer ),
[this, self, the_socket, my_buffer, handler]
( const boost::system::error_code& error,
std::size_t bytes_transferred )
{
the_socket.get().get_io_service().post(
std::bind(
handler,
error,
bytes_transferred,
the_socket,
my_buffer
)
);
// Read the next subscription.
async_receive_subscription(the_socket, handler);
}
);
}
void async_receive(const socket& the_socket, read_handler_signature handler)
{
auto self( this->shared_from_this() );
boost::asio::async_read(
the_socket.get(),
boost::asio::buffer( my_buffer ),
[this, self, the_socket, my_buffer, handler]
( const boost::system::error_code& error,
std::size_t bytes_transferred )
{
the_socket.get().get_io_service().post(
std::bind(
handler,
error,
bytes_transferred,
the_socket,
my_buffer
)
);
}
);
}
Is it possible to expect two responses by the same socket? Is there any synchronization mechanism that can be used to avoid this problem?
Thanks.
async_read is guaranteed to cause exactly one call of the handler.
You must not call async_read again until this handler has been called.
question:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.