qabstractsocket.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtNetwork module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QABSTRACTSOCKET_H
00043 #define QABSTRACTSOCKET_H
00044 
00045 #include <QtCore/qiodevice.h>
00046 #include <QtCore/qobject.h>
00047 #ifndef QT_NO_DEBUG_STREAM
00048 #include <QtCore/qdebug.h>
00049 #endif
00050 
00051 QT_BEGIN_HEADER
00052 
00053 QT_BEGIN_NAMESPACE
00054 
00055 QT_MODULE(Network)
00056 
00057 class QHostAddress;
00058 #ifndef QT_NO_NETWORKPROXY
00059 class QNetworkProxy;
00060 #endif
00061 class QAbstractSocketPrivate;
00062 class QAuthenticator;
00063 
00064 class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice
00065 {
00066     Q_OBJECT
00067 public:
00068     enum SocketType {
00069         TcpSocket,
00070         UdpSocket,
00071         UnknownSocketType = -1
00072     };
00073     enum NetworkLayerProtocol {
00074         IPv4Protocol,
00075         IPv6Protocol,
00076         UnknownNetworkLayerProtocol = -1
00077     };
00078     enum SocketError {
00079         ConnectionRefusedError,
00080         RemoteHostClosedError,
00081         HostNotFoundError,
00082         SocketAccessError,
00083         SocketResourceError,
00084         SocketTimeoutError,                     /* 5 */
00085         DatagramTooLargeError,
00086         NetworkError,
00087         AddressInUseError,
00088         SocketAddressNotAvailableError,
00089         UnsupportedSocketOperationError,        /* 10 */
00090         UnfinishedSocketOperationError,
00091         ProxyAuthenticationRequiredError,
00092         SslHandshakeFailedError,
00093         ProxyConnectionRefusedError,
00094         ProxyConnectionClosedError,             /* 15 */
00095         ProxyConnectionTimeoutError,
00096         ProxyNotFoundError,
00097         ProxyProtocolError,
00098 
00099         UnknownSocketError = -1
00100     };
00101     enum SocketState {
00102         UnconnectedState,
00103         HostLookupState,
00104         ConnectingState,
00105         ConnectedState,
00106         BoundState,
00107         ListeningState,
00108         ClosingState
00109 #ifdef QT3_SUPPORT
00110         ,
00111         Idle = UnconnectedState,
00112         HostLookup = HostLookupState,
00113         Connecting = ConnectingState,
00114         Connected = ConnectedState,
00115         Closing = ClosingState,
00116         Connection = ConnectedState
00117 #endif
00118     };
00119     enum SocketOption {
00120         LowDelayOption, // TCP_NODELAY
00121         KeepAliveOption // SO_KEEPALIVE
00122     };
00123 
00124     QAbstractSocket(SocketType socketType, QObject *parent);
00125     virtual ~QAbstractSocket();
00126 
00127     // ### Qt 5: Make connectToHost() and disconnectFromHost() virtual.
00128     void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
00129     void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
00130     void disconnectFromHost();
00131 
00132     bool isValid() const;
00133 
00134     qint64 bytesAvailable() const;
00135     qint64 bytesToWrite() const;
00136 
00137     bool canReadLine() const;
00138 
00139     quint16 localPort() const;
00140     QHostAddress localAddress() const;
00141     quint16 peerPort() const;
00142     QHostAddress peerAddress() const;
00143     QString peerName() const;
00144 
00145     // ### Qt 5: Make setReadBufferSize() virtual
00146     qint64 readBufferSize() const;
00147     void setReadBufferSize(qint64 size);
00148 
00149     void abort();
00150 
00151     // ### Qt 5: Make socketDescriptor() and setSocketDescriptor() virtual.
00152     int socketDescriptor() const;
00153     bool setSocketDescriptor(int socketDescriptor, SocketState state = ConnectedState,
00154                              OpenMode openMode = ReadWrite);
00155 
00156     // ### Qt 5: Make virtual?
00157     void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
00158     QVariant socketOption(QAbstractSocket::SocketOption option);
00159 
00160     SocketType socketType() const;
00161     SocketState state() const;
00162     SocketError error() const;
00163 
00164     // from QIODevice
00165     void close();
00166     bool isSequential() const;
00167     bool atEnd() const;
00168     bool flush();
00169 
00170     // for synchronous access
00171     // ### Qt 5: Make waitForConnected() and waitForDisconnected() virtual.
00172     bool waitForConnected(int msecs = 30000);
00173     bool waitForReadyRead(int msecs = 30000);
00174     bool waitForBytesWritten(int msecs = 30000);
00175     bool waitForDisconnected(int msecs = 30000);
00176 
00177 #ifndef QT_NO_NETWORKPROXY
00178     void setProxy(const QNetworkProxy &networkProxy);
00179     QNetworkProxy proxy() const;
00180 #endif
00181 
00182 Q_SIGNALS:
00183     void hostFound();
00184     void connected();
00185     void disconnected();
00186     void stateChanged(QAbstractSocket::SocketState);
00187     void error(QAbstractSocket::SocketError);
00188 #ifndef QT_NO_NETWORKPROXY
00189     void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
00190 #endif
00191 
00192 protected Q_SLOTS:
00193     void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
00194     void disconnectFromHostImplementation();
00195 
00196 protected:
00197     qint64 readData(char *data, qint64 maxlen);
00198     qint64 readLineData(char *data, qint64 maxlen);
00199     qint64 writeData(const char *data, qint64 len);
00200 
00201     void setSocketState(SocketState state);
00202     void setSocketError(SocketError socketError);
00203     void setLocalPort(quint16 port);
00204     void setLocalAddress(const QHostAddress &address);
00205     void setPeerPort(quint16 port);
00206     void setPeerAddress(const QHostAddress &address);
00207     void setPeerName(const QString &name);
00208 
00209     QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = 0);
00210 
00211 private:
00212     Q_DECLARE_PRIVATE(QAbstractSocket)
00213     Q_DISABLE_COPY(QAbstractSocket)
00214 
00215     Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress())
00216     Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &))
00217     Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt())
00218     Q_PRIVATE_SLOT(d_func(), void _q_testConnection())
00219     Q_PRIVATE_SLOT(d_func(), void _q_forceDisconnect())
00220 
00221 #ifdef QT3_SUPPORT
00222 public:
00223     enum Error {
00224         ErrConnectionRefused = ConnectionRefusedError,
00225         ErrHostNotFound = HostNotFoundError,
00226         ErrSocketRead = UnknownSocketError
00227     };
00228     inline QT3_SUPPORT int socket() const { return socketDescriptor(); }
00229     inline QT3_SUPPORT void setSocket(int socket) { setSocketDescriptor(socket); }
00230     inline QT3_SUPPORT qulonglong waitForMore(int msecs, bool *timeout = 0) const
00231     {
00232         QAbstractSocket *that = const_cast<QAbstractSocket *>(this);
00233         if (that->waitForReadyRead(msecs))
00234             return qulonglong(bytesAvailable());
00235         if (error() == SocketTimeoutError && timeout)
00236             *timeout = true;
00237         return 0;
00238     }
00239     typedef SocketState State;
00240 Q_SIGNALS:
00241     QT_MOC_COMPAT void connectionClosed(); // same as disconnected()
00242     QT_MOC_COMPAT void delayedCloseFinished(); // same as disconnected()
00243 
00244 
00245 #endif
00246 };
00247 
00248 #ifndef QT_NO_DEBUG_STREAM
00249 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError);
00250 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState);
00251 #endif
00252 
00253 QT_END_NAMESPACE
00254 
00255 QT_END_HEADER
00256 
00257 #endif // QABSTRACTSOCKET_H