4#include <private/qqmldebugserverconnection_p.h>
5#include <private/qqmldebugserver_p.h>
7#include <QtCore/qplugin.h>
8#include <QtNetwork/qtcpserver.h>
9#include <QtNetwork/qtcpsocket.h>
13class QTcpServerConnection :
public QQmlDebugServerConnection
16 Q_DISABLE_COPY(QTcpServerConnection)
19 QTcpServerConnection();
20 ~QTcpServerConnection() override;
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;
26 bool isConnected()
const override;
27 void disconnect() override;
29 void waitForConnection() override;
30 void flush() override;
39 QString m_hostaddress;
40 QTcpSocket *m_socket =
nullptr;
41 QTcpServer *m_tcpServer =
nullptr;
42 QQmlDebugServer *m_debugServer =
nullptr;
45QTcpServerConnection::QTcpServerConnection() {}
47QTcpServerConnection::~QTcpServerConnection()
53void QTcpServerConnection::setServer(QQmlDebugServer *server)
55 m_debugServer = server;
58bool QTcpServerConnection::isConnected()
const
60 return m_socket && m_socket->state() == QTcpSocket::ConnectedState;
63void QTcpServerConnection::disconnect()
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());
73 m_socket->deleteLater();
77bool QTcpServerConnection::setPortRange(
int portFrom,
int portTo,
bool block,
78 const QString &hostaddress)
80 m_portFrom = portFrom;
83 m_hostaddress = hostaddress;
88bool QTcpServerConnection::setFileName(
const QString &fileName,
bool block)
95void QTcpServerConnection::waitForConnection()
97 m_tcpServer->waitForNewConnection(-1);
100void QTcpServerConnection::flush()
106bool QTcpServerConnection::listen()
108 m_tcpServer =
new QTcpServer(
this);
109 QObject::connect(m_tcpServer, &QTcpServer::newConnection,
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 "
119 hostaddress = QHostAddress::Any;
121 int port = m_portFrom;
123 if (m_tcpServer->listen(hostaddress, port)) {
124 qDebug(
"QML Debugger: Waiting for connection on port %d...", 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);
133 qWarning(
"QML Debugger: Unable to listen to ports %d - %d.", m_portFrom, m_portTo);
140void QTcpServerConnection::newConnection()
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;
150 m_socket = m_tcpServer->nextPendingConnection();
151 m_socket->setParent(
this);
152 m_debugServer->setDevice(m_socket);
159 Q_PLUGIN_METADATA(IID QQmlDebugServerConnectionFactory_iid FILE
"qtcpserverconnection.json")
167 return (key == QLatin1String(
"QTcpServerConnection") ?
new QTcpServerConnection :
nullptr);
172#include "qtcpserverconnection.moc"