summaryrefslogtreecommitdiff
path: root/messages.cpp
blob: 68e46d221a43ec9e9358d32ff24c731c724ea72a (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "messages.h"

using namespace message;

/* MessageBase */

MessageBase::MessageBase() {
	type = MSG_TYPE_NONE;
}

void MessageBase::send(boost::asio::ip::tcp::socket& socket) {
	boost::asio::write(socket, boost::asio::buffer(&type, sizeof(type)));
	boost::asio::write(socket, b);
}

std::size_t MessageBase::payload_size() {
	return 0;
}

void MessageBase::read(boost::asio::ip::tcp::socket& socket) {
	boost::asio::streambuf::mutable_buffers_type buf = b.prepare(payload_size());
	std::size_t size = boost::asio::read(socket, buf);
	b.commit(size);
}

uint8_t MessageBase::read_type(boost::asio::ip::tcp::socket& socket) {
	uint8_t type;
	boost::asio::read(socket, boost::asio::buffer(&type, sizeof(uint8_t)));

	return type;
}

/* Hello */

Hello::Hello() {
	type = MSG_TYPE_HELLO;
}

Hello::Hello(uint8_t version) {
	type = MSG_TYPE_HELLO;

	std::ostream os(&b);
	os.write((const char*)&version, sizeof(version));
}

std::size_t Hello::payload_size() {
	return sizeof(uint8_t);
}

uint8_t Hello::read_version() {
	std::istream is(&b);
	uint8_t version;
	is.read((char*)&version, sizeof(version));
	b.consume(sizeof(version));

	return version;
}

/* Pos */

Pos::Pos() {
	type = MSG_TYPE_POS;
}

Pos::Pos(float x, float y, float z) {
	type = MSG_TYPE_POS;

	std::ostream os(&b);
	os.write((const char*)&x, sizeof(x));
	os.write((const char*)&y, sizeof(y));
	os.write((const char*)&z, sizeof(z));
}

std::size_t Pos::payload_size() {
	return sizeof(float)*3;
}

void Pos::get_pos(float& x, float& y, float& z) {
	std::istream is(&b);
	is.read((char*)&x, sizeof(x));
	is.read((char*)&y, sizeof(y));
	is.read((char*)&z, sizeof(z));
}

/* Chunk */

Chunk::Chunk() {
	type = MSG_TYPE_CHUNK;

	got_coords = false;
}

Chunk::Chunk(int64_t x, int64_t y) {
	type = MSG_TYPE_CHUNK;

	std::ostream os(&b);
	os.write((const char*)&x, sizeof(x));
	os.write((const char*)&y, sizeof(y));

	got_coords = true;
}

// TODO: move this elsewhere
const int chunk_size = 32;
const int chunk_size_total = chunk_size + 3;

std::size_t Chunk::payload_size() {
	return (got_coords ? sizeof(float)*chunk_size_total*chunk_size_total : sizeof(int64_t)*2);
}

void Chunk::set_data(float *data) {
	std::ostream os(&b);
	os.write((const char*)data, sizeof(float)*chunk_size_total*chunk_size_total);
}

float* Chunk::get_data() {
	float *data = new float[chunk_size_total*chunk_size_total];
	std::istream is(&b);
	is.read((char*)data, sizeof(float)*chunk_size_total*chunk_size_total);

	return data;
}

void Chunk::get_coords(int64_t& x, int64_t& y) {
	std::istream is(&b);
	is.read((char*)&x, sizeof(x));
	is.read((char*)&y, sizeof(y));
	got_coords = true;
}

/* Message */

Message::Message() {
	type = MSG_TYPE_MSG;

	str_len = 0;
	got_len = false;
}

Message::Message(std::string msg) {
	type = MSG_TYPE_MSG;

	str_len = msg.size();

	std::ostream os(&b);
	os.write((const char*)&str_len, sizeof(str_len));
	os.write(msg.c_str(), str_len);

	got_len = true;
}

std::size_t Message::payload_size() {
	return (got_len ? str_len : sizeof(uint16_t));
}

uint16_t Message::get_len() {
	std::istream is(&b);
	is.read((char*)&str_len, sizeof(str_len));

	got_len = true;

	return str_len;
}

std::string Message::get_str() {
	std::string s;

	char *buf = new char[str_len];
	std::istream is(&b);
	is.read(buf, str_len);

	s.assign(buf, str_len);
	delete[] buf;

	return s;
}

/* Player */

Player::Player() {
	type = MSG_TYPE_PLAYER;

	str_len = 0;
	got_len = false;
}

Player::Player(uint32_t id, Vector3 pos, std::string name) {
	type = MSG_TYPE_PLAYER;

	str_len = name.size();

	std::ostream os(&b);
	os.write((const char*)&id, sizeof(id));

	os.write((const char*)&pos.x, sizeof(pos.x));
	os.write((const char*)&pos.y, sizeof(pos.y));
	os.write((const char*)&pos.z, sizeof(pos.z));

	os.write((const char*)&str_len, sizeof(str_len));
	os.write(name.c_str(), str_len);

	got_len = true;
}

std::size_t Player::payload_size() {
	return (got_len ? str_len : (sizeof(uint32_t) + sizeof(float)*3 + sizeof(uint16_t)));
}

uint32_t Player::get_id() {
	std::istream is(&b);
	is.read((char*)&id, sizeof(id));

	return id;
}

uint16_t Player::get_len() {
	std::istream is(&b);
	is.read((char*)&str_len, sizeof(str_len));

	got_len = true;

	return str_len;
}

Vector3 Player::get_pos() {
	Vector3 pos;

	std::istream is(&b);
	is.read((char*)&pos.x, sizeof(pos.x));
	is.read((char*)&pos.y, sizeof(pos.y));
	is.read((char*)&pos.z, sizeof(pos.z));

	return pos;
}

std::string Player::get_str() {
	std::string s;

	char *buf = new char[str_len];
	std::istream is(&b);
	is.read(buf, str_len);

	s.assign(buf, str_len);
	delete[] buf;

	return s;
}