diff options
author | Vegard Storheil Eriksen <zyp@sakuya.local> | 2010-11-06 02:35:39 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@sakuya.local> | 2010-11-06 02:35:39 +0100 |
commit | cfef24ce8541ac3934ecfe7248a33d1099d94dfa (patch) | |
tree | 4335e25b6fd490fa8744fed82aa7273fc7a72395 /common | |
parent | df5932614261a825fb5a9283c321f47f7a198b24 (diff) |
First take on abstract ConnectionBase, ASIO listening and connection handling.
Diffstat (limited to 'common')
-rw-r--r-- | common/connectionbase.cpp | 16 | ||||
-rw-r--r-- | common/connectionbase.h | 40 |
2 files changed, 56 insertions, 0 deletions
diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp new file mode 100644 index 0000000..b2147fa --- /dev/null +++ b/common/connectionbase.cpp @@ -0,0 +1,16 @@ +#include "connectionbase.h" + +#include <iostream> +#include <string> + +void ConnectionBase::connected() { + std::cout << "Connection established." << std::endl; + + write_data((uint8_t*)"Hei!\n", 5); + + request_data(10); +} + +void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { + std::cout << "Fikk data: " << std::string((char*)data, bytes) << std::endl; +} diff --git a/common/connectionbase.h b/common/connectionbase.h new file mode 100644 index 0000000..1bfc437 --- /dev/null +++ b/common/connectionbase.h @@ -0,0 +1,40 @@ +#ifndef CONNECTIONBASE_H +#define CONNECTIONBASE_H + +#include <stdint.h> +#include <cstdlib> + +// Poor excuse for a real Message class. +class Message { + public: + typedef Message* p; +}; + +class ConnectionBase { + protected: + //! Signal that connection is established and ready to transfer data. + void connected(); + + //! Deliver received data. + //! \param data Pointer to received data. Ownership is retained by caller. + //! \param bytes Size of data. + void got_data(uint8_t* data, std::size_t bytes); + + //! Called to request data. + //! \param bytes Amount of data requested. + virtual void request_data(std::size_t bytes) = 0; + + //! Called to write data. + //! \param data Pointer to data to send. Ownership is transferred to callee. + //! \param bytes Size of data. + virtual void write_data(uint8_t* data, std::size_t bytes) = 0; + + public: + //! Send a message. + void send(const Message::p& msg); + + //! Get received message or null if queue empty. + Message::p recv(); +}; + +#endif |