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