summaryrefslogtreecommitdiff
path: root/common/message.h
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.h
parentc9b9d15a585df8f01b49510e4ff0dfb53ee7b9ef (diff)
Changed Message-API. Implemented Hello, Login, LoginResponse.
Diffstat (limited to 'common/message.h')
-rw-r--r--common/message.h107
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