From 42f93f6d3dceacfef0b54fb9dc778bbc00afea15 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Wed, 22 Aug 2018 01:14:19 +0500 Subject: Fixed crash when connection lost --- src/NetworkClient.cpp | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index e959faf..6273937 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -61,26 +61,30 @@ void NetworkClient::ExecNs() { network->SendPacket(*packet,compressionThreshold); }); - while (isRunning) { - listener.HandleAllEvents(); - - std::shared_ptr packet = network->ReceivePacket(state, compressionThreshold >= 0); - if (packet != nullptr) { - if (packet->GetPacketId() != PacketNamePlayCB::KeepAliveCB) { - PUSH_EVENT("ReceivedPacket", packet); - } - else { - timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); - auto packetKeepAlive = std::static_pointer_cast(packet); - auto packetKeepAliveSB = std::make_shared(packetKeepAlive->KeepAliveId); - network->SendPacket(*packetKeepAliveSB, compressionThreshold); - } - } - using namespace std::chrono_literals; - if (std::chrono::steady_clock::now() - timeOfLastKeepAlivePacket > 20s) { - packet = std::make_shared(); - std::static_pointer_cast(packet)->Reason = "Timeout: server not respond"; - PUSH_EVENT("ReceivedPacket", packet); - } - } + try { + while (isRunning) { + listener.HandleAllEvents(); + + std::shared_ptr packet = network->ReceivePacket(state, compressionThreshold >= 0); + if (packet != nullptr) { + if (packet->GetPacketId() != PacketNamePlayCB::KeepAliveCB) { + PUSH_EVENT("ReceivedPacket", packet); + } + else { + timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); + auto packetKeepAlive = std::static_pointer_cast(packet); + auto packetKeepAliveSB = std::make_shared(packetKeepAlive->KeepAliveId); + network->SendPacket(*packetKeepAliveSB, compressionThreshold); + } + } + using namespace std::chrono_literals; + if (std::chrono::steady_clock::now() - timeOfLastKeepAlivePacket > 20s) { + packet = std::make_shared(); + std::static_pointer_cast(packet)->Reason = "Timeout: server not respond"; + PUSH_EVENT("ReceivedPacket", packet); + } + } + } catch (std::exception &e) { + PUSH_EVENT("NetworkClientException", std::string(e.what())); + } } \ No newline at end of file -- cgit v1.2.3