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 <QtCore/qdeadlinetimer.h> |
23 | #include "private/qnetworkdatagram_p.h" |
24 | #include "private/qobject_p.h" |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | class QAuthenticator; |
29 | class QAbstractSocketEnginePrivate; |
30 | #ifndef QT_NO_NETWORKINTERFACE |
31 | class QNetworkInterface; |
32 | #endif |
33 | class QNetworkProxy; |
34 | |
35 | class QAbstractSocketEngineReceiver { |
36 | public: |
37 | virtual ~QAbstractSocketEngineReceiver(){} |
38 | virtual void readNotification()= 0; |
39 | virtual void writeNotification()= 0; |
40 | virtual void closeNotification()= 0; |
41 | virtual void exceptionNotification()= 0; |
42 | virtual void connectionNotification()= 0; |
43 | #ifndef QT_NO_NETWORKPROXY |
44 | virtual void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)= 0; |
45 | #endif |
46 | }; |
47 | |
48 | static constexpr std::chrono::seconds DefaultTimeout{30}; |
49 | |
50 | class Q_AUTOTEST_EXPORT QAbstractSocketEngine : public QObject |
51 | { |
52 | Q_OBJECT |
53 | Q_MOC_INCLUDE(<QtNetwork/qauthenticator.h>) |
54 | public: |
55 | |
56 | static QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent); |
57 | static QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent); |
58 | |
59 | QAbstractSocketEngine(QObject *parent = nullptr); |
60 | |
61 | enum SocketOption { |
62 | NonBlockingSocketOption, |
63 | BroadcastSocketOption, |
64 | ReceiveBufferSocketOption, |
65 | SendBufferSocketOption, |
66 | AddressReusable, |
67 | BindExclusively, |
68 | ReceiveOutOfBandData, |
69 | LowDelayOption, |
70 | KeepAliveOption, |
71 | MulticastTtlOption, |
72 | MulticastLoopbackOption, |
73 | TypeOfServiceOption, |
74 | ReceivePacketInformation, |
75 | ReceiveHopLimit, |
76 | MaxStreamsSocketOption, |
77 | PathMtuInformation |
78 | }; |
79 | |
80 | enum { |
81 | WantNone = 0, |
82 | WantDatagramSender = 0x01, |
83 | WantDatagramDestination = 0x02, |
84 | WantDatagramHopLimit = 0x04, |
85 | WantStreamNumber = 0x08, |
86 | WantEndOfRecord = 0x10, |
87 | |
88 | WantAll = 0xff |
89 | }; |
90 | Q_DECLARE_FLAGS(, PacketHeaderOption) |
91 | |
92 | virtual bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) = 0; |
93 | |
94 | virtual bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) = 0; |
95 | |
96 | virtual qintptr socketDescriptor() const = 0; |
97 | |
98 | virtual bool isValid() const = 0; |
99 | |
100 | virtual bool connectToHost(const QHostAddress &address, quint16 port) = 0; |
101 | virtual bool connectToHostByName(const QString &name, quint16 port) = 0; |
102 | virtual bool bind(const QHostAddress &address, quint16 port) = 0; |
103 | virtual bool listen(int backlog) = 0; |
104 | virtual qintptr accept() = 0; |
105 | virtual void close() = 0; |
106 | |
107 | virtual qint64 bytesAvailable() const = 0; |
108 | |
109 | virtual qint64 read(char *data, qint64 maxlen) = 0; |
110 | virtual qint64 write(const char *data, qint64 len) = 0; |
111 | |
112 | #ifndef QT_NO_UDPSOCKET |
113 | #ifndef QT_NO_NETWORKINTERFACE |
114 | virtual bool joinMulticastGroup(const QHostAddress &groupAddress, |
115 | const QNetworkInterface &iface) = 0; |
116 | virtual bool leaveMulticastGroup(const QHostAddress &groupAddress, |
117 | const QNetworkInterface &iface) = 0; |
118 | virtual QNetworkInterface multicastInterface() const = 0; |
119 | virtual bool setMulticastInterface(const QNetworkInterface &iface) = 0; |
120 | #endif // QT_NO_NETWORKINTERFACE |
121 | |
122 | virtual bool hasPendingDatagrams() const = 0; |
123 | virtual qint64 pendingDatagramSize() const = 0; |
124 | #endif // QT_NO_UDPSOCKET |
125 | |
126 | virtual qint64 (char *data, qint64 maxlen, QIpPacketHeader * = nullptr, |
127 | PacketHeaderOptions = WantNone) = 0; |
128 | virtual qint64 (const char *data, qint64 len, const QIpPacketHeader &) = 0; |
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(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
135 | bool *timedOut = nullptr) = 0; |
136 | virtual bool waitForWrite(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
137 | bool *timedOut = nullptr) = 0; |
138 | virtual bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, |
139 | bool checkRead, bool checkWrite, |
140 | QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
141 | bool *timedOut = nullptr) = 0; |
142 | |
143 | QAbstractSocket::SocketError error() const; |
144 | QString errorString() const; |
145 | QAbstractSocket::SocketState state() const; |
146 | QAbstractSocket::SocketType socketType() const; |
147 | QAbstractSocket::NetworkLayerProtocol protocol() const; |
148 | |
149 | QHostAddress localAddress() const; |
150 | quint16 localPort() const; |
151 | QHostAddress peerAddress() const; |
152 | quint16 peerPort() const; |
153 | int inboundStreamCount() const; |
154 | int outboundStreamCount() const; |
155 | |
156 | virtual bool isReadNotificationEnabled() const = 0; |
157 | virtual void setReadNotificationEnabled(bool enable) = 0; |
158 | virtual bool isWriteNotificationEnabled() const = 0; |
159 | virtual void setWriteNotificationEnabled(bool enable) = 0; |
160 | virtual bool isExceptionNotificationEnabled() const = 0; |
161 | virtual void setExceptionNotificationEnabled(bool enable) = 0; |
162 | |
163 | public Q_SLOTS: |
164 | void readNotification(); |
165 | void writeNotification(); |
166 | void closeNotification(); |
167 | void exceptionNotification(); |
168 | void connectionNotification(); |
169 | #ifndef QT_NO_NETWORKPROXY |
170 | void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator); |
171 | #endif |
172 | |
173 | public: |
174 | void setReceiver(QAbstractSocketEngineReceiver *receiver); |
175 | protected: |
176 | QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent = nullptr); |
177 | |
178 | void setError(QAbstractSocket::SocketError error, const QString &errorString) const; |
179 | void setState(QAbstractSocket::SocketState state); |
180 | void setSocketType(QAbstractSocket::SocketType socketType); |
181 | void setProtocol(QAbstractSocket::NetworkLayerProtocol protocol); |
182 | void setLocalAddress(const QHostAddress &address); |
183 | void setLocalPort(quint16 port); |
184 | void setPeerAddress(const QHostAddress &address); |
185 | void setPeerPort(quint16 port); |
186 | |
187 | private: |
188 | Q_DECLARE_PRIVATE(QAbstractSocketEngine) |
189 | Q_DISABLE_COPY_MOVE(QAbstractSocketEngine) |
190 | }; |
191 | |
192 | class QAbstractSocketEnginePrivate : public QObjectPrivate |
193 | { |
194 | Q_DECLARE_PUBLIC(QAbstractSocketEngine) |
195 | public: |
196 | QAbstractSocketEnginePrivate(); |
197 | |
198 | mutable QAbstractSocket::SocketError socketError; |
199 | mutable bool hasSetSocketError; |
200 | mutable QString socketErrorString; |
201 | QAbstractSocket::SocketState socketState; |
202 | QAbstractSocket::SocketType socketType; |
203 | QAbstractSocket::NetworkLayerProtocol socketProtocol; |
204 | QHostAddress localAddress; |
205 | quint16 localPort; |
206 | QHostAddress peerAddress; |
207 | quint16 peerPort; |
208 | int inboundStreamCount; |
209 | int outboundStreamCount; |
210 | QAbstractSocketEngineReceiver *receiver; |
211 | }; |
212 | |
213 | |
214 | class Q_AUTOTEST_EXPORT QSocketEngineHandler |
215 | { |
216 | protected: |
217 | QSocketEngineHandler(); |
218 | virtual ~QSocketEngineHandler(); |
219 | virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, |
220 | const QNetworkProxy &, QObject *parent) = 0; |
221 | virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent) = 0; |
222 | |
223 | private: |
224 | friend class QAbstractSocketEngine; |
225 | }; |
226 | |
227 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocketEngine::PacketHeaderOptions) |
228 | |
229 | QT_END_NAMESPACE |
230 | |
231 | #endif // QABSTRACTSOCKETENGINE_P_H |
232 | |