From d1b97b2ad19d18377f712b4ed14c8a47db375f62 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 6 Nov 2010 05:19:23 +0100 Subject: Added Message-class, interfaced ConnectionBase to Message. --- common/connectionbase.cpp | 35 +++++++++++++++++++++++++++++++++-- common/connectionbase.h | 15 ++++++++++----- common/message.cpp | 26 ++++++++++++++++++++++++++ common/message.h | 25 +++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 common/message.cpp create mode 100644 common/message.h (limited to 'common') diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp index b2147fa..c744646 100644 --- a/common/connectionbase.cpp +++ b/common/connectionbase.cpp @@ -3,14 +3,45 @@ #include #include +void ConnectionBase::prepare_next_message() { + next_message = Message::create(); + + request_data(next_message->deserialize(0, 0)); +} + void ConnectionBase::connected() { std::cout << "Connection established." << std::endl; write_data((uint8_t*)"Hei!\n", 5); - request_data(10); + prepare_next_message(); } void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { - std::cout << "Fikk data: " << std::string((char*)data, bytes) << std::endl; + size_t remaining = next_message->deserialize(data, bytes); + + if(remaining) { + request_data(remaining); + } else { + recv_queue.push(next_message); + + prepare_next_message(); + } +} + +void ConnectionBase::send(const Message::p& msg) { + std::pair smsg = msg->serialize(); + + write_data(smsg.first, smsg.second); +} + +Message::p ConnectionBase::recv() { + if(recv_queue.empty()) { + return Message::p(); + } + + Message::p msg = recv_queue.front(); + recv_queue.pop(); + + return msg; } diff --git a/common/connectionbase.h b/common/connectionbase.h index 1bfc437..8bd56e9 100644 --- a/common/connectionbase.h +++ b/common/connectionbase.h @@ -3,14 +3,19 @@ #include #include +#include -// Poor excuse for a real Message class. -class Message { - public: - typedef Message* p; -}; +#include "message.h" class ConnectionBase { + private: + std::queue recv_queue; + + Message::p next_message; + + //! Create a new message and initiate reception. + void prepare_next_message(); + protected: //! Signal that connection is established and ready to transfer data. void connected(); diff --git a/common/message.cpp b/common/message.cpp new file mode 100644 index 0000000..78731e2 --- /dev/null +++ b/common/message.cpp @@ -0,0 +1,26 @@ +#include "message.h" + +#include +#include + +Message::Message() : deserialize_size(0) { + +} + +Message::p Message::create() { + return Message::p(new Message); +} + +std::pair Message::serialize() { + return std::pair(0, 0); +} + +std::size_t Message::deserialize(uint8_t* data, std::size_t bytes) { + std::cout << "Fikk data (" << bytes << "): " << std::string((char*)data, bytes) << std::endl; + + if(deserialize_size == 0 && bytes == 0) { + return 8; + } + + return 0; +} diff --git a/common/message.h b/common/message.h new file mode 100644 index 0000000..57258c1 --- /dev/null +++ b/common/message.h @@ -0,0 +1,25 @@ +#ifndef MESSAGE_H +#define MESSAGE_H + +#include + +class Message { + private: + Message(); + + std::size_t deserialize_size; + + public: + typedef boost::shared_ptr p; + + static p create(); + + //! Serialize message. + std::pair serialize(); + + //! Deserialize message. + std::size_t deserialize(uint8_t* data, std::size_t bytes); + +}; + +#endif \ No newline at end of file -- cgit v1.2.3