From f561980fecbfcda4d2ee2fafbe01991a974ca141 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Thu, 30 Jul 2020 04:46:07 +0500 Subject: Improved connection exceptions handling --- src/NetworkClient.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index 6273937..ee79fb9 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -24,17 +24,29 @@ NetworkClient::NetworkClient(std::string address, unsigned short port, std::stri auto packet = network->ReceivePacket(Login); - while (!packet) - packet = network->ReceivePacket(Login); + for (int i = 0; i < 10 && !packet; i++) + packet = network->ReceivePacket(Login); + if (!packet) + throw std::runtime_error("Server not answered after LoginStart"); if (packet->GetPacketId() == PacketNameLoginCB::SetCompression) { auto compPacket = std::static_pointer_cast(packet); LOG(INFO) << "Compression threshold: " << compPacket->Threshold; compressionThreshold = compPacket->Threshold; packet.reset(); - while (!packet) - packet = network->ReceivePacket(Login, compressionThreshold >= 0); + for (int i = 0; i < 10 && !packet; i++) + packet = network->ReceivePacket(Login, compressionThreshold >= 0); + if (!packet) + throw std::runtime_error("Server not answered after SetCompression"); } + else if (packet->GetPacketId() == PacketNameLoginCB::Disconnect) { + auto disconnectPacket = std::static_pointer_cast(packet); + LOG(INFO) << "Server not allowed connection: " << disconnectPacket->Reason; + throw std::runtime_error(disconnectPacket->Reason); + } + else if (packet->GetPacketId() != PacketNameLoginCB::LoginSuccess) { + throw std::runtime_error("Unexpected packet type: " + std::to_string(packet->GetPacketId())); + } auto response = std::static_pointer_cast(packet); -- cgit v1.2.3