source: trunk/src/plugins/qmltooling/tcpserver/qtcpserverconnection.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 4.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtDeclarative module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
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.
23**
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.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtcpserverconnection.h"
43
44#include <QtNetwork/qtcpserver.h>
45#include <QtNetwork/qtcpsocket.h>
46
47#include <private/qdeclarativedebugserver_p.h>
48#include <private/qpacketprotocol_p.h>
49
50QT_BEGIN_NAMESPACE
51
52class QTcpServerConnectionPrivate {
53public:
54 QTcpServerConnectionPrivate();
55
56 int port;
57 QTcpSocket *socket;
58 QPacketProtocol *protocol;
59 QTcpServer *tcpServer;
60
61 QDeclarativeDebugServer *debugServer;
62};
63
64QTcpServerConnectionPrivate::QTcpServerConnectionPrivate() :
65 port(0),
66 socket(0),
67 protocol(0),
68 tcpServer(0),
69 debugServer(0)
70{
71}
72
73QTcpServerConnection::QTcpServerConnection() :
74 d_ptr(new QTcpServerConnectionPrivate)
75{
76
77}
78
79QTcpServerConnection::~QTcpServerConnection()
80{
81 delete d_ptr;
82}
83
84void QTcpServerConnection::setServer(QDeclarativeDebugServer *server)
85{
86 Q_D(QTcpServerConnection);
87 d->debugServer = server;
88}
89
90bool QTcpServerConnection::isConnected() const
91{
92 Q_D(const QTcpServerConnection);
93 return d->socket && d->socket->state() == QTcpSocket::ConnectedState;
94}
95
96void QTcpServerConnection::send(const QByteArray &message)
97{
98 Q_D(QTcpServerConnection);
99
100 if (!isConnected())
101 return;
102
103 QPacket pack;
104 pack.writeRawData(message.data(), message.length());
105
106 d->protocol->send(pack);
107 d->socket->flush();
108}
109
110void QTcpServerConnection::disconnect()
111{
112 Q_D(QTcpServerConnection);
113
114 delete d->protocol;
115 d->protocol = 0;
116 delete d->socket;
117 d->socket = 0;
118}
119
120void QTcpServerConnection::setPort(int port, bool block)
121{
122 Q_D(QTcpServerConnection);
123 d->port = port;
124
125 listen();
126 if (block)
127 d->tcpServer->waitForNewConnection(-1);
128}
129
130void QTcpServerConnection::listen()
131{
132 Q_D(QTcpServerConnection);
133
134 d->tcpServer = new QTcpServer(this);
135 QObject::connect(d->tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
136 if (d->tcpServer->listen(QHostAddress::Any, d->port))
137 qWarning("QDeclarativeDebugServer: Waiting for connection on port %d...", d->port);
138 else
139 qWarning("QDeclarativeDebugServer: Unable to listen on port %d", d->port);
140}
141
142
143void QTcpServerConnection::readyRead()
144{
145 Q_D(QTcpServerConnection);
146 QPacket packet = d->protocol->read();
147
148 QByteArray content = packet.data();
149 d->debugServer->receiveMessage(content);
150}
151
152void QTcpServerConnection::newConnection()
153{
154 Q_D(QTcpServerConnection);
155
156 if (d->socket) {
157 qWarning("QDeclarativeDebugServer: Another client is already connected");
158 QTcpSocket *faultyConnection = d->tcpServer->nextPendingConnection();
159 delete faultyConnection;
160 return;
161 }
162
163 d->socket = d->tcpServer->nextPendingConnection();
164 d->socket->setParent(this);
165 d->protocol = new QPacketProtocol(d->socket, this);
166 QObject::connect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
167}
168
169
170Q_EXPORT_PLUGIN2(tcpserver, QTcpServerConnection)
171
172QT_END_NAMESPACE
173
Note: See TracBrowser for help on using the repository browser.