c++boostwebsocketboost-beastbeast-websockets

How to do async read/write Beast websockets alongside read/write of files?


I have my C++ program that forks into two processes, 1 (the original) and 2 (the forked process).

In the forked process (2), it execs program A that does a lot of computation.

The original process (1) communicates with that program A through standard input and output redirected to pipes.

I am trying to add a websocket connection to my code in the original process (1). I would like my original process to effectively select or epoll on whether there is data to be read from the pipe to program A or there is data to be read from the websocket connection.

Given that a beast websocket is not a file descriptor how can I do the effect of select or epoll?


Solution

  • Which version of Boost are you using? If it is relatively recent it should include support for boost::process::async_pipe which allows you to use I/O Objects besides sockets asynchronously with Asio. Examples are provided in the tutorials for the boost::process library. Since Beast uses the Asio library to perform I/O under the hood, you can combine the two quite easily.

    Given that a beast websocket is not a file descriptor...

    The Beast WebSocket is not a file descriptor, but it does use TCP sockets to perform I/O (see the linked examples above), and Asio is very good at using select/epoll with TCP sockets. Just make sure you are doing the async_read, async_write and io_service::run operations as usual.