使用 boost 连接已创建的命名管道

Connect with an already created named pipe with boost

我正在研究使用第三方库在 Windows 上使用 named pipes 进行 IPC 通信。在过去的几年里,我一直在为此使用 Win32 API。我有兴趣用一个久经考验的真正开源库替换我的实现。

我注意到 boost::process 有一个 async_pipe 的实现,它允许我将它与 boost::asio 一起使用,这对我的应用程序非常有帮助。

我想做的是在服务器上创建 named pipe,这是一个 C# 应用程序。 创建 pipe 后,使用 boost::process::async_pipe.

与客户端连接

我遇到的问题是我在 boost::process 中看不到 API 可以让我连接到已创建的 named pipeasync_pipeconstructors 创建 pipe 而不是连接到已创建的 pipe

下面是我目前在客户端中使用的代码,它错误地创建了 pipe

boost::asio::io_context ctx;
std::vector<char> buffer( 8196, 0 );

boost::process::async_pipe pipe{ ctx, R"(\.\pipe\TestPipe)" }; 

boost::asio::async_read( pipe, boost::asio::buffer( buffer ),
    [ &buffer ]( const boost::system::error_code& ec, std::size_t size )
    {
        if ( ec )
            std::cout << "Error: " << ec.message( ) << '\n';
        else
        {
            std::string message{ std::begin( buffer ), std::begin( buffer ) + size };
            std::cout << "Received message: " << message << '\n';
        }
    } );

ctx.run( );

我不确定我是否可以使用 boost::process 来实现我想要的。我想知道是否有一种方法可以使用 CreateFileW 连接 named pipe 然后将 HANDLE 传递给 async_pipe 但我没有找到任何关于那。

问题

如何使用 boost

连接已创建的 named pipe

好吧,所以我走错了路。 在 Github Link 上阅读了这个问题后,我意识到我需要使用 stream_handle 来代替。注意,pipe 必须在 OVERLAPPED 模式下打开才能工作。

创建 stream_handle

static boost::asio::windows::stream_handle OpenPipe( io_context& context )
{
    constexpr const wchar_t* pipeName{ LR"(\.\pipe\TestPipe)" };
    return { context, CreateFileW( pipeName,
                                   GENERIC_READ | GENERIC_WRITE,
                                   0, nullptr,
                                   OPEN_EXISTING,
                                   FILE_FLAG_OVERLAPPED,
                                   nullptr ) };
}

创建 stream_handle 后,您可以使用它提供的 async 功能进行通信。

使用stream_handle

std::vector<char> buffer( 8196, 0 );
pipe.async_read_some( boost::asio::buffer( buffer ), 
    [ &buffer ]( auto ec, auto size ) { } );