Qt 4.8
qtcpserverconnection.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qtcpserverconnection.h"
43 
44 #include <QtCore/qplugin.h>
45 #include <QtNetwork/qtcpserver.h>
46 #include <QtNetwork/qtcpsocket.h>
47 
48 #include <private/qdeclarativedebugserver_p.h>
49 #include <private/qpacketprotocol_p.h>
50 
52 
54 public:
56 
57  int port;
58  bool block;
62 
64 };
65 
67  port(0),
68  block(false),
69  socket(0),
70  protocol(0),
71  tcpServer(0),
72  debugServer(0)
73 {
74 }
75 
78 {
79 
80 }
81 
83 {
84  delete d_ptr;
85 }
86 
88 {
90  d->debugServer = server;
91 }
92 
94 {
96  return d->socket && d->socket->state() == QTcpSocket::ConnectedState;
97 }
98 
100 {
102 
103  if (!isConnected()
104  || !d->protocol || !d->socket)
105  return;
106 
107  QPacket pack;
108  pack.writeRawData(message.data(), message.length());
109 
110  d->protocol->send(pack);
111  d->socket->flush();
112 }
113 
115 {
117 
118  // protocol might still be processing packages at this point
119  d->protocol->deleteLater();
120  d->protocol = 0;
121  d->socket->deleteLater();
122  d->socket = 0;
123 }
124 
126 {
128  if (d->protocol->packetsAvailable() > 0) {
129  QPacket packet = d->protocol->read();
130  d->debugServer->receiveMessage(packet.data());
131  return true;
132  } else {
133  return d->protocol->waitForReadyRead(-1);
134  }
135 }
136 
137 void QTcpServerConnection::setPort(int port, bool block)
138 {
140  d->port = port;
141  d->block = block;
142 
143  listen();
144  if (block)
145  d->tcpServer->waitForNewConnection(-1);
146 }
147 
149 {
151 
152  d->tcpServer = new QTcpServer(this);
153  QObject::connect(d->tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
154  if (d->tcpServer->listen(QHostAddress::Any, d->port)) {
155  qDebug("QDeclarativeDebugServer: Waiting for connection on port %d...", d->port);
156  } else {
157  qWarning("QDeclarativeDebugServer: Unable to listen on port %d", d->port);
158  }
159 }
160 
161 
163 {
165  if (!d->protocol)
166  return;
167 
168  while (d->protocol->packetsAvailable() > 0) {
169  QPacket packet = d->protocol->read();
170  d->debugServer->receiveMessage(packet.data());
171  }
172 }
173 
175 {
177 
178  if (d->socket) {
179  qWarning("QDeclarativeDebugServer: Another client is already connected");
180  QTcpSocket *faultyConnection = d->tcpServer->nextPendingConnection();
181  delete faultyConnection;
182  return;
183  }
184 
185  d->socket = d->tcpServer->nextPendingConnection();
186  d->socket->setParent(this);
187  d->protocol = new QPacketProtocol(d->socket, this);
188  QObject::connect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
189 
190  if (d->block) {
191  d->protocol->waitForReadyRead(-1);
192  }
193 }
194 
196 
198 
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPacketProtocol class encapsulates communicating discrete packets across fragmented IO channels...
#define Q_D(Class)
Definition: qglobal.h:2482
The QPacket class encapsulates an unfragmentable packet of data to be transmitted by QPacketProtocol...
void send(const QByteArray &message)
Q_CORE_EXPORT void qDebug(const char *,...)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setServer(QDeclarativeDebugServer *server)
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray data() const
Returns raw packet data.
The QTcpSocket class provides a TCP socket.
Definition: qtcpsocket.h:56
QDeclarativeDebugServer * debugServer
int length() const
Same as size().
Definition: qbytearray.h:356
static QAuServer & server()
Definition: qsound.cpp:79
The QTcpServer class provides a TCP-based server.
Definition: qtcpserver.h:61
void setPort(int port, bool bock)
QTcpServerConnectionPrivate * d_ptr
int writeRawData(const char *, int len)
Writes len bytes from s to the stream.
#define Q_EXPORT_PLUGIN2(PLUGIN, PLUGINCLASS)
Definition: qplugin.h:136