summaryrefslogtreecommitdiff
path: root/messages.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-06-01 19:02:52 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-06-01 19:02:52 +0200
commitdc104e37842049e040f7e39f4e1aab56cde1488c (patch)
tree7a2b98b1007fe7325752ae5a8e728d1410072dfb /messages.cpp
Moved common files here.
Diffstat (limited to 'messages.cpp')
-rw-r--r--messages.cpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/messages.cpp b/messages.cpp
new file mode 100644
index 0000000..7bff612
--- /dev/null
+++ b/messages.cpp
@@ -0,0 +1,129 @@
+#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;
+}