diff options
Diffstat (limited to 'src/tcpserver.cpp')
-rw-r--r-- | src/tcpserver.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/tcpserver.cpp b/src/tcpserver.cpp new file mode 100644 index 0000000..3f34997 --- /dev/null +++ b/src/tcpserver.cpp @@ -0,0 +1,32 @@ +#include "tcpserver.h" + +#include <boost/bind.hpp> + +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; + } + + connect_callback(connection); +} + +void TCPServer::get_connection(boost::function<void (Connection::p)> f) { + connect_callback = f; + + // Start listening for a connection attempt. + listen(); +} |