void NamedPipe::openServerPipe()

in src/agent/NamedPipe.cc [206:252]


void NamedPipe::openServerPipe(LPCWSTR pipeName, OpenMode::t openMode,
                               int outBufferSize, int inBufferSize) {
    ASSERT(isClosed());
    ASSERT((openMode & OpenMode::Duplex) != 0);
    const DWORD winOpenMode =
              ((openMode & OpenMode::Reading) ? PIPE_ACCESS_INBOUND : 0)
            | ((openMode & OpenMode::Writing) ? PIPE_ACCESS_OUTBOUND : 0)
            | FILE_FLAG_FIRST_PIPE_INSTANCE
            | FILE_FLAG_OVERLAPPED;
    const auto sd = createPipeSecurityDescriptorOwnerFullControl();
    ASSERT(sd && "error creating data pipe SECURITY_DESCRIPTOR");
    SECURITY_ATTRIBUTES sa = {};
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = sd.get();
    HANDLE handle = CreateNamedPipeW(
        pipeName,
        /*dwOpenMode=*/winOpenMode,
        /*dwPipeMode=*/rejectRemoteClientsPipeFlag(),
        /*nMaxInstances=*/1,
        /*nOutBufferSize=*/outBufferSize,
        /*nInBufferSize=*/inBufferSize,
        /*nDefaultTimeOut=*/30000,
        &sa);
    TRACE("opened server pipe [%s], handle == %p",
        utf8FromWide(pipeName).c_str(), handle);
    ASSERT(handle != INVALID_HANDLE_VALUE && "Could not open server pipe");
    m_name = pipeName;
    m_handle = handle;
    m_openMode = openMode;

    // Start an asynchronous connection attempt.
    m_connectEvent = createEvent();
    memset(&m_connectOver, 0, sizeof(m_connectOver));
    m_connectOver.hEvent = m_connectEvent.get();
    BOOL success = ConnectNamedPipe(m_handle, &m_connectOver);
    const auto err = GetLastError();
    if (!success && err == ERROR_PIPE_CONNECTED) {
        success = TRUE;
    }
    if (success) {
        TRACE("Server pipe [%s] connected", utf8FromWide(pipeName).c_str());
        m_connectEvent.dispose();
        startPipeWorkers();
    } else if (err != ERROR_IO_PENDING) {
        ASSERT(false && "ConnectNamedPipe call failed");
    }
}