'How can I effectively use boost::process::async_pipe for both writing and reading?
I've already seen the boost::process tutorial... but there the example is a single write then a single read from the child process. I want to know if it is possible to have both async_pipes alive (read and write) during the child process lifetime.
I'm having trouble reading from child->parent pipe (read_pipe
, in my code). I can only read from it after closing the parent->child pipe (write_pipe
), but that means that I won't write anything ever again on this child process, right? This makes sense, but there is some workaround to maintain a bi-directional channel? My final objective is to continuously alternate between reading and writing chunks.
#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <iostream>
#include <string>
#include <vector>
typedef std::function<void(const boost::system::error_code & ec, std::size_t n)> Handler;
int main(){
std::vector<char> vread_buffer(4096);
boost::asio::io_service ios;
boost::process::async_pipe read_pipe{ios};
boost::process::async_pipe write_pipe{ios};
auto child_process = boost::process::child( "stockfish_12_win_x64/stockfish_20090216_x64.exe" ,
boost::process::std_in < write_pipe ,
boost::process::std_out > read_pipe );
Handler on_stdout,on_stdin;
std::string read_string;
on_stdout = [&](const boost::system::error_code & ec, size_t n){
std::cout << "I'm reading " << n << " characters from the child process. Can't wait for more!" << std::endl;
read_string.reserve( read_string.size() + n );
read_string.insert( read_string.end() , vread_buffer.begin() , vread_buffer.begin() + n );
if(!ec) boost::asio::async_read( read_pipe , boost::asio::buffer(vread_buffer) , on_stdout );
};
on_stdin = [&]( const boost::system::error_code & ec, std::size_t n ){
std::cout << "I know that " << n << " characters were sent to the process. Yay! " << std::endl;
};
// {...} Suppose an undefined amount of time has passed
// Expected: To see the initial print from the program calling the on_stdout
// ... but it doesn't call anything.
boost::asio::async_read( read_pipe , boost::asio::buffer(vread_buffer) , on_stdout );
ios.poll();
ios.restart();
// Expected: To see the "on_stdin" handler being called... and it was!
std::string write_string = "uci\n";
boost::asio::async_write( write_pipe , boost::asio::buffer(write_string) , on_stdin );
ios.poll();
ios.restart();
// Sending a async_read will never do anything unless I close the write_pipe.
// How can I tell the io_service that last write was done so I can read again?
boost::asio::async_read( read_pipe , boost::asio::buffer(vread_buffer) , on_stdout );
ios.poll();
ios.restart();
}
Solution 1:[1]
I think I figured it out. I should use boost::asio::async_read_until
passing a delimiter (char/regex/string) instead of boost::asio::async_read
This way, boost::process::buffer
should be changed to boost::asio::dynamic_buffer
std::string read_string;
auto d_buffer = boost::asio::dynamic_buffer(read_string);
// First read
boost::asio::async_read_until( read_pipe , d_buffer , '\n' , on_stdout );
ios.poll();
ios.restart();
// Any amount of writes can be done here with async_write
// Second read
d_buffer.consume(d_buffer.size(); // Will append new content into read_string anyway, this is just to read_string doesn't grow indefinitely. Also, I noted unexpected behavior if not consume all content (new content appended at random middle points)
boost::asio::async_read_until( read_pipe , d_buffer , '\n' , on_stdout );
ios.poll();
ios.restart();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | R. Pires |