summaryrefslogtreecommitdiff
path: root/messages.cpp
blob: bf50d8c450a27dcbb85b5a685c474c13a4780384 (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
#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;
}