From 174ac25d9788e5604ae37cf9048e307a97bbdff8 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 15 Nov 2010 07:01:56 +0100 Subject: Connection API changes. --- common/connectionbase.cpp | 12 +----------- common/connectionbase.h | 9 +++------ server/connection.cpp | 10 ++++++++++ server/connection.h | 9 +++++++++ 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp index 5dadf7d..c269349 100644 --- a/common/connectionbase.cpp +++ b/common/connectionbase.cpp @@ -4,15 +4,7 @@ #include -void ConnectionBase::prepare_next_message() { - //next_message = make_shared(); - - //request_data(next_message->deserialize(0, 0)); -} - -void ConnectionBase::connected() { - std::cout << "Connection established." << std::endl; - +void ConnectionBase::start_recv() { request_data(4); } @@ -67,8 +59,6 @@ void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { } got_message(m); - - request_data(4); } void ConnectionBase::send(const Message::p& msg) { diff --git a/common/connectionbase.h b/common/connectionbase.h index 3e64551..ef09f83 100644 --- a/common/connectionbase.h +++ b/common/connectionbase.h @@ -11,13 +11,10 @@ class ConnectionBase { private: Message::Type pending_type; std::size_t pending_size; - - //! Create a new message and initiate reception. - void prepare_next_message(); - + protected: - //! Signal that connection is established and ready to transfer data. - void connected(); + //! Initiate reception of a message. + void start_recv(); //! Deliver received data. //! \param data Pointer to received data. Ownership is retained by caller. diff --git a/server/connection.cpp b/server/connection.cpp index 32b24da..f7315b2 100644 --- a/server/connection.cpp +++ b/server/connection.cpp @@ -29,6 +29,10 @@ void Connection::request_data(std::size_t bytes) { // boost::asio::placeholders::error } +void Connection::got_message(const Message::p& msg) { + recv_callback(msg); +} + void Connection::write_data(uint8_t* data, std::size_t bytes) { boost::asio::async_write(socket, boost::asio::buffer(data, bytes), boost::bind(&Connection::handle_write, shared_from_this())); @@ -37,3 +41,9 @@ void Connection::write_data(uint8_t* data, std::size_t bytes) { Connection::p Connection::create(boost::asio::io_service& io_service) { return Connection::p(new Connection(io_service)); } + +void Connection::recv(boost::function f) { + recv_callback = f; + + start_recv(); +} \ No newline at end of file diff --git a/server/connection.h b/server/connection.h index b6a3cd3..3428b88 100644 --- a/server/connection.h +++ b/server/connection.h @@ -3,6 +3,7 @@ #include #include +#include #include "../common/connectionbase.h" @@ -12,6 +13,8 @@ class Connection : public ConnectionBase, public boost::enable_shared_from_this< boost::asio::ip::tcp::socket socket; + boost::function recv_callback; + Connection(boost::asio::io_service& io_service); //! Callback for when data is read. @@ -26,12 +29,18 @@ class Connection : public ConnectionBase, public boost::enable_shared_from_this< //! Implements write_data(). virtual void write_data(uint8_t* data, std::size_t bytes); + + //! Implements got_message(). + virtual void got_message(const Message::p& msg); public: typedef boost::shared_ptr p; //! Constructs a new instance and returns a shared pointer. static p create(boost::asio::io_service& io_service); + + //! Initiates an asynchronous message receive. + void recv(boost::function f); }; #endif -- cgit v1.2.3