Which socket read get first when both asio::async_read and asio::read are active?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Which socket read get first when both asio::async_read and asio::read are active?

Boost - Users mailing list
Hi,

I have a socket server program to activate in asio::async_read for receive any messages initiated from client. But when the server sends a message to the client, it uses sync write and flowing a sync read (asio::read) to immediately receive the response. The issue I can think of is who will get first read when both asio::async_read and asio::read are active?

Thank you.

Kind regards.


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Which socket read get first when both asio::async_read and asio::read are active?

Boost - Users mailing list
On 2/08/2017 13:30, jupiter wrote:
> I have a socket server program to activate in asio::async_read for
> receive any messages initiated from client. But when the server sends a
> message to the client, it uses sync write and flowing a sync read
> (asio::read) to immediately receive the response. The issue I can think
> of is who will get first read when both asio::async_read and asio::read
> are active?

One or both of them selected at semi-random by the OS.  Basically: don't
do that.

(Especially because both read and async_read are not necessarily single
operations -- they can internally be composed of multiple calls to
[async_]read_some.)

Each individual data block will only be received by at most one of the
handlers, but a single "response" could come in as arbitrarily many
blocks, and if you have multiple read operations pending simultaneously
then the chances of multiple blocks hitting multiple handlers are nearly
certain.

You will need to remove the sync read and only do the async read; your
async read handler (or downstream code) will need to recognise the
difference between an unsolicited message and a response and handle it
accordingly.

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Which socket read get first when both asio::async_read and asio::read are active?

Boost - Users mailing list
Thanks for the advice.


On Wed, Aug 2, 2017 at 12:38 PM, Gavin Lambert via Boost-users <[hidden email]> wrote:
On 2/08/2017 13:30, jupiter wrote:
I have a socket server program to activate in asio::async_read for receive any messages initiated from client. But when the server sends a message to the client, it uses sync write and flowing a sync read (asio::read) to immediately receive the response. The issue I can think of is who will get first read when both asio::async_read and asio::read are active?

One or both of them selected at semi-random by the OS.  Basically: don't do that.

(Especially because both read and async_read are not necessarily single operations -- they can internally be composed of multiple calls to [async_]read_some.)

Each individual data block will only be received by at most one of the handlers, but a single "response" could come in as arbitrarily many blocks, and if you have multiple read operations pending simultaneously then the chances of multiple blocks hitting multiple handlers are nearly certain.

You will need to remove the sync read and only do the async read; your async read handler (or downstream code) will need to recognise the difference between an unsolicited message and a response and handle it accordingly.

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...