diff options
Diffstat (limited to 'Swiften/Network')
-rw-r--r-- | Swiften/Network/BoostConnectionServer.cpp | 31 | ||||
-rw-r--r-- | Swiften/Network/BoostConnectionServer.h | 25 | ||||
-rw-r--r-- | Swiften/Network/Makefile.inc | 1 | ||||
-rw-r--r-- | Swiften/Network/Timer.cpp | 6 |
4 files changed, 60 insertions, 3 deletions
diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp new file mode 100644 index 0000000..18a3ca4 --- /dev/null +++ b/Swiften/Network/BoostConnectionServer.cpp @@ -0,0 +1,31 @@ +#include "Swiften/Network/BoostConnectionServer.h" + +#include <boost/bind.hpp> + +#include "Swiften/EventLoop/MainEventLoop.h" + +namespace Swift { + +BoostConnectionServer::BoostConnectionServer(int port, boost::asio::io_service* ioService) : acceptor_(*ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) { +} + + +void BoostConnectionServer::start() { + acceptNextConnection(); +} + +void BoostConnectionServer::acceptNextConnection() { + boost::shared_ptr<BoostConnection> newConnection(new BoostConnection(&acceptor_.io_service())); + acceptor_.async_accept(newConnection->getSocket(), + boost::bind(&BoostConnectionServer::handleAccept, shared_from_this(), newConnection, boost::asio::placeholders::error)); +} + +void BoostConnectionServer::handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error) { + if (!error) { + MainEventLoop::postEvent(boost::bind(boost::ref(onNewConnection), newConnection), shared_from_this()); + newConnection->listen(); + acceptNextConnection(); + } +} + +} diff --git a/Swiften/Network/BoostConnectionServer.h b/Swiften/Network/BoostConnectionServer.h new file mode 100644 index 0000000..c92318e --- /dev/null +++ b/Swiften/Network/BoostConnectionServer.h @@ -0,0 +1,25 @@ +#pragma once + +#include <boost/shared_ptr.hpp> +#include <boost/enable_shared_from_this.hpp> +#include <boost/asio.hpp> + +#include "Swiften/Network/BoostConnection.h" +#include "Swiften/Network/ConnectionServer.h" +#include "Swiften/EventLoop/EventOwner.h" + +namespace Swift { + class BoostConnectionServer : public ConnectionServer, public EventOwner, public boost::enable_shared_from_this<BoostConnectionServer> { + public: + BoostConnectionServer(int port, boost::asio::io_service* ioService); + + void start(); + + private: + void acceptNextConnection(); + void handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error); + + private: + boost::asio::ip::tcp::acceptor acceptor_; + }; +} diff --git a/Swiften/Network/Makefile.inc b/Swiften/Network/Makefile.inc index d8c1ee9..94a8727 100644 --- a/Swiften/Network/Makefile.inc +++ b/Swiften/Network/Makefile.inc @@ -4,6 +4,7 @@ SWIFTEN_SOURCES += \ Swiften/Network/ConnectionFactory.cpp \ Swiften/Network/BoostConnection.cpp \ Swiften/Network/BoostConnectionFactory.cpp \ + Swiften/Network/BoostConnectionServer.cpp \ Swiften/Network/ConnectionServer.cpp \ Swiften/Network/BoostIOServiceThread.cpp \ Swiften/Network/Timer.cpp diff --git a/Swiften/Network/Timer.cpp b/Swiften/Network/Timer.cpp index 8999113..f3b296c 100644 --- a/Swiften/Network/Timer.cpp +++ b/Swiften/Network/Timer.cpp @@ -8,13 +8,13 @@ namespace Swift { Timer::Timer(int milliseconds) : timeout_(milliseconds), ioService_(0), thread_(0), timer_(0) { - ioService_ = new boost::asio::io_service(); + ioService_ = new boost::asio::io_service(); } Timer::~Timer() { //MainEventLoop::removeEventsFromOwner(shared_from_this()); - ioService_->stop(); - thread_->join(); + ioService_->stop(); + thread_->join(); delete timer_; delete thread_; delete ioService_; |