summaryrefslogtreecommitdiff
path: root/src/libs/qmldebug/qmldebugconnectionmanager.cpp
blob: 16e2855d87c6427f0cf7216406637f3fb3da54f4 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "qmldebugconnectionmanager.h"
#include "qmldebugconnection.h"

#include <utils/qtcassert.h>
#include <utils/url.h>

namespace QmlDebug {

QmlDebugConnectionManager::QmlDebugConnectionManager(QObject *parent) : QObject(parent)
{
}

QmlDebugConnectionManager::~QmlDebugConnectionManager()
{
    // Don't receive any signals from the dtors of child objects while our own dtor is running.
    // That can lead to invalid reads.
    if (m_connection)
        disconnectConnectionSignals();
}

void QmlDebugConnectionManager::connectToServer(const QUrl &server)
{
    if (m_server != server) {
        m_server = server;
        destroyConnection();
        stopConnectionTimer();
    }
    if (server.scheme() == Utils::urlTcpScheme())
        connectToTcpServer();
    else if (server.scheme() == Utils::urlSocketScheme())
        startLocalServer();
    else
        QTC_ASSERT(false, emit connectionFailed());
}

void QmlDebugConnectionManager::disconnectFromServer()
{
    m_server.clear();
    destroyConnection();
    stopConnectionTimer();
}

bool QmlDebugConnectionManager::isConnecting() const
{
    return m_connectionTimer.isActive();
}

static quint16 port16(const QUrl &url)
{
    const int port32 = url.port();
    QTC_ASSERT(port32 > 0 && port32 <= std::numeric_limits<quint16>::max(), return 0);
    return static_cast<quint16>(port32);
}

void QmlDebugConnectionManager::connectToTcpServer()
{
    // Calling this again when we're already trying means "reset the retry timer". This is
    // useful in cases where we have to parse the port from the output. We might waste retries
    // on an initial guess for the port.
    stopConnectionTimer();
    connect(&m_connectionTimer, &QTimer::timeout, this, [this]{
        QTC_ASSERT(!isConnected(), return);

        if (++(m_numRetries) < m_maximumRetries) {
            if (m_connection.isNull()) {
                // If the previous connection failed, recreate it.
                createConnection();
                m_connection->connectToHost(m_server.host(), port16(m_server));
            } else if (m_numRetries < 3
                       && m_connection->socketState() != QAbstractSocket::ConnectedState) {
                // If we don't get connected in the first retry interval, drop the socket and try
                // with a new one. On some operating systems (maxOS) the very first connection to a
                // TCP server takes a very long time to get established and this helps.
                // On other operating systems (windows) every connection takes forever to get
                // established. So, after tearing down and rebuilding the socket twice, just
                // keep trying with the same one.
                m_connection->connectToHost(m_server.host(), port16(m_server));
            } // Else leave it alone and wait for hello.
        } else {
            // On final timeout, clear the connection.
            stopConnectionTimer();
            destroyConnection();
            emit connectionFailed();
        }
    });
    m_connectionTimer.start(m_retryInterval);

    if (m_connection.isNull()) {
        createConnection();
        QTC_ASSERT(m_connection, emit connectionFailed(); return);
        m_connection->connectToHost(m_server.host(), port16(m_server));
    }
}

void QmlDebugConnectionManager::startLocalServer()
{
    stopConnectionTimer();
    connect(&m_connectionTimer, &QTimer::timeout, this, [this] {
        QTC_ASSERT(!isConnected(), return);

        // We leave the server running as some application might currently be trying to
        // connect. Don't cut this off, or the application might hang on the hello mutex.
        // qmlConnectionFailed() might drop the connection, which is fatal. We detect this
        // here and signal it accordingly.

        if (!m_connection || ++(m_numRetries) >= m_maximumRetries) {
            stopConnectionTimer();
            destroyConnection();
            emit connectionFailed();
        }
    });
    m_connectionTimer.start(m_retryInterval);

    if (m_connection.isNull()) {
        // Otherwise, reuse the same one
        createConnection();
        QTC_ASSERT(m_connection, emit connectionFailed(); return);
        m_connection->startLocalServer(m_server.path());
    }
}

void QmlDebugConnectionManager::retryConnect()
{
    destroyConnection();
    if (m_server.scheme() == Utils::urlSocketScheme()) {
        startLocalServer();
    } else if (m_server.scheme() == Utils::urlTcpScheme()) {
        connectToTcpServer();
    } else {
        emit connectionFailed();
    }
}

void QmlDebugConnectionManager::logState(const QString &message)
{
    Q_UNUSED(message)
}

QmlDebugConnection *QmlDebugConnectionManager::connection() const
{
    return m_connection.data();
}

void QmlDebugConnectionManager::createConnection()
{
    QTC_ASSERT(m_connection.isNull(), destroyConnection());

    m_connection.reset(new QmlDebug::QmlDebugConnection);

    createClients();
    connectConnectionSignals();
}

void QmlDebugConnectionManager::connectConnectionSignals()
{
    QTC_ASSERT(m_connection, return);
    QObject::connect(m_connection.data(), &QmlDebug::QmlDebugConnection::connected,
                     this, &QmlDebugConnectionManager::qmlDebugConnectionOpened);
    QObject::connect(m_connection.data(), &QmlDebug::QmlDebugConnection::disconnected,
                     this, &QmlDebugConnectionManager::qmlDebugConnectionClosed);
    QObject::connect(m_connection.data(), &QmlDebug::QmlDebugConnection::connectionFailed,
                     this, &QmlDebugConnectionManager::qmlDebugConnectionFailed);

    QObject::connect(m_connection.data(), &QmlDebug::QmlDebugConnection::logStateChange,
                     this, &QmlDebugConnectionManager::logState);
    QObject::connect(m_connection.data(), &QmlDebug::QmlDebugConnection::logError,
                     this, &QmlDebugConnectionManager::logState);
}

void QmlDebugConnectionManager::disconnectConnectionSignals()
{
    QTC_ASSERT(m_connection, return);
    m_connection->disconnect();
}

bool QmlDebugConnectionManager::isConnected() const
{
    return m_connection && m_connection->isConnected();
}

void QmlDebugConnectionManager::destroyConnection()
{
    // This might be called indirectly by QDebugConnectionPrivate::readyRead().
    // Therefore, allow the function to complete before deleting the object.
    if (m_connection) {
        // Don't receive any more signals from the connection or the client
        disconnectConnectionSignals();
        destroyClients();
        m_connection.take()->deleteLater();
    }
}

void QmlDebugConnectionManager::qmlDebugConnectionOpened()
{
    logState(tr("Debug connection opened."));
    QTC_ASSERT(m_connection, return);
    QTC_ASSERT(m_connection->isConnected(), return);
    stopConnectionTimer();
    emit connectionOpened();
}

void QmlDebugConnectionManager::qmlDebugConnectionClosed()
{
    logState(tr("Debug connection closed."));
    QTC_ASSERT(m_connection, return);
    QTC_ASSERT(!m_connection->isConnected(), return);
    destroyConnection();
    emit connectionClosed();
}

void QmlDebugConnectionManager::qmlDebugConnectionFailed()
{
    logState(tr("Debug connection failed."));
    QTC_ASSERT(m_connection, return);
    QTC_ASSERT(!m_connection->isConnected(), /**/);

    destroyConnection();
    // The retry handler, driven by m_connectionTimer should decide to retry or signal a failure.

    QTC_ASSERT(m_connectionTimer.isActive(), emit connectionFailed());
}

void QmlDebugConnectionManager::stopConnectionTimer()
{
    m_connectionTimer.stop();
    m_connectionTimer.disconnect();
    m_numRetries = 0;
}

} // namespace QmlDebug