socket/qtcpserver.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtNetwork 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//#define QTCPSERVER_DEBUG -
43 -
44/*! \class QTcpServer -
45 -
46 \brief The QTcpServer class provides a TCP-based server. -
47 -
48 \reentrant -
49 \ingroup network -
50 \inmodule QtNetwork -
51 -
52 This class makes it possible to accept incoming TCP connections. -
53 You can specify the port or have QTcpServer pick one -
54 automatically. You can listen on a specific address or on all the -
55 machine's addresses. -
56 -
57 Call listen() to have the server listen for incoming connections. -
58 The newConnection() signal is then emitted each time a client -
59 connects to the server. -
60 -
61 Call nextPendingConnection() to accept the pending connection as -
62 a connected QTcpSocket. The function returns a pointer to a -
63 QTcpSocket in QAbstractSocket::ConnectedState that you can use for -
64 communicating with the client. -
65 -
66 If an error occurs, serverError() returns the type of error, and -
67 errorString() can be called to get a human readable description of -
68 what happened. -
69 -
70 When listening for connections, the address and port on which the -
71 server is listening are available as serverAddress() and -
72 serverPort(). -
73 -
74 Calling close() makes QTcpServer stop listening for incoming -
75 connections. -
76 -
77 Although QTcpServer is mostly designed for use with an event -
78 loop, it's possible to use it without one. In that case, you must -
79 use waitForNewConnection(), which blocks until either a -
80 connection is available or a timeout expires. -
81 -
82 \sa QTcpSocket, {Fortune Server Example}, {Threaded Fortune Server Example}, -
83 {Loopback Example}, {Torrent Example} -
84*/ -
85 -
86/*! \fn void QTcpServer::newConnection() -
87 -
88 This signal is emitted every time a new connection is available. -
89 -
90 \sa hasPendingConnections(), nextPendingConnection() -
91*/ -
92 -
93/*! \fn void QTcpServer::acceptError(QAbstractSocket::SocketError socketError) -
94 \since 5.0 -
95 -
96 This signal is emitted when accepting a new connection results in an error. -
97 The \a socketError parameter describes the type of error that occurred. -
98 -
99 \sa pauseAccepting(), resumeAccepting() -
100*/ -
101 -
102#include "qtcpserver.h" -
103#include "private/qobject_p.h" -
104#include "qalgorithms.h" -
105#include "qhostaddress.h" -
106#include "qlist.h" -
107#include "qpointer.h" -
108#include "qabstractsocketengine_p.h" -
109#include "qtcpsocket.h" -
110#include "qnetworkproxy.h" -
111 -
112QT_BEGIN_NAMESPACE -
113 -
114#define Q_CHECK_SOCKETENGINE(returnValue) do { \ -
115 if (!d->socketEngine) { \ -
116 return returnValue; \ -
117 } } while (0) -
118 -
119class QTcpServerPrivate : public QObjectPrivate, public QAbstractSocketEngineReceiver -
120{ -
121 Q_DECLARE_PUBLIC(QTcpServer) -
122public: -
123 QTcpServerPrivate(); -
124 ~QTcpServerPrivate(); -
125 -
126 QList<QTcpSocket *> pendingConnections; -
127 -
128 quint16 port; -
129 QHostAddress address; -
130 -
131 QAbstractSocket::SocketState state; -
132 QAbstractSocketEngine *socketEngine; -
133 -
134 QAbstractSocket::SocketError serverSocketError; -
135 QString serverSocketErrorString; -
136 -
137 int maxConnections; -
138 -
139#ifndef QT_NO_NETWORKPROXY -
140 QNetworkProxy proxy; -
141 QNetworkProxy resolveProxy(const QHostAddress &address, quint16 port); -
142#endif -
143 -
144 // from QAbstractSocketEngineReceiver -
145 void readNotification(); -
146 void closeNotification() { readNotification(); }
never executed: }
0
147 inline void writeNotification() {} -
148 inline void exceptionNotification() {} -
149 inline void connectionNotification() {} -
150#ifndef QT_NO_NETWORKPROXY -
151 inline void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *) {} -
152#endif -
153 -
154}; -
155 -
156/*! \internal -
157*/ -
158QTcpServerPrivate::QTcpServerPrivate() -
159 : port(0) -
160 , state(QAbstractSocket::UnconnectedState) -
161 , socketEngine(0) -
162 , serverSocketError(QAbstractSocket::UnknownSocketError) -
163 , maxConnections(30) -
164{ -
165}
executed: }
Execution Count:491
491
166 -
167/*! \internal -
168*/ -
169QTcpServerPrivate::~QTcpServerPrivate() -
170{ -
171} -
172 -
173#ifndef QT_NO_NETWORKPROXY -
174/*! \internal -
175 -
176 Resolve the proxy to its final value. -
177*/ -
178QNetworkProxy QTcpServerPrivate::resolveProxy(const QHostAddress &address, quint16 port) -
179{ -
180 if (address.isLoopback())
evaluated: address.isLoopback()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:430
18-430
181 return QNetworkProxy::NoProxy;
executed: return QNetworkProxy::NoProxy;
Execution Count:18
18
182 -
183 QList<QNetworkProxy> proxies;
executed (the execution status of this line is deduced): QList<QNetworkProxy> proxies;
-
184 if (proxy.type() != QNetworkProxy::DefaultProxy) {
evaluated: proxy.type() != QNetworkProxy::DefaultProxy
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:426
4-426
185 // a non-default proxy was set with setProxy -
186 proxies << proxy;
executed (the execution status of this line is deduced): proxies << proxy;
-
187 } else {
executed: }
Execution Count:4
4
188 // try the application settings instead -
189 QNetworkProxyQuery query(port, QString(), QNetworkProxyQuery::TcpServer);
executed (the execution status of this line is deduced): QNetworkProxyQuery query(port, QString(), QNetworkProxyQuery::TcpServer);
-
190 proxies = QNetworkProxyFactory::proxyForQuery(query);
executed (the execution status of this line is deduced): proxies = QNetworkProxyFactory::proxyForQuery(query);
-
191 }
executed: }
Execution Count:426
426
192 -
193 // return the first that we can use -
194 foreach (const QNetworkProxy &p, proxies) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(proxies)> _container_(proxies); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QNetworkProxy &p = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
195 if (p.capabilities() & QNetworkProxy::ListeningCapability)
evaluated: p.capabilities() & QNetworkProxy::ListeningCapability
TRUEFALSE
yes
Evaluation Count:424
yes
Evaluation Count:10
10-424
196 return p;
executed: return p;
Execution Count:424
424
197 }
executed: }
Execution Count:10
10
198 -
199 // no proxy found -
200 // DefaultProxy will raise an error -
201 return QNetworkProxy(QNetworkProxy::DefaultProxy);
executed: return QNetworkProxy(QNetworkProxy::DefaultProxy);
Execution Count:6
6
202} -
203#endif -
204 -
205/*! \internal -
206*/ -
207void QTcpServerPrivate::readNotification() -
208{ -
209 Q_Q(QTcpServer);
executed (the execution status of this line is deduced): QTcpServer * const q = q_func();
-
210 for (;;) {
executed (the execution status of this line is deduced): for (;;) {
-
211 if (pendingConnections.count() >= maxConnections) {
evaluated: pendingConnections.count() >= maxConnections
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:921
1-921
212#if defined (QTCPSERVER_DEBUG) -
213 qDebug("QTcpServerPrivate::_q_processIncomingConnection() too many connections"); -
214#endif -
215 if (socketEngine->isReadNotificationEnabled())
partially evaluated: socketEngine->isReadNotificationEnabled()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
216 socketEngine->setReadNotificationEnabled(false);
executed: socketEngine->setReadNotificationEnabled(false);
Execution Count:1
1
217 return;
executed: return;
Execution Count:1
1
218 } -
219 -
220 int descriptor = socketEngine->accept();
executed (the execution status of this line is deduced): int descriptor = socketEngine->accept();
-
221 if (descriptor == -1) {
evaluated: descriptor == -1
TRUEFALSE
yes
Evaluation Count:455
yes
Evaluation Count:466
455-466
222 if (socketEngine->error() != QAbstractSocket::TemporaryError) {
partially evaluated: socketEngine->error() != QAbstractSocket::TemporaryError
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:455
0-455
223 q->pauseAccepting();
never executed (the execution status of this line is deduced): q->pauseAccepting();
-
224 serverSocketError = socketEngine->error();
never executed (the execution status of this line is deduced): serverSocketError = socketEngine->error();
-
225 serverSocketErrorString = socketEngine->errorString();
never executed (the execution status of this line is deduced): serverSocketErrorString = socketEngine->errorString();
-
226 emit q->acceptError(serverSocketError);
never executed (the execution status of this line is deduced): q->acceptError(serverSocketError);
-
227 }
never executed: }
0
228 break;
executed: break;
Execution Count:455
455
229 } -
230#if defined (QTCPSERVER_DEBUG) -
231 qDebug("QTcpServerPrivate::_q_processIncomingConnection() accepted socket %i", descriptor); -
232#endif -
233 q->incomingConnection(descriptor);
executed (the execution status of this line is deduced): q->incomingConnection(descriptor);
-
234 -
235 QPointer<QTcpServer> that = q;
executed (the execution status of this line is deduced): QPointer<QTcpServer> that = q;
-
236 emit q->newConnection();
executed (the execution status of this line is deduced): q->newConnection();
-
237 if (!that || !q->isListening())
partially evaluated: !that
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:466
evaluated: !q->isListening()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:465
0-466
238 return;
executed: return;
Execution Count:1
1
239 }
executed: }
Execution Count:465
465
240}
executed: }
Execution Count:455
455
241 -
242/*! -
243 Constructs a QTcpServer object. -
244 -
245 \a parent is passed to the QObject constructor. -
246 -
247 \sa listen(), setSocketDescriptor() -
248*/ -
249QTcpServer::QTcpServer(QObject *parent) -
250 : QObject(*new QTcpServerPrivate, parent) -
251{ -
252}
executed: }
Execution Count:491
491
253 -
254/*! -
255 Destroys the QTcpServer object. If the server is listening for -
256 connections, the socket is automatically closed. -
257 -
258 Any client \l{QTcpSocket}s that are still connected must either -
259 disconnect or be reparented before the server is deleted. -
260 -
261 \sa close() -
262*/ -
263QTcpServer::~QTcpServer() -
264{ -
265 close();
executed (the execution status of this line is deduced): close();
-
266}
executed: }
Execution Count:489
489
267 -
268/*! -
269 Tells the server to listen for incoming connections on address \a -
270 address and port \a port. If \a port is 0, a port is chosen -
271 automatically. If \a address is QHostAddress::Any, the server -
272 will listen on all network interfaces. -
273 -
274 Returns true on success; otherwise returns false. -
275 -
276 \sa isListening() -
277*/ -
278bool QTcpServer::listen(const QHostAddress &address, quint16 port) -
279{ -
280 Q_D(QTcpServer);
executed (the execution status of this line is deduced): QTcpServerPrivate * const d = d_func();
-
281 if (d->state == QAbstractSocket::ListeningState) {
evaluated: d->state == QAbstractSocket::ListeningState
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:448
2-448
282 qWarning("QTcpServer::listen() called when already listening");
executed (the execution status of this line is deduced): QMessageLogger("socket/qtcpserver.cpp", 282, __PRETTY_FUNCTION__).warning("QTcpServer::listen() called when already listening");
-
283 return false;
executed: return false;
Execution Count:2
2
284 } -
285 -
286 QAbstractSocket::NetworkLayerProtocol proto = address.protocol();
executed (the execution status of this line is deduced): QAbstractSocket::NetworkLayerProtocol proto = address.protocol();
-
287 QHostAddress addr = address;
executed (the execution status of this line is deduced): QHostAddress addr = address;
-
288 -
289#ifdef QT_NO_NETWORKPROXY -
290 static const QNetworkProxy &proxy = *(QNetworkProxy *)0; -
291#else -
292 QNetworkProxy proxy = d->resolveProxy(addr, port);
executed (the execution status of this line is deduced): QNetworkProxy proxy = d->resolveProxy(addr, port);
-
293#endif -
294 -
295 delete d->socketEngine;
executed (the execution status of this line is deduced): delete d->socketEngine;
-
296 d->socketEngine = QAbstractSocketEngine::createSocketEngine(QAbstractSocket::TcpSocket, proxy, this);
executed (the execution status of this line is deduced): d->socketEngine = QAbstractSocketEngine::createSocketEngine(QAbstractSocket::TcpSocket, proxy, this);
-
297 if (!d->socketEngine) {
evaluated: !d->socketEngine
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:442
6-442
298 d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError;
executed (the execution status of this line is deduced): d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError;
-
299 d->serverSocketErrorString = tr("Operation on socket is not supported");
executed (the execution status of this line is deduced): d->serverSocketErrorString = tr("Operation on socket is not supported");
-
300 return false;
executed: return false;
Execution Count:6
6
301 } -
302#ifndef QT_NO_BEARERMANAGEMENT -
303 //copy network session down to the socket engine (if it has been set) -
304 d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));
executed (the execution status of this line is deduced): d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));
-
305#endif -
306 if (!d->socketEngine->initialize(QAbstractSocket::TcpSocket, proto)) {
partially evaluated: !d->socketEngine->initialize(QAbstractSocket::TcpSocket, proto)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:442
0-442
307 d->serverSocketError = d->socketEngine->error();
never executed (the execution status of this line is deduced): d->serverSocketError = d->socketEngine->error();
-
308 d->serverSocketErrorString = d->socketEngine->errorString();
never executed (the execution status of this line is deduced): d->serverSocketErrorString = d->socketEngine->errorString();
-
309 return false;
never executed: return false;
0
310 } -
311 proto = d->socketEngine->protocol();
executed (the execution status of this line is deduced): proto = d->socketEngine->protocol();
-
312 if (addr.protocol() == QAbstractSocket::AnyIPProtocol && proto == QAbstractSocket::IPv4Protocol)
evaluated: addr.protocol() == QAbstractSocket::AnyIPProtocol
TRUEFALSE
yes
Evaluation Count:272
yes
Evaluation Count:170
partially evaluated: proto == QAbstractSocket::IPv4Protocol
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:272
0-272
313 addr = QHostAddress::AnyIPv4;
never executed: addr = QHostAddress::AnyIPv4;
0
314 -
315#if defined(Q_OS_UNIX) -
316 // Under Unix, we want to be able to bind to the port, even if a socket on -
317 // the same address-port is in TIME_WAIT. Under Windows this is possible -
318 // anyway -- furthermore, the meaning of reusable on Windows is different: -
319 // it means that you can use the same address-port for multiple listening -
320 // sockets. -
321 // Don't abort though if we can't set that option. For example the socks -
322 // engine doesn't support that option, but that shouldn't prevent us from -
323 // trying to bind/listen. -
324 d->socketEngine->setOption(QAbstractSocketEngine::AddressReusable, 1);
executed (the execution status of this line is deduced): d->socketEngine->setOption(QAbstractSocketEngine::AddressReusable, 1);
-
325#endif -
326 -
327 if (!d->socketEngine->bind(addr, port)) {
evaluated: !d->socketEngine->bind(addr, port)
TRUEFALSE
yes
Evaluation Count:11
yes
Evaluation Count:431
11-431
328 d->serverSocketError = d->socketEngine->error();
executed (the execution status of this line is deduced): d->serverSocketError = d->socketEngine->error();
-
329 d->serverSocketErrorString = d->socketEngine->errorString();
executed (the execution status of this line is deduced): d->serverSocketErrorString = d->socketEngine->errorString();
-
330 return false;
executed: return false;
Execution Count:11
11
331 } -
332 -
333 if (!d->socketEngine->listen()) {
partially evaluated: !d->socketEngine->listen()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:431
0-431
334 d->serverSocketError = d->socketEngine->error();
never executed (the execution status of this line is deduced): d->serverSocketError = d->socketEngine->error();
-
335 d->serverSocketErrorString = d->socketEngine->errorString();
never executed (the execution status of this line is deduced): d->serverSocketErrorString = d->socketEngine->errorString();
-
336 return false;
never executed: return false;
0
337 } -
338 -
339 d->socketEngine->setReceiver(d);
executed (the execution status of this line is deduced): d->socketEngine->setReceiver(d);
-
340 d->socketEngine->setReadNotificationEnabled(true);
executed (the execution status of this line is deduced): d->socketEngine->setReadNotificationEnabled(true);
-
341 -
342 d->state = QAbstractSocket::ListeningState;
executed (the execution status of this line is deduced): d->state = QAbstractSocket::ListeningState;
-
343 d->address = d->socketEngine->localAddress();
executed (the execution status of this line is deduced): d->address = d->socketEngine->localAddress();
-
344 d->port = d->socketEngine->localPort();
executed (the execution status of this line is deduced): d->port = d->socketEngine->localPort();
-
345 -
346#if defined (QTCPSERVER_DEBUG) -
347 qDebug("QTcpServer::listen(%i, \"%s\") == true (listening on port %i)", port, -
348 address.toString().toLatin1().constData(), d->socketEngine->localPort()); -
349#endif -
350 return true;
executed: return true;
Execution Count:431
431
351} -
352 -
353/*! -
354 Returns true if the server is currently listening for incoming -
355 connections; otherwise returns false. -
356 -
357 \sa listen() -
358*/ -
359bool QTcpServer::isListening() const -
360{ -
361 Q_D(const QTcpServer);
executed (the execution status of this line is deduced): const QTcpServerPrivate * const d = d_func();
-
362 Q_CHECK_SOCKETENGINE(false);
executed: return false;
Execution Count:17
executed: }
Execution Count:470
evaluated: !d->socketEngine
TRUEFALSE
yes
Evaluation Count:17
yes
Evaluation Count:470
partially evaluated: 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:470
0-470
363 return d->socketEngine->state() == QAbstractSocket::ListeningState;
executed: return d->socketEngine->state() == QAbstractSocket::ListeningState;
Execution Count:470
470
364} -
365 -
366/*! -
367 Closes the server. The server will no longer listen for incoming -
368 connections. -
369 -
370 \sa listen() -
371*/ -
372void QTcpServer::close() -
373{ -
374 Q_D(QTcpServer);
executed (the execution status of this line is deduced): QTcpServerPrivate * const d = d_func();
-
375 -
376 qDeleteAll(d->pendingConnections);
executed (the execution status of this line is deduced): qDeleteAll(d->pendingConnections);
-
377 d->pendingConnections.clear();
executed (the execution status of this line is deduced): d->pendingConnections.clear();
-
378 -
379 if (d->socketEngine) {
evaluated: d->socketEngine
TRUEFALSE
yes
Evaluation Count:444
yes
Evaluation Count:55
55-444
380 d->socketEngine->close();
executed (the execution status of this line is deduced): d->socketEngine->close();
-
381 QT_TRY {
partially evaluated: true
TRUEFALSE
yes
Evaluation Count:444
no
Evaluation Count:0
0-444
382 d->socketEngine->deleteLater();
executed (the execution status of this line is deduced): d->socketEngine->deleteLater();
-
383 } QT_CATCH(const std::bad_alloc &) {
executed: }
Execution Count:444
444
384 // in out of memory situations, the socketEngine -
385 // will be deleted in ~QTcpServer (it's a child-object of this) -
386 }
never executed: }
0
387 d->socketEngine = 0;
executed (the execution status of this line is deduced): d->socketEngine = 0;
-
388 }
executed: }
Execution Count:444
444
389 -
390 d->state = QAbstractSocket::UnconnectedState;
executed (the execution status of this line is deduced): d->state = QAbstractSocket::UnconnectedState;
-
391}
executed: }
Execution Count:499
499
392 -
393/*! -
394 Returns the native socket descriptor the server uses to listen -
395 for incoming instructions, or -1 if the server is not listening. -
396 -
397 If the server is using QNetworkProxy, the returned descriptor may -
398 not be usable with native socket functions. -
399 -
400 \sa setSocketDescriptor(), isListening() -
401*/ -
402qintptr QTcpServer::socketDescriptor() const -
403{ -
404 Q_D(const QTcpServer);
executed (the execution status of this line is deduced): const QTcpServerPrivate * const d = d_func();
-
405 Q_CHECK_SOCKETENGINE(-1);
executed: return -1;
Execution Count:2
never executed: }
partially evaluated: !d->socketEngine
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
never evaluated: 0
0-2
406 return d->socketEngine->socketDescriptor();
never executed: return d->socketEngine->socketDescriptor();
0
407} -
408 -
409/*! -
410 Sets the socket descriptor this server should use when listening -
411 for incoming connections to \a socketDescriptor. Returns true if -
412 the socket is set successfully; otherwise returns false. -
413 -
414 The socket is assumed to be in listening state. -
415 -
416 \sa socketDescriptor(), isListening() -
417*/ -
418bool QTcpServer::setSocketDescriptor(qintptr socketDescriptor) -
419{ -
420 Q_D(QTcpServer);
executed (the execution status of this line is deduced): QTcpServerPrivate * const d = d_func();
-
421 if (isListening()) {
partially evaluated: isListening()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4
0-4
422 qWarning("QTcpServer::setSocketDescriptor() called when already listening");
never executed (the execution status of this line is deduced): QMessageLogger("socket/qtcpserver.cpp", 422, __PRETTY_FUNCTION__).warning("QTcpServer::setSocketDescriptor() called when already listening");
-
423 return false;
never executed: return false;
0
424 } -
425 -
426 if (d->socketEngine)
evaluated: d->socketEngine
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
427 delete d->socketEngine;
executed: delete d->socketEngine;
Execution Count:2
2
428 d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this);
executed (the execution status of this line is deduced): d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this);
-
429 if (!d->socketEngine) {
partially evaluated: !d->socketEngine
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4
0-4
430 d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError;
never executed (the execution status of this line is deduced): d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError;
-
431 d->serverSocketErrorString = tr("Operation on socket is not supported");
never executed (the execution status of this line is deduced): d->serverSocketErrorString = tr("Operation on socket is not supported");
-
432 return false;
never executed: return false;
0
433 } -
434#ifndef QT_NO_BEARERMANAGEMENT -
435 //copy network session down to the socket engine (if it has been set) -
436 d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));
executed (the execution status of this line is deduced): d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));
-
437#endif -
438 if (!d->socketEngine->initialize(socketDescriptor, QAbstractSocket::ListeningState)) {
evaluated: !d->socketEngine->initialize(socketDescriptor, QAbstractSocket::ListeningState)
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
439 d->serverSocketError = d->socketEngine->error();
executed (the execution status of this line is deduced): d->serverSocketError = d->socketEngine->error();
-
440 d->serverSocketErrorString = d->socketEngine->errorString();
executed (the execution status of this line is deduced): d->serverSocketErrorString = d->socketEngine->errorString();
-
441#if defined (QTCPSERVER_DEBUG) -
442 qDebug("QTcpServer::setSocketDescriptor(%i) failed (%s)", socketDescriptor, -
443 d->serverSocketErrorString.toLatin1().constData()); -
444#endif -
445 return false;
executed: return false;
Execution Count:2
2
446 } -
447 -
448 d->socketEngine->setReceiver(d);
executed (the execution status of this line is deduced): d->socketEngine->setReceiver(d);
-
449 d->socketEngine->setReadNotificationEnabled(true);
executed (the execution status of this line is deduced): d->socketEngine->setReadNotificationEnabled(true);
-
450 -
451 d->state = d->socketEngine->state();
executed (the execution status of this line is deduced): d->state = d->socketEngine->state();
-
452 d->address = d->socketEngine->localAddress();
executed (the execution status of this line is deduced): d->address = d->socketEngine->localAddress();
-
453 d->port = d->socketEngine->localPort();
executed (the execution status of this line is deduced): d->port = d->socketEngine->localPort();
-
454 -
455#if defined (QTCPSERVER_DEBUG) -
456 qDebug("QTcpServer::setSocketDescriptor(%i) succeeded.", socketDescriptor); -
457#endif -
458 return true;
executed: return true;
Execution Count:2
2
459} -
460 -
461/*! -
462 Returns the server's port if the server is listening for -
463 connections; otherwise returns 0. -
464 -
465 \sa serverAddress(), listen() -
466*/ -
467quint16 QTcpServer::serverPort() const -
468{ -
469 Q_D(const QTcpServer);
executed (the execution status of this line is deduced): const QTcpServerPrivate * const d = d_func();
-
470 Q_CHECK_SOCKETENGINE(0);
executed: return 0;
Execution Count:2
executed: }
Execution Count:431
evaluated: !d->socketEngine
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:431
partially evaluated: 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:431
0-431
471 return d->socketEngine->localPort();
executed: return d->socketEngine->localPort();
Execution Count:431
431
472} -
473 -
474/*! -
475 Returns the server's address if the server is listening for -
476 connections; otherwise returns QHostAddress::Null. -
477 -
478 \sa serverPort(), listen() -
479*/ -
480QHostAddress QTcpServer::serverAddress() const -
481{ -
482 Q_D(const QTcpServer);
executed (the execution status of this line is deduced): const QTcpServerPrivate * const d = d_func();
-
483 Q_CHECK_SOCKETENGINE(QHostAddress(QHostAddress::Null));
executed: return QHostAddress(QHostAddress::Null);
Execution Count:2
executed: }
Execution Count:17
evaluated: !d->socketEngine
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:17
partially evaluated: 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:17
0-17
484 return d->socketEngine->localAddress();
executed: return d->socketEngine->localAddress();
Execution Count:17
17
485} -
486 -
487/*! -
488 Waits for at most \a msec milliseconds or until an incoming -
489 connection is available. Returns true if a connection is -
490 available; otherwise returns false. If the operation timed out -
491 and \a timedOut is not 0, *\a timedOut will be set to true. -
492 -
493 This is a blocking function call. Its use is disadvised in a -
494 single-threaded GUI application, since the whole application will -
495 stop responding until the function returns. -
496 waitForNewConnection() is mostly useful when there is no event -
497 loop available. -
498 -
499 The non-blocking alternative is to connect to the newConnection() -
500 signal. -
501 -
502 If msec is -1, this function will not time out. -
503 -
504 \sa hasPendingConnections(), nextPendingConnection() -
505*/ -
506bool QTcpServer::waitForNewConnection(int msec, bool *timedOut) -
507{ -
508 Q_D(QTcpServer);
executed (the execution status of this line is deduced): QTcpServerPrivate * const d = d_func();
-
509 if (d->state != QAbstractSocket::ListeningState)
partially evaluated: d->state != QAbstractSocket::ListeningState
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:283
0-283
510 return false;
never executed: return false;
0
511 -
512 if (!d->socketEngine->waitForRead(msec, timedOut)) {
evaluated: !d->socketEngine->waitForRead(msec, timedOut)
TRUEFALSE
yes
Evaluation Count:35
yes
Evaluation Count:248
35-248
513 d->serverSocketError = d->socketEngine->error();
executed (the execution status of this line is deduced): d->serverSocketError = d->socketEngine->error();
-
514 d->serverSocketErrorString = d->socketEngine->errorString();
executed (the execution status of this line is deduced): d->serverSocketErrorString = d->socketEngine->errorString();
-
515 return false;
executed: return false;
Execution Count:35
35
516 } -
517 -
518 if (timedOut && *timedOut)
evaluated: timedOut
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:247
partially evaluated: *timedOut
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-247
519 return false;
never executed: return false;
0
520 -
521 d->readNotification();
executed (the execution status of this line is deduced): d->readNotification();
-
522 -
523 return true;
executed: return true;
Execution Count:248
248
524} -
525 -
526/*! -
527 Returns true if the server has a pending connection; otherwise -
528 returns false. -
529 -
530 \sa nextPendingConnection(), setMaxPendingConnections() -
531*/ -
532bool QTcpServer::hasPendingConnections() const -
533{ -
534 return !d_func()->pendingConnections.isEmpty();
executed: return !d_func()->pendingConnections.isEmpty();
Execution Count:18
18
535} -
536 -
537/*! -
538 Returns the next pending connection as a connected QTcpSocket -
539 object. -
540 -
541 The socket is created as a child of the server, which means that -
542 it is automatically deleted when the QTcpServer object is -
543 destroyed. It is still a good idea to delete the object -
544 explicitly when you are done with it, to avoid wasting memory. -
545 -
546 0 is returned if this function is called when there are no pending -
547 connections. -
548 -
549 \note The returned QTcpSocket object cannot be used from another -
550 thread. If you want to use an incoming connection from another thread, -
551 you need to override incomingConnection(). -
552 -
553 \sa hasPendingConnections() -
554*/ -
555QTcpSocket *QTcpServer::nextPendingConnection() -
556{ -
557 Q_D(QTcpServer);
executed (the execution status of this line is deduced): QTcpServerPrivate * const d = d_func();
-
558 if (d->pendingConnections.isEmpty())
evaluated: d->pendingConnections.isEmpty()
TRUEFALSE
yes
Evaluation Count:45
yes
Evaluation Count:281
45-281
559 return 0;
executed: return 0;
Execution Count:45
45
560 -
561 if (!d->socketEngine->isReadNotificationEnabled())
evaluated: !d->socketEngine->isReadNotificationEnabled()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:280
1-280
562 d->socketEngine->setReadNotificationEnabled(true);
executed: d->socketEngine->setReadNotificationEnabled(true);
Execution Count:1
1
563 -
564 return d->pendingConnections.takeFirst();
executed: return d->pendingConnections.takeFirst();
Execution Count:281
281
565} -
566 -
567/*! -
568 This virtual function is called by QTcpServer when a new -
569 connection is available. The \a socketDescriptor argument is the -
570 native socket descriptor for the accepted connection. -
571 -
572 The base implementation creates a QTcpSocket, sets the socket -
573 descriptor and then stores the QTcpSocket in an internal list of -
574 pending connections. Finally newConnection() is emitted. -
575 -
576 Reimplement this function to alter the server's behavior when a -
577 connection is available. -
578 -
579 If this server is using QNetworkProxy then the \a socketDescriptor -
580 may not be usable with native socket functions, and should only be -
581 used with QTcpSocket::setSocketDescriptor(). -
582 -
583 \note If you want to handle an incoming connection as a new QTcpSocket -
584 object in another thread you have to pass the socketDescriptor -
585 to the other thread and create the QTcpSocket object there and -
586 use its setSocketDescriptor() method. -
587 -
588 \sa newConnection(), nextPendingConnection(), addPendingConnection() -
589*/ -
590void QTcpServer::incomingConnection(qintptr socketDescriptor) -
591{ -
592#if defined (QTCPSERVER_DEBUG) -
593 qDebug("QTcpServer::incomingConnection(%i)", socketDescriptor); -
594#endif -
595 -
596 QTcpSocket *socket = new QTcpSocket(this);
executed (the execution status of this line is deduced): QTcpSocket *socket = new QTcpSocket(this);
-
597 socket->setSocketDescriptor(socketDescriptor);
executed (the execution status of this line is deduced): socket->setSocketDescriptor(socketDescriptor);
-
598 addPendingConnection(socket);
executed (the execution status of this line is deduced): addPendingConnection(socket);
-
599}
executed: }
Execution Count:286
286
600 -
601/*! -
602 This function is called by QTcpServer::incomingConnection() -
603 to add the \a socket to the list of pending incoming connections. -
604 -
605 \note Don't forget to call this member from reimplemented -
606 incomingConnection() if you do not want to break the -
607 Pending Connections mechanism. -
608 -
609 \sa incomingConnection() -
610 \since 4.7 -
611*/ -
612void QTcpServer::addPendingConnection(QTcpSocket* socket) -
613{ -
614 d_func()->pendingConnections.append(socket);
executed (the execution status of this line is deduced): d_func()->pendingConnections.append(socket);
-
615}
executed: }
Execution Count:286
286
616 -
617/*! -
618 Sets the maximum number of pending accepted connections to \a -
619 numConnections. QTcpServer will accept no more than \a -
620 numConnections incoming connections before -
621 nextPendingConnection() is called. By default, the limit is 30 -
622 pending connections. -
623 -
624 Clients may still able to connect after the server has reached -
625 its maximum number of pending connections (i.e., QTcpSocket can -
626 still emit the connected() signal). QTcpServer will stop -
627 accepting the new connections, but the operating system may -
628 still keep them in queue. -
629 -
630 \sa maxPendingConnections(), hasPendingConnections() -
631*/ -
632void QTcpServer::setMaxPendingConnections(int numConnections) -
633{ -
634 d_func()->maxConnections = numConnections;
executed (the execution status of this line is deduced): d_func()->maxConnections = numConnections;
-
635}
executed: }
Execution Count:7
7
636 -
637/*! -
638 Returns the maximum number of pending accepted connections. The -
639 default is 30. -
640 -
641 \sa setMaxPendingConnections(), hasPendingConnections() -
642*/ -
643int QTcpServer::maxPendingConnections() const -
644{ -
645 return d_func()->maxConnections;
executed: return d_func()->maxConnections;
Execution Count:8
8
646} -
647 -
648/*! -
649 Returns an error code for the last error that occurred. -
650 -
651 \sa errorString() -
652*/ -
653QAbstractSocket::SocketError QTcpServer::serverError() const -
654{ -
655 return d_func()->serverSocketError;
executed: return d_func()->serverSocketError;
Execution Count:17
17
656} -
657 -
658/*! -
659 Returns a human readable description of the last error that -
660 occurred. -
661 -
662 \sa serverError() -
663*/ -
664QString QTcpServer::errorString() const -
665{ -
666 return d_func()->serverSocketErrorString;
executed: return d_func()->serverSocketErrorString;
Execution Count:20
20
667} -
668 -
669/*! -
670 \since 5.0 -
671 -
672 Pauses accepting new connections. Queued connections will remain in queue. -
673 -
674 \sa resumeAccepting() -
675*/ -
676void QTcpServer::pauseAccepting() -
677{ -
678 d_func()->socketEngine->setReadNotificationEnabled(false);
never executed (the execution status of this line is deduced): d_func()->socketEngine->setReadNotificationEnabled(false);
-
679}
never executed: }
0
680 -
681/*! -
682 \since 5.0 -
683 -
684 Resumes accepting new connections. -
685 -
686 \sa pauseAccepting() -
687*/ -
688void QTcpServer::resumeAccepting() -
689{ -
690 d_func()->socketEngine->setReadNotificationEnabled(true);
never executed (the execution status of this line is deduced): d_func()->socketEngine->setReadNotificationEnabled(true);
-
691}
never executed: }
0
692 -
693#ifndef QT_NO_NETWORKPROXY -
694/*! -
695 \since 4.1 -
696 -
697 Sets the explicit network proxy for this socket to \a networkProxy. -
698 -
699 To disable the use of a proxy for this socket, use the -
700 QNetworkProxy::NoProxy proxy type: -
701 -
702 \snippet code/src_network_socket_qtcpserver.cpp 0 -
703 -
704 \sa proxy(), QNetworkProxy -
705*/ -
706void QTcpServer::setProxy(const QNetworkProxy &networkProxy) -
707{ -
708 Q_D(QTcpServer);
executed (the execution status of this line is deduced): QTcpServerPrivate * const d = d_func();
-
709 d->proxy = networkProxy;
executed (the execution status of this line is deduced): d->proxy = networkProxy;
-
710}
executed: }
Execution Count:4
4
711 -
712/*! -
713 \since 4.1 -
714 -
715 Returns the network proxy for this socket. -
716 By default QNetworkProxy::DefaultProxy is used. -
717 -
718 \sa setProxy(), QNetworkProxy -
719*/ -
720QNetworkProxy QTcpServer::proxy() const -
721{ -
722 Q_D(const QTcpServer);
executed (the execution status of this line is deduced): const QTcpServerPrivate * const d = d_func();
-
723 return d->proxy;
executed: return d->proxy;
Execution Count:416
416
724} -
725#endif // QT_NO_NETWORKPROXY -
726 -
727QT_END_NAMESPACE -
728 -
729#include "moc_qtcpserver.cpp" -
730 -
731 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial