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

#include "vector.h"

#include <boost/asio.hpp>

namespace message {

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

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

class Message : public MessageBase {
	protected:
		uint16_t str_len;
		bool got_len;

	public:
		Message();
		Message(std::string msg);

		virtual std::size_t payload_size();
		uint16_t get_len();
		std::string get_str();
};

class Player : public MessageBase {
	protected:
		uint32_t id;
		uint16_t str_len;
		bool got_len;

	public:
		Player();
		Player(uint32_t id, Vector3 pos, std::string name);

		virtual std::size_t payload_size();
		uint32_t get_id();
		Vector3 get_pos();
		uint16_t get_len();
		std::string get_str();
};

}

#endif