简体   繁体   English

通过 boost 连接已创建的名为 pipe

[英]Connect with an already created named pipe with boost

I'm looking in to using third party libraries for IPC communication using named pipes on Windows .我正在寻找使用第三方库在Windows上使用named pipes进行 IPC 通信。 I've been using the Win32 API for this for the last few years.在过去的几年里,我一直在使用Win32 API I was interested in replacing my implementation with a tried and true open source library.我有兴趣用一个久经考验的真正开源库来替换我的实现。

I noticed that boost::process has an implementation of an async_pipe which would allow me to use it with boost::asio which would be really helpful for my application.我注意到boost::process有一个async_pipe的实现,它允许我将它与boost::asio一起使用,这对我的应用程序非常有帮助。

What I'm trying to do is create the named pipe on the server, which is a C# application.我要做的是在服务器上创建named pipeC#应用程序。 Once the pipe has been created, connect to it with a client using the boost::process::async_pipe .创建pipe后,使用boost::process::async_pipe与客户端连接。

The problem I'm having is I don't see an API in boost::process that would allow me to connect with an already created named pipe .我遇到的问题是我在boost::process中看不到 API ,这将允许我连接已创建的named pipe The constructors for async_pipe create the pipe instead of connecting to an already created pipe . async_pipeconstructors函数创建pipe而不是连接到已经创建的pipe

Below is the code I'm currently using in the client which is erroneously creating the 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( );

I'm unsure if I can use boost::process to achieve what I want.我不确定是否可以使用boost::process来实现我想要的。 I'm wondering if there is a way I could use CreateFileW to connect with the named pipe and then pass the HANDLE to async_pipe but I haven't found any documentation regarding that.我想知道是否有一种方法可以使用CreateFileWnamed pipe连接,然后将HANDLE传递给async_pipe但我还没有找到任何相关文档。

Question问题

How can I connect with an already created named pipe using boost如何使用boost连接已创建的named pipe

OK, so I was going about it the wrong way.好的,所以我走错了路。 After reading this issue on Github Link I realized I needed to use a stream_handle instead.在 Github Link上阅读此问题后,我意识到我需要改用stream_handle Note, the pipe must be opened in OVERLAPPED mode for this to work.请注意, pipe必须在OVERLAPPED模式下打开才能正常工作。

Creating the stream_handle创建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 ) };
}

Once the stream_handle is created you can use the async functions it provides for communication.创建stream_handle ,您可以使用它提供的async函数进行通信。

Using the stream_handle使用stream_handle

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM