diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-03 17:03:59 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-03 17:03:59 +0200 |
commit | f7decf41d8d0062901cd39c42a3669a80537c7df (patch) | |
tree | d3032265df11eee6e3eaf8b4b081524bef5e72b2 /src/network/NetworkClient.cpp | |
parent | 2017-07-29 (diff) | |
download | AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.gz AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.bz2 AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.lz AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.xz AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.zst AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.zip |
Diffstat (limited to '')
-rw-r--r-- | src/network/NetworkClient.cpp | 93 |
1 files changed, 0 insertions, 93 deletions
diff --git a/src/network/NetworkClient.cpp b/src/network/NetworkClient.cpp deleted file mode 100644 index 73880cf..0000000 --- a/src/network/NetworkClient.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include <network/NetworkClient.hpp> - -NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username, bool &quit) - : network(address, port), isRunning(quit) { - state = Handshaking; - - PacketHandshake handshake; - handshake.protocolVersion = 335; - handshake.serverAddress = "127.0.0.1"; - handshake.serverPort = 25565; - handshake.nextState = 2; - network.SendPacket(handshake); - state = Login; - - PacketLoginStart loginStart; - loginStart.Username = "HelloOne"; - network.SendPacket(loginStart); - - auto response = std::static_pointer_cast<PacketLoginSuccess>(network.ReceivePacket(Login)); - if (response->Username != username) { - throw std::logic_error("Received username is not sended username"); - } - - state = Play; - - isActive = true; - std::thread thread(&NetworkClient::NetworkLoop, this); - std::swap(networkThread, thread); -} - -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"); - LOG(INFO) << "Network thread is started"; - try { - while (isActive) { - toSendMutex.lock(); - while (!toSend.empty()) { - if (toSend.front()!=nullptr) - network.SendPacket(*toSend.front()); - toSend.pop(); - } - toSendMutex.unlock(); - auto packet = network.ReceivePacket(state); - 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); - } - } - using namespace std::chrono_literals; - if (std::chrono::steady_clock::now() - timeOfLastKeepAlivePacket > 20s) { - auto disconnectPacket = std::make_shared<PacketDisconnectPlay>(); - disconnectPacket->Reason = "Timeout"; - toReceiveMutex.lock(); - toReceive.push(disconnectPacket); - toReceiveMutex.unlock(); - break; - } - } - } catch (std::exception &e) { - LOG(ERROR) << "Exception catched in NetworkLoop: " << e.what(); - isRunning = false; - } - LOG(INFO) << "Network thread is stopped"; -} |