From be08c29dd5e18c41b5c2f05b7178bab90f04e026 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 15 Nov 2010 04:58:54 +0100 Subject: Changed Message-API. Implemented Hello, Login, LoginResponse. --- common/message.h | 107 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 32 deletions(-) (limited to 'common/message.h') 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 +#include +using boost::make_shared; +using boost::dynamic_pointer_cast; #include #include -class Message { - private: - Message(); - - std::size_t deserialize_size; - - friend class ConnectionBase; - - //! Serialize message. - std::pair 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 p; - - static p create(); - - //! Return payload type. - Payload::Type type(); - - //! Return reference to payload. - template - T& payload() { - return dynamic_cast(*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 serialize() = 0; + virtual void deserialize(uint8_t* data, std::size_t bytes) = 0; + }; + + class Hello : public Base { + public: + typedef boost::shared_ptr p; + + Hello(); + Hello(const std::string& v); + + std::string version; + + virtual std::pair serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + + class Login : public Base { + public: + typedef boost::shared_ptr p; + + Login(); + Login(const std::string& n); + + std::string nick; + + virtual std::pair serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + + class LoginResponse : public Base { + public: + typedef boost::shared_ptr p; + + LoginResponse(); + LoginResponse(bool ok); + + bool login_ok; + + virtual std::pair serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + + typedef boost::shared_ptr p; }; #endif -- cgit v1.2.3