From e0b7f23cd967db7e713b9de960cad9bceb9fece8 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 13 Jan 2018 14:31:24 +0500 Subject: New implementation of NC is using event-system --- src/NetworkClient.cpp | 65 ++++++++++++++++++++++++--------------------------- src/NetworkClient.hpp | 9 ++++--- 2 files changed, 35 insertions(+), 39 deletions(-) (limited to 'src') 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 #include "Network.hpp" +#include "Event.hpp" NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username) { network = std::make_unique(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 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) { - 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 = eventData.get>(); + 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); + } } - 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); } } - using namespace std::chrono_literals; - if (std::chrono::steady_clock::now() - timeOfLastKeepAlivePacket > 20s) { - auto disconnectPacket = std::make_shared(); - disconnectPacket->Reason = "Timeout: server not respond"; - toReceive.push(disconnectPacket); - } } \ No newline at end of file diff --git a/src/NetworkClient.hpp b/src/NetworkClient.hpp index 2bcbd9f..f372468 100644 --- a/src/NetworkClient.hpp +++ b/src/NetworkClient.hpp @@ -4,6 +4,7 @@ #include #include #include +#include class Network; struct Packet; @@ -16,12 +17,10 @@ class NetworkClient { ConnectionState state; int compressionThreshold = -1; std::chrono::steady_clock::time_point timeOfLastKeepAlivePacket; + std::thread thread; + bool isRunning=true; + void ExecNs(); public: NetworkClient(std::string address, unsigned short port, std::string username); ~NetworkClient(); - - std::shared_ptr ReceivePacket(); - void SendPacket(std::shared_ptr packet); - - void UpdatePacket(); }; \ No newline at end of file -- cgit v1.2.3