简体   繁体   中英

uv_start_read on windows named pipe client waits until the server closes its pipe and results in EOF

I have two programs, one that I've written and works as a pipe server and uses io completion ports. the other isn't written by me but it's open source and it uses libuv heavily for its async processing.

now I want to edit this program to add the pipe functionality through libuv and to be able to contact the server.

I can connect to the server using uv_pipe_connect and I get the connect callback triggered, then I start reading with uv_read_start which returns 0 (no error) so I expect the allocation callback and read callback get triggered once the server writes to the client.

however when the server writes some bytes and I receive a notification through iocp that an amount of bytes were written, the client doesn't receive anything and no any callbacks are invoked. And on closing the server process the read callback is invoked with error EOF

this is the libuv related code I'm using:

class IPipeListener;

class PipeClient
{

public:

    PipeClient(IPipeListener* listener, const std::string& pipe_name);

    ~PipeClient();

    void stop();

private:

    static void onAllocBuff(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);

    static void onConnect(uv_connect_t *connect_req, int result);

    static void onRead(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);

    static void onWrite(uv_write_t* req, int result);

    // a pod structure used to read and write data 
    PipeCommand command;
    IPipeListener* m_listener = nullptr;
    uv_pipe_t m_pipe;
    uv_connect_t m_connect;
    uv_read_t read_req;
    uv_write_t write_req;
};

PipeClient::PipeClient(IPipeListener* listener, const std::string& pipe_name)
{
    if (!listener || pipe_name.empty())
        return;
    m_listener = listener;
    uv_pipe_init(uv_default_loop(), &m_pipe, 1);
    m_connect.data = this;
    write_req.data = this;
    read_req.data = this;
    uv_pipe_connect(&m_connect, &m_pipe, pipe_name.c_str(), onConnect);
}

void PipeClient::onAllocBuff(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
{
    MessageBoxA(0, "onAllocBuff", "onAllocBuff", 0);
    auto pipe = static_cast<PipeClient*>(handle->data);
    buf->base = reinterpret_cast<char*>(&pipe->command);
    buf->len = sizeof(pipe->command);
}

void PipeClient::onConnect(uv_connect_t* connect_req, int result)
{
    MessageBoxA(0, "onConnect", "onConnect", 0);
    auto pipe = static_cast<PipeClient*>(connect_req->data);
    if (result < 0)
    {
        pipe->command.code = PipeCommand::OpCode::Error;
        pipe->m_listener->onPipeCommand(pipe->command);
        return;
    }
    MessageBoxA(0, "starting read", "notify", 0);
    int r = uv_read_start(connect_req->handle, onAllocBuff, onRead);
    if (r != 0)
    {
        std::string err_msg = "failed to start reading with error : ";
        err_msg += uv_err_name(r);
        MessageBoxA(0, err_msg.c_str(), "error", 0);
    }
}

void PipeClient::onRead(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
{
    MessageBoxA(0, "onRead", "onRead", 0);
    auto pipe = static_cast<PipeClient*>(stream->data);
    uv_read_stop(stream);
    if (nread < 0)
    {
        std::string err_msg = "failed to read with error : ";
        err_msg += uv_err_name(nread);
        MessageBoxA(0, err_msg.c_str(), "error", 0);
        pipe->command.code = PipeCommand::OpCode::Error;
        pipe->m_listener->onPipeCommand(pipe->command);
        return;
    }

    pipe->m_listener->onPipeCommand(pipe->command);
    uv_buf_t write_buff;
    write_buff.base = reinterpret_cast<char*>(&pipe->command);
    write_buff.len = sizeof(pipe->command);
    uv_write(&pipe->write_req,
        stream, &write_buff, 1, onWrite);
}

I had to set the ipc flag to 0 in uv_pipe_init and set the data of m_pipe to point to my PipeClient class.

from here: https://github.com/libuv/libuv/blob/v1.x/src/win/pipe.c

I saw that the pipe reads writes aren't ordinary iocp reads writes if the ipc flag is set, instead the take another methods that I didn't want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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