Line | Source Code | Coverage |
---|
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | class QTcpServerPrivate : public QObjectPrivate, public QAbstractSocketEngineReceiver | - |
10 | { | - |
11 | inline QTcpServer* q_func() { return static_cast<QTcpServer *>(q_ptr); } inline const QTcpServer* q_func() const { return static_cast<const QTcpServer *>(q_ptr); } friend class QTcpServer; | - |
12 | public: | - |
13 | QTcpServerPrivate(); | - |
14 | ~QTcpServerPrivate(); | - |
15 | | - |
16 | QList<QTcpSocket *> pendingConnections; | - |
17 | | - |
18 | quint16 port; | - |
19 | QHostAddress address; | - |
20 | | - |
21 | QAbstractSocket::SocketState state; | - |
22 | QAbstractSocketEngine *socketEngine; | - |
23 | | - |
24 | QAbstractSocket::SocketError serverSocketError; | - |
25 | QString serverSocketErrorString; | - |
26 | | - |
27 | int maxConnections; | - |
28 | | - |
29 | | - |
30 | QNetworkProxy proxy; | - |
31 | QNetworkProxy resolveProxy(const QHostAddress &address, quint16 port); | - |
32 | | - |
33 | | - |
34 | | - |
35 | void readNotification(); | - |
36 | void closeNotification() { readNotification(); } | 0 |
37 | inline void writeNotification() {} | - |
38 | inline void exceptionNotification() {} | - |
39 | inline void connectionNotification() {} | - |
40 | | - |
41 | inline void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *) {} | - |
42 | | - |
43 | | - |
44 | }; | - |
45 | | - |
46 | | - |
47 | | - |
48 | QTcpServerPrivate::QTcpServerPrivate() | - |
49 | : port(0) | - |
50 | , state(QAbstractSocket::UnconnectedState) | - |
51 | , socketEngine(0) | - |
52 | , serverSocketError(QAbstractSocket::UnknownSocketError) | - |
53 | , maxConnections(30) | - |
54 | { | - |
55 | } executed: } Execution Count:491 | 491 |
56 | | - |
57 | | - |
58 | | - |
59 | QTcpServerPrivate::~QTcpServerPrivate() | - |
60 | { | - |
61 | } | - |
62 | | - |
63 | | - |
64 | | - |
65 | | - |
66 | | - |
67 | | - |
68 | QNetworkProxy QTcpServerPrivate::resolveProxy(const QHostAddress &address, quint16 port) | - |
69 | { | - |
70 | if (address.isLoopback()) evaluated: address.isLoopback() yes Evaluation Count:18 | yes Evaluation Count:430 |
| 18-430 |
71 | return QNetworkProxy::NoProxy; executed: return QNetworkProxy::NoProxy; Execution Count:18 | 18 |
72 | | - |
73 | QList<QNetworkProxy> proxies; | - |
74 | if (proxy.type() != QNetworkProxy::DefaultProxy) { evaluated: proxy.type() != QNetworkProxy::DefaultProxy yes Evaluation Count:4 | yes Evaluation Count:426 |
| 4-426 |
75 | | - |
76 | proxies << proxy; | - |
77 | } else { executed: } Execution Count:4 | 4 |
78 | | - |
79 | QNetworkProxyQuery query(port, QString(), QNetworkProxyQuery::TcpServer); | - |
80 | proxies = QNetworkProxyFactory::proxyForQuery(query); | - |
81 | } executed: } Execution Count:426 | 426 |
82 | | - |
83 | | - |
84 | 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;})) { | - |
85 | if (p.capabilities() & QNetworkProxy::ListeningCapability) evaluated: p.capabilities() & QNetworkProxy::ListeningCapability yes Evaluation Count:424 | yes Evaluation Count:10 |
| 10-424 |
86 | return p; executed: return p; Execution Count:424 | 424 |
87 | } executed: } Execution Count:10 | 10 |
88 | | - |
89 | | - |
90 | | - |
91 | return QNetworkProxy(QNetworkProxy::DefaultProxy); executed: return QNetworkProxy(QNetworkProxy::DefaultProxy); Execution Count:6 | 6 |
92 | } | - |
93 | | - |
94 | | - |
95 | | - |
96 | | - |
97 | void QTcpServerPrivate::readNotification() | - |
98 | { | - |
99 | QTcpServer * const q = q_func(); | - |
100 | for (;;) { | - |
101 | if (pendingConnections.count() >= maxConnections) { evaluated: pendingConnections.count() >= maxConnections yes Evaluation Count:1 | yes Evaluation Count:921 |
| 1-921 |
102 | | - |
103 | | - |
104 | | - |
105 | if (socketEngine->isReadNotificationEnabled()) partially evaluated: socketEngine->isReadNotificationEnabled() yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
106 | socketEngine->setReadNotificationEnabled(false); executed: socketEngine->setReadNotificationEnabled(false); Execution Count:1 | 1 |
107 | return; executed: return; Execution Count:1 | 1 |
108 | } | - |
109 | | - |
110 | int descriptor = socketEngine->accept(); | - |
111 | if (descriptor == -1) { evaluated: descriptor == -1 yes Evaluation Count:455 | yes Evaluation Count:466 |
| 455-466 |
112 | if (socketEngine->error() != QAbstractSocket::TemporaryError) { partially evaluated: socketEngine->error() != QAbstractSocket::TemporaryError no Evaluation Count:0 | yes Evaluation Count:455 |
| 0-455 |
113 | q->pauseAccepting(); | - |
114 | serverSocketError = socketEngine->error(); | - |
115 | serverSocketErrorString = socketEngine->errorString(); | - |
116 | q->acceptError(serverSocketError); | - |
117 | } | 0 |
118 | break; executed: break; Execution Count:455 | 455 |
119 | } | - |
120 | | - |
121 | | - |
122 | | - |
123 | q->incomingConnection(descriptor); | - |
124 | | - |
125 | QPointer<QTcpServer> that = q; | - |
126 | q->newConnection(); | - |
127 | if (!that || !q->isListening()) partially evaluated: !that no Evaluation Count:0 | yes Evaluation Count:466 |
evaluated: !q->isListening() yes Evaluation Count:1 | yes Evaluation Count:465 |
| 0-466 |
128 | return; executed: return; Execution Count:1 | 1 |
129 | } executed: } Execution Count:465 | 465 |
130 | } executed: } Execution Count:455 | 455 |
131 | QTcpServer::QTcpServer(QObject *parent) | - |
132 | : QObject(*new QTcpServerPrivate, parent) | - |
133 | { | - |
134 | } executed: } Execution Count:491 | 491 |
135 | QTcpServer::~QTcpServer() | - |
136 | { | - |
137 | close(); | - |
138 | } executed: } Execution Count:489 | 489 |
139 | bool QTcpServer::listen(const QHostAddress &address, quint16 port) | - |
140 | { | - |
141 | QTcpServerPrivate * const d = d_func(); | - |
142 | if (d->state == QAbstractSocket::ListeningState) { evaluated: d->state == QAbstractSocket::ListeningState yes Evaluation Count:2 | yes Evaluation Count:448 |
| 2-448 |
143 | QMessageLogger("socket/qtcpserver.cpp", 282, __PRETTY_FUNCTION__).warning("QTcpServer::listen() called when already listening"); | - |
144 | return false; executed: return false; Execution Count:2 | 2 |
145 | } | - |
146 | | - |
147 | QAbstractSocket::NetworkLayerProtocol proto = address.protocol(); | - |
148 | QHostAddress addr = address; | - |
149 | | - |
150 | | - |
151 | | - |
152 | | - |
153 | QNetworkProxy proxy = d->resolveProxy(addr, port); | - |
154 | | - |
155 | | - |
156 | delete d->socketEngine; | - |
157 | d->socketEngine = QAbstractSocketEngine::createSocketEngine(QAbstractSocket::TcpSocket, proxy, this); | - |
158 | if (!d->socketEngine) { evaluated: !d->socketEngine yes Evaluation Count:6 | yes Evaluation Count:442 |
| 6-442 |
159 | d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError; | - |
160 | d->serverSocketErrorString = tr("Operation on socket is not supported"); | - |
161 | return false; executed: return false; Execution Count:6 | 6 |
162 | } | - |
163 | | - |
164 | | - |
165 | d->socketEngine->setProperty("_q_networksession", property("_q_networksession")); | - |
166 | | - |
167 | if (!d->socketEngine->initialize(QAbstractSocket::TcpSocket, proto)) { partially evaluated: !d->socketEngine->initialize(QAbstractSocket::TcpSocket, proto) no Evaluation Count:0 | yes Evaluation Count:442 |
| 0-442 |
168 | d->serverSocketError = d->socketEngine->error(); | - |
169 | d->serverSocketErrorString = d->socketEngine->errorString(); | - |
170 | return false; never executed: return false; | 0 |
171 | } | - |
172 | proto = d->socketEngine->protocol(); | - |
173 | if (addr.protocol() == QAbstractSocket::AnyIPProtocol && proto == QAbstractSocket::IPv4Protocol) evaluated: addr.protocol() == QAbstractSocket::AnyIPProtocol yes Evaluation Count:272 | yes Evaluation Count:170 |
partially evaluated: proto == QAbstractSocket::IPv4Protocol no Evaluation Count:0 | yes Evaluation Count:272 |
| 0-272 |
174 | addr = QHostAddress::AnyIPv4; never executed: addr = QHostAddress::AnyIPv4; | 0 |
175 | d->socketEngine->setOption(QAbstractSocketEngine::AddressReusable, 1); | - |
176 | | - |
177 | | - |
178 | if (!d->socketEngine->bind(addr, port)) { evaluated: !d->socketEngine->bind(addr, port) yes Evaluation Count:11 | yes Evaluation Count:431 |
| 11-431 |
179 | d->serverSocketError = d->socketEngine->error(); | - |
180 | d->serverSocketErrorString = d->socketEngine->errorString(); | - |
181 | return false; executed: return false; Execution Count:11 | 11 |
182 | } | - |
183 | | - |
184 | if (!d->socketEngine->listen()) { partially evaluated: !d->socketEngine->listen() no Evaluation Count:0 | yes Evaluation Count:431 |
| 0-431 |
185 | d->serverSocketError = d->socketEngine->error(); | - |
186 | d->serverSocketErrorString = d->socketEngine->errorString(); | - |
187 | return false; never executed: return false; | 0 |
188 | } | - |
189 | | - |
190 | d->socketEngine->setReceiver(d); | - |
191 | d->socketEngine->setReadNotificationEnabled(true); | - |
192 | | - |
193 | d->state = QAbstractSocket::ListeningState; | - |
194 | d->address = d->socketEngine->localAddress(); | - |
195 | d->port = d->socketEngine->localPort(); | - |
196 | | - |
197 | | - |
198 | | - |
199 | | - |
200 | | - |
201 | return true; executed: return true; Execution Count:431 | 431 |
202 | } | - |
203 | | - |
204 | | - |
205 | | - |
206 | | - |
207 | | - |
208 | | - |
209 | | - |
210 | bool QTcpServer::isListening() const | - |
211 | { | - |
212 | const QTcpServerPrivate * const d = d_func(); | - |
213 | do { if (!d->socketEngine) { return false; } } while (0); executed: return false; Execution Count:17 executed: } Execution Count:470 evaluated: !d->socketEngine yes Evaluation Count:17 | yes Evaluation Count:470 |
partially evaluated: 0 no Evaluation Count:0 | yes Evaluation Count:470 |
| 0-470 |
214 | return d->socketEngine->state() == QAbstractSocket::ListeningState; executed: return d->socketEngine->state() == QAbstractSocket::ListeningState; Execution Count:470 | 470 |
215 | } | - |
216 | | - |
217 | | - |
218 | | - |
219 | | - |
220 | | - |
221 | | - |
222 | | - |
223 | void QTcpServer::close() | - |
224 | { | - |
225 | QTcpServerPrivate * const d = d_func(); | - |
226 | | - |
227 | qDeleteAll(d->pendingConnections); | - |
228 | d->pendingConnections.clear(); | - |
229 | | - |
230 | if (d->socketEngine) { evaluated: d->socketEngine yes Evaluation Count:444 | yes Evaluation Count:55 |
| 55-444 |
231 | d->socketEngine->close(); | - |
232 | if (true) { partially evaluated: true yes Evaluation Count:444 | no Evaluation Count:0 |
| 0-444 |
233 | d->socketEngine->deleteLater(); | - |
234 | } else { executed: } Execution Count:444 | 444 |
235 | | - |
236 | | - |
237 | } | 0 |
238 | d->socketEngine = 0; | - |
239 | } executed: } Execution Count:444 | 444 |
240 | | - |
241 | d->state = QAbstractSocket::UnconnectedState; | - |
242 | } executed: } Execution Count:499 | 499 |
243 | qintptr QTcpServer::socketDescriptor() const | - |
244 | { | - |
245 | const QTcpServerPrivate * const d = d_func(); | - |
246 | do { if (!d->socketEngine) { return -1; } } while (0); executed: return -1; Execution Count:2 partially evaluated: !d->socketEngine yes Evaluation Count:2 | no Evaluation Count:0 |
| 0-2 |
247 | return d->socketEngine->socketDescriptor(); never executed: return d->socketEngine->socketDescriptor(); | 0 |
248 | } | - |
249 | bool QTcpServer::setSocketDescriptor(qintptr socketDescriptor) | - |
250 | { | - |
251 | QTcpServerPrivate * const d = d_func(); | - |
252 | if (isListening()) { partially evaluated: isListening() no Evaluation Count:0 | yes Evaluation Count:4 |
| 0-4 |
253 | QMessageLogger("socket/qtcpserver.cpp", 422, __PRETTY_FUNCTION__).warning("QTcpServer::setSocketDescriptor() called when already listening"); | - |
254 | return false; never executed: return false; | 0 |
255 | } | - |
256 | | - |
257 | if (d->socketEngine) evaluated: d->socketEngine yes Evaluation Count:2 | yes Evaluation Count:2 |
| 2 |
258 | delete d->socketEngine; executed: delete d->socketEngine; Execution Count:2 | 2 |
259 | d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this); | - |
260 | if (!d->socketEngine) { partially evaluated: !d->socketEngine no Evaluation Count:0 | yes Evaluation Count:4 |
| 0-4 |
261 | d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError; | - |
262 | d->serverSocketErrorString = tr("Operation on socket is not supported"); | - |
263 | return false; never executed: return false; | 0 |
264 | } | - |
265 | | - |
266 | | - |
267 | d->socketEngine->setProperty("_q_networksession", property("_q_networksession")); | - |
268 | | - |
269 | if (!d->socketEngine->initialize(socketDescriptor, QAbstractSocket::ListeningState)) { evaluated: !d->socketEngine->initialize(socketDescriptor, QAbstractSocket::ListeningState) yes Evaluation Count:2 | yes Evaluation Count:2 |
| 2 |
270 | d->serverSocketError = d->socketEngine->error(); | - |
271 | d->serverSocketErrorString = d->socketEngine->errorString(); | - |
272 | | - |
273 | | - |
274 | | - |
275 | | - |
276 | return false; executed: return false; Execution Count:2 | 2 |
277 | } | - |
278 | | - |
279 | d->socketEngine->setReceiver(d); | - |
280 | d->socketEngine->setReadNotificationEnabled(true); | - |
281 | | - |
282 | d->state = d->socketEngine->state(); | - |
283 | d->address = d->socketEngine->localAddress(); | - |
284 | d->port = d->socketEngine->localPort(); | - |
285 | | - |
286 | | - |
287 | | - |
288 | | - |
289 | return true; executed: return true; Execution Count:2 | 2 |
290 | } | - |
291 | | - |
292 | | - |
293 | | - |
294 | | - |
295 | | - |
296 | | - |
297 | | - |
298 | quint16 QTcpServer::serverPort() const | - |
299 | { | - |
300 | const QTcpServerPrivate * const d = d_func(); | - |
301 | do { if (!d->socketEngine) { return 0; } } while (0); executed: return 0; Execution Count:2 executed: } Execution Count:431 evaluated: !d->socketEngine yes Evaluation Count:2 | yes Evaluation Count:431 |
partially evaluated: 0 no Evaluation Count:0 | yes Evaluation Count:431 |
| 0-431 |
302 | return d->socketEngine->localPort(); executed: return d->socketEngine->localPort(); Execution Count:431 | 431 |
303 | } | - |
304 | | - |
305 | | - |
306 | | - |
307 | | - |
308 | | - |
309 | | - |
310 | | - |
311 | QHostAddress QTcpServer::serverAddress() const | - |
312 | { | - |
313 | const QTcpServerPrivate * const d = d_func(); | - |
314 | do { if (!d->socketEngine) { return QHostAddress(QHostAddress::Null); } } while (0); partially evaluated: 0 no Evaluation Count:0 | yes Evaluation Count:17 |
executed: return QHostAddress(QHostAddress::Null); Execution Count:2 executed: } Execution Count:17 evaluated: !d->socketEngine yes Evaluation Count:2 | yes Evaluation Count:17 |
| 0-17 |
315 | return d->socketEngine->localAddress(); executed: return d->socketEngine->localAddress(); Execution Count:17 | 17 |
316 | } | - |
317 | bool QTcpServer::waitForNewConnection(int msec, bool *timedOut) | - |
318 | { | - |
319 | QTcpServerPrivate * const d = d_func(); | - |
320 | if (d->state != QAbstractSocket::ListeningState) partially evaluated: d->state != QAbstractSocket::ListeningState no Evaluation Count:0 | yes Evaluation Count:283 |
| 0-283 |
321 | return false; never executed: return false; | 0 |
322 | | - |
323 | if (!d->socketEngine->waitForRead(msec, timedOut)) { evaluated: !d->socketEngine->waitForRead(msec, timedOut) yes Evaluation Count:35 | yes Evaluation Count:248 |
| 35-248 |
324 | d->serverSocketError = d->socketEngine->error(); | - |
325 | d->serverSocketErrorString = d->socketEngine->errorString(); | - |
326 | return false; executed: return false; Execution Count:35 | 35 |
327 | } | - |
328 | | - |
329 | if (timedOut && *timedOut) evaluated: timedOut yes Evaluation Count:1 | yes Evaluation Count:247 |
partially evaluated: *timedOut no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-247 |
330 | return false; never executed: return false; | 0 |
331 | | - |
332 | d->readNotification(); | - |
333 | | - |
334 | return true; executed: return true; Execution Count:248 | 248 |
335 | } | - |
336 | | - |
337 | | - |
338 | | - |
339 | | - |
340 | | - |
341 | | - |
342 | | - |
343 | bool QTcpServer::hasPendingConnections() const | - |
344 | { | - |
345 | return !d_func()->pendingConnections.isEmpty(); executed: return !d_func()->pendingConnections.isEmpty(); Execution Count:18 | 18 |
346 | } | - |
347 | QTcpSocket *QTcpServer::nextPendingConnection() | - |
348 | { | - |
349 | QTcpServerPrivate * const d = d_func(); | - |
350 | if (d->pendingConnections.isEmpty()) evaluated: d->pendingConnections.isEmpty() yes Evaluation Count:45 | yes Evaluation Count:281 |
| 45-281 |
351 | return 0; executed: return 0; Execution Count:45 | 45 |
352 | | - |
353 | if (!d->socketEngine->isReadNotificationEnabled()) evaluated: !d->socketEngine->isReadNotificationEnabled() yes Evaluation Count:1 | yes Evaluation Count:280 |
| 1-280 |
354 | d->socketEngine->setReadNotificationEnabled(true); executed: d->socketEngine->setReadNotificationEnabled(true); Execution Count:1 | 1 |
355 | | - |
356 | return d->pendingConnections.takeFirst(); executed: return d->pendingConnections.takeFirst(); Execution Count:281 | 281 |
357 | } | - |
358 | void QTcpServer::incomingConnection(qintptr socketDescriptor) | - |
359 | { | - |
360 | | - |
361 | | - |
362 | | - |
363 | | - |
364 | QTcpSocket *socket = new QTcpSocket(this); | - |
365 | socket->setSocketDescriptor(socketDescriptor); | - |
366 | addPendingConnection(socket); | - |
367 | } executed: } Execution Count:286 | 286 |
368 | void QTcpServer::addPendingConnection(QTcpSocket* socket) | - |
369 | { | - |
370 | d_func()->pendingConnections.append(socket); | - |
371 | } executed: } Execution Count:286 | 286 |
372 | void QTcpServer::setMaxPendingConnections(int numConnections) | - |
373 | { | - |
374 | d_func()->maxConnections = numConnections; | - |
375 | } executed: } Execution Count:7 | 7 |
376 | | - |
377 | | - |
378 | | - |
379 | | - |
380 | | - |
381 | | - |
382 | | - |
383 | int QTcpServer::maxPendingConnections() const | - |
384 | { | - |
385 | return d_func()->maxConnections; executed: return d_func()->maxConnections; Execution Count:8 | 8 |
386 | } | - |
387 | | - |
388 | | - |
389 | | - |
390 | | - |
391 | | - |
392 | | - |
393 | QAbstractSocket::SocketError QTcpServer::serverError() const | - |
394 | { | - |
395 | return d_func()->serverSocketError; executed: return d_func()->serverSocketError; Execution Count:17 | 17 |
396 | } | - |
397 | | - |
398 | | - |
399 | | - |
400 | | - |
401 | | - |
402 | | - |
403 | | - |
404 | QString QTcpServer::errorString() const | - |
405 | { | - |
406 | return d_func()->serverSocketErrorString; executed: return d_func()->serverSocketErrorString; Execution Count:20 | 20 |
407 | } | - |
408 | void QTcpServer::pauseAccepting() | - |
409 | { | - |
410 | d_func()->socketEngine->setReadNotificationEnabled(false); | - |
411 | } | 0 |
412 | void QTcpServer::resumeAccepting() | - |
413 | { | - |
414 | d_func()->socketEngine->setReadNotificationEnabled(true); | - |
415 | } | 0 |
416 | void QTcpServer::setProxy(const QNetworkProxy &networkProxy) | - |
417 | { | - |
418 | QTcpServerPrivate * const d = d_func(); | - |
419 | d->proxy = networkProxy; | - |
420 | } executed: } Execution Count:4 | 4 |
421 | QNetworkProxy QTcpServer::proxy() const | - |
422 | { | - |
423 | const QTcpServerPrivate * const d = d_func(); | - |
424 | return d->proxy; executed: return d->proxy; Execution Count:416 | 416 |
425 | } | - |
426 | | - |
427 | | - |
428 | | - |
429 | | - |
430 | | - |
| | |