diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-08 16:06:22 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-08 16:06:22 +0100 |
commit | c9b9d15a585df8f01b49510e4ff0dfb53ee7b9ef (patch) | |
tree | 4bec517a83bca880c459f36fc99242f902874a87 /common | |
parent | 2b73fe37b292c22dbc823fe9a225dd2826cdff1d (diff) |
Started on payload classes.
Diffstat (limited to 'common')
-rw-r--r-- | common/message.cpp | 49 | ||||
-rw-r--r-- | common/message.h | 28 | ||||
-rw-r--r-- | common/payload.h | 49 |
3 files changed, 103 insertions, 23 deletions
diff --git a/common/message.cpp b/common/message.cpp index 4ddd543..ddfaaa7 100644 --- a/common/message.cpp +++ b/common/message.cpp @@ -1,27 +1,21 @@ #include "message.h" +#include <stdexcept> #include <cstring> #include <iostream> -Message::Message() : deserialize_size(0) { - -} - -Message::p Message::create() { - return Message::p(new Message); -} - std::pair<uint8_t*, std::size_t> Message::serialize() { - std::size_t s = payload.size(); + //std::size_t s = payload.size(); - uint8_t* buf = new uint8_t[4 + s]; + //uint8_t* buf = new uint8_t[4 + s]; - *(uint32_t*)buf = (uint32_t)s; + //*(uint16_t*)buf = (uint16_t)s; - memcpy(buf + 4, payload.c_str(), payload.size()); + //memcpy(buf + 4, payload.c_str(), payload.size()); - return std::pair<uint8_t*, std::size_t>(buf, 4 + s); + //return std::pair<uint8_t*, std::size_t>(buf, 4 + s); + return std::pair<uint8_t*, std::size_t>(0, 0); } std::size_t Message::deserialize(uint8_t* data, std::size_t bytes) { @@ -30,10 +24,35 @@ std::size_t Message::deserialize(uint8_t* data, std::size_t bytes) { } if(deserialize_size == 0 && bytes == 4) { - return (std::size_t)(*(int32_t*)data); + uint16_t* header = (uint16_t*)data; + + deserialize_size = (std::size_t)header[0]; + payload_type = (Payload::Type)header[1]; + + return deserialize_size; + } + + if(!deserialize_size) { + throw std::runtime_error("Deserialization header error."); + } + + if(bytes != deserialize_size) { + throw std::runtime_error("Deserialization attempted with incomplete data."); } - payload = std::string((char*)data, bytes); + //payload = std::string((char*)data, bytes); return 0; } + +Message::Message() : deserialize_size(0) { + +} + +Message::p Message::create() { + return Message::p(new Message); +} + +Payload::Type Message::type() { + return payload_type; +} diff --git a/common/message.h b/common/message.h index 1d6ae8b..44343ae 100644 --- a/common/message.h +++ b/common/message.h @@ -1,6 +1,8 @@ #ifndef MESSAGE_H #define MESSAGE_H +#include "payload.h" + #include <boost/shared_ptr.hpp> #include <stdint.h> @@ -11,14 +13,8 @@ class Message { Message(); std::size_t deserialize_size; - - public: - typedef boost::shared_ptr<Message> p; - static p create(); - - //! Temporary payload. - std::string payload; + friend class ConnectionBase; //! Serialize message. std::pair<uint8_t*, std::size_t> serialize(); @@ -26,6 +22,22 @@ class Message { //! Deserialize message. std::size_t deserialize(uint8_t* data, std::size_t bytes); + Payload::Type payload_type; + Payload::Base* payload_data; + + public: + typedef boost::shared_ptr<Message> p; + + static p create(); + + //! Return payload type. + Payload::Type type(); + + //! Return reference to payload. + template <class T> + T& payload() { + return dynamic_cast<T&>(*payload_data); + } }; -#endif
\ No newline at end of file +#endif diff --git a/common/payload.h b/common/payload.h new file mode 100644 index 0000000..fbdcd1d --- /dev/null +++ b/common/payload.h @@ -0,0 +1,49 @@ +#ifndef PAYLOAD_H +#define PAYLOAD_H + +#include <utility> +#include <stdint.h> + +namespace Payload { + namespace Types { + //! Payload types. + enum Type { + Undefined, + Hello, + Login, + LoginResponse, + LobbyStatus, + LobbyAction, + GameStart, + Ready, + RoundStart, + RoundState, + RoundAction, + RoundEnd, + GameEnd + }; + } + using Types::Type; + + class Base { + protected: + friend class Message; + + virtual std::pair<uint8_t*, std::size_t> serialize() = 0; + + Base(Type t) : type(t) {} + + public: + const Type type; + }; + + class Hello : public Base { + protected: + virtual std::pair<uint8_t*, std::size_t> serialize(); + + public: + Hello() : Base(Types::Hello) {} + }; +}; + +#endif |