diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 10:31:24 +0100 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 10:57:41 +0100 |
commit | e0b7f23cd967db7e713b9de960cad9bceb9fece8 (patch) | |
tree | 4c97ce06d0f1b34f6678fa69d2988b29d4a606ee /src/NetworkClient.cpp | |
parent | Finally removed all SFML references (diff) | |
download | AltCraft-e0b7f23cd967db7e713b9de960cad9bceb9fece8.tar AltCraft-e0b7f23cd967db7e713b9de960cad9bceb9fece8.tar.gz AltCraft-e0b7f23cd967db7e713b9de960cad9bceb9fece8.tar.bz2 AltCraft-e0b7f23cd967db7e713b9de960cad9bceb9fece8.tar.lz AltCraft-e0b7f23cd967db7e713b9de960cad9bceb9fece8.tar.xz AltCraft-e0b7f23cd967db7e713b9de960cad9bceb9fece8.tar.zst AltCraft-e0b7f23cd967db7e713b9de960cad9bceb9fece8.zip |
Diffstat (limited to '')
-rw-r--r-- | src/NetworkClient.cpp | 65 |
1 files changed, 31 insertions, 34 deletions
diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index 99d89f9..e959faf 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -3,6 +3,7 @@ #include <easylogging++.h> #include "Network.hpp" +#include "Event.hpp" NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username) { network = std::make_unique<Network>(address, port); @@ -44,46 +45,42 @@ NetworkClient::NetworkClient(std::string address, unsigned short port, std::stri timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); state = Play; + thread = std::thread(&NetworkClient::ExecNs,this); } NetworkClient::~NetworkClient() { + isRunning = false; + thread.join(); } -std::shared_ptr<Packet> NetworkClient::ReceivePacket() { - if (toReceive.empty()) - return std::shared_ptr < Packet > (nullptr); - auto ret = toReceive.front(); - toReceive.pop(); - return ret; -} - -void NetworkClient::SendPacket(std::shared_ptr<Packet> packet) { - toSend.push(packet); -} - -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); +void NetworkClient::ExecNs() { + EventListener listener; + + listener.RegisterHandler("SendPacket", [&](const Event& eventData) { + std::shared_ptr<Packet> packet = eventData.get<std::shared_ptr<Packet>>(); + network->SendPacket(*packet,compressionThreshold); + }); + + while (isRunning) { + listener.HandleAllEvents(); + + std::shared_ptr<Packet> 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<PacketKeepAliveCB>(packet); + auto packetKeepAliveSB = std::make_shared<PacketKeepAliveSB>(packetKeepAlive->KeepAliveId); + network->SendPacket(*packetKeepAliveSB, compressionThreshold); + } } - 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) { + packet = std::make_shared<PacketDisconnectPlay>(); + std::static_pointer_cast<PacketDisconnectPlay>(packet)->Reason = "Timeout: server not respond"; + PUSH_EVENT("ReceivedPacket", packet); } } - 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 |