WriteFile en una tubería con nombre a veces devuelve ERROR_NO_DATA

Tengo un programa C ++ que está creando una tubería con nombre para escribir datos. Algunos clientes han informado de una situación en la que el cliente se conecta a la tubería con nombre pero el extremo del servidor no puede escribir los datos (conERROR_NO_DATA).

Este código de error no se explica realmente en ninguna página de MSDN que pude encontrar; ¿Alguien tiene alguna idea sobre cómo solucionar esto? ¿O cuál es la causa?


Código abierto:
ostringstream pipeName;
pipeName << "\\\\.\\pipe\\unique-named-pipe-" << GetCurrentProcessId();

pipeHandle = CreateNamedPipeA(
    pipeName.str().c_str(),              // pipe name
    PIPE_ACCESS_DUPLEX,                  // open mode
    PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, // pipe mode
    PIPE_UNLIMITED_INSTANCES,            // max instances
    512,                                 // output buffer size
    512,                                 // input buffer size
    0,                                   // use default timeouts
    NULL);                               // security attributes

if (INVALID_HANDLE_VALUE == pipeHandle)
{
    THROW("Failed to create named pipe", GetLastError());
}

cout << "Pipe ready" << endl;

// Wait for a client to connect to the pipe        
BOOL status = ConnectNamedPipe(pipeHandle, NULL);

if (!status)
{
    DWORD lastError = GetLastError();

    if (ERROR_PIPE_CONNECTED != lastError)
    {
        THROW("Failed to wait for client to open pipe", lastError);
    }
    else
    {
        // Ignore, see MSDN docs for ConnectNamedPipe() for details.
    }
}


Código de escritura:
// response is a std::string
int writeOffset = 0;
int length = response.length();

while ((int) response.length() > writeOffset)
{
    DWORD bytesWritten;

    BOOL status = WriteFile(
        pipeHandle,
        response.c_str() + writeOffset,
        length - writeOffset,
        &bytesWritten,
        NULL);

    if (!status)
    {
        // This sometimes fails with ERROR_NO_DATA, why??
        THROW("Failed to send via named pipe", GetLastError());
    }

    writeOffset += bytesWritten;
}


Throw macro
#define THROW(message, errorCode) \
{ \
    fprintf(stderr, "%s: line: %d file: %s error:0x%x\n", \
            message, __LINE__, __FILE__, errorCode); \
    fflush(stderr); \
    throw message; \
} \

¡Gracias

Respuestas a la pregunta(1)

Su respuesta a la pregunta