summaryrefslogtreecommitdiffstats
path: root/old/network/NetworkClient.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-29 16:55:16 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-29 16:55:16 +0200
commitf942405184c2d6067fb5303b58a225edf7e452b1 (patch)
tree83e70c7e3019e5b195c9caf41194b2113fa76d7f /old/network/NetworkClient.cpp
parent2017-07-26 (diff)
downloadAltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.gz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.bz2
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.lz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.xz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.zst
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.zip
Diffstat (limited to 'old/network/NetworkClient.cpp')
-rw-r--r--old/network/NetworkClient.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/old/network/NetworkClient.cpp b/old/network/NetworkClient.cpp
new file mode 100644
index 0000000..0b759e6
--- /dev/null
+++ b/old/network/NetworkClient.cpp
@@ -0,0 +1,93 @@
+#include "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";
+}