5#include <private/qqmldebugserverconnection_p.h>
6#include <private/qqmldebugserver_p.h>
8#include <QtCore/qplugin.h>
9#include <QtNetwork/qtcpserver.h>
10#include <QtNetwork/qtcpsocket.h>
14class QTcpServerConnection :
public QQmlDebugServerConnection
17 Q_DISABLE_COPY(QTcpServerConnection)
20 QTcpServerConnection();
21 ~QTcpServerConnection() override;
23 void setServer(QQmlDebugServer *server) override;
24 bool setPortRange(
int portFrom,
int portTo,
bool block,
const QString &hostaddress) override;
25 bool setFileName(
const QString &fileName,
bool block) override;
27 bool isConnected()
const override;
28 void disconnect() override;
30 void waitForConnection() override;
31 void flush() override;
40 QString m_hostaddress;
41 QTcpSocket *m_socket =
nullptr;
42 QTcpServer *m_tcpServer =
nullptr;
43 QQmlDebugServer *m_debugServer =
nullptr;
46QTcpServerConnection::QTcpServerConnection() {}
48QTcpServerConnection::~QTcpServerConnection()
54void QTcpServerConnection::setServer(QQmlDebugServer *server)
56 m_debugServer = server;
59bool QTcpServerConnection::isConnected()
const
61 return m_socket && m_socket->state() == QTcpSocket::ConnectedState;
64void QTcpServerConnection::disconnect()
66 while (m_socket && m_socket->bytesToWrite() > 0) {
67 if (!m_socket->waitForBytesWritten()) {
68 qWarning(
"QML Debugger: Failed to send remaining %lld bytes on disconnect.",
69 m_socket->bytesToWrite());
74 m_socket->deleteLater();
78bool QTcpServerConnection::setPortRange(
int portFrom,
int portTo,
bool block,
79 const QString &hostaddress)
81 m_portFrom = portFrom;
84 m_hostaddress = hostaddress;
89bool QTcpServerConnection::setFileName(
const QString &fileName,
bool block)
96void QTcpServerConnection::waitForConnection()
98 m_tcpServer->waitForNewConnection(-1);
101void QTcpServerConnection::flush()
107bool QTcpServerConnection::listen()
109 m_tcpServer =
new QTcpServer(
this);
110 QObject::connect(m_tcpServer, &QTcpServer::newConnection,
111 this, &QTcpServerConnection::newConnection);
112 QHostAddress hostaddress;
113 if (!m_hostaddress.isEmpty()) {
114 if (!hostaddress.setAddress(m_hostaddress)) {
115 hostaddress = QHostAddress::Any;
116 qDebug(
"QML Debugger: Incorrect host address provided. So accepting connections "
120 hostaddress = QHostAddress::Any;
122 int port = m_portFrom;
124 if (m_tcpServer->listen(hostaddress, port)) {
125 qDebug(
"QML Debugger: Waiting for connection on port %d...", port);
129 }
while (port <= m_portTo);
130 if (port > m_portTo) {
131 if (m_portFrom == m_portTo)
132 qWarning(
"QML Debugger: Unable to listen to port %d.", m_portFrom);
134 qWarning(
"QML Debugger: Unable to listen to ports %d - %d.", m_portFrom, m_portTo);
141void QTcpServerConnection::newConnection()
143 if (m_socket && m_socket->peerPort()) {
144 qWarning(
"QML Debugger: Another client is already connected.");
145 QTcpSocket *faultyConnection = m_tcpServer->nextPendingConnection();
146 delete faultyConnection;
151 m_socket = m_tcpServer->nextPendingConnection();
152 m_socket->setParent(
this);
153 m_debugServer->setDevice(m_socket);
160 Q_PLUGIN_METADATA(IID QQmlDebugServerConnectionFactory_iid FILE
"qtcpserverconnection.json")
168 return (key == QLatin1String(
"QTcpServerConnection") ?
new QTcpServerConnection :
nullptr);
173#include "qtcpserverconnection.moc"