summaryrefslogtreecommitdiff
path: root/messages.cpp
blob: 2de0e6edb2262e09f872d092a7f1b2f4e4d3a268 (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
#include "messages.h"

using namespace message;

/* MessageBase */

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

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;
}

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

std::string MessageBase::read_string(boost::asio::ip::tcp::socket& socket) {
	uint16_t str_len;
	read(socket, str_len);

	std::string s;

	char *buf = new char[str_len];
	boost::asio::read(socket, boost::asio::buffer(buf, str_len));

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

	return s;
}

void MessageBase::write_string(boost::asio::ip::tcp::socket& socket, std::string str) {
	uint16_t str_len = str.size();
	write(socket, str_len);

	boost::asio::write(socket, boost::asio::buffer(str));
}

/* Hello */

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

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

	this->version = version;
}

void Hello::do_send(boost::asio::ip::tcp::socket& socket) {
	write(socket, version);
}

void Hello::recv(boost::asio::ip::tcp::socket& socket) {
	read(socket, version);
}

uint8_t Hello::get_version() {
	return version;
}

/* Pos */

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

Pos::Pos(Vector3 pos) {
	type = MSG_TYPE_POS;

	this->id = 0;
	this->pos = pos;
}

Pos::Pos(uint32_t id, Vector3 pos) {
	type = MSG_TYPE_POS;

	this->id = id;
	this->pos = pos;
}

void Pos::do_send(boost::asio::ip::tcp::socket& socket) {
	write(socket, id);
	write(socket, pos);
}

void Pos::recv(boost::asio::ip::tcp::socket& socket) {
	read(socket, id);
	read(socket, pos);
}

uint32_t Pos::get_id() {
	return id;
}

Vector3 Pos::get_pos() {
	return pos;
}

/* Chunk */

Chunk::Chunk() {
	type = MSG_TYPE_CHUNK;
}

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

	this->x = x;
	this->y = y;
}

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

void Chunk::do_send(boost::asio::ip::tcp::socket& socket) {
	write(socket, x);
	write(socket, y);

	boost::asio::write(socket, boost::asio::buffer(data, sizeof(float)*chunk_size_total*chunk_size_total));
}

void Chunk::recv(boost::asio::ip::tcp::socket& socket) {
	read(socket, x);
	read(socket, y);

	// do not free, caller takes ownership
	data = new float[chunk_size_total*chunk_size_total];
	boost::asio::read(socket, boost::asio::buffer(data, sizeof(float)*chunk_size_total*chunk_size_total));
}

void Chunk::set_data(float *data) {
	// caller owns data
	this->data = data;
}

float* Chunk::get_data() {
	return data;
}

void Chunk::get_coords(int64_t& x, int64_t& y) {
	x = this->x;
	y = this->y;
}

/* Message */

Message::Message() {
	type = MSG_TYPE_MSG;
}

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

	this->msg = msg;
}

void Message::do_send(boost::asio::ip::tcp::socket& socket) {
	write_string(socket, msg);
}

void Message::recv(boost::asio::ip::tcp::socket& socket) {
	msg = read_string(socket);
}

std::string Message::get_str() {
	return msg;
}

/* Player */

Player::Player() {
	type = MSG_TYPE_PLAYER;
}

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

	this->id = id;
	this->pos = pos;
	this->name = name;
}

void Player::do_send(boost::asio::ip::tcp::socket& socket) {
	write(socket, id);
	write(socket, pos);
	write_string(socket, name);
}

void Player::recv(boost::asio::ip::tcp::socket& socket) {
	read(socket, id);
	read(socket, pos);
	name = read_string(socket);
}

uint32_t Player::get_id() {
	return id;
}

Vector3 Player::get_pos() {
	return pos;
}

std::string Player::get_name() {
	return name;
}

/* Object */

Object::Object() {
	type = MSG_TYPE_OBJECT;
}

Object::Object(uint32_t obj_type, Vector3 pos) {
	type = MSG_TYPE_OBJECT;

	this->obj_type = obj_type;
	this->pos = pos;
}

void Object::do_send(boost::asio::ip::tcp::socket& socket) {
	write(socket, obj_type);
	write(socket, pos);
}

void Object::recv(boost::asio::ip::tcp::socket& socket) {
	read(socket, obj_type);
	read(socket, pos);
}

uint32_t Object::get_type() {
	return obj_type;
}

Vector3 Object::get_pos() {
	return pos;
}