1 /****************************************************************************
3 ** Copyright (C) 2011 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qtcpserverconnection.h"
44 #include <QtNetwork/qtcpserver.h>
45 #include <QtNetwork/qtcpsocket.h>
47 #include <private/qdeclarativedebugserver_p.h>
48 #include <private/qpacketprotocol_p.h>
52 class QTcpServerConnectionPrivate {
54 QTcpServerConnectionPrivate();
58 QPacketProtocol *protocol;
59 QTcpServer *tcpServer;
61 QDeclarativeDebugServer *debugServer;
64 QTcpServerConnectionPrivate::QTcpServerConnectionPrivate() :
73 QTcpServerConnection::QTcpServerConnection() :
74 d_ptr(new QTcpServerConnectionPrivate)
79 QTcpServerConnection::~QTcpServerConnection()
84 void QTcpServerConnection::setServer(QDeclarativeDebugServer *server)
86 Q_D(QTcpServerConnection);
87 d->debugServer = server;
90 bool QTcpServerConnection::isConnected() const
92 Q_D(const QTcpServerConnection);
93 return d->socket && d->socket->state() == QTcpSocket::ConnectedState;
96 void QTcpServerConnection::send(const QByteArray &message)
98 Q_D(QTcpServerConnection);
101 || !d->protocol || !d->socket)
105 pack.writeRawData(message.data(), message.length());
107 d->protocol->send(pack);
111 void QTcpServerConnection::disconnect()
113 Q_D(QTcpServerConnection);
115 // protocol might still be processing packages at this point
116 d->protocol->deleteLater();
118 d->socket->deleteLater();
122 void QTcpServerConnection::setPort(int port, bool block)
124 Q_D(QTcpServerConnection);
129 d->tcpServer->waitForNewConnection(-1);
132 void QTcpServerConnection::listen()
134 Q_D(QTcpServerConnection);
136 d->tcpServer = new QTcpServer(this);
137 QObject::connect(d->tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
138 if (d->tcpServer->listen(QHostAddress::Any, d->port))
139 qWarning("QDeclarativeDebugServer: Waiting for connection on port %d...", d->port);
141 qWarning("QDeclarativeDebugServer: Unable to listen on port %d", d->port);
145 void QTcpServerConnection::readyRead()
147 Q_D(QTcpServerConnection);
151 QPacket packet = d->protocol->read();
153 QByteArray content = packet.data();
154 d->debugServer->receiveMessage(content);
157 void QTcpServerConnection::newConnection()
159 Q_D(QTcpServerConnection);
162 qWarning("QDeclarativeDebugServer: Another client is already connected");
163 QTcpSocket *faultyConnection = d->tcpServer->nextPendingConnection();
164 delete faultyConnection;
168 d->socket = d->tcpServer->nextPendingConnection();
169 d->socket->setParent(this);
170 d->protocol = new QPacketProtocol(d->socket, this);
171 QObject::connect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
175 Q_EXPORT_PLUGIN2(tcpserver, QTcpServerConnection)