diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-15 12:14:30 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-15 12:14:30 +0100 |
commit | 2c6d82abe3c82041991066ca84ac067771756818 (patch) | |
tree | 75ce382a860bda58e913952a0bbdcd917864c014 /common | |
parent | 129781a715f88c9398a5716af1aa1203b1fee89a (diff) |
Added Message::NullBase and Message::BoostBase. Edited Message::GameStart to contain list of players.
Diffstat (limited to 'common')
-rw-r--r-- | common/message.cpp | 65 | ||||
-rw-r--r-- | common/message.h | 40 |
2 files changed, 83 insertions, 22 deletions
diff --git a/common/message.cpp b/common/message.cpp index 526a3fd..a72bac9 100644 --- a/common/message.cpp +++ b/common/message.cpp @@ -1,6 +1,51 @@ #include "message.h" +#include <boost/serialization/vector.hpp> +#include <boost/serialization/string.hpp> + #include <cstring> +#include <sstream> + +Message::Base::Base(Type t) : type(t) { + +} + +Message::NullBase::NullBase(Type t) : Base(t) { + +} + +std::pair<uint8_t*, std::size_t> Message::NullBase::serialize() { + return std::pair<uint8_t*, std::size_t>(0, 0); +} + +void Message::NullBase::deserialize(uint8_t* data, std::size_t bytes) { + +} + +Message::BoostBase::BoostBase(Type t) : Base(t) { + +} + +std::pair<uint8_t*, std::size_t> Message::BoostBase::serialize() { + std::ostringstream os; + boost::archive::text_oarchive oa(os); + + serialize(oa); + + std::size_t s = os.str().size(); + uint8_t* buf = new uint8_t[s]; + + memcpy(buf, os.str().c_str(), s); + + return std::pair<uint8_t*, std::size_t>(buf, s); +} + +void Message::BoostBase::deserialize(uint8_t* data, std::size_t bytes) { + std::istringstream is(std::string((char*)data, bytes)); + boost::archive::text_iarchive ia(is); + + deserialize(ia); +} Message::Hello::Hello() : Base(Types::Hello) { @@ -64,26 +109,18 @@ void Message::LoginResponse::deserialize(uint8_t* data, std::size_t bytes) { login_ok = bool(data[0]); } -Message::GameStart::GameStart() : Base(Types::GameStart) { +Message::GameStart::GameStart() : BoostBase(Types::GameStart) { } -std::pair<uint8_t*, std::size_t> Message::GameStart::serialize() { - return std::pair<uint8_t*, std::size_t>(0, 0); -} - -void Message::GameStart::deserialize(uint8_t* data, std::size_t bytes) { - +void Message::GameStart::serialize(boost::archive::text_oarchive& ar) { + ar & players; } -Message::Ready::Ready() : Base(Types::Ready) { - -} - -std::pair<uint8_t*, std::size_t> Message::Ready::serialize() { - return std::pair<uint8_t*, std::size_t>(0, 0); +void Message::GameStart::deserialize(boost::archive::text_iarchive& ar) { + ar & players; } -void Message::Ready::deserialize(uint8_t* data, std::size_t bytes) { +Message::Ready::Ready() : NullBase(Types::Ready) { } diff --git a/common/message.h b/common/message.h index b2d3078..bd68efe 100644 --- a/common/message.h +++ b/common/message.h @@ -6,7 +6,11 @@ using boost::make_shared; using boost::dynamic_pointer_cast; +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + #include <stdint.h> +#include <vector> #include <string> namespace Message { @@ -32,7 +36,7 @@ namespace Message { class Base { protected: - Base(Type t) : type(t) {} + Base(Type t); public: const Type type; @@ -41,6 +45,27 @@ namespace Message { virtual void deserialize(uint8_t* data, std::size_t bytes) = 0; }; + class NullBase : public Base { + protected: + NullBase(Type t); + + public: + virtual std::pair<uint8_t*, std::size_t> serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + + class BoostBase : public Base { + protected: + BoostBase(Type t); + + public: + virtual std::pair<uint8_t*, std::size_t> serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + + virtual void serialize(boost::archive::text_oarchive& ar) = 0; + virtual void deserialize(boost::archive::text_iarchive& ar) = 0; + }; + class Hello : public Base { public: typedef boost::shared_ptr<Hello> p; @@ -80,24 +105,23 @@ namespace Message { virtual void deserialize(uint8_t* data, std::size_t bytes); }; - class GameStart : public Base { + class GameStart : public BoostBase { public: typedef boost::shared_ptr<LoginResponse> p; GameStart(); - virtual std::pair<uint8_t*, std::size_t> serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); + std::vector<std::string> players; + + virtual void serialize(boost::archive::text_oarchive& ar); + virtual void deserialize(boost::archive::text_iarchive& ar); }; - class Ready : public Base { + class Ready : public NullBase { public: typedef boost::shared_ptr<Ready> p; Ready(); - - 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; |