blob: b97a3f04b8327770af537a6ac6e584af1aba962e (
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
|
#include "ThreadNetwork.hpp"
ThreadNetwork::ThreadNetwork() {
}
ThreadNetwork::~ThreadNetwork() {
delete nc;
}
void ThreadNetwork::Execute() {
bool isRunning;
EventListener listener;
listener.RegisterHandler(EventType::GlobalAppState, [this](EventData eventData) {
auto data = std::get<GlobalAppStateData>(eventData);
state = data.state;
});
listener.RegisterHandler(EventType::ConnectToServer, [this, &isRunning](EventData eventData) {
auto data = std::get<ConnectToServerData>(eventData);
if (data.address == "" || data.port == 0)
LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT";
if (nc != nullptr) {
LOG(ERROR) << "Already connected";
return;
}
LOG(INFO) << "Connecting to server";
try {
nc = new NetworkClient(data.address, data.port, "HelloOne", isRunning);
} catch (std::exception &e) {
LOG(WARNING) << "CONNECTION FAIL";
LOG(FATAL) << "Can't connect to server: " << e.what();
}
LOG(INFO) << "Connected to server";
EventAgregator::PushEvent(EventType::ConnectionSuccessfull, ConnectionSuccessfullData{nc});
});
listener.RegisterHandler(EventType::RequestNetworkClient, [this](EventData eventData) {
EventAgregator::PushEvent(EventType::RegisterNetworkClient, RegisterNetworkClientData{nc});
});
/*listener.RegisterHandler(EventType::SendPacket, [this](EventData eventData) {
auto data = std::get<SendPacketData>(eventData);
auto packet = data.packet;
if (nc)
nc->SendPacket(packet);
else
LOG(ERROR) << "Send packet, while not connected to server";
});*/
while (state != GlobalState::Exiting) {
/*auto packet = nc ? nc->ReceivePacket() : nullptr;
if (packet != nullptr)
EventAgregator::PushEvent(EventType::ReceivePacket, ReceivePacketData{packet});*/
listener.HandleEvent();
}
}
|