Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtcpserverconnection.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <private/qqmldebugserverconnection_p.h>
5#include <private/qqmldebugserver_p.h>
6
7#include <QtCore/qplugin.h>
8#include <QtNetwork/qtcpserver.h>
9#include <QtNetwork/qtcpsocket.h>
10
12
14{
16 Q_DISABLE_COPY(QTcpServerConnection)
17
18public:
20 ~QTcpServerConnection() override;
21
22 void setServer(QQmlDebugServer *server) override;
23 bool setPortRange(int portFrom, int portTo, bool block, const QString &hostaddress) override;
24 bool setFileName(const QString &fileName, bool block) override;
25
26 bool isConnected() const override;
27 void disconnect() override;
28
29 void waitForConnection() override;
30 void flush() override;
31
32private:
33 void newConnection();
34 bool listen();
35
36 int m_portFrom = 0;
37 int m_portTo = 0;
38 bool m_block = false;
39 QString m_hostaddress;
40 QTcpSocket *m_socket = nullptr;
41 QTcpServer *m_tcpServer = nullptr;
42 QQmlDebugServer *m_debugServer = nullptr;
43};
44
46
52
57
59{
60 return m_socket && m_socket->state() == QTcpSocket::ConnectedState;
61}
62
64{
65 while (m_socket && m_socket->bytesToWrite() > 0) {
66 if (!m_socket->waitForBytesWritten()) {
67 qWarning("QML Debugger: Failed to send remaining %lld bytes on disconnect.",
68 m_socket->bytesToWrite());
69 break;
70 }
71 }
72
73 m_socket->deleteLater();
74 m_socket = nullptr;
75}
76
77bool QTcpServerConnection::setPortRange(int portFrom, int portTo, bool block,
78 const QString &hostaddress)
79{
80 m_portFrom = portFrom;
81 m_portTo = portTo;
82 m_block = block;
83 m_hostaddress = hostaddress;
84
85 return listen();
86}
87
89{
91 Q_UNUSED(block);
92 return false;
93}
94
99
101{
102 if (m_socket)
103 m_socket->flush();
104}
105
106bool QTcpServerConnection::listen()
107{
108 m_tcpServer = new QTcpServer(this);
110 this, &QTcpServerConnection::newConnection);
111 QHostAddress hostaddress;
112 if (!m_hostaddress.isEmpty()) {
113 if (!hostaddress.setAddress(m_hostaddress)) {
114 hostaddress = QHostAddress::Any;
115 qDebug("QML Debugger: Incorrect host address provided. So accepting connections "
116 "from any host.");
117 }
118 } else {
119 hostaddress = QHostAddress::Any;
120 }
121 int port = m_portFrom;
122 do {
123 if (m_tcpServer->listen(hostaddress, port)) {
124 qDebug("QML Debugger: Waiting for connection on port %d...", port);
125 break;
126 }
127 ++port;
128 } while (port <= m_portTo);
129 if (port > m_portTo) {
130 if (m_portFrom == m_portTo)
131 qWarning("QML Debugger: Unable to listen to port %d.", m_portFrom);
132 else
133 qWarning("QML Debugger: Unable to listen to ports %d - %d.", m_portFrom, m_portTo);
134 return false;
135 } else {
136 return true;
137 }
138}
139
140void QTcpServerConnection::newConnection()
141{
142 if (m_socket && m_socket->peerPort()) {
143 qWarning("QML Debugger: Another client is already connected.");
144 QTcpSocket *faultyConnection = m_tcpServer->nextPendingConnection();
145 delete faultyConnection;
146 return;
147 }
148
149 delete m_socket;
150 m_socket = m_tcpServer->nextPendingConnection();
151 m_socket->setParent(this);
152 m_debugServer->setDevice(m_socket);
153}
154
155
164
166{
167 return (key == QLatin1String("QTcpServerConnection") ? new QTcpServerConnection : nullptr);
168}
169
171
172#include "qtcpserverconnection.moc"
bool flush()
This function writes as much as possible from the internal write buffer to the underlying network soc...
quint16 peerPort() const
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0.
qint64 bytesToWrite() const override
Returns the number of bytes that are waiting to be written.
bool waitForBytesWritten(int msecs=30000) override
\reimp
SocketState state() const
Returns the state of the socket.
The QHostAddress class provides an IP address.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2195
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
virtual void setDevice(QIODevice *socket)=0
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QQmlDebugServerConnection * create(const QString &key) override
void setServer(QQmlDebugServer *server) override
bool isConnected() const override
bool setFileName(const QString &fileName, bool block) override
bool setPortRange(int portFrom, int portTo, bool block, const QString &hostaddress) override
void waitForConnection() override
The QTcpServer class provides a TCP-based server.
Definition qtcpserver.h:22
void newConnection()
This signal is emitted every time a new connection is available, regardless of whether it has been ad...
virtual QTcpSocket * nextPendingConnection()
Returns the next pending connection as a connected QTcpSocket object.
bool listen(const QHostAddress &address=QHostAddress::Any, quint16 port=0)
Tells the server to listen for incoming connections on address address and port port.
bool waitForNewConnection(int msec=0, bool *timedOut=nullptr)
Waits for at most msec milliseconds or until an incoming connection is available.
The QTcpSocket class provides a TCP socket.
Definition qtcpsocket.h:18
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall DBusPendingCall return DBusPendingCall return dbus_int32_t return DBusServer * server
EGLOutputPortEXT port
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
GLuint64 key
#define QQmlDebugServerConnectionFactory_iid
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define Q_OBJECT
#define Q_PLUGIN_METADATA(x)
#define Q_INTERFACES(x)
#define Q_UNUSED(x)
view create()