Named pipe wait for client in background until client connects

ⅰ亾dé卋堺 提交于 2019-12-24 08:13:09

问题


I'm trying to make my named pipe server skip the block-wait of the ConnectNamedPipe function until my client tries to connect. So I want is for my code to continue past the ConnetNamedPipe line until the end but keep the connection for my pipe open in the backgroun. If I use the PIPE_NOWAIT mode when creating the pipe it just returns imediatelly and the pipe closes before my client can connect.

I know that I tried doing this using a thread but even when I create a thread and execute the ConnectNamedPipe part of the code within the thread it still waits on this line instead of continuing with my code. Once it reaches the end of my server cpp file code I want to connect to the pipe with my client.

My Pipe:

hPipe = CreateNamedPipe(
    lpszPipename,             
    PIPE_ACCESS_OUTBOUND,       // one way access, only send data
    PIPE_TYPE_MESSAGE |       // message type pipe
    PIPE_READMODE_MESSAGE |   // message-read mode
    PIPE_WAIT,                
    PIPE_UNLIMITED_INSTANCES, 
    512,
    512,                  
    0,                        
    NULL);                    

if (hPipe == INVALID_HANDLE_VALUE)
{
    Out->msg(ERR,"Creating the pipe failed.");
}

 // Create a thread for this client.
hThread = CreateThread(
    NULL,              // no security attribute
    0,                 // default stack size
    InstanceThread,    // thread proc
    (LPVOID) hPipe,    // thread parameter
    0,                 // not suspended
    &dwThreadId);      // returns thread ID

if (hThread == NULL)
{
    Out->msg(ERR,"Creating the thread failed.");
}
else CloseHandle(hThread);

// Close the pipe.
 CloseHandle(hPipe);

My Thread:

DWORD WINAPI InstanceThread(LPVOID lpvParam)
{
    Out->msg(output::SEV_INFO,"Waiting for client to connect.");
    fConnected = ConnectNamedPipe(hPipe, NULL) ? //This is where the execution hangs
    TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

    HANDLE hHeap      = GetProcessHeap();
    TCHAR* pchReply   = (TCHAR*)HeapAlloc(hHeap, 0, BUFSIZE*sizeof(TCHAR));

    DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0;
    BOOL fSuccess = FALSE;
    HANDLE hPipe  = NULL;

// The thread's parameter is a handle to a pipe object instance.

    hPipe = (HANDLE) lpvParam;
    uint32_t startTime = time(NULL);
    uint32_t elapsedTime;
// Loop until done reading
    while ((elapsedTime < 60))
    {
        elapsedTime = difftime(time(NULL), startTime);
        // Write to the pipe.
        fSuccess = WriteFile(
            hPipe,        // handle to pipe
            pchReply,     // buffer to write from
            cbReplyBytes, // number of bytes to write
            &cbWritten,   // number of bytes written
            NULL);        // not overlapped I/O

        if (!fSuccess || cbReplyBytes != cbWritten)
        {
            Out->msg(ERR,"InstanceThread WriteFile failed.");
            break;
        }
    }

// Flush the pipe to allow the client to read the pipe's contents
// before disconnecting. Then disconnect the pipe, and close the
// handle to this pipe instance.

    FlushFileBuffers(hPipe);
    DisconnectNamedPipe(hPipe);
    CloseHandle(hPipe);

    HeapFree(hHeap, 0, pchReply);

    Out->msg(output::SEV_INFO,"InstanceThread exitting.\n");
    return 1;
}

回答1:


I'm trying to make my named pipe server skip the block-wait of the ConnectNamedPipe function until my client tries to connect.

So you're talking about the server.

So I want my code to continue past the ConnectNamedPipe line while still being able to connect to my server.

So you're talking about the client.

It doesn't make sense. ConnectNamedPipe() is a server-side function, and you don't have anything useful to do in a thread dedicated to it except block until a client connects. So do that.



来源:https://stackoverflow.com/questions/23078433/named-pipe-wait-for-client-in-background-until-client-connects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!