1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtNetwork module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QABSTRACTSOCKETENGINE_P_H
42#define QABSTRACTSOCKETENGINE_P_H
43
44//
45// W A R N I N G
46// -------------
47//
48// This file is not part of the Qt API. It exists purely as an
49// implementation detail. This header file may change from version to
50// version without notice, or even be removed.
51//
52// We mean it.
53//
54
55#include <QtNetwork/private/qtnetworkglobal_p.h>
56#include "QtNetwork/qhostaddress.h"
57#include "QtNetwork/qabstractsocket.h"
58#include "private/qobject_p.h"
59#include "private/qnetworkdatagram_p.h"
60
61QT_BEGIN_NAMESPACE
62
63class QAuthenticator;
64class QAbstractSocketEnginePrivate;
65#ifndef QT_NO_NETWORKINTERFACE
66class QNetworkInterface;
67#endif
68class QNetworkProxy;
69
70class QAbstractSocketEngineReceiver {
71public:
72 virtual ~QAbstractSocketEngineReceiver(){}
73 virtual void readNotification()= 0;
74 virtual void writeNotification()= 0;
75 virtual void closeNotification()= 0;
76 virtual void exceptionNotification()= 0;
77 virtual void connectionNotification()= 0;
78#ifndef QT_NO_NETWORKPROXY
79 virtual void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)= 0;
80#endif
81};
82
83class Q_AUTOTEST_EXPORT QAbstractSocketEngine : public QObject
84{
85 Q_OBJECT
86 Q_MOC_INCLUDE(<QtNetwork/qauthenticator.h>)
87public:
88
89 static QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent);
90 static QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent);
91
92 QAbstractSocketEngine(QObject *parent = nullptr);
93
94 enum SocketOption {
95 NonBlockingSocketOption,
96 BroadcastSocketOption,
97 ReceiveBufferSocketOption,
98 SendBufferSocketOption,
99 AddressReusable,
100 BindExclusively,
101 ReceiveOutOfBandData,
102 LowDelayOption,
103 KeepAliveOption,
104 MulticastTtlOption,
105 MulticastLoopbackOption,
106 TypeOfServiceOption,
107 ReceivePacketInformation,
108 ReceiveHopLimit,
109 MaxStreamsSocketOption,
110 PathMtuInformation
111 };
112
113 enum PacketHeaderOption {
114 WantNone = 0,
115 WantDatagramSender = 0x01,
116 WantDatagramDestination = 0x02,
117 WantDatagramHopLimit = 0x04,
118 WantStreamNumber = 0x08,
119 WantEndOfRecord = 0x10,
120
121 WantAll = 0xff
122 };
123 Q_DECLARE_FLAGS(PacketHeaderOptions, PacketHeaderOption)
124
125 virtual bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) = 0;
126
127 virtual bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) = 0;
128
129 virtual qintptr socketDescriptor() const = 0;
130
131 virtual bool isValid() const = 0;
132
133 virtual bool connectToHost(const QHostAddress &address, quint16 port) = 0;
134 virtual bool connectToHostByName(const QString &name, quint16 port) = 0;
135 virtual bool bind(const QHostAddress &address, quint16 port) = 0;
136 virtual bool listen() = 0;
137 virtual int accept() = 0;
138 virtual void close() = 0;
139
140 virtual qint64 bytesAvailable() const = 0;
141
142 virtual qint64 read(char *data, qint64 maxlen) = 0;
143 virtual qint64 write(const char *data, qint64 len) = 0;
144
145#ifndef QT_NO_UDPSOCKET
146#ifndef QT_NO_NETWORKINTERFACE
147 virtual bool joinMulticastGroup(const QHostAddress &groupAddress,
148 const QNetworkInterface &iface) = 0;
149 virtual bool leaveMulticastGroup(const QHostAddress &groupAddress,
150 const QNetworkInterface &iface) = 0;
151 virtual QNetworkInterface multicastInterface() const = 0;
152 virtual bool setMulticastInterface(const QNetworkInterface &iface) = 0;
153#endif // QT_NO_NETWORKINTERFACE
154
155 virtual bool hasPendingDatagrams() const = 0;
156 virtual qint64 pendingDatagramSize() const = 0;
157#endif // QT_NO_UDPSOCKET
158
159 virtual qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *header = nullptr,
160 PacketHeaderOptions = WantNone) = 0;
161 virtual qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header) = 0;
162 virtual qint64 bytesToWrite() const = 0;
163
164 virtual int option(SocketOption option) const = 0;
165 virtual bool setOption(SocketOption option, int value) = 0;
166
167 virtual bool waitForRead(int msecs = 30000, bool *timedOut = nullptr) = 0;
168 virtual bool waitForWrite(int msecs = 30000, bool *timedOut = nullptr) = 0;
169 virtual bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
170 bool checkRead, bool checkWrite,
171 int msecs = 30000, bool *timedOut = nullptr) = 0;
172
173 QAbstractSocket::SocketError error() const;
174 QString errorString() const;
175 QAbstractSocket::SocketState state() const;
176 QAbstractSocket::SocketType socketType() const;
177 QAbstractSocket::NetworkLayerProtocol protocol() const;
178
179 QHostAddress localAddress() const;
180 quint16 localPort() const;
181 QHostAddress peerAddress() const;
182 quint16 peerPort() const;
183 int inboundStreamCount() const;
184 int outboundStreamCount() const;
185
186 virtual bool isReadNotificationEnabled() const = 0;
187 virtual void setReadNotificationEnabled(bool enable) = 0;
188 virtual bool isWriteNotificationEnabled() const = 0;
189 virtual void setWriteNotificationEnabled(bool enable) = 0;
190 virtual bool isExceptionNotificationEnabled() const = 0;
191 virtual void setExceptionNotificationEnabled(bool enable) = 0;
192
193public Q_SLOTS:
194 void readNotification();
195 void writeNotification();
196 void closeNotification();
197 void exceptionNotification();
198 void connectionNotification();
199#ifndef QT_NO_NETWORKPROXY
200 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
201#endif
202
203public:
204 void setReceiver(QAbstractSocketEngineReceiver *receiver);
205protected:
206 QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent = nullptr);
207
208 void setError(QAbstractSocket::SocketError error, const QString &errorString) const;
209 void setState(QAbstractSocket::SocketState state);
210 void setSocketType(QAbstractSocket::SocketType socketType);
211 void setProtocol(QAbstractSocket::NetworkLayerProtocol protocol);
212 void setLocalAddress(const QHostAddress &address);
213 void setLocalPort(quint16 port);
214 void setPeerAddress(const QHostAddress &address);
215 void setPeerPort(quint16 port);
216
217private:
218 Q_DECLARE_PRIVATE(QAbstractSocketEngine)
219 Q_DISABLE_COPY_MOVE(QAbstractSocketEngine)
220};
221
222class QAbstractSocketEnginePrivate : public QObjectPrivate
223{
224 Q_DECLARE_PUBLIC(QAbstractSocketEngine)
225public:
226 QAbstractSocketEnginePrivate();
227
228 mutable QAbstractSocket::SocketError socketError;
229 mutable bool hasSetSocketError;
230 mutable QString socketErrorString;
231 QAbstractSocket::SocketState socketState;
232 QAbstractSocket::SocketType socketType;
233 QAbstractSocket::NetworkLayerProtocol socketProtocol;
234 QHostAddress localAddress;
235 quint16 localPort;
236 QHostAddress peerAddress;
237 quint16 peerPort;
238 int inboundStreamCount;
239 int outboundStreamCount;
240 QAbstractSocketEngineReceiver *receiver;
241};
242
243
244class Q_AUTOTEST_EXPORT QSocketEngineHandler
245{
246protected:
247 QSocketEngineHandler();
248 virtual ~QSocketEngineHandler();
249 virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
250 const QNetworkProxy &, QObject *parent) = 0;
251 virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent) = 0;
252
253private:
254 friend class QAbstractSocketEngine;
255};
256
257Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocketEngine::PacketHeaderOptions)
258
259QT_END_NAMESPACE
260
261#endif // QABSTRACTSOCKETENGINE_P_H
262