| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the Qt3Support module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "q3socketdevice.h"
|
|---|
| 43 | #ifndef QT_NO_NETWORK
|
|---|
| 44 |
|
|---|
| 45 | #include "qwindowdefs.h"
|
|---|
| 46 | #include <string.h>
|
|---|
| 47 |
|
|---|
| 48 | QT_BEGIN_NAMESPACE
|
|---|
| 49 |
|
|---|
| 50 | //#define Q3SOCKETDEVICE_DEBUG
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | class Q3SocketDevicePrivate
|
|---|
| 54 | {
|
|---|
| 55 | public:
|
|---|
| 56 | Q3SocketDevicePrivate( Q3SocketDevice::Protocol p )
|
|---|
| 57 | : protocol(p)
|
|---|
| 58 | { }
|
|---|
| 59 |
|
|---|
| 60 | Q3SocketDevice::Protocol protocol;
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | /*!
|
|---|
| 65 | \class Q3SocketDevice
|
|---|
| 66 | \brief The Q3SocketDevice class provides a platform-independent low-level socket API.
|
|---|
| 67 |
|
|---|
| 68 | \compat
|
|---|
| 69 | \reentrant
|
|---|
| 70 |
|
|---|
| 71 | This class provides a low level API for working with sockets. Users of
|
|---|
| 72 | this class are assumed to have networking experience. For most users the
|
|---|
| 73 | Q3Socket class provides a much easier and high level alternative, but
|
|---|
| 74 | certain things (like UDP) can't be done with Q3Socket and if you need a
|
|---|
| 75 | platform-independent API for those, Q3SocketDevice is the right choice.
|
|---|
| 76 |
|
|---|
| 77 | The essential purpose of the class is to provide a QIODevice that
|
|---|
| 78 | works on sockets, wrapped in a platform-independent API.
|
|---|
| 79 |
|
|---|
| 80 | When calling connect() or bind(), Q3SocketDevice detects the
|
|---|
| 81 | protocol family (IPv4, IPv6) automatically. Passing the protocol
|
|---|
| 82 | family to Q3SocketDevice's constructor or to setSocket() forces
|
|---|
| 83 | creation of a socket device of a specific protocol. If not set, the
|
|---|
| 84 | protocol will be detected at the first call to connect() or bind().
|
|---|
| 85 |
|
|---|
| 86 | \sa Q3Socket, QSocketNotifier, QHostAddress
|
|---|
| 87 | */
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | /*!
|
|---|
| 91 | \enum Q3SocketDevice::Protocol
|
|---|
| 92 |
|
|---|
| 93 | This enum type describes the protocol family of the socket. Possible values
|
|---|
| 94 | are:
|
|---|
| 95 |
|
|---|
| 96 | \value IPv4 The socket is an IPv4 socket.
|
|---|
| 97 | \value IPv6 The socket is an IPv6 socket.
|
|---|
| 98 | \value Unknown The protocol family of the socket is not known. This can
|
|---|
| 99 | happen if you use Q3SocketDevice with an already existing socket; it
|
|---|
| 100 | tries to determine the protocol family, but this can fail if the
|
|---|
| 101 | protocol family is not known to Q3SocketDevice.
|
|---|
| 102 |
|
|---|
| 103 | \sa protocol() setSocket()
|
|---|
| 104 | */
|
|---|
| 105 |
|
|---|
| 106 | /*!
|
|---|
| 107 | \enum Q3SocketDevice::Error
|
|---|
| 108 |
|
|---|
| 109 | This enum type describes the error states of Q3SocketDevice.
|
|---|
| 110 |
|
|---|
| 111 | \value NoError No error has occurred.
|
|---|
| 112 |
|
|---|
| 113 | \value AlreadyBound The device is already bound, according to bind().
|
|---|
| 114 |
|
|---|
| 115 | \value Inaccessible The operating system or firewall prohibited
|
|---|
| 116 | the action.
|
|---|
| 117 |
|
|---|
| 118 | \value NoResources The operating system ran out of a resource.
|
|---|
| 119 |
|
|---|
| 120 | \value InternalError An internal error occurred in Q3SocketDevice.
|
|---|
| 121 |
|
|---|
| 122 | \value Impossible An attempt was made to do something which makes
|
|---|
| 123 | no sense. For example:
|
|---|
| 124 | \snippet doc/src/snippets/code/src_qt3support_network_q3socketdevice.cpp 0
|
|---|
| 125 | The libc ::close() closes the socket, but Q3SocketDevice is not aware
|
|---|
| 126 | of this. So when you call writeBlock(), the impossible happens.
|
|---|
| 127 |
|
|---|
| 128 | \value NoFiles The operating system will not let Q3SocketDevice open
|
|---|
| 129 | another file.
|
|---|
| 130 |
|
|---|
| 131 | \value ConnectionRefused A connection attempt was rejected by the
|
|---|
| 132 | peer.
|
|---|
| 133 |
|
|---|
| 134 | \value NetworkFailure There is a network failure.
|
|---|
| 135 |
|
|---|
| 136 | \value UnknownError The operating system did something
|
|---|
| 137 | unexpected.
|
|---|
| 138 |
|
|---|
| 139 | \omitvalue Bug
|
|---|
| 140 | */
|
|---|
| 141 |
|
|---|
| 142 | /*!
|
|---|
| 143 | \enum Q3SocketDevice::Type
|
|---|
| 144 |
|
|---|
| 145 | This enum type describes the type of the socket:
|
|---|
| 146 | \value Stream a stream socket (TCP, usually)
|
|---|
| 147 | \value Datagram a datagram socket (UDP, usually)
|
|---|
| 148 | */
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /*!
|
|---|
| 152 | Creates a Q3SocketDevice object for the existing socket \a socket.
|
|---|
| 153 |
|
|---|
| 154 | The \a type argument must match the actual socket type; use \c
|
|---|
| 155 | Q3SocketDevice::Stream for a reliable, connection-oriented TCP
|
|---|
| 156 | socket, or Q3SocketDevice::Datagram for an unreliable,
|
|---|
| 157 | connectionless UDP socket.
|
|---|
| 158 | */
|
|---|
| 159 | Q3SocketDevice::Q3SocketDevice( int socket, Type type )
|
|---|
| 160 | : fd( socket ), t( type ), p( 0 ), pp( 0 ), e( NoError ),
|
|---|
| 161 | d(new Q3SocketDevicePrivate(Unknown))
|
|---|
| 162 | {
|
|---|
| 163 | #if defined(Q3SOCKETDEVICE_DEBUG)
|
|---|
| 164 | qDebug( "Q3SocketDevice: Created Q3SocketDevice %p (socket %x, type %d)",
|
|---|
| 165 | this, socket, type );
|
|---|
| 166 | #endif
|
|---|
| 167 | init();
|
|---|
| 168 | setSocket( socket, type );
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /*!
|
|---|
| 172 | Creates a Q3SocketDevice object for a stream or datagram socket.
|
|---|
| 173 |
|
|---|
| 174 | The \a type argument must be either Q3SocketDevice::Stream for a
|
|---|
| 175 | reliable, connection-oriented TCP socket, or \c
|
|---|
| 176 | Q3SocketDevice::Datagram for an unreliable UDP socket.
|
|---|
| 177 |
|
|---|
| 178 | The socket is created as an IPv4 socket.
|
|---|
| 179 |
|
|---|
| 180 | \sa blocking() protocol()
|
|---|
| 181 | */
|
|---|
| 182 | Q3SocketDevice::Q3SocketDevice( Type type )
|
|---|
| 183 | : fd( -1 ), t( type ), p( 0 ), pp( 0 ), e( NoError ),
|
|---|
| 184 | d(new Q3SocketDevicePrivate(IPv4))
|
|---|
| 185 | {
|
|---|
| 186 | #if defined(Q3SOCKETDEVICE_DEBUG)
|
|---|
| 187 | qDebug( "Q3SocketDevice: Created Q3SocketDevice object %p, type %d",
|
|---|
| 188 | this, type );
|
|---|
| 189 | #endif
|
|---|
| 190 | init();
|
|---|
| 191 | setSocket( createNewSocket(), type );
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /*!
|
|---|
| 195 | Creates a Q3SocketDevice object for a stream or datagram socket.
|
|---|
| 196 |
|
|---|
| 197 | The \a type argument must be either Q3SocketDevice::Stream for a
|
|---|
| 198 | reliable, connection-oriented TCP socket, or \c
|
|---|
| 199 | Q3SocketDevice::Datagram for an unreliable UDP socket.
|
|---|
| 200 |
|
|---|
| 201 | The \a protocol indicates whether the socket should be of type IPv4
|
|---|
| 202 | or IPv6. Passing \c Unknown is not meaningful in this context and you
|
|---|
| 203 | should avoid using (it creates an IPv4 socket, but your code is not easily
|
|---|
| 204 | readable).
|
|---|
| 205 |
|
|---|
| 206 | The argument \a dummy is necessary for compatibility with some
|
|---|
| 207 | compilers.
|
|---|
| 208 |
|
|---|
| 209 | \sa blocking() protocol()
|
|---|
| 210 | */
|
|---|
| 211 | Q3SocketDevice::Q3SocketDevice( Type type, Protocol protocol, int )
|
|---|
| 212 | : fd( -1 ), t( type ), p( 0 ), pp( 0 ), e( NoError ),
|
|---|
| 213 | d(new Q3SocketDevicePrivate(protocol))
|
|---|
| 214 | {
|
|---|
| 215 | #if defined(Q3SOCKETDEVICE_DEBUG)
|
|---|
| 216 | qDebug( "Q3SocketDevice: Created Q3SocketDevice object %p, type %d",
|
|---|
| 217 | this, type );
|
|---|
| 218 | #endif
|
|---|
| 219 | init();
|
|---|
| 220 | setSocket( createNewSocket(), type );
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /*!
|
|---|
| 224 | Destroys the socket device and closes the socket if it is open.
|
|---|
| 225 | */
|
|---|
| 226 | Q3SocketDevice::~Q3SocketDevice()
|
|---|
| 227 | {
|
|---|
| 228 | close();
|
|---|
| 229 | delete d;
|
|---|
| 230 | d = 0;
|
|---|
| 231 | #if defined(Q3SOCKETDEVICE_DEBUG)
|
|---|
| 232 | qDebug( "Q3SocketDevice: Destroyed Q3SocketDevice %p", this );
|
|---|
| 233 | #endif
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | /*!
|
|---|
| 238 | Returns true if this is a valid socket; otherwise returns false.
|
|---|
| 239 |
|
|---|
| 240 | \sa socket()
|
|---|
| 241 | */
|
|---|
| 242 | bool Q3SocketDevice::isValid() const
|
|---|
| 243 | {
|
|---|
| 244 | return fd != -1;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 | /*!
|
|---|
| 249 | \fn Type Q3SocketDevice::type() const
|
|---|
| 250 |
|
|---|
| 251 | Returns the socket type which is either Q3SocketDevice::Stream
|
|---|
| 252 | or Q3SocketDevice::Datagram.
|
|---|
| 253 |
|
|---|
| 254 | \sa socket()
|
|---|
| 255 | */
|
|---|
| 256 | Q3SocketDevice::Type Q3SocketDevice::type() const
|
|---|
| 257 | {
|
|---|
| 258 | return t;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /*!
|
|---|
| 262 | Returns the socket's protocol family, which is one of \c Unknown, \c IPv4,
|
|---|
| 263 | or \c IPv6.
|
|---|
| 264 |
|
|---|
| 265 | Q3SocketDevice either creates a socket with a well known protocol family or
|
|---|
| 266 | it uses an already existing socket. In the first case, this function
|
|---|
| 267 | returns the protocol family it was constructed with. In the second case, it
|
|---|
| 268 | tries to determine the protocol family of the socket; if this fails, it
|
|---|
| 269 | returns \c Unknown.
|
|---|
| 270 |
|
|---|
| 271 | \sa Protocol setSocket()
|
|---|
| 272 | */
|
|---|
| 273 | Q3SocketDevice::Protocol Q3SocketDevice::protocol() const
|
|---|
| 274 | {
|
|---|
| 275 | if ( d->protocol == Unknown )
|
|---|
| 276 | d->protocol = getProtocol();
|
|---|
| 277 | return d->protocol;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /*!
|
|---|
| 281 | Returns the socket number, or -1 if it is an invalid socket.
|
|---|
| 282 |
|
|---|
| 283 | \sa isValid(), type()
|
|---|
| 284 | */
|
|---|
| 285 | int Q3SocketDevice::socket() const
|
|---|
| 286 | {
|
|---|
| 287 | return fd;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 | /*!
|
|---|
| 292 | Sets the socket device to operate on the existing socket \a
|
|---|
| 293 | socket.
|
|---|
| 294 |
|
|---|
| 295 | The \a type argument must match the actual socket type; use \c
|
|---|
| 296 | Q3SocketDevice::Stream for a reliable, connection-oriented TCP
|
|---|
| 297 | socket, or Q3SocketDevice::Datagram for an unreliable,
|
|---|
| 298 | connectionless UDP socket.
|
|---|
| 299 |
|
|---|
| 300 | Any existing socket is closed.
|
|---|
| 301 |
|
|---|
| 302 | \sa isValid(), close()
|
|---|
| 303 | */
|
|---|
| 304 | void Q3SocketDevice::setSocket( int socket, Type type )
|
|---|
| 305 | {
|
|---|
| 306 | if ( fd != -1 ) // close any open socket
|
|---|
| 307 | close();
|
|---|
| 308 | #if defined(Q3SOCKETDEVICE_DEBUG)
|
|---|
| 309 | qDebug( "Q3SocketDevice::setSocket: socket %x, type %d", socket, type );
|
|---|
| 310 | #endif
|
|---|
| 311 | t = type;
|
|---|
| 312 | fd = socket;
|
|---|
| 313 | d->protocol = Unknown;
|
|---|
| 314 | e = NoError;
|
|---|
| 315 | resetStatus();
|
|---|
| 316 | open( ReadWrite );
|
|---|
| 317 | fetchConnectionParameters();
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 | /*!
|
|---|
| 322 | Opens the socket using the specified QIODevice file \a mode. This
|
|---|
| 323 | function is called from the Q3SocketDevice constructors and from
|
|---|
| 324 | the setSocket() function. You should not call it yourself.
|
|---|
| 325 |
|
|---|
| 326 | \sa close()
|
|---|
| 327 | */
|
|---|
| 328 | bool Q3SocketDevice::open( OpenMode mode )
|
|---|
| 329 | {
|
|---|
| 330 | if ( isOpen() || !isValid() )
|
|---|
| 331 | return false;
|
|---|
| 332 | #if defined(Q3SOCKETDEVICE_DEBUG)
|
|---|
| 333 | qDebug( "Q3SocketDevice::open: mode %x", mode );
|
|---|
| 334 | #endif
|
|---|
| 335 | setOpenMode( (mode & ReadWrite) | Unbuffered );
|
|---|
| 336 | return true;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /*!
|
|---|
| 340 | \fn bool Q3SocketDevice::open(int mode)
|
|---|
| 341 | \overload
|
|---|
| 342 | */
|
|---|
| 343 | /*!
|
|---|
| 344 | The current Q3SocketDevice implementation does not buffer at all,
|
|---|
| 345 | so this is a no-op. This function always returns true.
|
|---|
| 346 | */
|
|---|
| 347 | bool Q3SocketDevice::flush()
|
|---|
| 348 | {
|
|---|
| 349 | return true;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 | /*!
|
|---|
| 354 | \reimp
|
|---|
| 355 |
|
|---|
| 356 | The size is meaningless for a socket, therefore this function returns 0.
|
|---|
| 357 | */
|
|---|
| 358 | QIODevice::Offset Q3SocketDevice::size() const
|
|---|
| 359 | {
|
|---|
| 360 | return 0;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 | /*!
|
|---|
| 365 | The read/write index is meaningless for a socket, therefore this
|
|---|
| 366 | function returns 0.
|
|---|
| 367 | */
|
|---|
| 368 | QIODevice::Offset Q3SocketDevice::at() const
|
|---|
| 369 | {
|
|---|
| 370 | return 0;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 | /*!
|
|---|
| 375 | The read/write index is meaningless for a socket, therefore this
|
|---|
| 376 | function does nothing and returns true.
|
|---|
| 377 |
|
|---|
| 378 | The \a offset parameter is ignored.
|
|---|
| 379 | */
|
|---|
| 380 | bool Q3SocketDevice::at( Offset /* offset */ )
|
|---|
| 381 | {
|
|---|
| 382 | return true;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 | /*!
|
|---|
| 387 | \reimp
|
|---|
| 388 |
|
|---|
| 389 | Returns true if no data is currently available at the socket;
|
|---|
| 390 | otherwise returns false.
|
|---|
| 391 | */
|
|---|
| 392 | bool Q3SocketDevice::atEnd() const
|
|---|
| 393 | {
|
|---|
| 394 | return bytesAvailable() <= 0;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | /*!
|
|---|
| 398 | Returns true if the address of this socket can be used by other
|
|---|
| 399 | sockets at the same time, and false if this socket claims
|
|---|
| 400 | exclusive ownership.
|
|---|
| 401 |
|
|---|
| 402 | \sa setAddressReusable()
|
|---|
| 403 | */
|
|---|
| 404 | bool Q3SocketDevice::addressReusable() const
|
|---|
| 405 | {
|
|---|
| 406 | return option( ReuseAddress );
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 | /*!
|
|---|
| 411 | Sets the address of this socket to be usable by other sockets too
|
|---|
| 412 | if \a enable is true, and to be used exclusively by this socket if
|
|---|
| 413 | \a enable is false.
|
|---|
| 414 |
|
|---|
| 415 | When a socket is reusable, other sockets can use the same port
|
|---|
| 416 | number (and IP address), which is generally useful. Of course
|
|---|
| 417 | other sockets cannot use the same
|
|---|
| 418 | (address,port,peer-address,peer-port) 4-tuple as this socket, so
|
|---|
| 419 | there is no risk of confusing the two TCP connections.
|
|---|
| 420 |
|
|---|
| 421 | \sa addressReusable()
|
|---|
| 422 | */
|
|---|
| 423 | void Q3SocketDevice::setAddressReusable( bool enable )
|
|---|
| 424 | {
|
|---|
| 425 | setOption( ReuseAddress, enable );
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 | /*!
|
|---|
| 430 | Returns the size of the operating system receive buffer.
|
|---|
| 431 |
|
|---|
| 432 | \sa setReceiveBufferSize()
|
|---|
| 433 | */
|
|---|
| 434 | int Q3SocketDevice::receiveBufferSize() const
|
|---|
| 435 | {
|
|---|
| 436 | return option( ReceiveBuffer );
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 | /*!
|
|---|
| 441 | Sets the size of the operating system receive buffer to \a size.
|
|---|
| 442 |
|
|---|
| 443 | The operating system receive buffer size effectively limits two
|
|---|
| 444 | things: how much data can be in transit at any one moment, and how
|
|---|
| 445 | much data can be received in one iteration of the main event loop.
|
|---|
| 446 |
|
|---|
| 447 | The default is operating system-dependent. A socket that receives
|
|---|
| 448 | large amounts of data is probably best with a buffer size of
|
|---|
| 449 | 49152.
|
|---|
| 450 | */
|
|---|
| 451 | void Q3SocketDevice::setReceiveBufferSize( uint size )
|
|---|
| 452 | {
|
|---|
| 453 | setOption( ReceiveBuffer, size );
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 | /*!
|
|---|
| 458 | Returns the size of the operating system send buffer.
|
|---|
| 459 |
|
|---|
| 460 | \sa setSendBufferSize()
|
|---|
| 461 | */
|
|---|
| 462 | int Q3SocketDevice::sendBufferSize() const
|
|---|
| 463 | {
|
|---|
| 464 | return option( SendBuffer );
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 | /*!
|
|---|
| 469 | Sets the size of the operating system send buffer to \a size.
|
|---|
| 470 |
|
|---|
| 471 | The operating system send buffer size effectively limits how much
|
|---|
| 472 | data can be in transit at any one moment.
|
|---|
| 473 |
|
|---|
| 474 | The default is operating system-dependent. A socket that sends
|
|---|
| 475 | large amounts of data is probably best with a buffer size of
|
|---|
| 476 | 49152.
|
|---|
| 477 | */
|
|---|
| 478 | void Q3SocketDevice::setSendBufferSize( uint size )
|
|---|
| 479 | {
|
|---|
| 480 | setOption( SendBuffer, size );
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 | /*!
|
|---|
| 485 | Returns the port number of this socket device. This may be 0 for a
|
|---|
| 486 | while, but is set to something sensible as soon as a sensible
|
|---|
| 487 | value is available.
|
|---|
| 488 |
|
|---|
| 489 | Note that Qt always uses native byte order, i.e. 67 is 67 in Qt;
|
|---|
| 490 | there is no need to call htons().
|
|---|
| 491 | */
|
|---|
| 492 | quint16 Q3SocketDevice::port() const
|
|---|
| 493 | {
|
|---|
| 494 | return p;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 | /*!
|
|---|
| 499 | Returns the address of this socket device. This may be 0.0.0.0 for
|
|---|
| 500 | a while, but is set to something sensible as soon as a sensible
|
|---|
| 501 | value is available.
|
|---|
| 502 | */
|
|---|
| 503 | QHostAddress Q3SocketDevice::address() const
|
|---|
| 504 | {
|
|---|
| 505 | return a;
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 | /*!
|
|---|
| 510 | Returns the first error seen.
|
|---|
| 511 | */
|
|---|
| 512 | Q3SocketDevice::Error Q3SocketDevice::error() const
|
|---|
| 513 | {
|
|---|
| 514 | return e;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 | /*!
|
|---|
| 519 | Allows subclasses to set the error state to \a err.
|
|---|
| 520 | */
|
|---|
| 521 | void Q3SocketDevice::setError( Error err )
|
|---|
| 522 | {
|
|---|
| 523 | e = err;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | /*! \fn Q3SocketDevice::readBlock(char *data, Q_ULONG maxlen)
|
|---|
| 527 |
|
|---|
| 528 | Reads \a maxlen bytes from the socket into \a data and returns the
|
|---|
| 529 | number of bytes read. Returns -1 if an error occurred. Returning 0
|
|---|
| 530 | is not an error. For Stream sockets, 0 is returned when the remote
|
|---|
| 531 | host closes the connection. For Datagram sockets, 0 is a valid
|
|---|
| 532 | datagram size.
|
|---|
| 533 | */
|
|---|
| 534 |
|
|---|
| 535 | /*! \fn Q3SocketDevice::writeBlock(const char *data, Q_ULONG len)
|
|---|
| 536 |
|
|---|
| 537 | Writes \a len bytes to the socket from \a data and returns the
|
|---|
| 538 | number of bytes written. Returns -1 if an error occurred.
|
|---|
| 539 |
|
|---|
| 540 | This is used for Q3SocketDevice::Stream sockets.
|
|---|
| 541 | */
|
|---|
| 542 |
|
|---|
| 543 | /*!
|
|---|
| 544 | \fn Q_LONG Q3SocketDevice::writeBlock( const char * data, Q_ULONG len,
|
|---|
| 545 | const QHostAddress & host, Q_UINT16 port )
|
|---|
| 546 | \overload
|
|---|
| 547 |
|
|---|
| 548 | Writes \a len bytes to the socket from \a data and returns the
|
|---|
| 549 | number of bytes written. Returns -1 if an error occurred.
|
|---|
| 550 |
|
|---|
| 551 | This is used for Q3SocketDevice::Datagram sockets. You must
|
|---|
| 552 | specify the \a host and \a port of the destination of the data.
|
|---|
| 553 | */
|
|---|
| 554 |
|
|---|
| 555 | /*!
|
|---|
| 556 | \fn bool Q3SocketDevice::isSequential() const
|
|---|
| 557 | \internal
|
|---|
| 558 | */
|
|---|
| 559 |
|
|---|
| 560 | /*!
|
|---|
| 561 | \fn qint64 Q3SocketDevice::readData( char *data, qint64 maxlen )
|
|---|
| 562 |
|
|---|
| 563 | Reads \a maxlen bytes from the socket into \a data and returns the
|
|---|
| 564 | number of bytes read. Returns -1 if an error occurred.
|
|---|
| 565 | */
|
|---|
| 566 |
|
|---|
| 567 | /*!
|
|---|
| 568 | \fn int Q3SocketDevice::createNewSocket()
|
|---|
| 569 |
|
|---|
| 570 | Creates a new socket identifier. Returns -1 if there is a failure
|
|---|
| 571 | to create the new identifier; error() explains why.
|
|---|
| 572 |
|
|---|
| 573 | \sa setSocket()
|
|---|
| 574 | */
|
|---|
| 575 |
|
|---|
| 576 | /*!
|
|---|
| 577 | \fn void Q3SocketDevice::close()
|
|---|
| 578 | \reimp
|
|---|
| 579 |
|
|---|
| 580 | Closes the socket and sets the socket identifier to -1 (invalid).
|
|---|
| 581 |
|
|---|
| 582 | (This function ignores errors; if there are any then a file
|
|---|
| 583 | descriptor leakage might result. As far as we know, the only error
|
|---|
| 584 | that can arise is EBADF, and that would of course not cause
|
|---|
| 585 | leakage. There may be OS-specific errors that we haven't come
|
|---|
| 586 | across, however.)
|
|---|
| 587 |
|
|---|
| 588 | \sa open()
|
|---|
| 589 | */
|
|---|
| 590 |
|
|---|
| 591 | /*!
|
|---|
| 592 | \fn bool Q3SocketDevice::blocking() const
|
|---|
| 593 |
|
|---|
| 594 | Returns true if the socket is valid and in blocking mode;
|
|---|
| 595 | otherwise returns false.
|
|---|
| 596 |
|
|---|
| 597 | Note that this function does not set error().
|
|---|
| 598 |
|
|---|
| 599 | \warning On Windows, this function always returns true since the
|
|---|
| 600 | ioctlsocket() function is broken.
|
|---|
| 601 |
|
|---|
| 602 | \sa setBlocking(), isValid()
|
|---|
| 603 | */
|
|---|
| 604 |
|
|---|
| 605 | /*!
|
|---|
| 606 | \fn void Q3SocketDevice::setBlocking( bool enable )
|
|---|
| 607 |
|
|---|
| 608 | Makes the socket blocking if \a enable is true or nonblocking if
|
|---|
| 609 | \a enable is false.
|
|---|
| 610 |
|
|---|
| 611 | Sockets are blocking by default, but we recommend using
|
|---|
| 612 | nonblocking socket operations, especially for GUI programs that
|
|---|
| 613 | need to be responsive.
|
|---|
| 614 |
|
|---|
| 615 | \warning On Windows, this function should be used with care since
|
|---|
| 616 | whenever you use a QSocketNotifier on Windows, the socket is
|
|---|
| 617 | immediately made nonblocking.
|
|---|
| 618 |
|
|---|
| 619 | \sa blocking(), isValid()
|
|---|
| 620 | */
|
|---|
| 621 |
|
|---|
| 622 | /*!
|
|---|
| 623 | \fn int Q3SocketDevice::option( Option opt ) const
|
|---|
| 624 |
|
|---|
| 625 | Returns the value of the socket option \a opt.
|
|---|
| 626 | */
|
|---|
| 627 |
|
|---|
| 628 | /*!
|
|---|
| 629 | \fn void Q3SocketDevice::setOption( Option opt, int v )
|
|---|
| 630 |
|
|---|
| 631 | Sets the socket option \a opt to \a v.
|
|---|
| 632 | */
|
|---|
| 633 |
|
|---|
| 634 | /*!
|
|---|
| 635 | \fn bool Q3SocketDevice::connect( const QHostAddress &addr, Q_UINT16 port )
|
|---|
| 636 |
|
|---|
| 637 | Connects to the IP address and port specified by \a addr and \a
|
|---|
| 638 | port. Returns true if it establishes a connection; otherwise returns false.
|
|---|
| 639 | If it returns false, error() explains why.
|
|---|
| 640 |
|
|---|
| 641 | Note that error() commonly returns NoError for non-blocking
|
|---|
| 642 | sockets; this just means that you can call connect() again in a
|
|---|
| 643 | little while and it'll probably succeed.
|
|---|
| 644 | */
|
|---|
| 645 |
|
|---|
| 646 | /*!
|
|---|
| 647 | \fn bool Q3SocketDevice::bind( const QHostAddress &address, Q_UINT16 port )
|
|---|
| 648 |
|
|---|
| 649 | Assigns a name to an unnamed socket. The name is the host address
|
|---|
| 650 | \a address and the port number \a port. If the operation succeeds,
|
|---|
| 651 | bind() returns true; otherwise it returns false without changing
|
|---|
| 652 | what port() and address() return.
|
|---|
| 653 |
|
|---|
| 654 | bind() is used by servers for setting up incoming connections.
|
|---|
| 655 | Call bind() before listen().
|
|---|
| 656 | */
|
|---|
| 657 |
|
|---|
| 658 | /*!
|
|---|
| 659 | \fn bool Q3SocketDevice::listen( int backlog )
|
|---|
| 660 |
|
|---|
| 661 | Specifies how many pending connections a server socket can have.
|
|---|
| 662 | Returns true if the operation was successful; otherwise returns
|
|---|
| 663 | false. A \a backlog value of 50 is quite common.
|
|---|
| 664 |
|
|---|
| 665 | The listen() call only applies to sockets where type() is \c
|
|---|
| 666 | Stream, i.e. not to \c Datagram sockets. listen() must not be
|
|---|
| 667 | called before bind() or after accept().
|
|---|
| 668 |
|
|---|
| 669 | \sa bind(), accept()
|
|---|
| 670 | */
|
|---|
| 671 |
|
|---|
| 672 | /*!
|
|---|
| 673 | \fn int Q3SocketDevice::accept()
|
|---|
| 674 |
|
|---|
| 675 | Extracts the first connection from the queue of pending
|
|---|
| 676 | connections for this socket and returns a new socket identifier.
|
|---|
| 677 | Returns -1 if the operation failed.
|
|---|
| 678 |
|
|---|
| 679 | \sa bind(), listen()
|
|---|
| 680 | */
|
|---|
| 681 |
|
|---|
| 682 | /*!
|
|---|
| 683 | \fn qint64 Q3SocketDevice::bytesAvailable() const
|
|---|
| 684 |
|
|---|
| 685 | Returns the number of bytes available for reading, or -1 if an
|
|---|
| 686 | error occurred.
|
|---|
| 687 |
|
|---|
| 688 | \warning On Microsoft Windows, we use the ioctlsocket() function
|
|---|
| 689 | to determine the number of bytes queued on the socket. According
|
|---|
| 690 | to Microsoft (KB Q125486), ioctlsocket() sometimes returns an
|
|---|
| 691 | incorrect number. The only safe way to determine the amount of
|
|---|
| 692 | data on the socket is to read it using readBlock(). QSocket has
|
|---|
| 693 | workarounds to deal with this problem.
|
|---|
| 694 | */
|
|---|
| 695 |
|
|---|
| 696 | /*!
|
|---|
| 697 | \fn Q_LONG Q3SocketDevice::waitForMore( int msecs, bool *timeout ) const
|
|---|
| 698 |
|
|---|
| 699 | Wait up to \a msecs milliseconds for more data to be available. If
|
|---|
| 700 | \a msecs is -1 the call will block indefinitely.
|
|---|
| 701 |
|
|---|
| 702 | Returns the number of bytes available for reading, or -1 if an
|
|---|
| 703 | error occurred.
|
|---|
| 704 |
|
|---|
| 705 | If \a timeout is non-null and no error occurred (i.e. it does not
|
|---|
| 706 | return -1): this function sets *\a timeout to true, if the reason
|
|---|
| 707 | for returning was that the timeout was reached; otherwise it sets
|
|---|
| 708 | *\a timeout to false. This is useful to find out if the peer
|
|---|
| 709 | closed the connection.
|
|---|
| 710 |
|
|---|
| 711 | \warning This is a blocking call and should be avoided in event
|
|---|
| 712 | driven applications.
|
|---|
| 713 |
|
|---|
| 714 | \sa bytesAvailable()
|
|---|
| 715 | */
|
|---|
| 716 |
|
|---|
| 717 | /*!
|
|---|
| 718 | \fn qint64 Q3SocketDevice::writeData( const char *data, qint64 len )
|
|---|
| 719 |
|
|---|
| 720 | Writes \a len bytes to the socket from \a data and returns the
|
|---|
| 721 | number of bytes written. Returns -1 if an error occurred.
|
|---|
| 722 |
|
|---|
| 723 | This is used for Q3SocketDevice::Stream sockets.
|
|---|
| 724 | */
|
|---|
| 725 |
|
|---|
| 726 | /*!
|
|---|
| 727 | \fn void Q3SocketDevice::fetchConnectionParameters()
|
|---|
| 728 |
|
|---|
| 729 | Fetches information about both ends of the connection: whatever is
|
|---|
| 730 | available.
|
|---|
| 731 | */
|
|---|
| 732 |
|
|---|
| 733 | /*!
|
|---|
| 734 | \fn Q_UINT16 Q3SocketDevice::peerPort() const
|
|---|
| 735 |
|
|---|
| 736 | Returns the port number of the port this socket device is
|
|---|
| 737 | connected to. This may be 0 for a while, but is set to something
|
|---|
| 738 | sensible as soon as a sensible value is available.
|
|---|
| 739 |
|
|---|
| 740 | Note that for Datagram sockets, this is the source port of the
|
|---|
| 741 | last packet received, and that it is in native byte order.
|
|---|
| 742 | */
|
|---|
| 743 |
|
|---|
| 744 | /*!
|
|---|
| 745 | \fn QHostAddress Q3SocketDevice::peerAddress() const
|
|---|
| 746 |
|
|---|
| 747 | Returns the address of the port this socket device is connected
|
|---|
| 748 | to. This may be 0.0.0.0 for a while, but is set to something
|
|---|
| 749 | sensible as soon as a sensible value is available.
|
|---|
| 750 |
|
|---|
| 751 | Note that for Datagram sockets, this is the source port of the
|
|---|
| 752 | last packet received.
|
|---|
| 753 | */
|
|---|
| 754 |
|
|---|
| 755 | QT_END_NAMESPACE
|
|---|
| 756 |
|
|---|
| 757 | #endif //QT_NO_NETWORK
|
|---|