因此,我编写了一个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;
}
}
管道设置可能有问题吗?我不明白为什么它几乎一直都有效,但是有时并没有明确的理由说明为什么或何时...
感谢您的任何提前帮助!
好的,所以我认为我已经解决了这个问题,也许不是最好的方法,但是对于我的目的来说,它似乎已经足够好了。
在设法重现问题之后,我开始尝试处理句柄可能处于打开状态或繁忙状态(感谢Karsten!)。我可以通过使用Windows Powershell并运行\。\ pipe \ name(其中名称为管道名称)来使游戏中的错误显示121错误。这将打开管道,并且游戏无法再连接,显示错误121。
我如何“修复”它:一秒钟后未建立连接时重新创建管道。通常在我的应用程序中,当服务器连接时,客户端已经准备就绪。因此,连接应立即进行。如果不是,现在将在一秒钟后重新创建管道,然后它将正常工作。当然,这不是一个干净的解决方法,但是我不知道手柄如何正常打开,因为通常尝试连接到管道的唯一应用程序是游戏...
但是无论如何,对于(仍然)某些奇怪原因导致问题发生的30倍中的1倍,这是一个不错的解决方法...
任何其他想法将不胜感激,但我认为这目前正在起作用:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句