-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.h
86 lines (67 loc) · 2.04 KB
/
events.h
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
#ifndef II_EVENTS_H
#define II_EVENTS_H
#include "utils.h"
namespace Event {
template<typename IntegerType>
std::string serialize(IntegerType n)
{
IntegerType hn = bswap(n);
return std::string(reinterpret_cast<char *>(&hn), sizeof(hn));
}
std::string serialize(char);
std::string serialize(std::string);
void serialize_args(std::stringstream &ss);
template<typename Type, typename ...Types>
void serialize_args(std::stringstream &ss, Type &arg, Types &...args) {
ss << serialize(arg);
serialize_args(ss, args...);
}
template<typename ...Types>
std::string serialize(Types &...args) {
std::stringstream ss;
serialize_args(ss, args...);
return ss.str();
}
template<typename IntegerType>
IntegerType parse(char const *str)
{
IntegerType n = *reinterpret_cast<IntegerType const *>(str);
return bswap(n);
}
struct SerializableEvent {
virtual std::string serialize() = 0;
};
template <char t>
struct EventType {
char type;
EventType() : type{t} {}
};
/* Server to client events */
struct NewGame : public EventType<0>, public SerializableEvent {
uint32_t maxx, maxy;
std::string player_names;
std::string serialize();
};
struct Pixel : public EventType<1>, public SerializableEvent {
char player_number;
uint32_t x, y;
std::string serialize();
};
struct PlayerEliminated : public EventType<2>, public SerializableEvent {
char player_number;
std::string serialize();
};
struct GameOver : public EventType<3>, public SerializableEvent {
std::string serialize();
};
/* Client to server events */
struct ClientEvent : public SerializableEvent {
uint64_t session_id;
int8_t turn_direction;
uint32_t next_expected_event_no;
std::string player_name;
std::string serialize();
bool parse(std::string const &);
};
}
#endif //II_EVENTS_H