From c9b9d15a585df8f01b49510e4ff0dfb53ee7b9ef Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 8 Nov 2010 16:06:22 +0100 Subject: Started on payload classes. --- common/message.cpp | 49 ++++++++++++++++++++++++++++++++++--------------- common/message.h | 28 ++++++++++++++++++++-------- common/payload.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ server/tcpserver.cpp | 2 +- 4 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 common/payload.h 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 #include #include -Message::Message() : deserialize_size(0) { - -} - -Message::p Message::create() { - return Message::p(new Message); -} - std::pair 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(buf, 4 + s); + //return std::pair(buf, 4 + s); + return std::pair(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 #include @@ -11,14 +13,8 @@ class Message { Message(); std::size_t deserialize_size; - - public: - typedef boost::shared_ptr p; - static p create(); - - //! Temporary payload. - std::string payload; + friend class ConnectionBase; //! Serialize message. std::pair 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 p; + + static p create(); + + //! Return payload type. + Payload::Type type(); + + //! Return reference to payload. + template + T& payload() { + return dynamic_cast(*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 +#include + +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 serialize() = 0; + + Base(Type t) : type(t) {} + + public: + const Type type; + }; + + class Hello : public Base { + protected: + virtual std::pair serialize(); + + public: + Hello() : Base(Types::Hello) {} + }; +}; + +#endif diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp index 8f345de..8799648 100644 --- a/server/tcpserver.cpp +++ b/server/tcpserver.cpp @@ -26,7 +26,7 @@ void TCPServer::handle_connection(Connection::p connection, const boost::system: connection->connected(); Message::p m = Message::create(); - m->payload = "Hei morn!"; + //m->payload = "Hei morn!"; connection->send(m); -- cgit v1.2.3