summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2015-09-17 18:21:44 (GMT)
committerSwift Review <review@swift.im>2015-12-17 08:58:51 (GMT)
commit9bce26254aa3c2feffdd751b59cdee5e903fd2bc (patch)
tree27bf31b4564216db6bf74818dc76f0ba23af074e
parentbea3559998c0ae0a2afca566046f08d0a201c0b2 (diff)
downloadswift-9bce26254aa3c2feffdd751b59cdee5e903fd2bc.zip
swift-9bce26254aa3c2feffdd751b59cdee5e903fd2bc.tar.bz2
Add event loop for integration in Boost ASIO
This allows execution of events inside an existing io_service if an application is already using Boost ASIO for other things and can share the io_service. Test-Information: Builds on OS X 10.11.2. Change-Id: I092ed7a25b24ef95d4664bae98ed84cc0f149073
-rw-r--r--Swiften/EventLoop/BoostASIOEventLoop.cpp37
-rw-r--r--Swiften/EventLoop/BoostASIOEventLoop.h34
-rw-r--r--Swiften/EventLoop/SConscript5
-rw-r--r--Swiften/Network/BoostIOServiceThread.cpp23
-rw-r--r--Swiften/Network/BoostIOServiceThread.h12
-rw-r--r--Swiften/Network/BoostNetworkFactories.cpp22
-rw-r--r--Swiften/Network/BoostNetworkFactories.h9
7 files changed, 118 insertions, 24 deletions
diff --git a/Swiften/EventLoop/BoostASIOEventLoop.cpp b/Swiften/EventLoop/BoostASIOEventLoop.cpp
new file mode 100644
index 0000000..f3c5618
--- /dev/null
+++ b/Swiften/EventLoop/BoostASIOEventLoop.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <Swiften/EventLoop/BoostASIOEventLoop.h>
+
+#include <boost/bind.hpp>
+
+namespace Swift {
+
+BoostASIOEventLoop::BoostASIOEventLoop(boost::shared_ptr<boost::asio::io_service> ioService) : ioService_(ioService) {
+
+}
+
+BoostASIOEventLoop::~BoostASIOEventLoop() {
+
+}
+
+void BoostASIOEventLoop::handleASIOEvent() {
+ {
+ boost::recursive_mutex::scoped_lock lock(isEventInASIOEventLoopMutex_);
+ isEventInASIOEventLoop_ = false;
+ }
+ handleNextEvent();
+}
+
+void BoostASIOEventLoop::eventPosted() {
+ boost::recursive_mutex::scoped_lock lock(isEventInASIOEventLoopMutex_);
+ if (!isEventInASIOEventLoop_) {
+ isEventInASIOEventLoop_ = true;
+ ioService_->post(boost::bind(&BoostASIOEventLoop::handleASIOEvent, this));
+ }
+}
+
+}
diff --git a/Swiften/EventLoop/BoostASIOEventLoop.h b/Swiften/EventLoop/BoostASIOEventLoop.h
new file mode 100644
index 0000000..a093199
--- /dev/null
+++ b/Swiften/EventLoop/BoostASIOEventLoop.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <boost/asio/io_service.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/thread.hpp>
+
+#include <Swiften/Base/API.h>
+#include <Swiften/EventLoop/Event.h>
+#include <Swiften/EventLoop/EventLoop.h>
+
+namespace Swift {
+ class SWIFTEN_API BoostASIOEventLoop : public EventLoop {
+ public:
+ BoostASIOEventLoop(boost::shared_ptr<boost::asio::io_service> ioService);
+ virtual ~BoostASIOEventLoop();
+
+ protected:
+ void handleASIOEvent();
+
+ virtual void eventPosted();
+
+ private:
+ boost::shared_ptr<boost::asio::io_service> ioService_;
+
+ bool isEventInASIOEventLoop_;
+ boost::recursive_mutex isEventInASIOEventLoopMutex_;
+ };
+}
diff --git a/Swiften/EventLoop/SConscript b/Swiften/EventLoop/SConscript
index d26661c..b810e8c 100644
--- a/Swiften/EventLoop/SConscript
+++ b/Swiften/EventLoop/SConscript
@@ -1,11 +1,12 @@
Import("swiften_env")
sources = [
+ "BoostASIOEventLoop.cpp",
+ "DummyEventLoop.cpp",
+ "Event.cpp",
"EventLoop.cpp",
"EventOwner.cpp",
- "Event.cpp",
"SimpleEventLoop.cpp",
- "DummyEventLoop.cpp",
"SingleThreadedEventLoop.cpp",
]
diff --git a/Swiften/Network/BoostIOServiceThread.cpp b/Swiften/Network/BoostIOServiceThread.cpp
index 6a89c9a..57b2cb4 100644
--- a/Swiften/Network/BoostIOServiceThread.cpp
+++ b/Swiften/Network/BoostIOServiceThread.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -10,18 +10,27 @@
namespace Swift {
-BoostIOServiceThread::BoostIOServiceThread() {
- ioService_ = boost::make_shared<boost::asio::io_service>();
- thread_ = new boost::thread(boost::bind(&BoostIOServiceThread::doRun, this));
+BoostIOServiceThread::BoostIOServiceThread(boost::shared_ptr<boost::asio::io_service> ioService) {
+ if (!!ioService) {
+ ioService_ = ioService;
+ thread_ = NULL;
+ }
+ else {
+ ioService_ = boost::make_shared<boost::asio::io_service>();
+ thread_ = new boost::thread(boost::bind(&BoostIOServiceThread::doRun, this));
+ }
}
BoostIOServiceThread::~BoostIOServiceThread() {
- ioService_->stop();
- thread_->join();
- delete thread_;
+ if (thread_) {
+ ioService_->stop();
+ thread_->join();
+ delete thread_;
+ }
}
void BoostIOServiceThread::doRun() {
+ assert(thread_);
boost::asio::io_service::work work(*ioService_);
ioService_->run();
}
diff --git a/Swiften/Network/BoostIOServiceThread.h b/Swiften/Network/BoostIOServiceThread.h
index 37047d1..428837b 100644
--- a/Swiften/Network/BoostIOServiceThread.h
+++ b/Swiften/Network/BoostIOServiceThread.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -15,7 +15,15 @@
namespace Swift {
class SWIFTEN_API BoostIOServiceThread {
public:
- BoostIOServiceThread();
+ /**
+ * Construct the object.
+ * @param ioService If this optional parameter is provided, the behaviour
+ * of this class changes completely - it no longer spawns its own thread
+ * and instead acts as a simple wrapper of the io_service. Use this if
+ * you are re-using an io_service from elsewhere (particularly if you
+ * are using the BoostASIOEventLoop).
+ */
+ BoostIOServiceThread(boost::shared_ptr<boost::asio::io_service> ioService = boost::shared_ptr<boost::asio::io_service>());
~BoostIOServiceThread();
boost::shared_ptr<boost::asio::io_service> getIOService() const {
diff --git a/Swiften/Network/BoostNetworkFactories.cpp b/Swiften/Network/BoostNetworkFactories.cpp
index 4ab42d9..871a38b 100644
--- a/Swiften/Network/BoostNetworkFactories.cpp
+++ b/Swiften/Network/BoostNetworkFactories.cpp
@@ -1,24 +1,24 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Network/BoostNetworkFactories.h>
-#include <Swiften/Network/BoostTimerFactory.h>
-#include <Swiften/Network/BoostConnectionFactory.h>
+#include <Swiften/Crypto/CryptoProvider.h>
+#include <Swiften/Crypto/PlatformCryptoProvider.h>
+#include <Swiften/IDN/IDNConverter.h>
+#include <Swiften/IDN/PlatformIDNConverter.h>
+#include <Swiften/Network/BoostConnectionFactory.h>
#include <Swiften/Network/BoostConnectionServerFactory.h>
-#include <Swiften/Network/PlatformNATTraversalWorker.h>
-#include <Swiften/Parser/PlatformXMLParserFactory.h>
+#include <Swiften/Network/BoostTimerFactory.h>
#include <Swiften/Network/NullNATTraverser.h>
+#include <Swiften/Network/PlatformNATTraversalWorker.h>
#include <Swiften/Network/PlatformNetworkEnvironment.h>
-#include <Swiften/TLS/PlatformTLSFactories.h>
#include <Swiften/Network/PlatformProxyProvider.h>
-#include <Swiften/IDN/PlatformIDNConverter.h>
-#include <Swiften/IDN/IDNConverter.h>
-#include <Swiften/Crypto/PlatformCryptoProvider.h>
-#include <Swiften/Crypto/CryptoProvider.h>
+#include <Swiften/Parser/PlatformXMLParserFactory.h>
+#include <Swiften/TLS/PlatformTLSFactories.h>
#ifdef USE_UNBOUND
#include <Swiften/Network/UnboundDomainNameResolver.h>
@@ -28,7 +28,7 @@
namespace Swift {
-BoostNetworkFactories::BoostNetworkFactories(EventLoop* eventLoop) : eventLoop(eventLoop){
+BoostNetworkFactories::BoostNetworkFactories(EventLoop* eventLoop, boost::shared_ptr<boost::asio::io_service> ioService) : ioServiceThread(ioService), eventLoop(eventLoop) {
timerFactory = new BoostTimerFactory(ioServiceThread.getIOService(), eventLoop);
connectionFactory = new BoostConnectionFactory(ioServiceThread.getIOService(), eventLoop);
connectionServerFactory = new BoostConnectionServerFactory(ioServiceThread.getIOService(), eventLoop);
diff --git a/Swiften/Network/BoostNetworkFactories.h b/Swiften/Network/BoostNetworkFactories.h
index 32fdd93..12755b9 100644
--- a/Swiften/Network/BoostNetworkFactories.h
+++ b/Swiften/Network/BoostNetworkFactories.h
@@ -8,8 +8,8 @@
#include <Swiften/Base/API.h>
#include <Swiften/Base/Override.h>
-#include <Swiften/Network/NetworkFactories.h>
#include <Swiften/Network/BoostIOServiceThread.h>
+#include <Swiften/Network/NetworkFactories.h>
namespace Swift {
class EventLoop;
@@ -18,7 +18,12 @@ namespace Swift {
class SWIFTEN_API BoostNetworkFactories : public NetworkFactories {
public:
- BoostNetworkFactories(EventLoop* eventLoop);
+ /**
+ * Construct the network factories, using the provided EventLoop.
+ * @param ioService If this optional parameter is provided, it will be
+ * used for the construction of the BoostIOServiceThread.
+ */
+ BoostNetworkFactories(EventLoop* eventLoop, boost::shared_ptr<boost::asio::io_service> ioService = boost::shared_ptr<boost::asio::io_service>());
virtual ~BoostNetworkFactories();
virtual TimerFactory* getTimerFactory() const SWIFTEN_OVERRIDE {