diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-10-09 18:39:48 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 03:39:32 +0100 |
commit | a81bcb1942ddad5989115061338e72211443e633 (patch) | |
tree | cb1cc6feadb7dfb49f96cbc9b2a4f2bfb9ad8488 /src/NetworkClient.cpp | |
parent | 2017-10-07 (diff) | |
download | AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.gz AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.bz2 AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.lz AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.xz AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.zst AltCraft-a81bcb1942ddad5989115061338e72211443e633.zip |
Diffstat (limited to 'src/NetworkClient.cpp')
-rw-r--r-- | src/NetworkClient.cpp | 77 |
1 files changed, 27 insertions, 50 deletions
diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index 74d7804..dd467e8 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -37,72 +37,49 @@ NetworkClient::NetworkClient(std::string address, unsigned short port, std::stri throw std::logic_error("Received username is not sended username: "+response->Username+" != "+username); } - state = Play; + timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); - isActive = true; - networkThread = std::thread(&NetworkClient::NetworkLoop, this); + state = Play; } NetworkClient::~NetworkClient() { - isActive = false; - networkThread.join(); } std::shared_ptr<Packet> NetworkClient::ReceivePacket() { if (toReceive.empty()) return std::shared_ptr < Packet > (nullptr); - toReceiveMutex.lock(); auto ret = toReceive.front(); toReceive.pop(); - toReceiveMutex.unlock(); return ret; } void NetworkClient::SendPacket(std::shared_ptr<Packet> packet) { - toSendMutex.lock(); toSend.push(packet); - toSendMutex.unlock(); } -void NetworkClient::NetworkLoop() { - auto timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); - el::Helpers::setThreadName("Network"); - bool validEnded = true; - - try { - while (isActive) { - toSendMutex.lock(); - while (!toSend.empty()) { - if (toSend.front() != nullptr) - network.SendPacket(*toSend.front(), compressionThreshold); - toSend.pop(); - } - toSendMutex.unlock(); - auto packet = network.ReceivePacket(state, compressionThreshold >= 0); - if (packet.get() != nullptr) { - if (packet->GetPacketId() != PacketNamePlayCB::KeepAliveCB) { - toReceiveMutex.lock(); - toReceive.push(packet); - toReceiveMutex.unlock(); - } else { - timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); - auto packetKeepAlive = std::static_pointer_cast<PacketKeepAliveCB>(packet); - auto packetKeepAliveSB = std::make_shared<PacketKeepAliveSB>(packetKeepAlive->KeepAliveId); - network.SendPacket(*packetKeepAliveSB, compressionThreshold); - } - } - using namespace std::chrono_literals; - if (std::chrono::steady_clock::now() - timeOfLastKeepAlivePacket > 20s) { - auto disconnectPacket = std::make_shared<PacketDisconnectPlay>(); - disconnectPacket->Reason = "Timeout: server not respond"; - toReceiveMutex.lock(); - toReceive.push(disconnectPacket); - toReceiveMutex.unlock(); - break; - } - } - } catch (std::exception &e) { - EventAgregator::PushEvent(EventType::NetworkClientException, NetworkClientExceptionData{ e.what() }); - validEnded = false; - } +void NetworkClient::UpdatePacket() { + while (!toSend.empty()) { + if (toSend.front() != nullptr) + network.SendPacket(*toSend.front(), compressionThreshold); + toSend.pop(); + } + + auto packet = network.ReceivePacket(state, compressionThreshold >= 0); + if (packet.get() != nullptr) { + if (packet->GetPacketId() != PacketNamePlayCB::KeepAliveCB) { + toReceive.push(packet); + } + else { + timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); + auto packetKeepAlive = std::static_pointer_cast<PacketKeepAliveCB>(packet); + auto packetKeepAliveSB = std::make_shared<PacketKeepAliveSB>(packetKeepAlive->KeepAliveId); + network.SendPacket(*packetKeepAliveSB, compressionThreshold); + } + } + using namespace std::chrono_literals; + if (std::chrono::steady_clock::now() - timeOfLastKeepAlivePacket > 20s) { + auto disconnectPacket = std::make_shared<PacketDisconnectPlay>(); + disconnectPacket->Reason = "Timeout: server not respond"; + toReceive.push(disconnectPacket); + } }
\ No newline at end of file |