命名管道有时不起作用

时间:2016-06-02 14:37:14

标签: c++ named-pipes unreal-engine4

所以我编写了一个C ++应用程序,通过命名管道连接到虚幻引擎4游戏。 95%的时间它完美地运作,但有时它似乎没有正确连接。这是非常随意的,所以我很难找到造成这个问题的问题。服务器在我的应用程序中创建,客户端在UE4游戏中创建。有时UE4游戏没有连接并显示错误121:

//
// MessageId: ERROR_SEM_TIMEOUT
//
// MessageText:
//
// The semaphore timeout period has expired.
//
#define ERROR_SEM_TIMEOUT                121L

正如我所说的那样,问题非常随机,我似乎找不到可能导致问题的具体原因。管道已成功创建,我可以在windows powershell中看到这个(get-childitem \。\ pipe)。

我想也许它对我使用的管道设置有什么好处?

这是我创建管道服务器的代码:

    DWORD erPipeServer::CreatePipeServer() {

    // create a SECURITY_ATTRIBUTES structure.
    if (!CreatePipeSecurity(&pSa))
    {
        dwError = GetLastError();
        //wprintf(L"CreatePipeSecurity failed w/err 0x%08lx\n", dwError);
        Cleanup();
        return dwError;
    }

    // Create the named pipe.
    hNamedPipe = CreateNamedPipe(
        pipename,             // Pipe name.
        PIPE_ACCESS_DUPLEX,         // The pipe is duplex; both server and 
                                    // client processes can read from and 
                                    // write to the pipe
        PIPE_TYPE_MESSAGE |         // Message type pipe 
        PIPE_READMODE_MESSAGE |     // Message-read mode 
        PIPE_NOWAIT,                  // Blocking mode is enabled
        PIPE_UNLIMITED_INSTANCES,   // Max. instances
        BUFFER_SIZE,                // Output buffer size in bytes
        BUFFER_SIZE,                // Input buffer size in bytes
        NMPWAIT_WAIT_FOREVER,   // Time-out interval
        pSa                         // Security attributes
        );

    if (hNamedPipe == INVALID_HANDLE_VALUE)
    {
        dwError = GetLastError();
        //wprintf(L"Unable to create named pipe w/err 0x%08lx\n", dwError);
        Cleanup();
        return dwError;
    }

    //wprintf(L"The named pipe (%s) is created.\n", pipename);
    return dwError;
}

这是我在虚幻引擎4中创建客户端的代码:

    // Try to open the named pipe identified by the pipe name.
        while (true)
        {
            hPipe = CreateFile(
                FULL_PIPE_NAME,                 // Pipe name 
                GENERIC_READ | GENERIC_WRITE,   // Read and write access
                0,                              // No sharing 
                NULL,                           // Default security attributes
                OPEN_ALWAYS,                  // Opens existing pipe
                0,                              // Default attributes
                NULL                            // No template file
                );

            // If the pipe handle is opened successfully ...
            if (hPipe != INVALID_HANDLE_VALUE)
            {
                GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Green, FString::Printf(TEXT("The named pipe %d is connected."), FULL_PIPE_NAME));
                break;
            }
            dwError = GetLastError();

            // Exit if an error other than ERROR_PIPE_BUSY occurs.
            if (ERROR_PIPE_BUSY != dwError)
            {
                GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Red, FString::Printf(TEXT("Unable to open named pipe ------ %d"),dwError));
                goto Cleanup;
            }

            // All pipe instances are busy, so wait for 5 seconds.
            if (!WaitNamedPipe(FULL_PIPE_NAME, 5000))
            {
                dwError = GetLastError();
                GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Red, FString::Printf(TEXT("Could not open pipe: 5 second wait timed out. %d"),dwError));
**THE 121 ERROR OCCURED HERE^^**
                goto Cleanup;
            }
        }

管道设置有什么问题吗?我不明白为什么它几乎一直在起作用,但有时候并没有明确的理由为什么或什么时候......

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:-1)

好的,所以我认为我已经解决了问题,也许不是最好的方式,但它似乎对我的目的来说已经足够好了。

在设法重现问题之后,我开始认为Handle可以打开或忙碌(感谢Karsten !!)。我可以通过使用Windows PowerShell并运行\。\ pipe \ name(其中name是管道名称)来错误地显示121错误。这将打开管道,游戏无法连接,显示错误121.

我如何修复"它:在一秒钟之后没有连接时重新创建管道。通常在我的应用程序连接服务器时,客户端已准备就绪。所以应该立即联系。如果不是,现在管道会在一秒钟之后重新创建,然后它就会起作用。当然这不是一个干净的修复,但我不知道手柄通常如何打开,因为通常唯一的试图连接到管道的应用程序是游戏......

但无论如何,对于1次30次出现问题(仍然)有些奇怪的原因,这是一个不错的解决办法......

任何其他想法将不胜感激,但现在我认为这是有用的。)