| 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 | #ifndef Q3SOCKETDEVICE_H
|
|---|
| 43 | #define Q3SOCKETDEVICE_H
|
|---|
| 44 |
|
|---|
| 45 | #include <QtCore/qiodevice.h>
|
|---|
| 46 | #include <QtNetwork/qhostaddress.h> // int->QHostAddress conversion
|
|---|
| 47 |
|
|---|
| 48 | QT_BEGIN_HEADER
|
|---|
| 49 |
|
|---|
| 50 | QT_BEGIN_NAMESPACE
|
|---|
| 51 |
|
|---|
| 52 | QT_MODULE(Qt3Support)
|
|---|
| 53 |
|
|---|
| 54 | #ifndef QT_NO_NETWORK
|
|---|
| 55 |
|
|---|
| 56 | class Q3SocketDevicePrivate;
|
|---|
| 57 |
|
|---|
| 58 | class Q_COMPAT_EXPORT Q3SocketDevice: public QIODevice
|
|---|
| 59 | {
|
|---|
| 60 | public:
|
|---|
| 61 | enum Type { Stream, Datagram };
|
|---|
| 62 | enum Protocol { IPv4, IPv6, Unknown };
|
|---|
| 63 |
|
|---|
| 64 | Q3SocketDevice( Type type = Stream );
|
|---|
| 65 | Q3SocketDevice( Type type, Protocol protocol, int dummy );
|
|---|
| 66 | Q3SocketDevice( int socket, Type type );
|
|---|
| 67 | virtual ~Q3SocketDevice();
|
|---|
| 68 |
|
|---|
| 69 | bool isValid() const;
|
|---|
| 70 | Type type() const;
|
|---|
| 71 | Protocol protocol() const;
|
|---|
| 72 |
|
|---|
| 73 | int socket() const;
|
|---|
| 74 | virtual void setSocket( int socket, Type type );
|
|---|
| 75 |
|
|---|
| 76 | bool open( OpenMode mode );
|
|---|
| 77 | bool open( int mode ) { return open((OpenMode)mode); }
|
|---|
| 78 | void close();
|
|---|
| 79 | bool flush();
|
|---|
| 80 |
|
|---|
| 81 | // Implementation of QIODevice abstract virtual functions
|
|---|
| 82 | Offset size() const;
|
|---|
| 83 | Offset at() const;
|
|---|
| 84 | bool at( Offset );
|
|---|
| 85 | bool atEnd() const;
|
|---|
| 86 |
|
|---|
| 87 | bool blocking() const;
|
|---|
| 88 | virtual void setBlocking( bool );
|
|---|
| 89 |
|
|---|
| 90 | bool addressReusable() const;
|
|---|
| 91 | virtual void setAddressReusable( bool );
|
|---|
| 92 |
|
|---|
| 93 | int receiveBufferSize() const;
|
|---|
| 94 | virtual void setReceiveBufferSize( uint );
|
|---|
| 95 | int sendBufferSize() const;
|
|---|
| 96 | virtual void setSendBufferSize( uint );
|
|---|
| 97 |
|
|---|
| 98 | virtual bool connect( const QHostAddress &, Q_UINT16 );
|
|---|
| 99 |
|
|---|
| 100 | virtual bool bind( const QHostAddress &, Q_UINT16 );
|
|---|
| 101 | virtual bool listen( int backlog );
|
|---|
| 102 | virtual int accept();
|
|---|
| 103 |
|
|---|
| 104 | qint64 bytesAvailable() const;
|
|---|
| 105 | Q_LONG waitForMore( int msecs, bool *timeout=0 ) const;
|
|---|
| 106 | virtual Q_LONG writeBlock( const char *data, Q_ULONG len,
|
|---|
| 107 | const QHostAddress & host, Q_UINT16 port );
|
|---|
| 108 | inline Q_LONG writeBlock(const char *data, Q_ULONG len)
|
|---|
| 109 | { return qint64(write(data, qint64(len))); }
|
|---|
| 110 | inline qint64 readBlock(char *data, Q_ULONG maxlen)
|
|---|
| 111 | { return qint64(read(data, qint64(maxlen))); }
|
|---|
| 112 |
|
|---|
| 113 | Q_UINT16 port() const;
|
|---|
| 114 | Q_UINT16 peerPort() const;
|
|---|
| 115 | QHostAddress address() const;
|
|---|
| 116 | QHostAddress peerAddress() const;
|
|---|
| 117 |
|
|---|
| 118 | enum Error {
|
|---|
| 119 | NoError,
|
|---|
| 120 | AlreadyBound,
|
|---|
| 121 | Inaccessible,
|
|---|
| 122 | NoResources,
|
|---|
| 123 | InternalError,
|
|---|
| 124 | Bug = InternalError, // ### remove in 4.0?
|
|---|
| 125 | Impossible,
|
|---|
| 126 | NoFiles,
|
|---|
| 127 | ConnectionRefused,
|
|---|
| 128 | NetworkFailure,
|
|---|
| 129 | UnknownError
|
|---|
| 130 | };
|
|---|
| 131 | Error error() const;
|
|---|
| 132 |
|
|---|
| 133 | inline bool isSequential() const { return true; }
|
|---|
| 134 |
|
|---|
| 135 | protected:
|
|---|
| 136 | void setError( Error err );
|
|---|
| 137 | qint64 readData(char *data, qint64 maxlen);
|
|---|
| 138 | qint64 writeData(const char *data, qint64 len);
|
|---|
| 139 |
|
|---|
| 140 | private:
|
|---|
| 141 | int fd;
|
|---|
| 142 | Type t;
|
|---|
| 143 | Q_UINT16 p;
|
|---|
| 144 | QHostAddress a;
|
|---|
| 145 | Q_UINT16 pp;
|
|---|
| 146 | QHostAddress pa;
|
|---|
| 147 | Q3SocketDevice::Error e;
|
|---|
| 148 | Q3SocketDevicePrivate * d;
|
|---|
| 149 |
|
|---|
| 150 | enum Option { Broadcast, ReceiveBuffer, ReuseAddress, SendBuffer };
|
|---|
| 151 |
|
|---|
| 152 | int option( Option ) const;
|
|---|
| 153 | virtual void setOption( Option, int );
|
|---|
| 154 |
|
|---|
| 155 | void fetchConnectionParameters();
|
|---|
| 156 | #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
|
|---|
| 157 | void fetchPeerConnectionParameters();
|
|---|
| 158 | #endif
|
|---|
| 159 |
|
|---|
| 160 | static void init();
|
|---|
| 161 | int createNewSocket();
|
|---|
| 162 | Protocol getProtocol() const;
|
|---|
| 163 |
|
|---|
| 164 | private: // Disabled copy constructor and operator=
|
|---|
| 165 | #if defined(Q_DISABLE_COPY)
|
|---|
| 166 | Q3SocketDevice( const Q3SocketDevice & );
|
|---|
| 167 | Q3SocketDevice &operator=( const Q3SocketDevice & );
|
|---|
| 168 | #endif
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | #endif // QT_NO_NETWORK
|
|---|
| 172 |
|
|---|
| 173 | QT_END_NAMESPACE
|
|---|
| 174 |
|
|---|
| 175 | QT_END_HEADER
|
|---|
| 176 |
|
|---|
| 177 | #endif // Q3SOCKETDEVICE_H
|
|---|