util/src/network/socket/qabstractsocketengine_p.h
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QABSTRACTSOCKETENGINE_P_H
       
    43 #define QABSTRACTSOCKETENGINE_P_H
       
    44 
       
    45 //
       
    46 //  W A R N I N G
       
    47 //  -------------
       
    48 //
       
    49 // This file is not part of the Qt API.  It exists purely as an
       
    50 // implementation detail.  This header file may change from version to
       
    51 // version without notice, or even be removed.
       
    52 //
       
    53 // We mean it.
       
    54 //
       
    55 
       
    56 #include "QtNetwork/qhostaddress.h"
       
    57 #include "QtNetwork/qabstractsocket.h"
       
    58 #include "private/qobject_p.h"
       
    59 
       
    60 QT_BEGIN_NAMESPACE
       
    61 
       
    62 class QAuthenticator;
       
    63 class QAbstractSocketEnginePrivate;
       
    64 class QNetworkProxy;
       
    65 
       
    66 class QAbstractSocketEngineReceiver {
       
    67 public:
       
    68     virtual ~QAbstractSocketEngineReceiver(){}
       
    69     virtual void readNotification()= 0;
       
    70     virtual void writeNotification()= 0;
       
    71     virtual void exceptionNotification()= 0;
       
    72     virtual void connectionNotification()= 0;
       
    73 #ifndef QT_NO_NETWORKPROXY
       
    74     virtual void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)= 0;
       
    75 #endif
       
    76 };
       
    77 
       
    78 class Q_AUTOTEST_EXPORT QAbstractSocketEngine : public QObject
       
    79 {
       
    80     Q_OBJECT
       
    81 public:
       
    82 
       
    83     static QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent);
       
    84     static QAbstractSocketEngine *createSocketEngine(int socketDescripter, QObject *parent);
       
    85 
       
    86     QAbstractSocketEngine(QObject *parent = 0);
       
    87 
       
    88     enum SocketOption {
       
    89         NonBlockingSocketOption,
       
    90         BroadcastSocketOption,
       
    91         ReceiveBufferSocketOption,
       
    92         SendBufferSocketOption,
       
    93         AddressReusable,
       
    94         BindExclusively,
       
    95         ReceiveOutOfBandData,
       
    96         LowDelayOption,
       
    97         KeepAliveOption
       
    98     };
       
    99 
       
   100     virtual bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) = 0;
       
   101 
       
   102     virtual bool initialize(int socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) = 0;
       
   103 
       
   104     virtual int socketDescriptor() const = 0;
       
   105 
       
   106     virtual bool isValid() const = 0;
       
   107 
       
   108     virtual bool connectToHost(const QHostAddress &address, quint16 port) = 0;
       
   109     virtual bool connectToHostByName(const QString &name, quint16 port) = 0;
       
   110     virtual bool bind(const QHostAddress &address, quint16 port) = 0;
       
   111     virtual bool listen() = 0;
       
   112     virtual int accept() = 0;
       
   113     virtual void close() = 0;
       
   114 
       
   115     virtual qint64 bytesAvailable() const = 0;
       
   116 
       
   117     virtual qint64 read(char *data, qint64 maxlen) = 0;
       
   118     virtual qint64 write(const char *data, qint64 len) = 0;
       
   119 
       
   120 #ifndef QT_NO_UDPSOCKET
       
   121     virtual qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *addr = 0,
       
   122                                 quint16 *port = 0) = 0;
       
   123     virtual qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &addr,
       
   124                                  quint16 port) = 0;
       
   125     virtual bool hasPendingDatagrams() const = 0;
       
   126     virtual qint64 pendingDatagramSize() const = 0;
       
   127 #endif
       
   128 
       
   129     virtual qint64 bytesToWrite() const = 0;
       
   130 
       
   131     virtual int option(SocketOption option) const = 0;
       
   132     virtual bool setOption(SocketOption option, int value) = 0;
       
   133 
       
   134     virtual bool waitForRead(int msecs = 30000, bool *timedOut = 0) = 0;
       
   135     virtual bool waitForWrite(int msecs = 30000, bool *timedOut = 0) = 0;
       
   136     virtual bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
       
   137 			    bool checkRead, bool checkWrite,
       
   138                             int msecs = 30000, bool *timedOut = 0) = 0;
       
   139 
       
   140     QAbstractSocket::SocketError error() const;
       
   141     QString errorString() const;
       
   142     QAbstractSocket::SocketState state() const;
       
   143     QAbstractSocket::SocketType socketType() const;
       
   144     QAbstractSocket::NetworkLayerProtocol protocol() const;
       
   145 
       
   146     QHostAddress localAddress() const;
       
   147     quint16 localPort() const;
       
   148     QHostAddress peerAddress() const;
       
   149     quint16 peerPort() const;
       
   150 
       
   151     virtual bool isReadNotificationEnabled() const = 0;
       
   152     virtual void setReadNotificationEnabled(bool enable) = 0;
       
   153     virtual bool isWriteNotificationEnabled() const = 0;
       
   154     virtual void setWriteNotificationEnabled(bool enable) = 0;
       
   155     virtual bool isExceptionNotificationEnabled() const = 0;
       
   156     virtual void setExceptionNotificationEnabled(bool enable) = 0;
       
   157 
       
   158 public Q_SLOTS:
       
   159     void readNotification();
       
   160     void writeNotification();
       
   161     void exceptionNotification();
       
   162     void connectionNotification();
       
   163 #ifndef QT_NO_NETWORKPROXY
       
   164     void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
       
   165 #endif
       
   166 
       
   167 public:
       
   168     void setReceiver(QAbstractSocketEngineReceiver *receiver);
       
   169 protected:
       
   170     QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent = 0);
       
   171 
       
   172     void setError(QAbstractSocket::SocketError error, const QString &errorString) const;
       
   173     void setState(QAbstractSocket::SocketState state);
       
   174     void setSocketType(QAbstractSocket::SocketType socketType);
       
   175     void setProtocol(QAbstractSocket::NetworkLayerProtocol protocol);
       
   176     void setLocalAddress(const QHostAddress &address);
       
   177     void setLocalPort(quint16 port);
       
   178     void setPeerAddress(const QHostAddress &address);
       
   179     void setPeerPort(quint16 port);
       
   180 
       
   181 private:
       
   182     Q_DECLARE_PRIVATE(QAbstractSocketEngine)
       
   183     Q_DISABLE_COPY(QAbstractSocketEngine)
       
   184 };
       
   185 
       
   186 class QAbstractSocketEnginePrivate : public QObjectPrivate
       
   187 {
       
   188     Q_DECLARE_PUBLIC(QAbstractSocketEngine)
       
   189 public:
       
   190     QAbstractSocketEnginePrivate();
       
   191 
       
   192     mutable QAbstractSocket::SocketError socketError;
       
   193     mutable bool hasSetSocketError;
       
   194     mutable QString socketErrorString;
       
   195     QAbstractSocket::SocketState socketState;
       
   196     QAbstractSocket::SocketType socketType;
       
   197     QAbstractSocket::NetworkLayerProtocol socketProtocol;
       
   198     QHostAddress localAddress;
       
   199     quint16 localPort;
       
   200     QHostAddress peerAddress;
       
   201     quint16 peerPort;
       
   202     QAbstractSocketEngineReceiver *receiver;
       
   203 };
       
   204 
       
   205 
       
   206 class Q_AUTOTEST_EXPORT QSocketEngineHandler
       
   207 {
       
   208 protected:
       
   209     QSocketEngineHandler();
       
   210     virtual ~QSocketEngineHandler();
       
   211     virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
       
   212                                                       const QNetworkProxy &, QObject *parent) = 0;
       
   213     virtual QAbstractSocketEngine *createSocketEngine(int socketDescripter, QObject *parent) = 0;
       
   214 
       
   215 private:
       
   216     friend class QAbstractSocketEngine;
       
   217 };
       
   218 
       
   219 QT_END_NAMESPACE
       
   220 
       
   221 #endif // QABSTRACTSOCKETENGINE_P_H