diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-07 17:08:15 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-07 17:08:15 +0200 |
commit | 8b275c0b0a1064a813ef14a109b64e1fce461893 (patch) | |
tree | 7bb1cc13d2eb9be2a344b4cd35f5e581df970f0d /src/ThreadNetwork.cpp | |
parent | 2017-08-05 (diff) | |
download | AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.gz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.bz2 AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.lz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.xz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.zst AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.zip |
Diffstat (limited to '')
-rw-r--r-- | src/ThreadNetwork.cpp | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/src/ThreadNetwork.cpp b/src/ThreadNetwork.cpp index 51c8989..f734c0c 100644 --- a/src/ThreadNetwork.cpp +++ b/src/ThreadNetwork.cpp @@ -4,18 +4,15 @@ ThreadNetwork::ThreadNetwork() { } -ThreadNetwork::~ThreadNetwork() { - delete nc; +ThreadNetwork::~ThreadNetwork() { } 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::Exit, [this] (EventData eventData) { + isRunning = false; + }); listener.RegisterHandler(EventType::ConnectToServer, [this](EventData eventData) { auto data = std::get<ConnectToServerData>(eventData); @@ -25,8 +22,8 @@ void ThreadNetwork::Execute() { LOG(ERROR) << "Already connected"; return; } - SetGlobalState(GlobalState::Connecting); LOG(INFO) << "Connecting to server"; + EventAgregator::PushEvent(EventType::Connecting, ConnectingData{}); try { nc = new NetworkClient(data.address, data.port, "HelloOne"); } catch (std::exception &e) { @@ -38,11 +35,24 @@ void ThreadNetwork::Execute() { EventAgregator::PushEvent(EventType::ConnectionSuccessfull, ConnectionSuccessfullData{nc}); }); - listener.RegisterHandler(EventType::RequestNetworkClient, [this](EventData eventData) { - EventAgregator::PushEvent(EventType::RegisterNetworkClient, RegisterNetworkClientData{nc}); - }); - - while (state != GlobalState::Exiting) { + listener.RegisterHandler(EventType::Disconnect, [this](EventData eventData) { + auto data = std::get<DisconnectData>(eventData); + EventAgregator::PushEvent(EventType::Disconnected, DisconnectedData{ data.reason }); + LOG(INFO) << "Disconnected: " << data.reason; + delete nc; + nc = nullptr; + }); + + listener.RegisterHandler(EventType::NetworkClientException, [this](EventData eventData) { + auto data = std::get<NetworkClientExceptionData>(eventData); + EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ data.what }); + }); + + LoopExecutionTimeController timer(std::chrono::milliseconds(16)); + while (isRunning) { listener.HandleEvent(); + + timer.Update(); } + delete nc; }
\ No newline at end of file |