diff options
Diffstat (limited to 'common/message.h')
-rw-r--r-- | common/message.h | 107 |
1 files changed, 75 insertions, 32 deletions
diff --git a/common/message.h b/common/message.h index 44343ae..56096ed 100644 --- a/common/message.h +++ b/common/message.h @@ -1,43 +1,86 @@ #ifndef MESSAGE_H #define MESSAGE_H -#include "payload.h" - #include <boost/shared_ptr.hpp> +#include <boost/make_shared.hpp> +using boost::make_shared; +using boost::dynamic_pointer_cast; #include <stdint.h> #include <string> -class Message { - private: - Message(); - - std::size_t deserialize_size; - - friend class ConnectionBase; - - //! Serialize message. - std::pair<uint8_t*, std::size_t> serialize(); - - //! 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); - } +namespace Message { + namespace Types { + //! Message types. + enum Type { + Undefined, + Hello, + Login, + LoginResponse, + LobbyStatus, + LobbyAction, + GameStart, + Ready, + RoundStart, + RoundState, + RoundAction, + RoundEnd, + GameEnd + }; + } + using Types::Type; + + class Base { + protected: + Base(Type t) : type(t) {} + + public: + const Type type; + + virtual std::pair<uint8_t*, std::size_t> serialize() = 0; + virtual void deserialize(uint8_t* data, std::size_t bytes) = 0; + }; + + class Hello : public Base { + public: + typedef boost::shared_ptr<Hello> p; + + Hello(); + Hello(const std::string& v); + + std::string version; + + virtual std::pair<uint8_t*, std::size_t> serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + + class Login : public Base { + public: + typedef boost::shared_ptr<Login> p; + + Login(); + Login(const std::string& n); + + std::string nick; + + virtual std::pair<uint8_t*, std::size_t> serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + + class LoginResponse : public Base { + public: + typedef boost::shared_ptr<LoginResponse> p; + + LoginResponse(); + LoginResponse(bool ok); + + bool login_ok; + + virtual std::pair<uint8_t*, std::size_t> serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + + typedef boost::shared_ptr<Base> p; }; #endif |