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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#include "message.h"
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <cstring>
#include <sstream>
Message::Base::Base(Type t) : type(t) {
}
Message::NullBase::NullBase(Type t) : Base(t) {
}
std::pair<uint8_t*, std::size_t> Message::NullBase::serialize() {
return std::pair<uint8_t*, std::size_t>(0, 0);
}
void Message::NullBase::deserialize(uint8_t* data, std::size_t bytes) {
}
Message::BoostBase::BoostBase(Type t) : Base(t) {
}
std::pair<uint8_t*, std::size_t> Message::BoostBase::serialize() {
std::ostringstream os;
boost::archive::text_oarchive oa(os);
serialize(oa);
std::size_t s = os.str().size();
uint8_t* buf = new uint8_t[s];
memcpy(buf, os.str().c_str(), s);
return std::pair<uint8_t*, std::size_t>(buf, s);
}
void Message::BoostBase::deserialize(uint8_t* data, std::size_t bytes) {
std::istringstream is(std::string((char*)data, bytes));
boost::archive::text_iarchive ia(is);
deserialize(ia);
}
Message::Hello::Hello() : Base(Types::Hello) {
}
Message::Hello::Hello(const std::string& v) : Base(Types::Hello), version(v) {
}
std::pair<uint8_t*, std::size_t> Message::Hello::serialize() {
std::size_t s = version.size();
uint8_t* buf = new uint8_t[s];
memcpy(buf, version.c_str(), version.size());
return std::pair<uint8_t*, std::size_t>(buf, s);
}
void Message::Hello::deserialize(uint8_t* data, std::size_t bytes) {
version = std::string((char*)data, bytes);
}
Message::Login::Login() : Base(Types::Login) {
}
Message::Login::Login(const std::string& n) : Base(Types::Login), nick(n) {
}
std::pair<uint8_t*, std::size_t> Message::Login::serialize() {
std::size_t s = nick.size();
uint8_t* buf = new uint8_t[s];
memcpy(buf, nick.c_str(), nick.size());
return std::pair<uint8_t*, std::size_t>(buf, s);
}
void Message::Login::deserialize(uint8_t* data, std::size_t bytes) {
nick = std::string((char*)data, bytes);
}
Message::LoginResponse::LoginResponse() : Base(Types::LoginResponse) {
}
Message::LoginResponse::LoginResponse(bool ok) : Base(Types::LoginResponse), login_ok(ok) {
}
std::pair<uint8_t*, std::size_t> Message::LoginResponse::serialize() {
uint8_t* buf = new uint8_t[1];
buf[0] = uint8_t(login_ok);
return std::pair<uint8_t*, std::size_t>(buf, 1);
}
void Message::LoginResponse::deserialize(uint8_t* data, std::size_t bytes) {
login_ok = bool(data[0]);
}
Message::GameStart::GameStart() : BoostBase(Types::GameStart) {
}
void Message::GameStart::serialize(boost::archive::text_oarchive& ar) {
ar & players;
}
void Message::GameStart::deserialize(boost::archive::text_iarchive& ar) {
ar & players;
}
Message::Ready::Ready() : NullBase(Types::Ready) {
}
|