1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the 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 QNATIVESOCKETENGINE_DEBUG |
43 | |
44 | /*! \class QNativeSocketEngine |
45 | \internal |
46 | |
47 | \brief The QNativeSocketEngine class provides low level access to a socket. |
48 | |
49 | \reentrant |
50 | \ingroup network |
51 | \inmodule QtNetwork |
52 | |
53 | QtSocketLayer provides basic socket functionality provided by the |
54 | operating system. It also keeps track of what state the socket is |
55 | in, and which errors that occur. |
56 | |
57 | The classes QTcpSocket, QUdpSocket and QTcpServer provide a |
58 | higher level API, and are in general more useful for the common |
59 | application. |
60 | |
61 | There are two main ways of initializing the a QNativeSocketEngine; either |
62 | create a new socket by passing the socket type (TcpSocket or |
63 | UdpSocket) and network layer protocol (IPv4Protocol or |
64 | IPv6Protocol) to initialize(), or pass an existing socket |
65 | descriptor and have QNativeSocketEngine determine the type and protocol |
66 | itself. The native socket descriptor can later be fetched by |
67 | calling socketDescriptor(). The socket is made non-blocking, but |
68 | blocking behavior can still be achieved by calling waitForRead() |
69 | and waitForWrite(). isValid() can be called to check if the socket |
70 | has been successfully initialized and is ready to use. |
71 | |
72 | To connect to a host, determine its address and pass this and the |
73 | port number to connectToHost(). The socket can then be used as a |
74 | TCP or UDP client. Otherwise; bind(), listen() and accept() are |
75 | used to have the socket function as a TCP or UDP server. Call |
76 | close() to close the socket. |
77 | |
78 | bytesAvailable() is called to determine how much data is available |
79 | for reading. read() and write() are used by both TCP and UDP |
80 | clients to exchange data with the connected peer. UDP clients can |
81 | also call hasMoreDatagrams(), nextDatagramSize(), |
82 | readDatagram(), and writeDatagram(). |
83 | |
84 | Call state() to determine the state of the socket, for |
85 | example, ListeningState or ConnectedState. socketType() tells |
86 | whether the socket is a TCP socket or a UDP socket, or if the |
87 | socket type is unknown. protocol() is used to determine the |
88 | socket's network layer protocol. |
89 | |
90 | localAddress(), localPort() are called to find the address and |
91 | port that are currently bound to the socket. If the socket is |
92 | connected, peerAddress() and peerPort() determine the address and |
93 | port of the connected peer. |
94 | |
95 | Finally, if any function should fail, error() and |
96 | errorString() can be called to determine the cause of the error. |
97 | */ |
98 | |
99 | #include <qabstracteventdispatcher.h> |
100 | #include <qsocketnotifier.h> |
101 | #include <qnetworkinterface.h> |
102 | |
103 | #include "qnativesocketengine_p.h" |
104 | #include <private/qthread_p.h> |
105 | #include <private/qobject_p.h> |
106 | |
107 | #if !defined(QT_NO_NETWORKPROXY) |
108 | # include "qnetworkproxy.h" |
109 | # include "qabstractsocket.h" |
110 | # include "qtcpserver.h" |
111 | #endif |
112 | |
113 | QT_BEGIN_NAMESPACE |
114 | |
115 | //#define QNATIVESOCKETENGINE_DEBUG |
116 | |
117 | #define Q_VOID |
118 | |
119 | // Common constructs |
120 | #define Q_CHECK_VALID_SOCKETLAYER(function, returnValue) do { \ |
121 | if (!isValid()) { \ |
122 | qWarning(""#function" was called on an uninitialized socket device"); \ |
123 | return returnValue; \ |
124 | } } while (0) |
125 | #define Q_CHECK_INVALID_SOCKETLAYER(function, returnValue) do { \ |
126 | if (isValid()) { \ |
127 | qWarning(""#function" was called on an already initialized socket device"); \ |
128 | return returnValue; \ |
129 | } } while (0) |
130 | #define Q_CHECK_STATE(function, checkState, returnValue) do { \ |
131 | if (d->socketState != (checkState)) { \ |
132 | qWarning(""#function" was not called in "#checkState); \ |
133 | return (returnValue); \ |
134 | } } while (0) |
135 | #define Q_CHECK_NOT_STATE(function, checkState, returnValue) do { \ |
136 | if (d->socketState == (checkState)) { \ |
137 | qWarning(""#function" was called in "#checkState); \ |
138 | return (returnValue); \ |
139 | } } while (0) |
140 | #define Q_CHECK_STATES(function, state1, state2, returnValue) do { \ |
141 | if (d->socketState != (state1) && d->socketState != (state2)) { \ |
142 | qWarning(""#function" was called" \ |
143 | " not in "#state1" or "#state2); \ |
144 | return (returnValue); \ |
145 | } } while (0) |
146 | #define Q_CHECK_TYPE(function, type, returnValue) do { \ |
147 | if (d->socketType != (type)) { \ |
148 | qWarning(#function" was called by a" \ |
149 | " socket other than "#type""); \ |
150 | return (returnValue); \ |
151 | } } while (0) |
152 | #define Q_TR(a) QT_TRANSLATE_NOOP(QNativeSocketEngine, a) |
153 | |
154 | /*! \internal |
155 | Constructs the private class and initializes all data members. |
156 | |
157 | On Windows, WSAStartup is called "recursively" for every |
158 | concurrent QNativeSocketEngine. This is safe, because WSAStartup and |
159 | WSACleanup are reference counted. |
160 | */ |
161 | QNativeSocketEnginePrivate::QNativeSocketEnginePrivate() : |
162 | socketDescriptor(-1), |
163 | readNotifier(0), |
164 | writeNotifier(0), |
165 | exceptNotifier(0) |
166 | { |
167 | } |
168 | |
169 | /*! \internal |
170 | Destructs the private class. |
171 | */ |
172 | QNativeSocketEnginePrivate::~QNativeSocketEnginePrivate() |
173 | { |
174 | } |
175 | |
176 | /*! \internal |
177 | |
178 | Sets the error and error string if not set already. The only |
179 | interesting error is the first one that occurred, and not the last |
180 | one. |
181 | */ |
182 | void QNativeSocketEnginePrivate::setError(QAbstractSocket::SocketError error, ErrorString errorString) const |
183 | { |
184 | if (hasSetSocketError) { |
185 | // Only set socket errors once for one engine; expect the |
186 | // socket to recreate its engine after an error. Note: There's |
187 | // one exception: SocketError(11) bypasses this as it's purely |
188 | // a temporary internal error condition. |
189 | // Another exception is the way the waitFor*() functions set |
190 | // an error when a timeout occurs. After the call to setError() |
191 | // they reset the hasSetSocketError to false |
192 | return; |
193 | } |
194 | if (error != QAbstractSocket::SocketError(11)) |
195 | hasSetSocketError = true; |
196 | |
197 | socketError = error; |
198 | |
199 | switch (errorString) { |
200 | case NonBlockingInitFailedErrorString: |
201 | socketErrorString = QNativeSocketEngine::tr("Unable to initialize non-blocking socket" ); |
202 | break; |
203 | case BroadcastingInitFailedErrorString: |
204 | socketErrorString = QNativeSocketEngine::tr("Unable to initialize broadcast socket" ); |
205 | break; |
206 | case NoIpV6ErrorString: |
207 | socketErrorString = QNativeSocketEngine::tr("Attempt to use IPv6 socket on a platform with no IPv6 support" ); |
208 | break; |
209 | case RemoteHostClosedErrorString: |
210 | socketErrorString = QNativeSocketEngine::tr("The remote host closed the connection" ); |
211 | break; |
212 | case TimeOutErrorString: |
213 | socketErrorString = QNativeSocketEngine::tr("Network operation timed out" ); |
214 | break; |
215 | case ResourceErrorString: |
216 | socketErrorString = QNativeSocketEngine::tr("Out of resources" ); |
217 | break; |
218 | case OperationUnsupportedErrorString: |
219 | socketErrorString = QNativeSocketEngine::tr("Unsupported socket operation" ); |
220 | break; |
221 | case ProtocolUnsupportedErrorString: |
222 | socketErrorString = QNativeSocketEngine::tr("Protocol type not supported" ); |
223 | break; |
224 | case InvalidSocketErrorString: |
225 | socketErrorString = QNativeSocketEngine::tr("Invalid socket descriptor" ); |
226 | break; |
227 | case HostUnreachableErrorString: |
228 | socketErrorString = QNativeSocketEngine::tr("Host unreachable" ); |
229 | break; |
230 | case NetworkUnreachableErrorString: |
231 | socketErrorString = QNativeSocketEngine::tr("Network unreachable" ); |
232 | break; |
233 | case AccessErrorString: |
234 | socketErrorString = QNativeSocketEngine::tr("Permission denied" ); |
235 | break; |
236 | case ConnectionTimeOutErrorString: |
237 | socketErrorString = QNativeSocketEngine::tr("Connection timed out" ); |
238 | break; |
239 | case ConnectionRefusedErrorString: |
240 | socketErrorString = QNativeSocketEngine::tr("Connection refused" ); |
241 | break; |
242 | case AddressInuseErrorString: |
243 | socketErrorString = QNativeSocketEngine::tr("The bound address is already in use" ); |
244 | break; |
245 | case AddressNotAvailableErrorString: |
246 | socketErrorString = QNativeSocketEngine::tr("The address is not available" ); |
247 | break; |
248 | case AddressProtectedErrorString: |
249 | socketErrorString = QNativeSocketEngine::tr("The address is protected" ); |
250 | break; |
251 | case DatagramTooLargeErrorString: |
252 | socketErrorString = QNativeSocketEngine::tr("Datagram was too large to send" ); |
253 | break; |
254 | case SendDatagramErrorString: |
255 | socketErrorString = QNativeSocketEngine::tr("Unable to send a message" ); |
256 | break; |
257 | case ReceiveDatagramErrorString: |
258 | socketErrorString = QNativeSocketEngine::tr("Unable to receive a message" ); |
259 | break; |
260 | case WriteErrorString: |
261 | socketErrorString = QNativeSocketEngine::tr("Unable to write" ); |
262 | break; |
263 | case ReadErrorString: |
264 | socketErrorString = QNativeSocketEngine::tr("Network error" ); |
265 | break; |
266 | case PortInuseErrorString: |
267 | socketErrorString = QNativeSocketEngine::tr("Another socket is already listening on the same port" ); |
268 | break; |
269 | case NotSocketErrorString: |
270 | socketErrorString = QNativeSocketEngine::tr("Operation on non-socket" ); |
271 | break; |
272 | case InvalidProxyTypeString: |
273 | socketErrorString = QNativeSocketEngine::tr("The proxy type is invalid for this operation" ); |
274 | break; |
275 | case UnknownSocketErrorString: |
276 | socketErrorString = QNativeSocketEngine::tr("Unknown error" ); |
277 | break; |
278 | } |
279 | } |
280 | |
281 | bool QNativeSocketEnginePrivate::checkProxy(const QHostAddress &address) |
282 | { |
283 | if (address == QHostAddress::LocalHost || address == QHostAddress::LocalHostIPv6) |
284 | return true; |
285 | |
286 | #if !defined(QT_NO_NETWORKPROXY) |
287 | QObject *parent = q_func()->parent(); |
288 | QNetworkProxy proxy; |
289 | if (QAbstractSocket *socket = qobject_cast<QAbstractSocket *>(parent)) { |
290 | proxy = socket->proxy(); |
291 | } else if (QTcpServer *server = qobject_cast<QTcpServer *>(parent)) { |
292 | proxy = server->proxy(); |
293 | } else { |
294 | // no parent -> no proxy |
295 | return true; |
296 | } |
297 | |
298 | if (proxy.type() == QNetworkProxy::DefaultProxy) |
299 | proxy = QNetworkProxy::applicationProxy(); |
300 | |
301 | if (proxy.type() != QNetworkProxy::DefaultProxy && |
302 | proxy.type() != QNetworkProxy::NoProxy) { |
303 | // QNativeSocketEngine doesn't do proxies |
304 | setError(QAbstractSocket::UnsupportedSocketOperationError, |
305 | QNativeSocketEnginePrivate::InvalidProxyTypeString); |
306 | return false; |
307 | } |
308 | #endif |
309 | |
310 | return true; |
311 | } |
312 | |
313 | /*! |
314 | Constructs a QNativeSocketEngine. |
315 | |
316 | \sa initialize() |
317 | */ |
318 | QNativeSocketEngine::QNativeSocketEngine(QObject *parent) |
319 | : QAbstractSocketEngine(*new QNativeSocketEnginePrivate(), parent) |
320 | { |
321 | } |
322 | |
323 | /*! |
324 | Destructs a QNativeSocketEngine. |
325 | */ |
326 | QNativeSocketEngine::~QNativeSocketEngine() |
327 | { |
328 | close(); |
329 | } |
330 | |
331 | /*! |
332 | Initializes a QNativeSocketEngine by creating a new socket of type \a |
333 | socketType and network layer protocol \a protocol. Returns true on |
334 | success; otherwise returns false. |
335 | |
336 | If the socket was already initialized, this function closes the |
337 | socket before reeinitializing it. |
338 | |
339 | The new socket is non-blocking, and for UDP sockets it's also |
340 | broadcast enabled. |
341 | */ |
342 | bool QNativeSocketEngine::initialize(QAbstractSocket::SocketType socketType, QAbstractSocket::NetworkLayerProtocol protocol) |
343 | { |
344 | Q_D(QNativeSocketEngine); |
345 | if (isValid()) |
346 | close(); |
347 | |
348 | #if defined(QT_NO_IPV6) |
349 | if (protocol == QAbstractSocket::IPv6Protocol) { |
350 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
351 | QNativeSocketEnginePrivate::NoIpV6ErrorString); |
352 | return false; |
353 | } |
354 | #endif |
355 | |
356 | // Create the socket |
357 | if (!d->createNewSocket(socketType, protocol)) { |
358 | #if defined (QNATIVESOCKETENGINE_DEBUG) |
359 | QString typeStr = QLatin1String("UnknownSocketType" ); |
360 | if (socketType == QAbstractSocket::TcpSocket) typeStr = QLatin1String("TcpSocket" ); |
361 | else if (socketType == QAbstractSocket::UdpSocket) typeStr = QLatin1String("UdpSocket" ); |
362 | QString protocolStr = QLatin1String("UnknownProtocol" ); |
363 | if (protocol == QAbstractSocket::IPv4Protocol) protocolStr = QLatin1String("IPv4Protocol" ); |
364 | else if (protocol == QAbstractSocket::IPv6Protocol) protocolStr = QLatin1String("IPv6Protocol" ); |
365 | qDebug("QNativeSocketEngine::initialize(type == %s, protocol == %s) failed: %s" , |
366 | typeStr.toLatin1().constData(), protocolStr.toLatin1().constData(), d->socketErrorString.toLatin1().constData()); |
367 | #endif |
368 | return false; |
369 | } |
370 | |
371 | // Make the socket nonblocking. |
372 | if (!setOption(NonBlockingSocketOption, 1)) { |
373 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
374 | QNativeSocketEnginePrivate::NonBlockingInitFailedErrorString); |
375 | close(); |
376 | return false; |
377 | } |
378 | |
379 | // Set the broadcasting flag if it's a UDP socket. |
380 | if (socketType == QAbstractSocket::UdpSocket |
381 | && !setOption(BroadcastSocketOption, 1)) { |
382 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
383 | QNativeSocketEnginePrivate::BroadcastingInitFailedErrorString); |
384 | close(); |
385 | return false; |
386 | } |
387 | |
388 | |
389 | // Make sure we receive out-of-band data |
390 | if (socketType == QAbstractSocket::TcpSocket |
391 | && !setOption(ReceiveOutOfBandData, 1)) { |
392 | qWarning("QNativeSocketEngine::initialize unable to inline out-of-band data" ); |
393 | } |
394 | |
395 | // Before Qt 4.6, we always set the send and receive buffer size to 49152 as |
396 | // this was found to be an optimal value. However, modern OS |
397 | // all have some kind of auto tuning for this and we therefore don't set |
398 | // this explictly anymore. |
399 | // If it introduces any performance regressions for Qt 4.6.x (x > 0) then |
400 | // it will be put back in. |
401 | // |
402 | // You can use tests/manual/qhttpnetworkconnection to test HTTP download speed |
403 | // with this. |
404 | // |
405 | // pre-4.6: |
406 | // setReceiveBufferSize(49152); |
407 | // setSendBufferSize(49152); |
408 | |
409 | d->socketType = socketType; |
410 | d->socketProtocol = protocol; |
411 | return true; |
412 | } |
413 | |
414 | /*! \overload |
415 | |
416 | Initializes the socket using \a socketDescriptor instead of |
417 | creating a new one. The socket type and network layer protocol are |
418 | determined automatically. The socket's state is set to \a |
419 | socketState. |
420 | |
421 | If the socket type is either TCP or UDP, it is made non-blocking. |
422 | UDP sockets are also broadcast enabled. |
423 | */ |
424 | bool QNativeSocketEngine::initialize(int socketDescriptor, QAbstractSocket::SocketState socketState) |
425 | { |
426 | Q_D(QNativeSocketEngine); |
427 | |
428 | if (isValid()) |
429 | close(); |
430 | |
431 | d->socketDescriptor = socketDescriptor; |
432 | |
433 | // determine socket type and protocol |
434 | if (!d->fetchConnectionParameters()) { |
435 | #if defined (QNATIVESOCKETENGINE_DEBUG) |
436 | qDebug("QNativeSocketEngine::initialize(socketDescriptor == %i) failed: %s" , |
437 | socketDescriptor, d->socketErrorString.toLatin1().constData()); |
438 | #endif |
439 | d->socketDescriptor = -1; |
440 | return false; |
441 | } |
442 | |
443 | if (d->socketType != QAbstractSocket::UnknownSocketType) { |
444 | // Make the socket nonblocking. |
445 | if (!setOption(NonBlockingSocketOption, 1)) { |
446 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
447 | QNativeSocketEnginePrivate::NonBlockingInitFailedErrorString); |
448 | close(); |
449 | return false; |
450 | } |
451 | |
452 | // Set the broadcasting flag if it's a UDP socket. |
453 | if (d->socketType == QAbstractSocket::UdpSocket |
454 | && !setOption(BroadcastSocketOption, 1)) { |
455 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
456 | QNativeSocketEnginePrivate::BroadcastingInitFailedErrorString); |
457 | close(); |
458 | return false; |
459 | } |
460 | } |
461 | |
462 | d->socketState = socketState; |
463 | return true; |
464 | } |
465 | |
466 | /*! |
467 | Returns true if the socket is valid; otherwise returns false. A |
468 | socket is valid if it has not been successfully initialized, or if |
469 | it has been closed. |
470 | */ |
471 | bool QNativeSocketEngine::isValid() const |
472 | { |
473 | Q_D(const QNativeSocketEngine); |
474 | return d->socketDescriptor != -1; |
475 | } |
476 | |
477 | /*! |
478 | Returns the native socket descriptor. Any use of this descriptor |
479 | stands the risk of being non-portable. |
480 | */ |
481 | int QNativeSocketEngine::socketDescriptor() const |
482 | { |
483 | Q_D(const QNativeSocketEngine); |
484 | return d->socketDescriptor; |
485 | } |
486 | |
487 | /*! |
488 | Connects to the IP address and port specified by \a address and \a |
489 | port. If the connection is established, this function returns true |
490 | and the socket enters ConnectedState. Otherwise, false is |
491 | returned. |
492 | |
493 | If false is returned, state() should be called to see if the |
494 | socket is in ConnectingState. If so, a delayed TCP connection is |
495 | taking place, and connectToHost() must be called again later to |
496 | determine if the connection was established successfully or |
497 | not. The second connection attempt must be made when the socket is |
498 | ready for writing. This state can be determined either by |
499 | connecting a QSocketNotifier to the socket descriptor returned by |
500 | socketDescriptor(), or by calling the blocking function |
501 | waitForWrite(). |
502 | |
503 | Example: |
504 | \snippet doc/src/snippets/code/src_network_socket_qnativesocketengine.cpp 0 |
505 | |
506 | Otherwise, error() should be called to determine the cause of the |
507 | error. |
508 | */ |
509 | bool QNativeSocketEngine::connectToHost(const QHostAddress &address, quint16 port) |
510 | { |
511 | Q_D(QNativeSocketEngine); |
512 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::connectToHost(), false); |
513 | |
514 | #if defined (QT_NO_IPV6) |
515 | if (address.protocol() == QAbstractSocket::IPv6Protocol) { |
516 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
517 | QNativeSocketEnginePrivate::NoIpV6ErrorString); |
518 | return false; |
519 | } |
520 | #endif |
521 | if (!d->checkProxy(address)) |
522 | return false; |
523 | |
524 | Q_CHECK_STATES(QNativeSocketEngine::connectToHost(), |
525 | QAbstractSocket::UnconnectedState, QAbstractSocket::ConnectingState, false); |
526 | |
527 | d->peerAddress = address; |
528 | d->peerPort = port; |
529 | bool connected = d->nativeConnect(address, port); |
530 | if (connected) |
531 | d->fetchConnectionParameters(); |
532 | |
533 | return connected; |
534 | } |
535 | |
536 | /*! |
537 | If there's a connection activity on the socket, process it. Then |
538 | notify our parent if there really was activity. |
539 | */ |
540 | void QNativeSocketEngine::connectionNotification() |
541 | { |
542 | Q_D(QNativeSocketEngine); |
543 | Q_ASSERT(state() == QAbstractSocket::ConnectingState); |
544 | |
545 | connectToHost(d->peerAddress, d->peerPort); |
546 | if (state() != QAbstractSocket::ConnectingState) { |
547 | // we changed states |
548 | QAbstractSocketEngine::connectionNotification(); |
549 | } |
550 | } |
551 | |
552 | /*! |
553 | Connects to the remote host name given by \a name on port \a |
554 | port. When this function is called, the upper-level will not |
555 | perform a hostname lookup. |
556 | |
557 | The native socket engine does not support this operation, |
558 | but some other socket engines (notably proxy-based ones) do. |
559 | */ |
560 | bool QNativeSocketEngine::connectToHostByName(const QString &name, quint16 port) |
561 | { |
562 | Q_UNUSED(name); |
563 | Q_UNUSED(port); |
564 | Q_D(QNativeSocketEngine); |
565 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
566 | QNativeSocketEnginePrivate::OperationUnsupportedErrorString); |
567 | return false; |
568 | } |
569 | |
570 | /*! |
571 | Binds the socket to the address \a address and port \a |
572 | port. Returns true on success; otherwise false is returned. The |
573 | port may be 0, in which case an arbitrary unused port is assigned |
574 | automatically by the operating system. |
575 | |
576 | Servers call this function to set up the server's address and |
577 | port. TCP servers must in addition call listen() after bind(). |
578 | */ |
579 | bool QNativeSocketEngine::bind(const QHostAddress &address, quint16 port) |
580 | { |
581 | Q_D(QNativeSocketEngine); |
582 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::bind(), false); |
583 | |
584 | #if defined (QT_NO_IPV6) |
585 | if (address.protocol() == QAbstractSocket::IPv6Protocol) { |
586 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, |
587 | QNativeSocketEnginePrivate::NoIpV6ErrorString); |
588 | return false; |
589 | } |
590 | #endif |
591 | if (!d->checkProxy(address)) |
592 | return false; |
593 | |
594 | Q_CHECK_STATE(QNativeSocketEngine::bind(), QAbstractSocket::UnconnectedState, false); |
595 | |
596 | if (!d->nativeBind(address, port)) |
597 | return false; |
598 | |
599 | d->fetchConnectionParameters(); |
600 | return true; |
601 | } |
602 | |
603 | /*! |
604 | Prepares a TCP server for accepting incoming connections. This |
605 | function must be called after bind(), and only by TCP sockets. |
606 | |
607 | After this function has been called, pending client connections |
608 | are detected by checking if the socket is ready for reading. This |
609 | can be done by either creating a QSocketNotifier, passing the |
610 | socket descriptor returned by socketDescriptor(), or by calling |
611 | the blocking function waitForRead(). |
612 | |
613 | Example: |
614 | \snippet doc/src/snippets/code/src_network_socket_qnativesocketengine.cpp 1 |
615 | |
616 | \sa bind(), accept() |
617 | */ |
618 | bool QNativeSocketEngine::listen() |
619 | { |
620 | Q_D(QNativeSocketEngine); |
621 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::listen(), false); |
622 | Q_CHECK_STATE(QNativeSocketEngine::listen(), QAbstractSocket::BoundState, false); |
623 | Q_CHECK_TYPE(QNativeSocketEngine::listen(), QAbstractSocket::TcpSocket, false); |
624 | |
625 | // We're using a backlog of 50. Most modern kernels support TCP |
626 | // syncookies by default, and if they do, the backlog is ignored. |
627 | // When there is no support for TCP syncookies, this value is |
628 | // fine. |
629 | return d->nativeListen(50); |
630 | } |
631 | |
632 | /*! |
633 | Accepts a pending connection from the socket, which must be in |
634 | ListeningState, and returns its socket descriptor. If no pending |
635 | connections are available, -1 is returned. |
636 | |
637 | \sa bind(), listen() |
638 | */ |
639 | int QNativeSocketEngine::accept() |
640 | { |
641 | Q_D(QNativeSocketEngine); |
642 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::accept(), -1); |
643 | Q_CHECK_STATE(QNativeSocketEngine::accept(), QAbstractSocket::ListeningState, false); |
644 | Q_CHECK_TYPE(QNativeSocketEngine::accept(), QAbstractSocket::TcpSocket, false); |
645 | |
646 | return d->nativeAccept(); |
647 | } |
648 | |
649 | #ifndef QT_NO_NETWORKINTERFACE |
650 | |
651 | /*! |
652 | \since 4.8 |
653 | */ |
654 | bool QNativeSocketEngine::joinMulticastGroup(const QHostAddress &groupAddress, |
655 | const QNetworkInterface &iface) |
656 | { |
657 | Q_D(QNativeSocketEngine); |
658 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::joinMulticastGroup(), false); |
659 | Q_CHECK_STATE(QNativeSocketEngine::joinMulticastGroup(), QAbstractSocket::BoundState, false); |
660 | Q_CHECK_TYPE(QNativeSocketEngine::joinMulticastGroup(), QAbstractSocket::UdpSocket, false); |
661 | return d->nativeJoinMulticastGroup(groupAddress, iface); |
662 | } |
663 | |
664 | /*! |
665 | \since 4.8 |
666 | */ |
667 | bool QNativeSocketEngine::leaveMulticastGroup(const QHostAddress &groupAddress, |
668 | const QNetworkInterface &iface) |
669 | { |
670 | Q_D(QNativeSocketEngine); |
671 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::leaveMulticastGroup(), false); |
672 | Q_CHECK_STATE(QNativeSocketEngine::leaveMulticastGroup(), QAbstractSocket::BoundState, false); |
673 | Q_CHECK_TYPE(QNativeSocketEngine::leaveMulticastGroup(), QAbstractSocket::UdpSocket, false); |
674 | return d->nativeLeaveMulticastGroup(groupAddress, iface); |
675 | } |
676 | |
677 | /*! \since 4.8 */ |
678 | QNetworkInterface QNativeSocketEngine::multicastInterface() const |
679 | { |
680 | Q_D(const QNativeSocketEngine); |
681 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::multicastInterface(), QNetworkInterface()); |
682 | Q_CHECK_TYPE(QNativeSocketEngine::multicastInterface(), QAbstractSocket::UdpSocket, QNetworkInterface()); |
683 | return d->nativeMulticastInterface(); |
684 | } |
685 | |
686 | /*! \since 4.8 */ |
687 | bool QNativeSocketEngine::setMulticastInterface(const QNetworkInterface &iface) |
688 | { |
689 | Q_D(QNativeSocketEngine); |
690 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::setMulticastInterface(), false); |
691 | Q_CHECK_TYPE(QNativeSocketEngine::setMulticastInterface(), QAbstractSocket::UdpSocket, false); |
692 | return d->nativeSetMulticastInterface(iface); |
693 | } |
694 | |
695 | #endif // QT_NO_NETWORKINTERFACE |
696 | |
697 | /*! |
698 | Returns the number of bytes that are currently available for |
699 | reading. On error, -1 is returned. |
700 | |
701 | For UDP sockets, this function returns the accumulated size of all |
702 | pending datagrams, and it is therefore more useful for UDP sockets |
703 | to call hasPendingDatagrams() and pendingDatagramSize(). |
704 | */ |
705 | qint64 QNativeSocketEngine::bytesAvailable() const |
706 | { |
707 | Q_D(const QNativeSocketEngine); |
708 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::bytesAvailable(), -1); |
709 | Q_CHECK_NOT_STATE(QNativeSocketEngine::bytesAvailable(), QAbstractSocket::UnconnectedState, false); |
710 | |
711 | return d->nativeBytesAvailable(); |
712 | } |
713 | |
714 | /*! |
715 | Returns true if there is at least one datagram pending. This |
716 | function is only called by UDP sockets, where a datagram can have |
717 | a size of 0. TCP sockets call bytesAvailable(). |
718 | */ |
719 | bool QNativeSocketEngine::hasPendingDatagrams() const |
720 | { |
721 | Q_D(const QNativeSocketEngine); |
722 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::hasPendingDatagrams(), false); |
723 | Q_CHECK_NOT_STATE(QNativeSocketEngine::hasPendingDatagrams(), QAbstractSocket::UnconnectedState, false); |
724 | Q_CHECK_TYPE(QNativeSocketEngine::hasPendingDatagrams(), QAbstractSocket::UdpSocket, false); |
725 | |
726 | return d->nativeHasPendingDatagrams(); |
727 | } |
728 | |
729 | /*! |
730 | Returns the size of the pending datagram, or -1 if no datagram is |
731 | pending. A datagram size of 0 is perfectly valid. This function is |
732 | called by UDP sockets before receiveMessage(). For TCP sockets, |
733 | call bytesAvailable(). |
734 | */ |
735 | qint64 QNativeSocketEngine::pendingDatagramSize() const |
736 | { |
737 | Q_D(const QNativeSocketEngine); |
738 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::pendingDatagramSize(), -1); |
739 | Q_CHECK_TYPE(QNativeSocketEngine::pendingDatagramSize(), QAbstractSocket::UdpSocket, false); |
740 | |
741 | return d->nativePendingDatagramSize(); |
742 | } |
743 | |
744 | /*! |
745 | Reads up to \a maxSize bytes of a datagram from the socket, |
746 | stores it in \a data and returns the number of bytes read. The |
747 | address and port of the sender are stored in \a address and \a |
748 | port. If either of these pointers is 0, the corresponding value is |
749 | discarded. |
750 | |
751 | To avoid unnecessarily loss of data, call pendingDatagramSize() to |
752 | determine the size of the pending message before reading it. If \a |
753 | maxSize is too small, the rest of the datagram will be lost. |
754 | |
755 | Returns -1 if an error occurred. |
756 | |
757 | \sa hasPendingDatagrams() |
758 | */ |
759 | qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxSize, QHostAddress *address, |
760 | quint16 *port) |
761 | { |
762 | Q_D(QNativeSocketEngine); |
763 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::readDatagram(), -1); |
764 | Q_CHECK_TYPE(QNativeSocketEngine::readDatagram(), QAbstractSocket::UdpSocket, false); |
765 | |
766 | return d->nativeReceiveDatagram(data, maxSize, address, port); |
767 | } |
768 | |
769 | /*! |
770 | Writes a UDP datagram of size \a size bytes to the socket from |
771 | \a data to the address \a host on port \a port, and returns the |
772 | number of bytes written, or -1 if an error occurred. |
773 | |
774 | Only one datagram is sent, and if there is too much data to fit |
775 | into a single datagram, the operation will fail and error() |
776 | will return QAbstractSocket::DatagramTooLargeError. Operating systems impose an |
777 | upper limit to the size of a datagram, but this size is different |
778 | on almost all platforms. Sending large datagrams is in general |
779 | disadvised, as even if they are sent successfully, they are likely |
780 | to be fragmented before arriving at their destination. |
781 | |
782 | Experience has shown that it is in general safe to send datagrams |
783 | no larger than 512 bytes. |
784 | |
785 | \sa readDatagram() |
786 | */ |
787 | qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 size, |
788 | const QHostAddress &host, quint16 port) |
789 | { |
790 | Q_D(QNativeSocketEngine); |
791 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::writeDatagram(), -1); |
792 | Q_CHECK_TYPE(QNativeSocketEngine::writeDatagram(), QAbstractSocket::UdpSocket, -1); |
793 | return d->nativeSendDatagram(data, size, host, port); |
794 | } |
795 | |
796 | /*! |
797 | Writes a block of \a size bytes from \a data to the socket. |
798 | Returns the number of bytes written, or -1 if an error occurred. |
799 | */ |
800 | qint64 QNativeSocketEngine::write(const char *data, qint64 size) |
801 | { |
802 | Q_D(QNativeSocketEngine); |
803 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::write(), -1); |
804 | Q_CHECK_STATE(QNativeSocketEngine::write(), QAbstractSocket::ConnectedState, -1); |
805 | return d->nativeWrite(data, size); |
806 | } |
807 | |
808 | |
809 | qint64 QNativeSocketEngine::bytesToWrite() const |
810 | { |
811 | return 0; |
812 | } |
813 | |
814 | /*! |
815 | Reads up to \a maxSize bytes into \a data from the socket. |
816 | Returns the number of bytes read, or -1 if an error occurred. |
817 | */ |
818 | qint64 QNativeSocketEngine::read(char *data, qint64 maxSize) |
819 | { |
820 | Q_D(QNativeSocketEngine); |
821 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::read(), -1); |
822 | Q_CHECK_STATES(QNativeSocketEngine::read(), QAbstractSocket::ConnectedState, QAbstractSocket::BoundState, -1); |
823 | |
824 | qint64 readBytes = d->nativeRead(data, maxSize); |
825 | |
826 | // Handle remote close |
827 | if (readBytes == 0 && d->socketType == QAbstractSocket::TcpSocket) { |
828 | d->setError(QAbstractSocket::RemoteHostClosedError, |
829 | QNativeSocketEnginePrivate::RemoteHostClosedErrorString); |
830 | close(); |
831 | return -1; |
832 | } else if (readBytes == -1) { |
833 | if (!d->hasSetSocketError) { |
834 | d->hasSetSocketError = true; |
835 | d->socketError = QAbstractSocket::NetworkError; |
836 | d->socketErrorString = qt_error_string(); |
837 | } |
838 | close(); |
839 | return -1; |
840 | } |
841 | return readBytes; |
842 | } |
843 | |
844 | /*! |
845 | Closes the socket. In order to use the socket again, initialize() |
846 | must be called. |
847 | */ |
848 | void QNativeSocketEngine::close() |
849 | { |
850 | Q_D(QNativeSocketEngine); |
851 | if (d->readNotifier) |
852 | d->readNotifier->setEnabled(false); |
853 | if (d->writeNotifier) |
854 | d->writeNotifier->setEnabled(false); |
855 | if (d->exceptNotifier) |
856 | d->exceptNotifier->setEnabled(false); |
857 | |
858 | if(d->socketDescriptor != -1) { |
859 | d->nativeClose(); |
860 | d->socketDescriptor = -1; |
861 | } |
862 | d->socketState = QAbstractSocket::UnconnectedState; |
863 | d->hasSetSocketError = false; |
864 | d->localPort = 0; |
865 | d->localAddress.clear(); |
866 | d->peerPort = 0; |
867 | d->peerAddress.clear(); |
868 | if (d->readNotifier) { |
869 | qDeleteInEventHandler(d->readNotifier); |
870 | d->readNotifier = 0; |
871 | } |
872 | if (d->writeNotifier) { |
873 | qDeleteInEventHandler(d->writeNotifier); |
874 | d->writeNotifier = 0; |
875 | } |
876 | if (d->exceptNotifier) { |
877 | qDeleteInEventHandler(d->exceptNotifier); |
878 | d->exceptNotifier = 0; |
879 | } |
880 | } |
881 | |
882 | /*! |
883 | Waits for \a msecs milliseconds or until the socket is ready for |
884 | reading. If \a timedOut is not 0 and \a msecs milliseconds have |
885 | passed, the value of \a timedOut is set to true. |
886 | |
887 | Returns true if data is available for reading; otherwise returns |
888 | false. |
889 | |
890 | This is a blocking function call; its use is disadvised in a |
891 | single threaded application, as the whole thread will stop |
892 | responding until the function returns. waitForRead() is most |
893 | useful when there is no event loop available. The general approach |
894 | is to create a QSocketNotifier, passing the socket descriptor |
895 | returned by socketDescriptor() to its constructor. |
896 | */ |
897 | bool QNativeSocketEngine::waitForRead(int msecs, bool *timedOut) |
898 | { |
899 | Q_D(const QNativeSocketEngine); |
900 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForRead(), false); |
901 | Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForRead(), |
902 | QAbstractSocket::UnconnectedState, false); |
903 | |
904 | if (timedOut) |
905 | *timedOut = false; |
906 | |
907 | int ret = d->nativeSelect(msecs, true); |
908 | if (ret == 0) { |
909 | if (timedOut) |
910 | *timedOut = true; |
911 | d->setError(QAbstractSocket::SocketTimeoutError, |
912 | QNativeSocketEnginePrivate::TimeOutErrorString); |
913 | d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions |
914 | return false; |
915 | } else if (state() == QAbstractSocket::ConnectingState) { |
916 | connectToHost(d->peerAddress, d->peerPort); |
917 | } |
918 | |
919 | return ret > 0; |
920 | } |
921 | |
922 | /*! |
923 | Waits for \a msecs milliseconds or until the socket is ready for |
924 | writing. If \a timedOut is not 0 and \a msecs milliseconds have |
925 | passed, the value of \a timedOut is set to true. |
926 | |
927 | Returns true if data is available for writing; otherwise returns |
928 | false. |
929 | |
930 | This is a blocking function call; its use is disadvised in a |
931 | single threaded application, as the whole thread will stop |
932 | responding until the function returns. waitForWrite() is most |
933 | useful when there is no event loop available. The general approach |
934 | is to create a QSocketNotifier, passing the socket descriptor |
935 | returned by socketDescriptor() to its constructor. |
936 | */ |
937 | bool QNativeSocketEngine::waitForWrite(int msecs, bool *timedOut) |
938 | { |
939 | Q_D(QNativeSocketEngine); |
940 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false); |
941 | Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForWrite(), |
942 | QAbstractSocket::UnconnectedState, false); |
943 | |
944 | if (timedOut) |
945 | *timedOut = false; |
946 | |
947 | int ret = d->nativeSelect(msecs, false); |
948 | // On Windows, the socket is in connected state if a call to |
949 | // select(writable) is successful. In this case we should not |
950 | // issue a second call to WSAConnect() |
951 | #if defined (Q_WS_WIN) |
952 | if (ret > 0) { |
953 | setState(QAbstractSocket::ConnectedState); |
954 | d_func()->fetchConnectionParameters(); |
955 | return true; |
956 | } else { |
957 | int value = 0; |
958 | int valueSize = sizeof(value); |
959 | if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) { |
960 | if (value == WSAECONNREFUSED) { |
961 | d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString); |
962 | d->socketState = QAbstractSocket::UnconnectedState; |
963 | return false; |
964 | } else if (value == WSAETIMEDOUT) { |
965 | d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString); |
966 | d->socketState = QAbstractSocket::UnconnectedState; |
967 | return false; |
968 | } else if (value == WSAEHOSTUNREACH) { |
969 | d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString); |
970 | d->socketState = QAbstractSocket::UnconnectedState; |
971 | return false; |
972 | } |
973 | } |
974 | } |
975 | #endif |
976 | |
977 | if (ret == 0) { |
978 | if (timedOut) |
979 | *timedOut = true; |
980 | d->setError(QAbstractSocket::SocketTimeoutError, |
981 | QNativeSocketEnginePrivate::TimeOutErrorString); |
982 | d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions |
983 | return false; |
984 | } else if (state() == QAbstractSocket::ConnectingState) { |
985 | connectToHost(d->peerAddress, d->peerPort); |
986 | } |
987 | |
988 | return ret > 0; |
989 | } |
990 | |
991 | bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, |
992 | bool checkRead, bool checkWrite, |
993 | int msecs, bool *timedOut) |
994 | { |
995 | Q_D(QNativeSocketEngine); |
996 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false); |
997 | Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForReadOrWrite(), |
998 | QAbstractSocket::UnconnectedState, false); |
999 | |
1000 | int ret = d->nativeSelect(msecs, checkRead, checkWrite, readyToRead, readyToWrite); |
1001 | // On Windows, the socket is in connected state if a call to |
1002 | // select(writable) is successful. In this case we should not |
1003 | // issue a second call to WSAConnect() |
1004 | #if defined (Q_WS_WIN) |
1005 | if (checkWrite && ((readyToWrite && *readyToWrite) || !readyToWrite) && ret > 0) { |
1006 | setState(QAbstractSocket::ConnectedState); |
1007 | d_func()->fetchConnectionParameters(); |
1008 | return true; |
1009 | } else { |
1010 | int value = 0; |
1011 | int valueSize = sizeof(value); |
1012 | if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) { |
1013 | if (value == WSAECONNREFUSED) { |
1014 | d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString); |
1015 | d->socketState = QAbstractSocket::UnconnectedState; |
1016 | return false; |
1017 | } else if (value == WSAETIMEDOUT) { |
1018 | d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString); |
1019 | d->socketState = QAbstractSocket::UnconnectedState; |
1020 | return false; |
1021 | } else if (value == WSAEHOSTUNREACH) { |
1022 | d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString); |
1023 | d->socketState = QAbstractSocket::UnconnectedState; |
1024 | return false; |
1025 | } |
1026 | } |
1027 | } |
1028 | #endif |
1029 | if (ret == 0) { |
1030 | if (timedOut) |
1031 | *timedOut = true; |
1032 | d->setError(QAbstractSocket::SocketTimeoutError, |
1033 | QNativeSocketEnginePrivate::TimeOutErrorString); |
1034 | d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions |
1035 | return false; |
1036 | } else if (state() == QAbstractSocket::ConnectingState) { |
1037 | connectToHost(d->peerAddress, d->peerPort); |
1038 | } |
1039 | |
1040 | return ret > 0; |
1041 | } |
1042 | |
1043 | /*! |
1044 | Returns the size of the operating system's socket receive |
1045 | buffer. Depending on the operating system, this size may be |
1046 | different from what has been set earlier with |
1047 | setReceiveBufferSize(). |
1048 | */ |
1049 | qint64 QNativeSocketEngine::receiveBufferSize() const |
1050 | { |
1051 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::receiveBufferSize(), -1); |
1052 | return option(ReceiveBufferSocketOption); |
1053 | } |
1054 | |
1055 | /*! |
1056 | Sets the size of the operating system receive buffer to \a size. |
1057 | |
1058 | For clients, this should be set before connectToHost() is called; |
1059 | otherwise it will have no effect. For servers, it should be called |
1060 | before listen(). |
1061 | |
1062 | The operating system receive buffer size effectively limits two |
1063 | things: how much data can be in transit at any one moment, and how |
1064 | much data can be received in one iteration of the main event loop. |
1065 | Setting the size of the receive buffer may have an impact on the |
1066 | socket's performance. |
1067 | |
1068 | The default value is operating system-dependent. |
1069 | */ |
1070 | void QNativeSocketEngine::setReceiveBufferSize(qint64 size) |
1071 | { |
1072 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::setReceiveBufferSize(), Q_VOID); |
1073 | setOption(ReceiveBufferSocketOption, size); |
1074 | } |
1075 | |
1076 | /*! |
1077 | Returns the size of the operating system send buffer. Depending on |
1078 | the operating system, this size may be different from what has |
1079 | been set earlier with setSendBufferSize(). |
1080 | */ |
1081 | qint64 QNativeSocketEngine::sendBufferSize() const |
1082 | { |
1083 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::setSendBufferSize(), -1); |
1084 | return option(SendBufferSocketOption); |
1085 | } |
1086 | |
1087 | /*! |
1088 | Sets the size of the operating system send buffer to \a size. |
1089 | |
1090 | The operating system send buffer size effectively limits how much |
1091 | data can be in transit at any one moment. Setting the size of the |
1092 | send buffer may have an impact on the socket's performance. |
1093 | |
1094 | The default value is operating system-dependent. |
1095 | */ |
1096 | void QNativeSocketEngine::setSendBufferSize(qint64 size) |
1097 | { |
1098 | Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::setSendBufferSize(), Q_VOID); |
1099 | setOption(SendBufferSocketOption, size); |
1100 | } |
1101 | |
1102 | |
1103 | /*! |
1104 | Sets the option \a option to the value \a value. |
1105 | */ |
1106 | bool QNativeSocketEngine::setOption(SocketOption option, int value) |
1107 | { |
1108 | Q_D(QNativeSocketEngine); |
1109 | return d->setOption(option, value); |
1110 | } |
1111 | |
1112 | /*! |
1113 | Returns the value of the option \a socketOption. |
1114 | */ |
1115 | int QNativeSocketEngine::option(SocketOption socketOption) const |
1116 | { |
1117 | Q_D(const QNativeSocketEngine); |
1118 | return d->option(socketOption); |
1119 | } |
1120 | |
1121 | bool QNativeSocketEngine::isReadNotificationEnabled() const |
1122 | { |
1123 | Q_D(const QNativeSocketEngine); |
1124 | return d->readNotifier && d->readNotifier->isEnabled(); |
1125 | } |
1126 | |
1127 | /* |
1128 | \internal |
1129 | \class QReadNotifier |
1130 | \brief The QReadNotifer class is used to improve performance. |
1131 | |
1132 | QReadNotifier is a private class used for performance reasons vs |
1133 | connecting to the QSocketNotifier activated() signal. |
1134 | */ |
1135 | class QReadNotifier : public QSocketNotifier |
1136 | { |
1137 | public: |
1138 | QReadNotifier(int fd, QNativeSocketEngine *parent) |
1139 | : QSocketNotifier(fd, QSocketNotifier::Read, parent) |
1140 | { engine = parent; } |
1141 | |
1142 | protected: |
1143 | bool event(QEvent *); |
1144 | |
1145 | QNativeSocketEngine *engine; |
1146 | }; |
1147 | |
1148 | bool QReadNotifier::event(QEvent *e) |
1149 | { |
1150 | if (e->type() == QEvent::SockAct) { |
1151 | engine->readNotification(); |
1152 | return true; |
1153 | } |
1154 | return QSocketNotifier::event(e); |
1155 | } |
1156 | |
1157 | /* |
1158 | \internal |
1159 | \class QWriteNotifier |
1160 | \brief The QWriteNotifer class is used to improve performance. |
1161 | |
1162 | QWriteNotifier is a private class used for performance reasons vs |
1163 | connecting to the QSocketNotifier activated() signal. |
1164 | */ |
1165 | class QWriteNotifier : public QSocketNotifier |
1166 | { |
1167 | public: |
1168 | QWriteNotifier(int fd, QNativeSocketEngine *parent) |
1169 | : QSocketNotifier(fd, QSocketNotifier::Write, parent) { engine = parent; } |
1170 | |
1171 | protected: |
1172 | bool event(QEvent *); |
1173 | |
1174 | QNativeSocketEngine *engine; |
1175 | }; |
1176 | |
1177 | bool QWriteNotifier::event(QEvent *e) |
1178 | { |
1179 | if (e->type() == QEvent::SockAct) { |
1180 | if (engine->state() == QAbstractSocket::ConnectingState) |
1181 | engine->connectionNotification(); |
1182 | else |
1183 | engine->writeNotification(); |
1184 | return true; |
1185 | } |
1186 | return QSocketNotifier::event(e); |
1187 | } |
1188 | |
1189 | class QExceptionNotifier : public QSocketNotifier |
1190 | { |
1191 | public: |
1192 | QExceptionNotifier(int fd, QNativeSocketEngine *parent) |
1193 | : QSocketNotifier(fd, QSocketNotifier::Exception, parent) { engine = parent; } |
1194 | |
1195 | protected: |
1196 | bool event(QEvent *); |
1197 | |
1198 | QNativeSocketEngine *engine; |
1199 | }; |
1200 | |
1201 | bool QExceptionNotifier::event(QEvent *e) |
1202 | { |
1203 | if (e->type() == QEvent::SockAct) { |
1204 | if (engine->state() == QAbstractSocket::ConnectingState) |
1205 | engine->connectionNotification(); |
1206 | else |
1207 | engine->exceptionNotification(); |
1208 | return true; |
1209 | } |
1210 | return QSocketNotifier::event(e); |
1211 | } |
1212 | |
1213 | void QNativeSocketEngine::setReadNotificationEnabled(bool enable) |
1214 | { |
1215 | Q_D(QNativeSocketEngine); |
1216 | if (d->readNotifier) { |
1217 | d->readNotifier->setEnabled(enable); |
1218 | } else if (enable && d->threadData->eventDispatcher) { |
1219 | d->readNotifier = new QReadNotifier(d->socketDescriptor, this); |
1220 | d->readNotifier->setEnabled(true); |
1221 | } |
1222 | } |
1223 | |
1224 | bool QNativeSocketEngine::isWriteNotificationEnabled() const |
1225 | { |
1226 | Q_D(const QNativeSocketEngine); |
1227 | return d->writeNotifier && d->writeNotifier->isEnabled(); |
1228 | } |
1229 | |
1230 | void QNativeSocketEngine::setWriteNotificationEnabled(bool enable) |
1231 | { |
1232 | Q_D(QNativeSocketEngine); |
1233 | if (d->writeNotifier) { |
1234 | d->writeNotifier->setEnabled(enable); |
1235 | } else if (enable && d->threadData->eventDispatcher) { |
1236 | d->writeNotifier = new QWriteNotifier(d->socketDescriptor, this); |
1237 | d->writeNotifier->setEnabled(true); |
1238 | } |
1239 | } |
1240 | |
1241 | bool QNativeSocketEngine::isExceptionNotificationEnabled() const |
1242 | { |
1243 | Q_D(const QNativeSocketEngine); |
1244 | return d->exceptNotifier && d->exceptNotifier->isEnabled(); |
1245 | } |
1246 | |
1247 | void QNativeSocketEngine::setExceptionNotificationEnabled(bool enable) |
1248 | { |
1249 | Q_D(QNativeSocketEngine); |
1250 | if (d->exceptNotifier) { |
1251 | d->exceptNotifier->setEnabled(enable); |
1252 | } else if (enable && d->threadData->eventDispatcher) { |
1253 | d->exceptNotifier = new QExceptionNotifier(d->socketDescriptor, this); |
1254 | d->exceptNotifier->setEnabled(true); |
1255 | } |
1256 | } |
1257 | |
1258 | QT_END_NAMESPACE |
1259 | |