ASIO: Same code for tcp, unix domain sockets (websockets via beast)

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

ASIO: Same code for tcp, unix domain sockets (websockets via beast)

Boost - Dev mailing list
Hello,

we are implementing a service that is to be available via tcp, unix
domain sockets (and websocket).

Websocket might be out of scope since it is not part of asio.

All those protocols are streaming protocols and should behave in the
same way: read some bytes, write some bytes.

I would like to have the code implemented only once to handle several
protocols. Not having one for local::stream_protocol::socket and another
one for ip::tcp::socket.

Until now I was not able to find something like base classes that allow
me to do that. I only found code that works for tcp or unix domain
sockets (local). The protocol is not to be decided at compile time but
on runtime. Hence I'm having problems using templates here.

The documentation of async_read states that the stream has to support
the AsyncReadStream concept. Is there a way to get something supporting
this concept from local::stream_protocol::socket and ip::tcp::socket?



Best regards,
Matthias


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: ASIO: Same code for tcp, unix domain sockets (websockets via beast)

Boost - Dev mailing list
On Wed, Aug 26, 2020 at 5:51 AM Matthias Loy via Boost
<[hidden email]> wrote:
> The documentation of async_read states that the stream has to support
> the AsyncReadStream concept. Is there a way to get something supporting
> this concept from local::stream_protocol::socket and ip::tcp::socket?

What exactly are the requirements for AsyncReadStream?

Thanks

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: ASIO: Same code for tcp, unix domain sockets (websockets via beast)

Boost - Dev mailing list
In reply to this post by Boost - Dev mailing list
On Wed, Aug 26, 2020 at 2:51 PM Matthias Loy via Boost <
[hidden email]> wrote:

> [...]
> All those protocols are streaming protocols and should behave in the
> same way: read some bytes, write some bytes.
>

Websocket is a stream of messages, but I suppose you could ignore the
message boundaries and treat each message as a section of a stream.


> [...]
> Until now I was not able to find something like base classes that allow
> me to do that. I only found code that works for tcp or unix domain
> sockets (local). The protocol is not to be decided at compile time but
> on runtime. Hence I'm having problems using templates here.
>
> The documentation of async_read states that the stream has to support
> the AsyncReadStream concept. Is there a way to get something supporting
> this concept from local::stream_protocol::socket and ip::tcp::socket?
>

You could do something along these lines, to wrap a std::variant<> of
different concrete socket types.

https://github.com/arvidn/libtorrent/blob/master/include/libtorrent/aux_/polymorphic_socket.hpp

--
Arvid Norberg

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: ASIO: Same code for tcp, unix domain sockets (websockets via beast)

Boost - Dev mailing list
There's a `generic` protocol type in ASIO that type-erases the protocol,
rather than the stream itself. You only pay for it when manipulating the
endpoint, not when performing async ops, here's an example of how to use it:
https://godbolt.org/z/T6Tjdh

On Wed, Aug 26, 2020 at 5:20 PM Arvid Norberg via Boost <
[hidden email]> wrote:

> On Wed, Aug 26, 2020 at 2:51 PM Matthias Loy via Boost <
> [hidden email]> wrote:
>
> > [...]
> > All those protocols are streaming protocols and should behave in the
> > same way: read some bytes, write some bytes.
> >
>
> Websocket is a stream of messages, but I suppose you could ignore the
> message boundaries and treat each message as a section of a stream.
>
>
> > [...]
> > Until now I was not able to find something like base classes that allow
> > me to do that. I only found code that works for tcp or unix domain
> > sockets (local). The protocol is not to be decided at compile time but
> > on runtime. Hence I'm having problems using templates here.
> >
> > The documentation of async_read states that the stream has to support
> > the AsyncReadStream concept. Is there a way to get something supporting
> > this concept from local::stream_protocol::socket and ip::tcp::socket?
> >
>
> You could do something along these lines, to wrap a std::variant<> of
> different concrete socket types.
>
>
> https://github.com/arvidn/libtorrent/blob/master/include/libtorrent/aux_/polymorphic_socket.hpp
>
> --
> Arvid Norberg
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost