summaryrefslogtreecommitdiffstats
path: root/src/NetworkClient.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/NetworkClient.cpp')
-rw-r--r--src/NetworkClient.cpp77
1 files changed, 27 insertions, 50 deletions
diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp
index 74d7804..dd467e8 100644
--- a/src/NetworkClient.cpp
+++ b/src/NetworkClient.cpp
@@ -37,72 +37,49 @@ NetworkClient::NetworkClient(std::string address, unsigned short port, std::stri
throw std::logic_error("Received username is not sended username: "+response->Username+" != "+username);
}
- state = Play;
+ timeOfLastKeepAlivePacket = std::chrono::steady_clock::now();
- isActive = true;
- networkThread = std::thread(&NetworkClient::NetworkLoop, this);
+ state = Play;
}
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");
- bool validEnded = true;
-
- try {
- while (isActive) {
- toSendMutex.lock();
- while (!toSend.empty()) {
- if (toSend.front() != nullptr)
- network.SendPacket(*toSend.front(), compressionThreshold);
- toSend.pop();
- }
- toSendMutex.unlock();
- auto packet = network.ReceivePacket(state, compressionThreshold >= 0);
- 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, compressionThreshold);
- }
- }
- 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";
- toReceiveMutex.lock();
- toReceive.push(disconnectPacket);
- toReceiveMutex.unlock();
- break;
- }
- }
- } catch (std::exception &e) {
- EventAgregator::PushEvent(EventType::NetworkClientException, NetworkClientExceptionData{ e.what() });
- validEnded = false;
- }
+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);
+ }
+ 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) {
+ auto disconnectPacket = std::make_shared<PacketDisconnectPlay>();
+ disconnectPacket->Reason = "Timeout: server not respond";
+ toReceive.push(disconnectPacket);
+ }
} \ No newline at end of file