From bf3b20f58d400c3961ce4d162cf314ff3ed4895e Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Tue, 30 Nov 2010 23:44:26 +0100 Subject: Simplified Message API. All messages are now serialized by Boost.Serialize. --- common/connectionbase.cpp | 184 +++++++++++++++++++++++++++++++++------------- common/message.cpp | 148 ------------------------------------- common/message.h | 113 ++++++++++++++-------------- 3 files changed, 189 insertions(+), 256 deletions(-) delete mode 100644 common/message.cpp (limited to 'common') diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp index a113e6a..75a9a9a 100644 --- a/common/connectionbase.cpp +++ b/common/connectionbase.cpp @@ -1,6 +1,15 @@ #include "connectionbase.h" +#include +#include +#include +#include +#include + +#include +#include #include +#include #include @@ -35,73 +44,146 @@ void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { pending_size = 0; - Message::p m; + try { + // Create Boost.Serialization archive of data. + std::istringstream is(std::string((char*)data, bytes)); + boost::archive::binary_iarchive ia(is); + + switch(pending_type) { + case Message::Types::Hello: { + Message::Hello::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::Login: { + Message::Login::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::LoginResponse: { + Message::LoginResponse::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::GameStart: { + Message::GameStart::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::Ready: { + Message::Ready::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::RoundStart: { + Message::RoundStart::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::RoundState: { + Message::RoundState::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::RoundAction: { + Message::RoundAction::p m = make_shared(); + ia & m; + got_message(m); + } break; + + case Message::Types::RoundEnd: { + Message::RoundEnd::p m = make_shared(); + ia & m; + got_message(m); + } break; + + default: + throw std::runtime_error("Deserialization attempted on unknown message type."); + } + + } catch(std::exception& e) { + error(std::string("Deserialization failed: ") + e.what()); + } +} + +void ConnectionBase::send(const Message::p& msg) { + // Create Boost.Serialization archive. + std::ostringstream os; + boost::archive::binary_oarchive oa(os); - switch(pending_type) { - case Message::Types::Hello: - m = make_shared(); - break; + switch(msg->type) { + case Message::Types::Hello: { + Message::Hello::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - case Message::Types::Login: - m = make_shared(); - break; + case Message::Types::Login: { + Message::Login::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - case Message::Types::LoginResponse: - m = make_shared(); - break; + case Message::Types::LoginResponse: { + Message::LoginResponse::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - case Message::Types::GameStart: - m = make_shared(); - break; + case Message::Types::GameStart: { + Message::GameStart::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - case Message::Types::Ready: - m = make_shared(); - break; + case Message::Types::Ready: { + Message::Ready::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - case Message::Types::RoundStart: - m = make_shared(); - break; + case Message::Types::RoundStart: { + Message::RoundStart::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - case Message::Types::RoundState: - m = make_shared(); - break; + case Message::Types::RoundState: { + Message::RoundState::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - case Message::Types::RoundAction: - m = make_shared(); - break; - - case Message::Types::RoundEnd: - m = make_shared(); - break; + case Message::Types::RoundAction: { + Message::RoundAction::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - default: - error("Deserialization attempted on unknown message type."); - } - - if(bytes) { - try { - m->deserialize(data, bytes); + case Message::Types::RoundEnd: { + Message::RoundEnd::p m = dynamic_pointer_cast(msg); + oa & m; + } break; - } catch(std::exception& e) { - error(std::string("Deserialization failed: ") + e.what()); - } + default: + throw std::runtime_error("Serialization attempted on unknown message type."); } - got_message(m); -} - -void ConnectionBase::send(const Message::p& msg) { - uint16_t* header = new uint16_t[2]; + // Extract data. + std::string s = os.str(); - std::pair smsg = msg->serialize(); + // Allocate buffer. + uint8_t* buf = new uint8_t[4 + s.size()]; + // Write header. + uint16_t* header = (uint16_t*)buf; header[0] = uint16_t(msg->type); - header[1] = uint16_t(smsg.second); + header[1] = uint16_t(s.size()); - write_data((uint8_t*)header, 4); - if(smsg.second) { - write_data(smsg.first, smsg.second); - } + // Copy data. + memcpy(buf + 4, s.c_str(), s.size()); + + // Send buffer. + write_data(buf, 4 + s.size()); } ConnectionBase::ConnectionBase() { diff --git a/common/message.cpp b/common/message.cpp deleted file mode 100644 index 5c5ab50..0000000 --- a/common/message.cpp +++ /dev/null @@ -1,148 +0,0 @@ -#include "message.h" - -#include -#include -#include -#include -#include - -#include -#include - -Message::Base::Base(Type t) : type(t) { - -} - -Message::NullBase::NullBase(Type t) : Base(t) { - -} - -std::pair Message::NullBase::serialize() { - return std::pair(0, 0); -} - -void Message::NullBase::deserialize(uint8_t* data, std::size_t bytes) { - -} - -template -Message::BoostBase::BoostBase(Type t) : Base(t) { - -} - -template -std::pair Message::BoostBase::serialize() { - std::ostringstream os; - boost::archive::binary_oarchive oa(os); - - oa & *this; - - std::size_t s = os.str().size(); - uint8_t* buf = new uint8_t[s]; - - memcpy(buf, os.str().c_str(), s); - - return std::pair(buf, s); -} - -template -void Message::BoostBase::deserialize(uint8_t* data, std::size_t bytes) { - std::istringstream is(std::string((char*)data, bytes)); - boost::archive::binary_iarchive ia(is); - - ia & *this; -} - -Message::Hello::Hello() : Base(Types::Hello) { - -} - -Message::Hello::Hello(const std::string& v) : Base(Types::Hello), version(v) { - -} - -std::pair Message::Hello::serialize() { - std::size_t s = version.size(); - uint8_t* buf = new uint8_t[s]; - - memcpy(buf, version.c_str(), version.size()); - - return std::pair(buf, s); -} - -void Message::Hello::deserialize(uint8_t* data, std::size_t bytes) { - version = std::string((char*)data, bytes); -} - -Message::Login::Login() : Base(Types::Login) { - -} - -Message::Login::Login(const std::string& n) : Base(Types::Login), nick(n) { - -} - -std::pair Message::Login::serialize() { - std::size_t s = nick.size(); - uint8_t* buf = new uint8_t[s]; - - memcpy(buf, nick.c_str(), nick.size()); - - return std::pair(buf, s); -} - -void Message::Login::deserialize(uint8_t* data, std::size_t bytes) { - nick = std::string((char*)data, bytes); -} - -Message::LoginResponse::LoginResponse() : Base(Types::LoginResponse) { - -} - -Message::LoginResponse::LoginResponse(bool ok) : Base(Types::LoginResponse), login_ok(ok) { - -} - -std::pair Message::LoginResponse::serialize() { - uint8_t* buf = new uint8_t[1]; - - buf[0] = uint8_t(login_ok); - - return std::pair(buf, 1); -} - -void Message::LoginResponse::deserialize(uint8_t* data, std::size_t bytes) { - login_ok = bool(data[0]); -} - -Message::GameStart::GameStart() : BoostBase(Types::GameStart) { - -} - -Message::Ready::Ready() : NullBase(Types::Ready) { - -} - -Message::RoundStart::RoundStart() : NullBase(Types::RoundStart) { - -} - -Message::RoundState::RoundState() : BoostBase(Types::RoundState) { - -} - -Message::RoundState::RoundState(const Player& pl_d, const Player& pl_r, const Player& pl_u, const Player& pl_l, const Tiles& d, const Actions& a) - : BoostBase(Types::RoundState), dora(d), possible_actions(a) { - players[0] = pl_d; - players[1] = pl_r; - players[2] = pl_u; - players[3] = pl_l; -} - -Message::RoundAction::RoundAction() : BoostBase(Types::RoundAction) { - -} - -Message::RoundEnd::RoundEnd() : NullBase(Types::RoundEnd) { - -} diff --git a/common/message.h b/common/message.h index 1c9276d..d2eb5d7 100644 --- a/common/message.h +++ b/common/message.h @@ -36,109 +36,96 @@ namespace Message { class Base { protected: - Base(Type t); + Base(Type t) : type(t) {} + virtual ~Base() {} public: const Type type; - - virtual std::pair serialize() = 0; - virtual void deserialize(uint8_t* data, std::size_t bytes) = 0; - }; - - class NullBase : public Base { - protected: - NullBase(Type t); - - public: - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); - }; - - template - class BoostBase : public Base { - protected: - BoostBase(Type t); - - public: - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); - - template - void serialize(Archive & ar, const unsigned int version) { - ar & dynamic_cast(*this); - } }; class Hello : public Base { public: typedef boost::shared_ptr p; - Hello(); - Hello(const std::string& v); + Hello(const std::string& v = "") : Base(Types::Hello), version(v) {} std::string version; - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); + template + void serialize(Archive & ar, const unsigned int v) { + ar & version; + } }; class Login : public Base { public: typedef boost::shared_ptr p; - Login(); - Login(const std::string& n); + Login(const std::string& n = "") : Base(Types::Login), nick(n) {} std::string nick; - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); + template + void serialize(Archive & ar, const unsigned int v) { + ar & nick; + } }; class LoginResponse : public Base { public: typedef boost::shared_ptr p; - LoginResponse(); - LoginResponse(bool ok); + LoginResponse(bool ok = false) : Base(Types::LoginResponse), login_ok(ok) {} bool login_ok; - virtual std::pair serialize(); - virtual void deserialize(uint8_t* data, std::size_t bytes); + template + void serialize(Archive & ar, const unsigned int v) { + ar & login_ok; + } }; - class GameStart : public BoostBase { + class GameStart : public Base { public: typedef boost::shared_ptr p; - GameStart(); + GameStart() : Base(Types::GameStart) {} std::vector players; int player_id; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & players; ar & player_id; } }; - class Ready : public NullBase { + class Ready : public Base { public: typedef boost::shared_ptr p; - Ready(); + Ready() : Base(Types::Ready) {} + + template + void serialize(Archive & ar, const unsigned int v) { + + } }; - class RoundStart : public NullBase { + class RoundStart : public Base { public: typedef boost::shared_ptr p; - RoundStart(); + RoundStart() : Base(Types::RoundStart) {} + + template + void serialize(Archive & ar, const unsigned int v) { + + } }; - class RoundState : public BoostBase { + class RoundState : public Base { public: typedef boost::shared_ptr p; @@ -152,15 +139,21 @@ namespace Message { Tiles pond; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & hand; ar & open; ar & pond; } }; - RoundState(); - RoundState(const Player& pl_d, const Player& pl_r, const Player& pl_u, const Player& pl_l, const Tiles& d, const Actions& a); + RoundState() : Base(Types::RoundState) {} + RoundState(const Player& pl_d, const Player& pl_r, const Player& pl_u, const Player& pl_l, const Tiles& d, const Actions& a) + : Base(Types::RoundState), dora(d), possible_actions(a) { + players[0] = pl_d; + players[1] = pl_r; + players[2] = pl_u; + players[3] = pl_l; + } Player players[4]; @@ -169,33 +162,39 @@ namespace Message { Actions possible_actions; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & players; ar & dora; ar & possible_actions; } }; - class RoundAction : public BoostBase { + class RoundAction : public Base { public: typedef boost::shared_ptr p; - RoundAction(); + RoundAction() : Base(Types::RoundAction) {} Action action; template - void serialize(Archive & ar, const unsigned int version) { + void serialize(Archive & ar, const unsigned int v) { ar & action; } }; - class RoundEnd : public NullBase { + class RoundEnd : public Base { public: typedef boost::shared_ptr p; - RoundEnd(); + RoundEnd() : Base(Types::RoundEnd) {} + + template + void serialize(Archive & ar, const unsigned int v) { + + } }; + typedef boost::shared_ptr p; }; -- cgit v1.2.3