blob: 51c89891a84bd4eb85c6a9c3e0fea5fdd926a7e2 (
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
|
#include "ThreadNetwork.hpp"
ThreadNetwork::ThreadNetwork() {
}
ThreadNetwork::~ThreadNetwork() {
delete nc;
}
void ThreadNetwork::Execute() {
state = GlobalState::InitialLoading;
EventListener listener;
listener.RegisterHandler(EventType::GlobalAppState, [this](EventData eventData) {
auto data = std::get<GlobalAppStateData>(eventData);
state = data.state;
});
listener.RegisterHandler(EventType::ConnectToServer, [this](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;
}
SetGlobalState(GlobalState::Connecting);
LOG(INFO) << "Connecting to server";
try {
nc = new NetworkClient(data.address, data.port, "HelloOne");
} catch (std::exception &e) {
LOG(WARNING) << "Connection failed";
EventAgregator::PushEvent(EventType::ConnectionFailed, ConnectionFailedData{e.what()});
return;
}
LOG(INFO) << "Connected to server";
EventAgregator::PushEvent(EventType::ConnectionSuccessfull, ConnectionSuccessfullData{nc});
});
listener.RegisterHandler(EventType::RequestNetworkClient, [this](EventData eventData) {
EventAgregator::PushEvent(EventType::RegisterNetworkClient, RegisterNetworkClientData{nc});
});
while (state != GlobalState::Exiting) {
listener.HandleEvent();
}
}
|