summaryrefslogtreecommitdiffstats
blob: eaef56cf19ca5e9a651bd4b02e6b3fe8527a0752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2011-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */


#pragma once

#include <vector>

#include <Swiften/Base/API.h>
#include <Swiften/Base/SafeString.h>
#include <Swiften/Network/BOSHConnection.h>
#include <Swiften/TLS/TLSOptions.h>

namespace Swift {
	class CachingDomainNameResolver;
	class EventLoop;
	class HTTPConnectProxiedConnectionFactory;
	class HTTPTrafficFilter;
	class TLSConnectionFactory;

	class SWIFTEN_API BOSHConnectionPool : public boost::bsignals::trackable {
		public:
			BOSHConnectionPool(const URL& boshURL, DomainNameResolver* resolver, ConnectionFactory* connectionFactory, XMLParserFactory* parserFactory, TLSContextFactory* tlsFactory, TimerFactory* timerFactory, EventLoop* eventLoop, const std::string& to, unsigned long long initialRID, const URL& boshHTTPConnectProxyURL, const SafeString& boshHTTPConnectProxyAuthID, const SafeString& boshHTTPConnectProxyAuthPassword, const TLSOptions& tlsOptions, boost::shared_ptr<HTTPTrafficFilter> trafficFilter = boost::shared_ptr<HTTPTrafficFilter>());
			~BOSHConnectionPool();
			void write(const SafeByteArray& data);
			void writeFooter();
			void close();
			void restartStream();

			boost::signal<void (BOSHError::ref)> onSessionTerminated;
			boost::signal<void ()> onSessionStarted;
			boost::signal<void (const SafeByteArray&)> onXMPPDataRead;
			boost::signal<void (const SafeByteArray&)> onBOSHDataRead;
			boost::signal<void (const SafeByteArray&)> onBOSHDataWritten;

		private:
			void handleDataRead(const SafeByteArray& data);
			void handleSessionStarted(const std::string& sid, size_t requests);
			void handleBOSHDataRead(const SafeByteArray& data);
			void handleBOSHDataWritten(const SafeByteArray& data);
			void handleSessionTerminated(BOSHError::ref condition);
			void handleConnectFinished(bool, BOSHConnection::ref connection);
			void handleConnectionDisconnected(bool error, BOSHConnection::ref connection);
			void handleHTTPError(const std::string& errorCode);

		private:
			BOSHConnection::ref createConnection();
			void destroyConnection(BOSHConnection::ref connection);
			void tryToSendQueuedData();
			BOSHConnection::ref getSuitableConnection();

		private:
			URL boshURL;
			ConnectionFactory* connectionFactory;
			XMLParserFactory* xmlParserFactory;
			TimerFactory* timerFactory;
			std::vector<BOSHConnection::ref> connections;
			std::string sid;
			unsigned long long rid;
			std::vector<SafeByteArray> dataQueue;
			bool pendingTerminate;
			std::string to;
			size_t requestLimit;
			int restartCount;
			bool pendingRestart;
			std::vector<ConnectionFactory*> myConnectionFactories;
			CachingDomainNameResolver* resolver;
	};
}