From 3adeaee96ab80cc6552798ad3f5812e934b45677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be> Date: Thu, 16 Jul 2009 18:45:48 +0200 Subject: Moved BoostConnectionServer to Swiften. diff --git a/Limber/main.cpp b/Limber/main.cpp index 5d1bb6a..c478924 100644 --- a/Limber/main.cpp +++ b/Limber/main.cpp @@ -1,15 +1,11 @@ -#include <iostream> #include <string> -#include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> -#include <boost/enable_shared_from_this.hpp> #include "Swiften/Elements/IQ.h" #include "Swiften/Elements/RosterPayload.h" #include "Swiften/Elements/VCard.h" #include "Swiften/Server/SimpleUserRegistry.h" -#include "Swiften/Base/ByteArray.h" #include "Swiften/Base/IDGenerator.h" #include "Swiften/EventLoop/MainEventLoop.h" #include "Swiften/EventLoop/SimpleEventLoop.h" @@ -18,43 +14,17 @@ #include "Swiften/Network/ConnectionServer.h" #include "Swiften/Network/BoostConnection.h" #include "Swiften/Network/BoostIOServiceThread.h" +#include "Swiften/Network/BoostConnectionServer.h" #include "Swiften/Server/ServerFromClientSession.h" #include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h" #include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h" using namespace Swift; -class BoostConnectionServer : public ConnectionServer, public EventOwner, public boost::enable_shared_from_this<BoostConnectionServer> { - public: - BoostConnectionServer(int port, boost::asio::io_service& ioService) : acceptor_(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) { - } - - void start() { - acceptNextConnection(); - } - - private: - void 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 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(); - } - } - - boost::asio::ip::tcp::acceptor acceptor_; -}; - class Server { public: Server(UserRegistry* userRegistry) : userRegistry_(userRegistry) { - serverFromClientConnectionServer_ = boost::shared_ptr<BoostConnectionServer>(new BoostConnectionServer(5224, boostIOServiceThread_.getIOService())); + serverFromClientConnectionServer_ = boost::shared_ptr<BoostConnectionServer>(new BoostConnectionServer(5224, &boostIOServiceThread_.getIOService())); serverFromClientConnectionServer_->onNewConnection.connect(boost::bind(&Server::handleNewConnection, this, _1)); serverFromClientConnectionServer_->start(); } 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_; -- cgit v0.10.2-6-g49f6