【发布时间】:2016-06-02 14:37:13
【问题描述】:
所以我编写了一个 C++ 应用程序,它通过命名管道连接到 Unreal Engine 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;
}
}
可能是管道设置有问题吗?我不明白为什么它几乎一直有效,但有时没有明确的原因或时间......
提前感谢您的帮助!
【问题讨论】:
-
那么在错误情况下,管道很忙,
WaitNamedPipe5 秒后超时?还是来自CreateFile的错误121? -
是的,客户端的 if 语句中发生了错误:if (!WaitNamedPipe(FULL_PIPE_NAME, 5000))
-
重启客户端后是否立即发生?也许还有一些已经失效的进程,保持句柄打开。
-
嗯,我不确定,我以为是 5 秒后发生的,但我还没有检查,我可以尝试重现它,看看是否是这种情况
-
嗨 Karsten,昨天我似乎无法重现它,但今天可以。是的,即使它说它超时,它也会立即发生。它在尝试连接后立即返回 121 错误。有没有办法检查手柄是否以及为什么打开。什么可能导致这种情况?
标签: c++ named-pipes unreal-engine4