0

I have an API which has a function that accepts an AsyncWriteStream as defined here:

http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/reference/AsyncWriteStream.html

This is currently used (and works) to stream data to a tcp socket, using:

http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/reference/basic_stream_socket.html

My question is that can this interface also be used to stream to a file on disk. I suspect the answer is yes but I would like to know how much effort is required and especially if there are existing implementations that support the interface.

So, to reiterate. The API function looks like:

template <class AsyncWriteStream>
void stream_read(AsyncWriteStream &stream, completion_callback CB) { ...

Internally the API writes data to AsyncWriteStream using boost::asio::async_write. I want AsyncWriteStream to then be able to stream to both tcp and file socket. Perhaps my question could also be phrased "can a basic_stream_socket be created that streams to disk instead of tcp?"

I need this to work on both Windows and Linux.

Jibbity jobby
  • 1,255
  • 2
  • 12
  • 26

1 Answers1

1

This is perhaps a duplicate. From the answers given here: Whats the deal with boost.asio and file i/o?:

For Windows use: windows::random_access_handle and manage the offset. (Note that windows::stream_handle does not support eof, see: C++ boost asio Windows file handle async_read_until infinite loop - no eof).

For Linux, open the file and then use posix::stream_descriptor. (Note that asio does not support epoll for file io, define BOOST_ASIO_DISABLE_EPOLL)

Both of the above appear to support the AsyncWriteStream interface, although I've still to test.

Jibbity jobby
  • 1,255
  • 2
  • 12
  • 26
  • 1
    Even though that should work, be aware that file access on Linux is mostly synchronous/blocking. Which means your `read_async` and `write_async` on such a stream would behave like a blocking read and write (only with async callback). If you are not ok with that you might need to defer the actual work to some kind of file worker thread or threadpool. This is e.g. the way how node.js/libuv handle asynchronous file access. – Matthias247 Sep 04 '17 at 09:44
  • Thanks, that's likely to be relevant. – Jibbity jobby Sep 04 '17 at 09:59