From cfef24ce8541ac3934ecfe7248a33d1099d94dfa Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 6 Nov 2010 02:35:39 +0100 Subject: First take on abstract ConnectionBase, ASIO listening and connection handling. --- common/connectionbase.cpp | 16 ++++++++++++++++ common/connectionbase.h | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 common/connectionbase.cpp create mode 100644 common/connectionbase.h (limited to 'common') 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 +#include + +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 +#include + +// 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 -- cgit v1.2.3