在客户端连接之前,命名管道在后台等待客户端

时间:2014-04-15 08:33:47

标签: c++ multithreading named-pipes

我正在尝试让我的命名管道服务器跳过ConnectNamedPipe函数的阻塞等待,直到我的客户端尝试连接。所以我想要的是我的代码继续通过ConnetNamedPipe线直到结束,但保持我的管道连接在背景中打开。如果我在创建管道时使用PIPE_NOWAIT模式,它只会返回imediatelly并在我的客户端连接之前关闭管道。

我知道我尝试使用线程执行此操作,但即使我创建一个线程并在线程中执行代码的ConnectNamedPipe部分,它仍然在此行等待而不是继续我的代码。一旦它到达我的服务器cpp文件代码的末尾,我想用我的客户端连接到管道。

我的管道:

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);

我的主题:

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 个答案:

答案 0 :(得分:1)

  

我正在尝试让我的命名管道服务器跳过ConnectNamedPipe函数的阻塞等待,直到我的客户端尝试连接。

所以你在谈论服务器。

  

所以我希望我的代码能够继续通过ConnectNamedPipe行,同时仍能连接到我的服务器。

所以你在谈论客户。

没有意义。 ConnectNamedPipe()是一个服务器端函数,除了阻塞直到客户端连接之外,你在专用于它的线程中没有任何有用的东西。那样做。

相关问题