From 66fd05d46a6ead4d780bdb4da1ede43058c272da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sun, 2 Aug 2009 16:07:00 +0200
Subject: Slimber: Handle ConnectionServer disconnects more cleanly.


diff --git a/Slimber/Server.cpp b/Slimber/Server.cpp
index 28e0659..a944ab9 100644
--- a/Slimber/Server.cpp
+++ b/Slimber/Server.cpp
@@ -53,19 +53,23 @@ void Server::start() {
 	serverFromClientConnectionServer = 
 			boost::shared_ptr<BoostConnectionServer>(new BoostConnectionServer(
 					clientConnectionPort, &boostIOServiceThread.getIOService()));
-	serverFromClientConnectionServer->onNewConnection.connect(
-			boost::bind(&Server::handleNewClientConnection, this, _1));
-	serverFromClientConnectionServer->onStopped.connect(
-			boost::bind(&Server::handleClientConnectionServerStopped, this, _1));
+	serverFromClientConnectionServerSignalConnections.push_back(
+		serverFromClientConnectionServer->onNewConnection.connect(
+				boost::bind(&Server::handleNewClientConnection, this, _1)));
+	serverFromClientConnectionServerSignalConnections.push_back(
+		serverFromClientConnectionServer->onStopped.connect(
+				boost::bind(&Server::handleClientConnectionServerStopped, this, _1)));
 
 	assert(!serverFromNetworkConnectionServer);
 	serverFromNetworkConnectionServer = 
 		boost::shared_ptr<BoostConnectionServer>(new BoostConnectionServer(
 			linkLocalConnectionPort, &boostIOServiceThread.getIOService()));
-	serverFromNetworkConnectionServer->onNewConnection.connect(
-			boost::bind(&Server::handleNewLinkLocalConnection, this, _1));
-	serverFromNetworkConnectionServer->onStopped.connect(
-			boost::bind(&Server::handleLinkLocalConnectionServerStopped, this, _1));
+	serverFromNetworkConnectionServerSignalConnections.push_back(
+		serverFromNetworkConnectionServer->onNewConnection.connect(
+				boost::bind(&Server::handleNewLinkLocalConnection, this, _1)));
+	serverFromNetworkConnectionServerSignalConnections.push_back(
+		serverFromNetworkConnectionServer->onStopped.connect(
+				boost::bind(&Server::handleLinkLocalConnectionServerStopped, this, _1)));
 
 	assert(!presenceManager);
 	presenceManager = new LinkLocalPresenceManager(linkLocalServiceBrowser);
@@ -108,12 +112,18 @@ void Server::stop(boost::optional<ServerError> e) {
 
 	if (serverFromNetworkConnectionServer) {
 		serverFromNetworkConnectionServer->stop();
-		serverFromNetworkConnectionServer->cancelAllEvents();
+		foreach(boost::bsignals::connection& connection, serverFromNetworkConnectionServerSignalConnections) {
+			connection.disconnect();
+		}
+		serverFromNetworkConnectionServerSignalConnections.clear();
 		serverFromNetworkConnectionServer.reset();
 	}
 	if (serverFromClientConnectionServer) {
 		serverFromClientConnectionServer->stop();
-		serverFromClientConnectionServer->cancelAllEvents();
+		foreach(boost::bsignals::connection& connection, serverFromClientConnectionServerSignalConnections) {
+			connection.disconnect();
+		}
+		serverFromClientConnectionServerSignalConnections.clear();
 		serverFromClientConnectionServer.reset();
 	}
 
@@ -350,7 +360,6 @@ void Server::handlePresenceChanged(boost::shared_ptr<Presence> presence) {
 }
 
 void Server::handleClientConnectionServerStopped(boost::optional<BoostConnectionServer::Error> e) {
-	std::cout << "Client server stoppedd " << (bool) e << std::endl;
 	if (e) {
 		if (*e == BoostConnectionServer::Conflict) {
 			stop(ServerError(ServerError::C2SPortConflict));
@@ -365,7 +374,6 @@ void Server::handleClientConnectionServerStopped(boost::optional<BoostConnection
 }
 
 void Server::handleLinkLocalConnectionServerStopped(boost::optional<BoostConnectionServer::Error> e) {
-	std::cout << "LL server stoppedd " << (bool) e << std::endl;
 	if (e) {
 		if (*e == BoostConnectionServer::Conflict) {
 			stop(ServerError(ServerError::LinkLocalPortConflict));
diff --git a/Slimber/Server.h b/Slimber/Server.h
index ff3f70d..372a3d3 100644
--- a/Slimber/Server.h
+++ b/Slimber/Server.h
@@ -91,10 +91,12 @@ namespace Swift {
 			LinkLocalPresenceManager* presenceManager;
 			bool stopping;
 			boost::shared_ptr<BoostConnectionServer> serverFromClientConnectionServer;
+			std::vector<boost::bsignals::connection> serverFromClientConnectionServerSignalConnections;
 			boost::shared_ptr<ServerFromClientSession> serverFromClientSession;
 			boost::shared_ptr<Presence> lastPresence;
 			JID selfJID;
 			boost::shared_ptr<BoostConnectionServer> serverFromNetworkConnectionServer;
+			std::vector<boost::bsignals::connection> serverFromNetworkConnectionServerSignalConnections;
 			std::vector< boost::shared_ptr<Session> > linkLocalSessions;
 			std::vector< boost::shared_ptr<LinkLocalConnector> > connectors;
 			std::vector< boost::shared_ptr<SessionTracer> > tracers;
diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp
index 97c1316..4e83ad5 100644
--- a/Swiften/Network/BoostConnectionServer.cpp
+++ b/Swiften/Network/BoostConnectionServer.cpp
@@ -42,10 +42,6 @@ void BoostConnectionServer::stop(boost::optional<Error> e) {
 	MainEventLoop::postEvent(boost::bind(boost::ref(onStopped), e), shared_from_this());
 }
 
-void BoostConnectionServer::cancelAllEvents() {
-	MainEventLoop::removeEventsFromOwner(shared_from_this());
-}
-
 void BoostConnectionServer::acceptNextConnection() {
 	boost::shared_ptr<BoostConnection> newConnection(new BoostConnection(&acceptor_->io_service()));
 	acceptor_->async_accept(newConnection->getSocket(), 
diff --git a/Swiften/Network/BoostConnectionServer.h b/Swiften/Network/BoostConnectionServer.h
index 13d87a5..d8e5eb4 100644
--- a/Swiften/Network/BoostConnectionServer.h
+++ b/Swiften/Network/BoostConnectionServer.h
@@ -20,7 +20,6 @@ namespace Swift {
 
 			void start();
 			void stop();
-			virtual void cancelAllEvents();
 
 			boost::signal<void (boost::optional<Error>)> onStopped;
 
diff --git a/Swiften/Network/ConnectionServer.h b/Swiften/Network/ConnectionServer.h
index 9300092..539367d 100644
--- a/Swiften/Network/ConnectionServer.h
+++ b/Swiften/Network/ConnectionServer.h
@@ -10,8 +10,6 @@ namespace Swift {
 		public:
 			virtual ~ConnectionServer();
 
-			virtual void cancelAllEvents() = 0;
-
 			boost::signal<void (boost::shared_ptr<Connection>)> onNewConnection;
 	};
 }
-- 
cgit v0.10.2-6-g49f6