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 QtNetwork 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 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 |
|
---|
56 | #include "QtNetwork/qhostaddress.h"
|
---|
57 | #include "private/qabstractsocketengine_p.h"
|
---|
58 | #ifndef Q_OS_WIN
|
---|
59 | #include "qplatformdefs.h"
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | QT_BEGIN_NAMESPACE
|
---|
63 |
|
---|
64 | #ifndef Q_OS_WIN
|
---|
65 | // Almost always the same. If not, specify in qplatformdefs.h.
|
---|
66 | #if !defined(QT_SOCKOPTLEN_T)
|
---|
67 | # define QT_SOCKOPTLEN_T QT_SOCKLEN_T
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | // Tru64 redefines accept -> _accept with _XOPEN_SOURCE_EXTENDED
|
---|
71 | static inline int qt_socket_accept(int s, struct sockaddr *addr, QT_SOCKLEN_T *addrlen)
|
---|
72 | { return ::accept(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen)); }
|
---|
73 | #if defined(accept)
|
---|
74 | # undef accept
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | // UnixWare 7 redefines listen -> _listen
|
---|
78 | static inline int qt_socket_listen(int s, int backlog)
|
---|
79 | { return ::listen(s, backlog); }
|
---|
80 | #if defined(listen)
|
---|
81 | # undef listen
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | // UnixWare 7 redefines socket -> _socket
|
---|
85 | static inline int qt_socket_socket(int domain, int type, int protocol)
|
---|
86 | { return ::socket(domain, type, protocol); }
|
---|
87 | #if defined(socket)
|
---|
88 | # undef socket
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | class QNativeSocketEnginePrivate;
|
---|
94 |
|
---|
95 | class Q_AUTOTEST_EXPORT QNativeSocketEngine : public QAbstractSocketEngine
|
---|
96 | {
|
---|
97 | Q_OBJECT
|
---|
98 | public:
|
---|
99 | QNativeSocketEngine(QObject *parent = 0);
|
---|
100 | ~QNativeSocketEngine();
|
---|
101 |
|
---|
102 | bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol);
|
---|
103 | bool initialize(int socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState);
|
---|
104 |
|
---|
105 | int socketDescriptor() const;
|
---|
106 |
|
---|
107 | bool isValid() const;
|
---|
108 |
|
---|
109 | bool connectToHost(const QHostAddress &address, quint16 port);
|
---|
110 | bool connectToHostByName(const QString &name, quint16 port);
|
---|
111 | bool bind(const QHostAddress &address, quint16 port);
|
---|
112 | bool listen();
|
---|
113 | int accept();
|
---|
114 | void close();
|
---|
115 |
|
---|
116 | qint64 bytesAvailable() const;
|
---|
117 |
|
---|
118 | qint64 read(char *data, qint64 maxlen);
|
---|
119 | qint64 write(const char *data, qint64 len);
|
---|
120 |
|
---|
121 | qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *addr = 0,
|
---|
122 | quint16 *port = 0);
|
---|
123 | qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &addr,
|
---|
124 | quint16 port);
|
---|
125 | bool hasPendingDatagrams() const;
|
---|
126 | qint64 pendingDatagramSize() const;
|
---|
127 |
|
---|
128 | qint64 receiveBufferSize() const;
|
---|
129 | void setReceiveBufferSize(qint64 bufferSize);
|
---|
130 |
|
---|
131 | qint64 sendBufferSize() const;
|
---|
132 | void setSendBufferSize(qint64 bufferSize);
|
---|
133 |
|
---|
134 | int option(SocketOption option) const;
|
---|
135 | bool setOption(SocketOption option, int value);
|
---|
136 |
|
---|
137 | bool waitForRead(int msecs = 30000, bool *timedOut = 0);
|
---|
138 | bool waitForWrite(int msecs = 30000, bool *timedOut = 0);
|
---|
139 | bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
|
---|
140 | bool checkRead, bool checkWrite,
|
---|
141 | int msecs = 30000, bool *timedOut = 0);
|
---|
142 |
|
---|
143 | bool isReadNotificationEnabled() const;
|
---|
144 | void setReadNotificationEnabled(bool enable);
|
---|
145 | bool isWriteNotificationEnabled() const;
|
---|
146 | void setWriteNotificationEnabled(bool enable);
|
---|
147 | bool isExceptionNotificationEnabled() const;
|
---|
148 | void setExceptionNotificationEnabled(bool enable);
|
---|
149 |
|
---|
150 | public Q_SLOTS:
|
---|
151 | // non-virtual override;
|
---|
152 | void connectionNotification();
|
---|
153 |
|
---|
154 | private:
|
---|
155 | Q_DECLARE_PRIVATE(QNativeSocketEngine)
|
---|
156 | Q_DISABLE_COPY(QNativeSocketEngine)
|
---|
157 | };
|
---|
158 |
|
---|
159 | #ifdef Q_OS_WIN
|
---|
160 | class QWindowsSockInit
|
---|
161 | {
|
---|
162 | public:
|
---|
163 | QWindowsSockInit();
|
---|
164 | ~QWindowsSockInit();
|
---|
165 | int version;
|
---|
166 | };
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | class QSocketNotifier;
|
---|
170 |
|
---|
171 | class QNativeSocketEnginePrivate : public QAbstractSocketEnginePrivate
|
---|
172 | {
|
---|
173 | Q_DECLARE_PUBLIC(QNativeSocketEngine)
|
---|
174 | public:
|
---|
175 | QNativeSocketEnginePrivate();
|
---|
176 | ~QNativeSocketEnginePrivate();
|
---|
177 |
|
---|
178 | int socketDescriptor;
|
---|
179 |
|
---|
180 | QSocketNotifier *readNotifier, *writeNotifier, *exceptNotifier;
|
---|
181 |
|
---|
182 | #ifdef Q_OS_WIN
|
---|
183 | QWindowsSockInit winSock;
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | enum ErrorString {
|
---|
187 | NonBlockingInitFailedErrorString,
|
---|
188 | BroadcastingInitFailedErrorString,
|
---|
189 | NoIpV6ErrorString,
|
---|
190 | RemoteHostClosedErrorString,
|
---|
191 | TimeOutErrorString,
|
---|
192 | ResourceErrorString,
|
---|
193 | OperationUnsupportedErrorString,
|
---|
194 | ProtocolUnsupportedErrorString,
|
---|
195 | InvalidSocketErrorString,
|
---|
196 | HostUnreachableErrorString,
|
---|
197 | NetworkUnreachableErrorString,
|
---|
198 | AccessErrorString,
|
---|
199 | ConnectionTimeOutErrorString,
|
---|
200 | ConnectionRefusedErrorString,
|
---|
201 | AddressInuseErrorString,
|
---|
202 | AddressNotAvailableErrorString,
|
---|
203 | AddressProtectedErrorString,
|
---|
204 | DatagramTooLargeErrorString,
|
---|
205 | SendDatagramErrorString,
|
---|
206 | ReceiveDatagramErrorString,
|
---|
207 | WriteErrorString,
|
---|
208 | ReadErrorString,
|
---|
209 | PortInuseErrorString,
|
---|
210 | NotSocketErrorString,
|
---|
211 | InvalidProxyTypeString,
|
---|
212 |
|
---|
213 | UnknownSocketErrorString = -1
|
---|
214 | };
|
---|
215 |
|
---|
216 | void setError(QAbstractSocket::SocketError error, ErrorString errorString) const;
|
---|
217 |
|
---|
218 | // native functions
|
---|
219 | int option(QNativeSocketEngine::SocketOption option) const;
|
---|
220 | bool setOption(QNativeSocketEngine::SocketOption option, int value);
|
---|
221 |
|
---|
222 | bool createNewSocket(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol);
|
---|
223 |
|
---|
224 | bool nativeConnect(const QHostAddress &address, quint16 port);
|
---|
225 | bool nativeBind(const QHostAddress &address, quint16 port);
|
---|
226 | bool nativeListen(int backlog);
|
---|
227 | int nativeAccept();
|
---|
228 | qint64 nativeBytesAvailable() const;
|
---|
229 |
|
---|
230 | bool nativeHasPendingDatagrams() const;
|
---|
231 | qint64 nativePendingDatagramSize() const;
|
---|
232 | qint64 nativeReceiveDatagram(char *data, qint64 maxLength,
|
---|
233 | QHostAddress *address, quint16 *port);
|
---|
234 | qint64 nativeSendDatagram(const char *data, qint64 length,
|
---|
235 | const QHostAddress &host, quint16 port);
|
---|
236 | qint64 nativeRead(char *data, qint64 maxLength);
|
---|
237 | qint64 nativeWrite(const char *data, qint64 length);
|
---|
238 | int nativeSelect(int timeout, bool selectForRead) const;
|
---|
239 | int nativeSelect(int timeout, bool checkRead, bool checkWrite,
|
---|
240 | bool *selectForRead, bool *selectForWrite) const;
|
---|
241 |
|
---|
242 | void nativeClose();
|
---|
243 |
|
---|
244 | bool checkProxy(const QHostAddress &address);
|
---|
245 | bool fetchConnectionParameters();
|
---|
246 | };
|
---|
247 |
|
---|
248 | QT_END_NAMESPACE
|
---|
249 |
|
---|
250 | #endif // QNATIVESOCKETENGINE_P_H
|
---|