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 /server/main.cpp | |
parent | df5932614261a825fb5a9283c321f47f7a198b24 (diff) |
First take on abstract ConnectionBase, ASIO listening and connection handling.
Diffstat (limited to 'server/main.cpp')
-rw-r--r-- | server/main.cpp | 58 |
1 files changed, 2 insertions, 56 deletions
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 <iostream> -#include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/enable_shared_from_this.hpp> #include <boost/asio.hpp> -using boost::asio::ip::tcp; - -class tcp_connection : public boost::enable_shared_from_this<tcp_connection> { - public: - typedef boost::shared_ptr<tcp_connection> 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; |