From fff9046b7299872f31c0483c692fed7fee9a69f3 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Wed, 25 Jan 2017 14:34:53 +0100 Subject: [PATCH] [IPCPipe] Correctly shutdown the pipe --- src/Components/Modules/Discovery.cpp | 28 ++++++------ src/Components/Modules/IPCPipe.cpp | 68 ++++++++++++++-------------- src/Components/Modules/News.cpp | 32 ++++++------- 3 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/Components/Modules/Discovery.cpp b/src/Components/Modules/Discovery.cpp index 0c74ca39..01e14601 100644 --- a/src/Components/Modules/Discovery.cpp +++ b/src/Components/Modules/Discovery.cpp @@ -21,30 +21,28 @@ namespace Components // Not sure if that's the best way though Discovery::IsPerforming = 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 maxPort = Dvar::Var("net_discoveryPortRangeMax").get(); - Network::BroadcastRange(minPort, maxPort, Utils::String::VA("discovery %s", Discovery::Challenge.data())); + unsigned int minPort = Dvar::Var("net_discoveryPortRangeMin").get(); + unsigned int maxPort = Dvar::Var("net_discoveryPortRangeMax").get(); + 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; - } - - std::this_thread::sleep_for(50ms); + Discovery::IsPerforming = false; } + + std::this_thread::sleep_for(50ms); } }); diff --git a/src/Components/Modules/IPCPipe.cpp b/src/Components/Modules/IPCPipe.cpp index 5698a338..bc664de4 100644 --- a/src/Components/Modules/IPCPipe.cpp +++ b/src/Components/Modules/IPCPipe.cpp @@ -109,12 +109,16 @@ namespace Components if (this->pipe && INVALID_HANDLE_VALUE != this->pipe) { + CancelIoEx(this->pipe, nullptr); + //DeleteFileA(this->pipeFile); + if (this->type == IPCTYPE_SERVER) DisconnectNamedPipe(this->pipe); CloseHandle(this->pipe); Logger::Print("Disconnected from the pipe.\n"); } + this->pipe = nullptr; this->threadAttached = false; if (this->thread.joinable()) @@ -140,42 +144,40 @@ namespace Components 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"); - return; + 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(); } - 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) - { - 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)); - } + ZeroMemory(&pipe->packet, sizeof(pipe->packet)); } } @@ -237,7 +239,7 @@ namespace Components void IPCPipe::preDestroy() { - //IPCPipe::ServerPipe.destroy(); - //IPCPipe::ClientPipe.destroy(); + IPCPipe::ServerPipe.destroy(); + IPCPipe::ClientPipe.destroy(); } } diff --git a/src/Components/Modules/News.cpp b/src/Components/Modules/News.cpp index 56ed9002..cd133499 100644 --- a/src/Components/Modules/News.cpp +++ b/src/Components/Modules/News.cpp @@ -159,29 +159,27 @@ namespace Components if (!Utils::IsWineEnvironment()) { 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 (!data.empty()) + if (!Loader::PerformingUnitTests()) + { + while (!News::Terminate) { - Localization::Set("MPUI_MOTD_TEXT", data); - } + News::CheckForUpdate(); - if (!Loader::PerformingUnitTests()) - { - while (!News::Terminate) + // Sleep for 3 minutes + for (int i = 0; i < 180 && !News::Terminate; ++i) { - News::CheckForUpdate(); - - // Sleep for 3 minutes - for (int i = 0; i < 180 && !News::Terminate; ++i) - { - std::this_thread::sleep_for(1s); - } + std::this_thread::sleep_for(1s); } } }