summaryrefslogtreecommitdiffstats
path: root/NetworkClient.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-04-21 15:31:43 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-04-21 15:31:43 +0200
commit71760514fc044f273914c17b1510fa3cf10419b3 (patch)
tree72f822efde300d8e1e9de1cc578d431ff72e0e03 /NetworkClient.cpp
parent2017-04-15 (diff)
downloadAltCraft-71760514fc044f273914c17b1510fa3cf10419b3.tar
AltCraft-71760514fc044f273914c17b1510fa3cf10419b3.tar.gz
AltCraft-71760514fc044f273914c17b1510fa3cf10419b3.tar.bz2
AltCraft-71760514fc044f273914c17b1510fa3cf10419b3.tar.lz
AltCraft-71760514fc044f273914c17b1510fa3cf10419b3.tar.xz
AltCraft-71760514fc044f273914c17b1510fa3cf10419b3.tar.zst
AltCraft-71760514fc044f273914c17b1510fa3cf10419b3.zip
Diffstat (limited to 'NetworkClient.cpp')
-rw-r--r--NetworkClient.cpp36
1 files changed, 26 insertions, 10 deletions
diff --git a/NetworkClient.cpp b/NetworkClient.cpp
index e8ba7ca..d6835e0 100644
--- a/NetworkClient.cpp
+++ b/NetworkClient.cpp
@@ -1,8 +1,7 @@
#include "NetworkClient.hpp"
#include "PacketParser.hpp"
#include "PacketBuilder.hpp"
-#include "json.h"
-#include <chrono>
+#include "json.hpp"
ServerInfo NetworkClient::ServerPing(std::string address, unsigned short port) {
ServerInfo info;
@@ -55,20 +54,24 @@ ServerInfo NetworkClient::ServerPing(std::string address, unsigned short port) {
return info;
}
-NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username) : m_network(address, port) {
+NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username) : m_network(address,
+ port) {
m_network.SendHandshake(username);
+ Update();
+ m_networkThread = std::thread(&NetworkClient::MainLoop, this);
}
NetworkClient::~NetworkClient() {
-
+ isContinue=false;
+ m_networkThread.join();
}
-Packet NetworkClient::GetPacket() {
- if (m_received.size()<1)
- return Packet(-1);
+Packet * NetworkClient::GetPacket() {
+ if (m_received.size() < 1)
+ return nullptr;
Packet packet = m_received.front();
m_received.pop();
- return packet;
+ return new Packet(packet);
}
void NetworkClient::AddPacketToQueue(Packet packet) {
@@ -76,16 +79,29 @@ void NetworkClient::AddPacketToQueue(Packet packet) {
}
void NetworkClient::Update() {
- if (m_toSend.size()>0){
+ if (m_toSend.size() > 0) {
m_network.SendPacket(m_toSend.front());
m_toSend.pop();
}
Packet received = m_network.ReceivePacket();
- if (received.GetId()==0x1F){
+ if (received.GetId() == 0x1F) {
PacketParser::Parse(received);
Packet response = PacketBuilder::CPlay0x0B(received.GetField(0).GetVarInt());
m_network.SendPacket(response);
return;
}
+ m_updateMutex.lock();
m_received.push(received);
+ m_updateMutex.unlock();
+}
+
+void NetworkClient::MainLoop() {
+ try {
+ while (isContinue) {
+ Update();
+ }
+ } catch (int e){
+ std::cerr<<"NetworkClient exception: "<<e<<std::endl;
+ }
+
}