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. --- server/SConstruct | 8 +++---- server/connection.cpp | 35 +++++++++++++++++++++++++++++++ server/connection.h | 38 +++++++++++++++++++++++++++++++++ server/main.cpp | 58 ++------------------------------------------------- server/tcpserver.cpp | 28 +++++++++++++++++++++++++ server/tcpserver.h | 22 +++++++++++++++++++ 6 files changed, 129 insertions(+), 60 deletions(-) create mode 100644 server/connection.cpp create mode 100644 server/connection.h create mode 100644 server/tcpserver.cpp create mode 100644 server/tcpserver.h (limited to 'server') diff --git a/server/SConstruct b/server/SConstruct index d1def28..fe7244e 100644 --- a/server/SConstruct +++ b/server/SConstruct @@ -10,14 +10,14 @@ AddOption('--profiling', action = 'store_true') env.Append(LIBS = ['boost_system', 'pthread']) if not GetOption('release'): - env.Append(CPPFLAGS = ['-Wall', '-g']) + env.Append(CPPFLAGS = ['-Wall', '-g']) if GetOption('profiling'): - env.Append(CPPFLAGS = ['-pg']) - env.Append(LINKFLAGS = ['-pg']) + env.Append(CPPFLAGS = ['-pg']) + env.Append(LINKFLAGS = ['-pg']) Export('env') -env.Program('aotenjoud', Glob('*.cpp')) +env.Program('aotenjoud', Glob('*.cpp') + ['../common/connectionbase.cpp']) # vim: syn=python diff --git a/server/connection.cpp b/server/connection.cpp new file mode 100644 index 0000000..f846d3b --- /dev/null +++ b/server/connection.cpp @@ -0,0 +1,35 @@ +#include "connection.h" + +#include + +Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) { + +} + +void Connection::handle_read(uint8_t* data, std::size_t bytes) { + got_data(data, bytes); + + delete[] data; +} + +void Connection::handle_write() { + +} + +void Connection::request_data(std::size_t bytes) { + uint8_t* buf = new uint8_t[bytes]; + + boost::asio::async_read(socket, boost::asio::buffer(buf, bytes), + boost::bind(&Connection::handle_read, shared_from_this(), buf, bytes)); + + // boost::asio::placeholders::error +} + +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())); +} + +Connection::p Connection::create(boost::asio::io_service& io_service) { + return Connection::p(new Connection(io_service)); +} diff --git a/server/connection.h b/server/connection.h new file mode 100644 index 0000000..aba7734 --- /dev/null +++ b/server/connection.h @@ -0,0 +1,38 @@ +#ifndef CONNECTION_H +#define CONNECTION_H + +#include +#include + +#include "../common/connectionbase.h" + +class Connection : public ConnectionBase, public boost::enable_shared_from_this { + private: + friend class TCPServer; + + boost::asio::ip::tcp::socket socket; + + Connection(boost::asio::io_service& io_service); + + private: + //! Callback for when data is read. + void handle_read(uint8_t* data, std::size_t bytes); + + //! Callback for when data is written. + void handle_write(); + + protected: + //! Implements request_data(). + virtual void request_data(std::size_t bytes); + + //! Implements write_data(). + virtual void write_data(uint8_t* data, std::size_t bytes); + + public: + typedef boost::shared_ptr p; + + //! Constructs a new instance and returns a shared pointer. + static p create(boost::asio::io_service& io_service); +}; + +#endif diff --git a/server/main.cpp b/server/main.cpp index 9341623..3e311e5 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -1,67 +1,13 @@ #include -#include -#include -#include #include -using boost::asio::ip::tcp; - -class tcp_connection : public boost::enable_shared_from_this { - public: - typedef boost::shared_ptr pointer; - - static pointer create(boost::asio::io_service& io_service) { - return pointer(new tcp_connection(io_service)); - } - - tcp::socket& socket() { - return socket_; - } - - void start() { - boost::asio::async_write(socket_, boost::asio::buffer("Hei!\n"), - boost::bind(&tcp_connection::handle_write, shared_from_this())); - } - - private: - tcp::socket socket_; - - tcp_connection(boost::asio::io_service& io_service) : socket_(io_service) { - } - - void handle_write() { - } -}; - -class tcp_server { - public: - tcp_server(boost::asio::io_service& io_service) - : acceptor_(io_service, tcp::endpoint(tcp::v4(), 12345)) { - start_accept(); - } - - private: - tcp::acceptor acceptor_; - - void start_accept() { - tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.io_service()); - - acceptor_.async_accept(new_connection->socket(), boost::bind(&tcp_server::handle_accept, this, new_connection, boost::asio::placeholders::error)); - } - - void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code& error) { - if(!error) { - new_connection->start(); - start_accept(); - } - } -}; +#include "tcpserver.h" int main() { try { boost::asio::io_service io_service; - tcp_server server(io_service); + TCPServer server(io_service); std::cout << "Listening to port 12345" << std::endl; diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp new file mode 100644 index 0000000..528d1a2 --- /dev/null +++ b/server/tcpserver.cpp @@ -0,0 +1,28 @@ +#include "tcpserver.h" + +#include + +TCPServer::TCPServer(boost::asio::io_service& io_service) + : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 12345)) { + + // Start listening for first connection attempt. + listen(); +} + +void TCPServer::listen() { + Connection::p new_connection = Connection::create(acceptor_.io_service()); + + acceptor_.async_accept(new_connection->socket, + boost::bind(&TCPServer::handle_connection, this, new_connection, boost::asio::placeholders::error)); +} + +void TCPServer::handle_connection(Connection::p connection, const boost::system::error_code& error) { + if(error) { + return; + } + + connection->connected(); + + // Start listening for another connection attempt. + listen(); +} \ No newline at end of file diff --git a/server/tcpserver.h b/server/tcpserver.h new file mode 100644 index 0000000..5d0cbba --- /dev/null +++ b/server/tcpserver.h @@ -0,0 +1,22 @@ +#ifndef TCPSERVER_H +#define TCPSERVER_H + +#include + +#include "connection.h" + +class TCPServer { + private: + boost::asio::ip::tcp::acceptor acceptor_; + + //! Listen for incoming connection. + void listen(); + + //! Handle new connection. + void handle_connection(Connection::p connection, const boost::system::error_code& error); + + public: + TCPServer(boost::asio::io_service& io_service); +}; + +#endif -- cgit v1.2.3