-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventHandler.cpp
43 lines (36 loc) · 1.01 KB
/
EventHandler.cpp
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
/*
* EventHandler.cpp
*
* Created on: Feb 16, 2015
* Author: rmin
*/
#include "EventHandler.h"
#include "Parser.h"
#include <chrono>
#include <future>
#include "Queue.h"
EventHandler::EventHandler(Network* network) {
this->network = network;
this->alive = true;
this->thread = new std::thread(&EventHandler::handling, this);
}
EventHandler::~EventHandler() {
this->alive = false;
this->thread->detach();
delete this->thread;
}
void EventHandler::handling() {
while (this->alive) handleEvent(events.pop());
}
void EventHandler::addEvent(GameEvent* event) { events.push(event); }
void EventHandler::handleEvent(GameEvent* eve) {
Message result, event;
result.addNode(Constants::MESSAGE_KEY_NAME, Constants::MESSAGE_KEY_EVENT);
parser.generateEvent(event, *eve);
std::vector<Message> eventsJson;
eventsJson.push_back(event);
result.addArray(Constants::MESSAGE_KEY_ARGS, eventsJson);
network->sendMessage(result);
delete eve;
}
EventHandler* eventHandler;