summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--messages.cpp71
-rw-r--r--messages.h22
2 files changed, 92 insertions, 1 deletions
diff --git a/messages.cpp b/messages.cpp
index bf50d8c..68e46d2 100644
--- a/messages.cpp
+++ b/messages.cpp
@@ -174,3 +174,74 @@ std::string Message::get_str() {
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;
+}
diff --git a/messages.h b/messages.h
index a476621..d521fef 100644
--- a/messages.h
+++ b/messages.h
@@ -1,6 +1,8 @@
#ifndef MESSAGES_H
#define MESSAGES_H
+#include "vector.h"
+
#include <boost/asio.hpp>
namespace message {
@@ -10,7 +12,8 @@ enum MessageType {
MSG_TYPE_HELLO,
MSG_TYPE_POS,
MSG_TYPE_CHUNK,
- MSG_TYPE_MSG
+ MSG_TYPE_MSG,
+ MSG_TYPE_PLAYER
};
class MessageBase {
@@ -75,6 +78,23 @@ class Message : public MessageBase {
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