summaryrefslogtreecommitdiff
path: root/common/message.cpp
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-11-15 04:58:54 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-11-15 04:58:54 +0100
commitbe08c29dd5e18c41b5c2f05b7178bab90f04e026 (patch)
tree98ad4c1bb0884df8345fa2f6ef0426b0929f9a54 /common/message.cpp
parentc9b9d15a585df8f01b49510e4ff0dfb53ee7b9ef (diff)
Changed Message-API. Implemented Hello, Login, LoginResponse.
Diffstat (limited to 'common/message.cpp')
-rw-r--r--common/message.cpp76
1 files changed, 43 insertions, 33 deletions
diff --git a/common/message.cpp b/common/message.cpp
index ddfaaa7..6e1901d 100644
--- a/common/message.cpp
+++ b/common/message.cpp
@@ -5,54 +5,64 @@
#include <iostream>
-std::pair<uint8_t*, std::size_t> Message::serialize() {
- //std::size_t s = payload.size();
+Message::Hello::Hello() : Base(Types::Hello) {
- //uint8_t* buf = new uint8_t[4 + s];
+}
+
+Message::Hello::Hello(const std::string& v) : Base(Types::Hello), version(v) {
- //*(uint16_t*)buf = (uint16_t)s;
+}
+
+std::pair<uint8_t*, std::size_t> Message::Hello::serialize() {
+ std::size_t s = version.size();
+ uint8_t* buf = new uint8_t[s];
- //memcpy(buf + 4, payload.c_str(), payload.size());
+ memcpy(buf, version.c_str(), version.size());
- //return std::pair<uint8_t*, std::size_t>(buf, 4 + s);
- return std::pair<uint8_t*, std::size_t>(0, 0);
+ return std::pair<uint8_t*, std::size_t>(buf, s);
}
-std::size_t Message::deserialize(uint8_t* data, std::size_t bytes) {
- if(deserialize_size == 0 && bytes == 0) {
- return 4;
- }
-
- if(deserialize_size == 0 && bytes == 4) {
- uint16_t* header = (uint16_t*)data;
-
- deserialize_size = (std::size_t)header[0];
- payload_type = (Payload::Type)header[1];
-
- return deserialize_size;
- }
+void Message::Hello::deserialize(uint8_t* data, std::size_t bytes) {
+ version = std::string((char*)data, bytes);
+}
+
+Message::Login::Login() : Base(Types::Login) {
- if(!deserialize_size) {
- throw std::runtime_error("Deserialization header error.");
- }
+}
+
+Message::Login::Login(const std::string& n) : Base(Types::Login), nick(n) {
- if(bytes != deserialize_size) {
- throw std::runtime_error("Deserialization attempted with incomplete data.");
- }
+}
+
+std::pair<uint8_t*, std::size_t> Message::Login::serialize() {
+ std::size_t s = nick.size();
+ uint8_t* buf = new uint8_t[s];
- //payload = std::string((char*)data, bytes);
+ memcpy(buf, nick.c_str(), nick.size());
- return 0;
+ return std::pair<uint8_t*, std::size_t>(buf, s);
}
-Message::Message() : deserialize_size(0) {
+void Message::Login::deserialize(uint8_t* data, std::size_t bytes) {
+ nick = std::string((char*)data, bytes);
+}
+
+Message::LoginResponse::LoginResponse() : Base(Types::LoginResponse) {
+
+}
+
+Message::LoginResponse::LoginResponse(bool ok) : Base(Types::LoginResponse), login_ok(ok) {
}
-Message::p Message::create() {
- return Message::p(new Message);
+std::pair<uint8_t*, std::size_t> Message::LoginResponse::serialize() {
+ uint8_t* buf = new uint8_t[1];
+
+ buf[1] = uint8_t(login_ok);
+
+ return std::pair<uint8_t*, std::size_t>(buf, 1);
}
-Payload::Type Message::type() {
- return payload_type;
+void Message::LoginResponse::deserialize(uint8_t* data, std::size_t bytes) {
+ login_ok = bool(data[0]);
}