summaryrefslogtreecommitdiff
path: root/messages.h
blob: 021c175f6772719f25618580b8a9ba7beb51121d (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
#ifndef MESSAGES_H
#define MESSAGES_H

#include <boost/asio.hpp>

namespace message {

enum MessageType {
	MSG_TYPE_NONE = 0,
	MSG_TYPE_HELLO,
	MSG_TYPE_POS,
	MSG_TYPE_CHUNK
};

class MessageBase {
	protected:
		uint8_t type;
		boost::asio::streambuf b;

	public:
		MessageBase();
		virtual ~MessageBase() {};

		void send(boost::asio::ip::tcp::socket& socket);
		virtual std::size_t payload_size();
		virtual void read(boost::asio::ip::tcp::socket& socket);

		static uint8_t read_type(boost::asio::ip::tcp::socket& socket);
};

class Hello : public MessageBase {
	public:
		Hello();
		Hello(uint8_t version);

		virtual std::size_t payload_size();
		uint8_t read_version();
};

class Pos : public MessageBase {
	public:
		Pos();
		Pos(float x, float y, float z);

		virtual std::size_t payload_size();
		void get_pos(float& x, float& y, float& z);
};

class Chunk : public MessageBase {
	protected:
		bool got_coords;

	public:
		Chunk();
		Chunk(int64_t x, int64_t y);

		virtual std::size_t payload_size();
		void set_data(float *data);
		float* get_data();
		void get_coords(int64_t& x, int64_t& y);
};

}

#endif