Why do socket::receive() and socket::async_receive() have different return types?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why do socket::receive() and socket::async_receive() have different return types?

Boost - Users mailing list
From the documentation I can see that socket::receive() returns a variable of type "std::size_t", while the asynchronous version socket::async_receive() returns "void". This is also true for socket::send() and socket::async_send().
I was expecting that they would have the same return types, so there might be a good reason behind.

I would be grateful if someone can shed some light on this.

Thank you.

Álvaro

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

Re: Why do socket::receive() and socket::async_receive() have different return types?

Boost - Users mailing list
Async operations don't have a "result" until the handler has been scheduled for execution. The result of the operation is sent to the handler.

Therefore, returning a size_t would be meaningless for the async versions of the operations since the number of bytes sent or received is not known when the call returns.

The non-async versions block until there is a result, so they are able to return it directly.

 

On Thu, 14 Jun 2018 at 15:49, Álvaro Cebrián Juan via Boost-users <[hidden email]> wrote:
From the documentation I can see that socket::receive() returns a variable of type "std::size_t", while the asynchronous version socket::async_receive() returns "void". This is also true for socket::send() and socket::async_send().
I was expecting that they would have the same return types, so there might be a good reason behind.

I would be grateful if someone can shed some light on this.

Thank you.

Álvaro
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Why do socket::receive() and socket::async_receive() have different return types?

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
AMDG

On 06/14/2018 08:04 AM, Álvaro Cebrián Juan via Boost-users wrote:

>>From the documentation I can see that socket::receive()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/receive.html>
> returns a variable of type "std::size_t", while the asynchronous version
> socket::async_receive()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/async_receive.html>
> returns "void". This is also true for socket::send()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/send.html>
> and socket::async_send()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/async_send.html>
> .
> I was expecting that they would have the same return types, so there might
> be a good reason behind.
>

  receive returns the number of bytes received.
async_receive cannot return this because it
is not known when the call returns.  Instead,
the size is passed to the read handler.

> I would be grateful if someone can shed some light on this.
>

In Christ,
Steven Watanabe
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Why do socket::receive() and socket::async_receive() have different return types?

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
On Thu, Jun 14, 2018 at 7:47 AM Álvaro Cebrián Juan via Boost-users
<[hidden email]> wrote:
> the asynchronous version socket::async_receive() returns "void".

If you want to get pendatic, the return type from an initiating
function is not void, but

    typename async_result<decay_t<CompletionToken>, Signature>::return_type

See:

<https://www.boost.org/doc/libs/1_67_0/doc/html/boost_asio/reference/asynchronous_operations/production_of_initiating_function_return_value.html>

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

Re: Why do socket::receive() and socket::async_receive() have different return types?

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
Hello,

Am 14.06.2018 um 16:04 schrieb Álvaro Cebrián Juan via Boost-users:
> From the documentation I can see that socket::receive()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/receive.html>
> returns a variable of type "std::size_t", while the asynchronous version
> socket::async_receive()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/async_receive.html>
> returns "void".

`socket::receive()` returns how much has been received and is similar to
`recv()`. `socket::async_receive()` does not actually receive stuff --
it only tells your `asio::io_service` that as *it* receives something it
shall pass that data on to your callback.

Because `socket::async_receive()` is asynchronous (it only registers the
socket <-> callback) and doesn't receive anything it can't return any
meaningful size.

This is one of the concepts of asynchronous development, I think
refreshing your knowledge about that would be benefital.

>This is also true for socket::send()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/send.html>
> and socket::async_send()
> <https://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/reference/basic_datagram_socket/async_send.html>.
> I was expecting that they would have the same return types, so there
> might be a good reason behind.
>
> I would be grateful if someone can shed some light on this.
>
> Thank you.
>
> Álvaro
>
>
> _______________________________________________
> 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

signature.asc (235 bytes) Download Attachment