1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qtcpserverconnection.h"
44 #include <QtCore/qplugin.h>
45 #include <QtNetwork/qtcpserver.h>
46 #include <QtNetwork/qtcpsocket.h>
48 #include <private/qdeclarativedebugserver_p.h>
49 #include <private/qpacketprotocol_p.h>
53 class QTcpServerConnectionPrivate {
55 QTcpServerConnectionPrivate();
60 QPacketProtocol *protocol;
61 QTcpServer *tcpServer;
63 QDeclarativeDebugServer *debugServer;
66 QTcpServerConnectionPrivate::QTcpServerConnectionPrivate() :
76 QTcpServerConnection::QTcpServerConnection() :
77 d_ptr(new QTcpServerConnectionPrivate)
82 QTcpServerConnection::~QTcpServerConnection()
87 void QTcpServerConnection::setServer(QDeclarativeDebugServer *server)
89 Q_D(QTcpServerConnection);
90 d->debugServer = server;
93 bool QTcpServerConnection::isConnected() const
95 Q_D(const QTcpServerConnection);
96 return d->socket && d->socket->state() == QTcpSocket::ConnectedState;
99 void QTcpServerConnection::send(const QList<QByteArray> &messages)
101 Q_D(QTcpServerConnection);
104 || !d->protocol || !d->socket)
107 foreach (const QByteArray &message, messages) {
109 pack.writeRawData(message.data(), message.length());
110 d->protocol->send(pack);
115 void QTcpServerConnection::disconnect()
117 Q_D(QTcpServerConnection);
119 // protocol might still be processing packages at this point
120 d->protocol->deleteLater();
122 d->socket->deleteLater();
126 bool QTcpServerConnection::waitForMessage()
128 Q_D(QTcpServerConnection);
129 return d->protocol->waitForReadyRead(-1);
132 void QTcpServerConnection::setPort(int port, bool block)
134 Q_D(QTcpServerConnection);
140 d->tcpServer->waitForNewConnection(-1);
143 void QTcpServerConnection::listen()
145 Q_D(QTcpServerConnection);
147 d->tcpServer = new QTcpServer(this);
148 QObject::connect(d->tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
149 if (d->tcpServer->listen(QHostAddress::Any, d->port))
150 qWarning("QDeclarativeDebugServer: Waiting for connection on port %d...", d->port);
152 qWarning("QDeclarativeDebugServer: Unable to listen on port %d", d->port);
156 void QTcpServerConnection::readyRead()
158 Q_D(QTcpServerConnection);
162 QPacket packet = d->protocol->read();
164 QByteArray content = packet.data();
165 d->debugServer->receiveMessage(content);
168 void QTcpServerConnection::newConnection()
170 Q_D(QTcpServerConnection);
172 if (d->socket && d->socket->peerPort()) {
173 qWarning("QDeclarativeDebugServer: Another client is already connected");
174 QTcpSocket *faultyConnection = d->tcpServer->nextPendingConnection();
175 delete faultyConnection;
180 d->socket = d->tcpServer->nextPendingConnection();
181 d->socket->setParent(this);
182 d->protocol = new QPacketProtocol(d->socket, this);
183 QObject::connect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
184 QObject::connect(d->protocol, SIGNAL(invalidPacket()), this, SLOT(invalidPacket()));
187 d->protocol->waitForReadyRead(-1);
191 void QTcpServerConnection::invalidPacket()
193 qWarning("QDeclarativeDebugServer: Received a corrupted packet! Giving up ...");
196 Q_EXPORT_PLUGIN2(tcpserver, QTcpServerConnection)