1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // Copyright (C) 2016 Intel Corporation. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #ifndef QABSTRACTSOCKETENGINE_P_H |
6 | #define QABSTRACTSOCKETENGINE_P_H |
7 | |
8 | // |
9 | // W A R N I N G |
10 | // ------------- |
11 | // |
12 | // This file is not part of the Qt API. It exists purely as an |
13 | // implementation detail. This header file may change from version to |
14 | // version without notice, or even be removed. |
15 | // |
16 | // We mean it. |
17 | // |
18 | |
19 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
20 | #include "QtNetwork/qhostaddress.h" |
21 | #include "QtNetwork/qabstractsocket.h" |
22 | #include "private/qobject_p.h" |
23 | #include "private/qnetworkdatagram_p.h" |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | class QAuthenticator; |
28 | class QAbstractSocketEnginePrivate; |
29 | #ifndef QT_NO_NETWORKINTERFACE |
30 | class QNetworkInterface; |
31 | #endif |
32 | class QNetworkProxy; |
33 | |
34 | class QAbstractSocketEngineReceiver { |
35 | public: |
36 | virtual ~QAbstractSocketEngineReceiver(){} |
37 | virtual void readNotification()= 0; |
38 | virtual void writeNotification()= 0; |
39 | virtual void closeNotification()= 0; |
40 | virtual void exceptionNotification()= 0; |
41 | virtual void connectionNotification()= 0; |
42 | #ifndef QT_NO_NETWORKPROXY |
43 | virtual void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)= 0; |
44 | #endif |
45 | }; |
46 | |
47 | class Q_AUTOTEST_EXPORT QAbstractSocketEngine : public QObject |
48 | { |
49 | Q_OBJECT |
50 | Q_MOC_INCLUDE(<QtNetwork/qauthenticator.h>) |
51 | public: |
52 | |
53 | static QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent); |
54 | static QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent); |
55 | |
56 | QAbstractSocketEngine(QObject *parent = nullptr); |
57 | |
58 | enum SocketOption { |
59 | NonBlockingSocketOption, |
60 | BroadcastSocketOption, |
61 | ReceiveBufferSocketOption, |
62 | SendBufferSocketOption, |
63 | AddressReusable, |
64 | BindExclusively, |
65 | ReceiveOutOfBandData, |
66 | LowDelayOption, |
67 | KeepAliveOption, |
68 | MulticastTtlOption, |
69 | MulticastLoopbackOption, |
70 | TypeOfServiceOption, |
71 | ReceivePacketInformation, |
72 | ReceiveHopLimit, |
73 | MaxStreamsSocketOption, |
74 | PathMtuInformation |
75 | }; |
76 | |
77 | enum { |
78 | WantNone = 0, |
79 | WantDatagramSender = 0x01, |
80 | WantDatagramDestination = 0x02, |
81 | WantDatagramHopLimit = 0x04, |
82 | WantStreamNumber = 0x08, |
83 | WantEndOfRecord = 0x10, |
84 | |
85 | WantAll = 0xff |
86 | }; |
87 | Q_DECLARE_FLAGS(, PacketHeaderOption) |
88 | |
89 | virtual bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) = 0; |
90 | |
91 | virtual bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) = 0; |
92 | |
93 | virtual qintptr socketDescriptor() const = 0; |
94 | |
95 | virtual bool isValid() const = 0; |
96 | |
97 | virtual bool connectToHost(const QHostAddress &address, quint16 port) = 0; |
98 | virtual bool connectToHostByName(const QString &name, quint16 port) = 0; |
99 | virtual bool bind(const QHostAddress &address, quint16 port) = 0; |
100 | virtual bool listen(int backlog) = 0; |
101 | virtual qintptr accept() = 0; |
102 | virtual void close() = 0; |
103 | |
104 | virtual qint64 bytesAvailable() const = 0; |
105 | |
106 | virtual qint64 read(char *data, qint64 maxlen) = 0; |
107 | virtual qint64 write(const char *data, qint64 len) = 0; |
108 | |
109 | #ifndef QT_NO_UDPSOCKET |
110 | #ifndef QT_NO_NETWORKINTERFACE |
111 | virtual bool joinMulticastGroup(const QHostAddress &groupAddress, |
112 | const QNetworkInterface &iface) = 0; |
113 | virtual bool leaveMulticastGroup(const QHostAddress &groupAddress, |
114 | const QNetworkInterface &iface) = 0; |
115 | virtual QNetworkInterface multicastInterface() const = 0; |
116 | virtual bool setMulticastInterface(const QNetworkInterface &iface) = 0; |
117 | #endif // QT_NO_NETWORKINTERFACE |
118 | |
119 | virtual bool hasPendingDatagrams() const = 0; |
120 | virtual qint64 pendingDatagramSize() const = 0; |
121 | #endif // QT_NO_UDPSOCKET |
122 | |
123 | virtual qint64 (char *data, qint64 maxlen, QIpPacketHeader * = nullptr, |
124 | PacketHeaderOptions = WantNone) = 0; |
125 | virtual qint64 (const char *data, qint64 len, const QIpPacketHeader &) = 0; |
126 | virtual qint64 bytesToWrite() const = 0; |
127 | |
128 | virtual int option(SocketOption option) const = 0; |
129 | virtual bool setOption(SocketOption option, int value) = 0; |
130 | |
131 | virtual bool waitForRead(int msecs = 30000, bool *timedOut = nullptr) = 0; |
132 | virtual bool waitForWrite(int msecs = 30000, bool *timedOut = nullptr) = 0; |
133 | virtual bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, |
134 | bool checkRead, bool checkWrite, |
135 | int msecs = 30000, bool *timedOut = nullptr) = 0; |
136 | |
137 | QAbstractSocket::SocketError error() const; |
138 | QString errorString() const; |
139 | QAbstractSocket::SocketState state() const; |
140 | QAbstractSocket::SocketType socketType() const; |
141 | QAbstractSocket::NetworkLayerProtocol protocol() const; |
142 | |
143 | QHostAddress localAddress() const; |
144 | quint16 localPort() const; |
145 | QHostAddress peerAddress() const; |
146 | quint16 peerPort() const; |
147 | int inboundStreamCount() const; |
148 | int outboundStreamCount() const; |
149 | |
150 | virtual bool isReadNotificationEnabled() const = 0; |
151 | virtual void setReadNotificationEnabled(bool enable) = 0; |
152 | virtual bool isWriteNotificationEnabled() const = 0; |
153 | virtual void setWriteNotificationEnabled(bool enable) = 0; |
154 | virtual bool isExceptionNotificationEnabled() const = 0; |
155 | virtual void setExceptionNotificationEnabled(bool enable) = 0; |
156 | |
157 | public Q_SLOTS: |
158 | void readNotification(); |
159 | void writeNotification(); |
160 | void closeNotification(); |
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 = nullptr); |
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_MOVE(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 | int inboundStreamCount; |
203 | int outboundStreamCount; |
204 | QAbstractSocketEngineReceiver *receiver; |
205 | }; |
206 | |
207 | |
208 | class Q_AUTOTEST_EXPORT QSocketEngineHandler |
209 | { |
210 | protected: |
211 | QSocketEngineHandler(); |
212 | virtual ~QSocketEngineHandler(); |
213 | virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, |
214 | const QNetworkProxy &, QObject *parent) = 0; |
215 | virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent) = 0; |
216 | |
217 | private: |
218 | friend class QAbstractSocketEngine; |
219 | }; |
220 | |
221 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocketEngine::PacketHeaderOptions) |
222 | |
223 | QT_END_NAMESPACE |
224 | |
225 | #endif // QABSTRACTSOCKETENGINE_P_H |
226 | |