src/network/socket/qnativesocketengine_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 QNATIVESOCKETENGINE_P_H
       
    43 #define QNATIVESOCKETENGINE_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 for the convenience
       
    50 // of the QLibrary class.  This header file may change from
       
    51 // version to version without notice, or even be removed.
       
    52 //
       
    53 // We mean it.
       
    54 //
       
    55 #include "QtNetwork/qhostaddress.h"
       
    56 #include "private/qabstractsocketengine_p.h"
       
    57 #ifndef Q_OS_WIN
       
    58 #  include "qplatformdefs.h"
       
    59 #else
       
    60 #  include <winsock2.h>
       
    61 #endif
       
    62 
       
    63 #ifdef Q_OS_SYMBIAN
       
    64 #include <private/qeventdispatcher_symbian_p.h>
       
    65 #include <unistd.h>
       
    66 #endif
       
    67 
       
    68 QT_BEGIN_NAMESPACE
       
    69 
       
    70 // Use our own defines and structs which we know are correct
       
    71 #  define QT_SS_MAXSIZE 128
       
    72 #  define QT_SS_ALIGNSIZE (sizeof(qint64))
       
    73 #  define QT_SS_PAD1SIZE (QT_SS_ALIGNSIZE - sizeof (short))
       
    74 #  define QT_SS_PAD2SIZE (QT_SS_MAXSIZE - (sizeof (short) + QT_SS_PAD1SIZE + QT_SS_ALIGNSIZE))
       
    75 struct qt_sockaddr_storage {
       
    76       short ss_family;
       
    77       char __ss_pad1[QT_SS_PAD1SIZE];
       
    78       qint64 __ss_align;
       
    79       char __ss_pad2[QT_SS_PAD2SIZE];
       
    80 };
       
    81 
       
    82 // sockaddr_in6 size changed between old and new SDK
       
    83 // Only the new version is the correct one, so always
       
    84 // use this structure.
       
    85 struct qt_in6_addr {
       
    86     quint8 qt_s6_addr[16];
       
    87 };
       
    88 struct qt_sockaddr_in6 {
       
    89     short   sin6_family;            /* AF_INET6 */
       
    90     quint16 sin6_port;              /* Transport level port number */
       
    91     quint32 sin6_flowinfo;          /* IPv6 flow information */
       
    92     struct  qt_in6_addr sin6_addr;  /* IPv6 address */
       
    93     quint32 sin6_scope_id;          /* set of interfaces for a scope */
       
    94 };
       
    95 
       
    96 union qt_sockaddr {
       
    97     sockaddr a;
       
    98     sockaddr_in a4;
       
    99     qt_sockaddr_in6 a6;
       
   100     qt_sockaddr_storage storage;
       
   101 };
       
   102 
       
   103 class QNativeSocketEnginePrivate;
       
   104 
       
   105 class Q_AUTOTEST_EXPORT QNativeSocketEngine : public QAbstractSocketEngine
       
   106 {
       
   107     Q_OBJECT
       
   108 public:
       
   109     QNativeSocketEngine(QObject *parent = 0);
       
   110     ~QNativeSocketEngine();
       
   111 
       
   112     bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol);
       
   113     bool initialize(int socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState);
       
   114 
       
   115     int socketDescriptor() const;
       
   116 
       
   117     bool isValid() const;
       
   118 
       
   119     bool connectToHost(const QHostAddress &address, quint16 port);
       
   120     bool connectToHostByName(const QString &name, quint16 port);
       
   121     bool bind(const QHostAddress &address, quint16 port);
       
   122     bool listen();
       
   123     int accept();
       
   124     void close();
       
   125 
       
   126     qint64 bytesAvailable() const;
       
   127 
       
   128     qint64 read(char *data, qint64 maxlen);
       
   129     qint64 write(const char *data, qint64 len);
       
   130 
       
   131     qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *addr = 0,
       
   132                             quint16 *port = 0);
       
   133     qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &addr,
       
   134                              quint16 port);
       
   135     bool hasPendingDatagrams() const;
       
   136     qint64 pendingDatagramSize() const;
       
   137 
       
   138     qint64 receiveBufferSize() const;
       
   139     void setReceiveBufferSize(qint64 bufferSize);
       
   140 
       
   141     qint64 sendBufferSize() const;
       
   142     void setSendBufferSize(qint64 bufferSize);
       
   143 
       
   144     int option(SocketOption option) const;
       
   145     bool setOption(SocketOption option, int value);
       
   146 
       
   147     bool waitForRead(int msecs = 30000, bool *timedOut = 0);
       
   148     bool waitForWrite(int msecs = 30000, bool *timedOut = 0);
       
   149     bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
       
   150 			    bool checkRead, bool checkWrite,
       
   151 			    int msecs = 30000, bool *timedOut = 0);
       
   152 
       
   153     bool isReadNotificationEnabled() const;
       
   154     void setReadNotificationEnabled(bool enable);
       
   155     bool isWriteNotificationEnabled() const;
       
   156     void setWriteNotificationEnabled(bool enable);
       
   157     bool isExceptionNotificationEnabled() const;
       
   158     void setExceptionNotificationEnabled(bool enable);
       
   159 
       
   160 public Q_SLOTS:
       
   161     // non-virtual override;
       
   162     void connectionNotification();
       
   163 
       
   164 private:
       
   165     Q_DECLARE_PRIVATE(QNativeSocketEngine)
       
   166     Q_DISABLE_COPY(QNativeSocketEngine)
       
   167 };
       
   168 
       
   169 #ifdef Q_OS_WIN
       
   170 class QWindowsSockInit
       
   171 {
       
   172 public:
       
   173     QWindowsSockInit();
       
   174     ~QWindowsSockInit();
       
   175     int version;
       
   176 };
       
   177 #endif
       
   178 
       
   179 class QSocketNotifier;
       
   180 
       
   181 class QNativeSocketEnginePrivate : public QAbstractSocketEnginePrivate
       
   182 {
       
   183     Q_DECLARE_PUBLIC(QNativeSocketEngine)
       
   184 public:
       
   185     QNativeSocketEnginePrivate();
       
   186     ~QNativeSocketEnginePrivate();
       
   187 
       
   188     int socketDescriptor;
       
   189 
       
   190     QSocketNotifier *readNotifier, *writeNotifier, *exceptNotifier;
       
   191 
       
   192 #ifdef Q_OS_WIN
       
   193     QWindowsSockInit winSock;
       
   194 #endif
       
   195 
       
   196     enum ErrorString {
       
   197         NonBlockingInitFailedErrorString,
       
   198         BroadcastingInitFailedErrorString,
       
   199         NoIpV6ErrorString,
       
   200         RemoteHostClosedErrorString,
       
   201         TimeOutErrorString,
       
   202         ResourceErrorString,
       
   203         OperationUnsupportedErrorString,
       
   204         ProtocolUnsupportedErrorString,
       
   205         InvalidSocketErrorString,
       
   206         HostUnreachableErrorString,
       
   207         NetworkUnreachableErrorString,
       
   208         AccessErrorString,
       
   209         ConnectionTimeOutErrorString,
       
   210         ConnectionRefusedErrorString,
       
   211         AddressInuseErrorString,
       
   212         AddressNotAvailableErrorString,
       
   213         AddressProtectedErrorString,
       
   214         DatagramTooLargeErrorString,
       
   215         SendDatagramErrorString,
       
   216         ReceiveDatagramErrorString,
       
   217         WriteErrorString,
       
   218         ReadErrorString,
       
   219         PortInuseErrorString,
       
   220         NotSocketErrorString,
       
   221         InvalidProxyTypeString,
       
   222 
       
   223         UnknownSocketErrorString = -1
       
   224     };
       
   225 
       
   226     void setError(QAbstractSocket::SocketError error, ErrorString errorString) const;
       
   227 
       
   228     // native functions
       
   229     int option(QNativeSocketEngine::SocketOption option) const;
       
   230     bool setOption(QNativeSocketEngine::SocketOption option, int value);
       
   231 
       
   232     bool createNewSocket(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol);
       
   233 
       
   234     bool nativeConnect(const QHostAddress &address, quint16 port);
       
   235     bool nativeBind(const QHostAddress &address, quint16 port);
       
   236     bool nativeListen(int backlog);
       
   237     int nativeAccept();
       
   238     qint64 nativeBytesAvailable() const;
       
   239 
       
   240     bool nativeHasPendingDatagrams() const;
       
   241     qint64 nativePendingDatagramSize() const;
       
   242     qint64 nativeReceiveDatagram(char *data, qint64 maxLength,
       
   243                                      QHostAddress *address, quint16 *port);
       
   244     qint64 nativeSendDatagram(const char *data, qint64 length,
       
   245                                   const QHostAddress &host, quint16 port);
       
   246     qint64 nativeRead(char *data, qint64 maxLength);
       
   247     qint64 nativeWrite(const char *data, qint64 length);
       
   248     int nativeSelect(int timeout, bool selectForRead) const;
       
   249     int nativeSelect(int timeout, bool checkRead, bool checkWrite,
       
   250 		     bool *selectForRead, bool *selectForWrite) const;
       
   251 
       
   252     void nativeClose();
       
   253 
       
   254     bool checkProxy(const QHostAddress &address);
       
   255     bool fetchConnectionParameters();
       
   256 };
       
   257 
       
   258 QT_END_NAMESPACE
       
   259 
       
   260 #endif // QNATIVESOCKETENGINE_P_H