summaryrefslogtreecommitdiffstats
path: root/src/NetworkClient.cpp
blob: 74d78046d583f2d37174fbaf22ab58859e5415e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "NetworkClient.hpp"

NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username)
		: network(address, port) {
	state = Handshaking;

	PacketHandshake handshake;
	handshake.protocolVersion = 338;
	handshake.serverAddress = address;
	handshake.serverPort = port;
	handshake.nextState = 2;
	network.SendPacket(handshake);
	state = Login;

	PacketLoginStart loginStart;
	loginStart.Username = "HelloOne";
	network.SendPacket(loginStart);


    auto packet = network.ReceivePacket(Login);

    while (!packet)
        packet = network.ReceivePacket(Login);

    if (packet->GetPacketId() == PacketNameLoginCB::SetCompression) {
        auto compPacket = std::static_pointer_cast<PacketSetCompression>(packet);
        LOG(INFO) << "Compression threshold: " << compPacket->Threshold;
        compressionThreshold = compPacket->Threshold;
        packet.reset();
        while (!packet)
            packet = network.ReceivePacket(Login, compressionThreshold >= 0);
    }

	auto response = std::static_pointer_cast<PacketLoginSuccess>(packet);

	if (response->Username != username) {
		throw std::logic_error("Received username is not sended username: "+response->Username+" != "+username);
	}

	state = Play;

	isActive = true;
	networkThread = std::thread(&NetworkClient::NetworkLoop, this);
}

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;
	}
}