summaryrefslogtreecommitdiffstats
blob: f79845aed3ce564eeb7795998b1d55b8d1a7cca6 (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
/*
 * Copyright (c) 2012-2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */


#pragma once

#include <memory>

#include <Swiften/Base/API.h>
#include <Swiften/Base/SafeString.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/Connector.h>
#include <Swiften/Network/HostAddressPort.h>

namespace Swift {
    class ConnectionFactory;

    class SWIFTEN_API ProxiedConnection : public Connection, public std::enable_shared_from_this<ProxiedConnection> {
        public:
            ProxiedConnection(DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory, const std::string& proxyHost, unsigned short proxyPort);
            virtual ~ProxiedConnection();

            virtual void listen();
            virtual void connect(const HostAddressPort& address);
            virtual void disconnect();
            virtual void write(const SafeByteArray& data);

            virtual HostAddressPort getLocalAddress() const;
            virtual HostAddressPort getRemoteAddress() const;

        private:
            void handleConnectFinished(Connection::ref connection);
            void handleDataRead(std::shared_ptr<SafeByteArray> data);
            void handleDisconnected(const boost::optional<Error>& error);
            void cancelConnector();

        protected:
            void setProxyInitializeFinished(bool success);

            virtual void initializeProxy() = 0;
            virtual void handleProxyInitializeData(std::shared_ptr<SafeByteArray> data) = 0;

            const HostAddressPort& getServer() const {
                return server_;
            }

            void reconnect();

        private:
            bool connected_;
            DomainNameResolver* resolver_;
            ConnectionFactory* connectionFactory_;
            TimerFactory* timerFactory_;
            std::string proxyHost_;
            unsigned short proxyPort_;
            HostAddressPort server_;
            Connector::ref connector_;
            std::shared_ptr<Connection> connection_;
    };
}