summaryrefslogtreecommitdiff
path: root/common/message.cpp
blob: 1429736d9197f26633e31a36567829e5130d2aba (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "message.h"

#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.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) {
	
}

template<class Sub>
Message::BoostBase<Sub>::BoostBase(Type t) : Base(t) {
	
}

template<class Sub>
std::pair<uint8_t*, std::size_t> Message::BoostBase<Sub>::serialize() {
	std::ostringstream os;
	boost::archive::text_oarchive oa(os);
	
	oa & *this;
	
	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);
}

template<class Sub>
void Message::BoostBase<Sub>::deserialize(uint8_t* data, std::size_t bytes) {
	std::istringstream is(std::string((char*)data, bytes));
	boost::archive::text_iarchive ia(is);
	
	ia & *this;
}

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<GameStart>(Types::GameStart) {
	
}

Message::Ready::Ready() : NullBase(Types::Ready) {
	
}

Message::RoundStart::RoundStart() : NullBase(Types::RoundStart) {
	
}

Message::RoundState::RoundState() : BoostBase<RoundState>(Types::RoundState) {
	
}

Message::RoundState::RoundState(const Player& pl_d, const Player& pl_r, const Player& pl_u, const Player& pl_l, const Tiles& d, const Actions& a)
	: BoostBase<RoundState>(Types::RoundState), dora(d), possible_actions(a) {
	players[0] = pl_d;
	players[1] = pl_r;
	players[2] = pl_u;
	players[3] = pl_l;
}

Message::RoundAction::RoundAction() : BoostBase<RoundAction>(Types::RoundAction) {
	
}

Message::RoundEnd::RoundEnd() : NullBase(Types::RoundEnd) {

}