[IPCPipe] Correctly shutdown the pipe

This commit is contained in:
momo5502 2017-01-25 14:34:53 +01:00
parent d31c1c5bb6
commit fff9046b72
3 changed files with 63 additions and 65 deletions

View File

@ -21,30 +21,28 @@ namespace Components
// Not sure if that's the best way though // Not sure if that's the best way though
Discovery::IsPerforming = false; Discovery::IsPerforming = false;
Discovery::IsTerminating = false; Discovery::IsTerminating = false;
Discovery::Thread = std::thread([] () Discovery::Thread = std::thread([]()
{ {
while (!Discovery::IsTerminating)
{ {
while (!Discovery::IsTerminating) if (Discovery::IsPerforming)
{ {
if (Discovery::IsPerforming) int start = Game::Sys_Milliseconds();
{
int start = Game::Sys_Milliseconds();
Logger::Print("Starting local server discovery...\n"); Logger::Print("Starting local server discovery...\n");
Discovery::Challenge = Utils::Cryptography::Rand::GenerateChallenge(); Discovery::Challenge = Utils::Cryptography::Rand::GenerateChallenge();
unsigned int minPort = Dvar::Var("net_discoveryPortRangeMin").get<unsigned int>(); unsigned int minPort = Dvar::Var("net_discoveryPortRangeMin").get<unsigned int>();
unsigned int maxPort = Dvar::Var("net_discoveryPortRangeMax").get<unsigned int>(); unsigned int maxPort = Dvar::Var("net_discoveryPortRangeMax").get<unsigned int>();
Network::BroadcastRange(minPort, maxPort, Utils::String::VA("discovery %s", Discovery::Challenge.data())); Network::BroadcastRange(minPort, maxPort, Utils::String::VA("discovery %s", Discovery::Challenge.data()));
Logger::Print("Discovery sent within %dms, awaiting responses...\n", Game::Sys_Milliseconds() - start); Logger::Print("Discovery sent within %dms, awaiting responses...\n", Game::Sys_Milliseconds() - start);
Discovery::IsPerforming = false; Discovery::IsPerforming = false;
}
std::this_thread::sleep_for(50ms);
} }
std::this_thread::sleep_for(50ms);
} }
}); });

View File

@ -109,12 +109,16 @@ namespace Components
if (this->pipe && INVALID_HANDLE_VALUE != this->pipe) if (this->pipe && INVALID_HANDLE_VALUE != this->pipe)
{ {
CancelIoEx(this->pipe, nullptr);
//DeleteFileA(this->pipeFile);
if (this->type == IPCTYPE_SERVER) DisconnectNamedPipe(this->pipe); if (this->type == IPCTYPE_SERVER) DisconnectNamedPipe(this->pipe);
CloseHandle(this->pipe); CloseHandle(this->pipe);
Logger::Print("Disconnected from the pipe.\n"); Logger::Print("Disconnected from the pipe.\n");
} }
this->pipe = nullptr;
this->threadAttached = false; this->threadAttached = false;
if (this->thread.joinable()) if (this->thread.joinable())
@ -140,42 +144,40 @@ namespace Components
void Pipe::ReceiveThread(Pipe* pipe) void Pipe::ReceiveThread(Pipe* pipe)
{ {
if (!pipe || pipe->type != IPCTYPE_SERVER || pipe->pipe == INVALID_HANDLE_VALUE || !pipe->pipe) return;
if (ConnectNamedPipe(pipe->pipe, nullptr) == FALSE)
{ {
if (!pipe || pipe->type != IPCTYPE_SERVER || pipe->pipe == INVALID_HANDLE_VALUE || !pipe->pipe) return; Logger::Print("Failed to initialize pipe reading.\n");
return;
}
if (ConnectNamedPipe(pipe->pipe, nullptr) == FALSE) Logger::Print("Client connected to the pipe\n");
pipe->connectCallback();
DWORD cbBytes;
while (pipe->threadAttached && pipe->pipe && pipe->pipe != INVALID_HANDLE_VALUE)
{
BOOL bResult = ReadFile(pipe->pipe, &pipe->packet, sizeof(pipe->packet), &cbBytes, nullptr);
if (bResult && cbBytes)
{ {
Logger::Print("Failed to initialize pipe reading.\n"); if (pipe->packetCallbacks.find(pipe->packet.command) != pipe->packetCallbacks.end())
return; {
pipe->packetCallbacks[pipe->packet.command](pipe->packet.buffer);
}
}
else if (pipe->threadAttached && pipe->pipe != INVALID_HANDLE_VALUE)
{
Logger::Print("Failed to read from client through pipe\n");
DisconnectNamedPipe(pipe->pipe);
ConnectNamedPipe(pipe->pipe, nullptr);
pipe->connectCallback();
} }
Logger::Print("Client connected to the pipe\n"); ZeroMemory(&pipe->packet, sizeof(pipe->packet));
pipe->connectCallback();
DWORD cbBytes;
while (pipe->threadAttached && pipe->pipe && pipe->pipe != INVALID_HANDLE_VALUE)
{
BOOL bResult = ReadFile(pipe->pipe, &pipe->packet, sizeof(pipe->packet), &cbBytes, nullptr);
if (bResult && cbBytes)
{
if (pipe->packetCallbacks.find(pipe->packet.command) != pipe->packetCallbacks.end())
{
pipe->packetCallbacks[pipe->packet.command](pipe->packet.buffer);
}
}
else if (pipe->threadAttached && pipe->pipe != INVALID_HANDLE_VALUE)
{
Logger::Print("Failed to read from client through pipe\n");
DisconnectNamedPipe(pipe->pipe);
ConnectNamedPipe(pipe->pipe, nullptr);
pipe->connectCallback();
}
ZeroMemory(&pipe->packet, sizeof(pipe->packet));
}
} }
} }
@ -237,7 +239,7 @@ namespace Components
void IPCPipe::preDestroy() void IPCPipe::preDestroy()
{ {
//IPCPipe::ServerPipe.destroy(); IPCPipe::ServerPipe.destroy();
//IPCPipe::ClientPipe.destroy(); IPCPipe::ClientPipe.destroy();
} }
} }

View File

@ -159,29 +159,27 @@ namespace Components
if (!Utils::IsWineEnvironment()) if (!Utils::IsWineEnvironment())
{ {
News::Terminate = false; News::Terminate = false;
News::Thread = std::thread([] () News::Thread = std::thread([]()
{ {
Localization::Set("MPUI_CHANGELOG_TEXT", Utils::Cache::GetFile("/iw4/changelog.txt"));
std::string data = Utils::Cache::GetFile("/iw4/motd.txt");
if (!data.empty())
{ {
Localization::Set("MPUI_CHANGELOG_TEXT", Utils::Cache::GetFile("/iw4/changelog.txt")); Localization::Set("MPUI_MOTD_TEXT", data);
}
std::string data = Utils::Cache::GetFile("/iw4/motd.txt"); if (!Loader::PerformingUnitTests())
{
if (!data.empty()) while (!News::Terminate)
{ {
Localization::Set("MPUI_MOTD_TEXT", data); News::CheckForUpdate();
}
if (!Loader::PerformingUnitTests()) // Sleep for 3 minutes
{ for (int i = 0; i < 180 && !News::Terminate; ++i)
while (!News::Terminate)
{ {
News::CheckForUpdate(); std::this_thread::sleep_for(1s);
// Sleep for 3 minutes
for (int i = 0; i < 180 && !News::Terminate; ++i)
{
std::this_thread::sleep_for(1s);
}
} }
} }
} }