ASIO: Same code for tcp, unix domain sockets (websockets via beast)
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
On Wed, Aug 26, 2020 at 5:51 AM Matthias Loy via Boost
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
On Wed, Aug 26, 2020 at 2:51 PM Matthias Loy via Boost < boost@lists.boost.org> 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_/pol... -- Arvid Norberg
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 < boost@lists.boost.org> wrote:
On Wed, Aug 26, 2020 at 2:51 PM Matthias Loy via Boost < boost@lists.boost.org> 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_/pol...
-- Arvid Norberg
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (4)
-
Arvid Norberg
-
Damian Jarek
-
Matthias Loy
-
Vinnie Falco