diff options
author | Vegard Storheil Eriksen <zyp@sakuya.local> | 2010-11-06 05:19:23 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@sakuya.local> | 2010-11-06 05:19:23 +0100 |
commit | d1b97b2ad19d18377f712b4ed14c8a47db375f62 (patch) | |
tree | 7c418b0a6fb45fb0677e5eeb2d3bb95af3d7b6f7 | |
parent | cfef24ce8541ac3934ecfe7248a33d1099d94dfa (diff) |
Added Message-class, interfaced ConnectionBase to Message.
-rw-r--r-- | common/connectionbase.cpp | 35 | ||||
-rw-r--r-- | common/connectionbase.h | 15 | ||||
-rw-r--r-- | common/message.cpp | 26 | ||||
-rw-r--r-- | common/message.h | 25 | ||||
-rw-r--r-- | server/SConstruct | 2 |
5 files changed, 95 insertions, 8 deletions
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 <iostream> #include <string> +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<uint8_t*, std::size_t> 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 <stdint.h> #include <cstdlib> +#include <queue> -// Poor excuse for a real Message class. -class Message { - public: - typedef Message* p; -}; +#include "message.h" class ConnectionBase { + private: + std::queue<Message::p> 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 <iostream> +#include <string> + +Message::Message() : deserialize_size(0) { + +} + +Message::p Message::create() { + return Message::p(new Message); +} + +std::pair<uint8_t*, std::size_t> Message::serialize() { + return std::pair<uint8_t*, std::size_t>(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 <boost/shared_ptr.hpp> + +class Message { + private: + Message(); + + std::size_t deserialize_size; + + public: + typedef boost::shared_ptr<Message> p; + + static p create(); + + //! Serialize message. + std::pair<uint8_t*, std::size_t> serialize(); + + //! Deserialize message. + std::size_t deserialize(uint8_t* data, std::size_t bytes); + +}; + +#endif
\ No newline at end of file diff --git a/server/SConstruct b/server/SConstruct index fe7244e..8f20dcb 100644 --- a/server/SConstruct +++ b/server/SConstruct @@ -18,6 +18,6 @@ if GetOption('profiling'): Export('env') -env.Program('aotenjoud', Glob('*.cpp') + ['../common/connectionbase.cpp']) +env.Program('aotenjoud', Glob('*.cpp') + Glob('../common/*.cpp')) # vim: syn=python |