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
86public:
87
88 static QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent);
89 static QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent);
90
91 QAbstractSocketEngine(QObject *parent = nullptr);
92
93 enum SocketOption {
94 NonBlockingSocketOption,
95 BroadcastSocketOption,
96 ReceiveBufferSocketOption,
97 SendBufferSocketOption,
98 AddressReusable,
99 BindExclusively,
100 ReceiveOutOfBandData,
101 LowDelayOption,
102 KeepAliveOption,
103 MulticastTtlOption,
104 MulticastLoopbackOption,
105 TypeOfServiceOption,
106 ReceivePacketInformation,
107 ReceiveHopLimit,
108 MaxStreamsSocketOption,
109 PathMtuInformation
110 };
111
112 enum PacketHeaderOption {
113 WantNone = 0,
114 WantDatagramSender = 0x01,
115 WantDatagramDestination = 0x02,
116 WantDatagramHopLimit = 0x04,
117 WantStreamNumber = 0x08,
118 WantEndOfRecord = 0x10,
119
120 WantAll = 0xff
121 };
122 Q_DECLARE_FLAGS(PacketHeaderOptions, PacketHeaderOption)
123
124 virtual bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) = 0;
125
126 virtual bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) = 0;
127
128 virtual qintptr socketDescriptor() const = 0;
129
130 virtual bool isValid() const = 0;
131
132 virtual bool connectToHost(const QHostAddress &address, quint16 port) = 0;
133 virtual bool connectToHostByName(const QString &name, quint16 port) = 0;
134 virtual bool bind(const QHostAddress &address, quint16 port) = 0;
135 virtual bool listen() = 0;
136 virtual int accept() = 0;
137 virtual void close() = 0;
138
139 virtual qint64 bytesAvailable() const = 0;
140
141 virtual qint64 read(char *data, qint64 maxlen) = 0;
142 virtual qint64 write(const char *data, qint64 len) = 0;
143
144#ifndef QT_NO_UDPSOCKET
145#ifndef QT_NO_NETWORKINTERFACE
146 virtual bool joinMulticastGroup(const QHostAddress &groupAddress,
147 const QNetworkInterface &iface) = 0;
148 virtual bool leaveMulticastGroup(const QHostAddress &groupAddress,
149 const QNetworkInterface &iface) = 0;
150 virtual QNetworkInterface multicastInterface() const = 0;
151 virtual bool setMulticastInterface(const QNetworkInterface &iface) = 0;
152#endif // QT_NO_NETWORKINTERFACE
153
154 virtual bool hasPendingDatagrams() const = 0;
155 virtual qint64 pendingDatagramSize() const = 0;
156#endif // QT_NO_UDPSOCKET
157
158 virtual qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *header = nullptr,
159 PacketHeaderOptions = WantNone) = 0;
160 virtual qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header) = 0;
161 virtual qint64 bytesToWrite() const = 0;
162
163 virtual int option(SocketOption option) const = 0;
164 virtual bool setOption(SocketOption option, int value) = 0;
165
166 virtual bool waitForRead(int msecs = 30000, bool *timedOut = nullptr) = 0;
167 virtual bool waitForWrite(int msecs = 30000, bool *timedOut = nullptr) = 0;
168 virtual bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
169 bool checkRead, bool checkWrite,
170 int msecs = 30000, bool *timedOut = nullptr) = 0;
171
172 QAbstractSocket::SocketError error() const;
173 QString errorString() const;
174 QAbstractSocket::SocketState state() const;
175 QAbstractSocket::SocketType socketType() const;
176 QAbstractSocket::NetworkLayerProtocol protocol() const;
177
178 QHostAddress localAddress() const;
179 quint16 localPort() const;
180 QHostAddress peerAddress() const;
181 quint16 peerPort() const;
182 int inboundStreamCount() const;
183 int outboundStreamCount() const;
184
185 virtual bool isReadNotificationEnabled() const = 0;
186 virtual void setReadNotificationEnabled(bool enable) = 0;
187 virtual bool isWriteNotificationEnabled() const = 0;
188 virtual void setWriteNotificationEnabled(bool enable) = 0;
189 virtual bool isExceptionNotificationEnabled() const = 0;
190 virtual void setExceptionNotificationEnabled(bool enable) = 0;
191
192public Q_SLOTS:
193 void readNotification();
194 void writeNotification();
195 void closeNotification();
196 void exceptionNotification();
197 void connectionNotification();
198#ifndef QT_NO_NETWORKPROXY
199 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
200#endif
201
202public:
203 void setReceiver(QAbstractSocketEngineReceiver *receiver);
204protected:
205 QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent = nullptr);
206
207 void setError(QAbstractSocket::SocketError error, const QString &errorString) const;
208 void setState(QAbstractSocket::SocketState state);
209 void setSocketType(QAbstractSocket::SocketType socketType);
210 void setProtocol(QAbstractSocket::NetworkLayerProtocol protocol);
211 void setLocalAddress(const QHostAddress &address);
212 void setLocalPort(quint16 port);
213 void setPeerAddress(const QHostAddress &address);
214 void setPeerPort(quint16 port);
215
216private:
217 Q_DECLARE_PRIVATE(QAbstractSocketEngine)
218 Q_DISABLE_COPY_MOVE(QAbstractSocketEngine)
219};
220
221class QAbstractSocketEnginePrivate : public QObjectPrivate
222{
223 Q_DECLARE_PUBLIC(QAbstractSocketEngine)
224public:
225 QAbstractSocketEnginePrivate();
226
227 mutable QAbstractSocket::SocketError socketError;
228 mutable bool hasSetSocketError;
229 mutable QString socketErrorString;
230 QAbstractSocket::SocketState socketState;
231 QAbstractSocket::SocketType socketType;
232 QAbstractSocket::NetworkLayerProtocol socketProtocol;
233 QHostAddress localAddress;
234 quint16 localPort;
235 QHostAddress peerAddress;
236 quint16 peerPort;
237 int inboundStreamCount;
238 int outboundStreamCount;
239 QAbstractSocketEngineReceiver *receiver;
240};
241
242
243class Q_AUTOTEST_EXPORT QSocketEngineHandler
244{
245protected:
246 QSocketEngineHandler();
247 virtual ~QSocketEngineHandler();
248 virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
249 const QNetworkProxy &, QObject *parent) = 0;
250 virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent) = 0;
251
252private:
253 friend class QAbstractSocketEngine;
254};
255
256Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocketEngine::PacketHeaderOptions)
257
258QT_END_NAMESPACE
259
260#endif // QABSTRACTSOCKETENGINE_P_H
261