1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qabstractsocketengine_p.h"
5
6#include "qnativesocketengine_p.h"
7
8#include "qmutex.h"
9#include "qnetworkproxy.h"
10
11QT_BEGIN_NAMESPACE
12
13class QSocketEngineHandlerList : public QList<QSocketEngineHandler*>
14{
15public:
16 QMutex mutex;
17};
18
19Q_GLOBAL_STATIC(QSocketEngineHandlerList, socketHandlers)
20
21QSocketEngineHandler::QSocketEngineHandler()
22{
23 if (!socketHandlers())
24 return;
25 QMutexLocker locker(&socketHandlers()->mutex);
26 socketHandlers()->prepend(t: this);
27}
28
29QSocketEngineHandler::~QSocketEngineHandler()
30{
31 if (!socketHandlers())
32 return;
33 QMutexLocker locker(&socketHandlers()->mutex);
34 socketHandlers()->removeAll(t: this);
35}
36
37QAbstractSocketEnginePrivate::QAbstractSocketEnginePrivate()
38 : socketError(QAbstractSocket::UnknownSocketError)
39 , hasSetSocketError(false)
40 , socketErrorString(QLatin1StringView(QT_TRANSLATE_NOOP(QSocketLayer, "Unknown error")))
41 , socketState(QAbstractSocket::UnconnectedState)
42 , socketType(QAbstractSocket::UnknownSocketType)
43 , socketProtocol(QAbstractSocket::UnknownNetworkLayerProtocol)
44 , localPort(0)
45 , peerPort(0)
46 , inboundStreamCount(0)
47 , outboundStreamCount(0)
48 , receiver(nullptr)
49{
50}
51
52QAbstractSocketEngine::QAbstractSocketEngine(QObject *parent)
53 : QObject(*new QAbstractSocketEnginePrivate(), parent)
54{
55}
56
57QAbstractSocketEngine::QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent)
58 : QObject(dd, parent)
59{
60}
61
62QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &proxy, QObject *parent)
63{
64#ifndef QT_NO_NETWORKPROXY
65 // proxy type must have been resolved by now
66 if (proxy.type() == QNetworkProxy::DefaultProxy)
67 return nullptr;
68#endif
69
70 QMutexLocker locker(&socketHandlers()->mutex);
71 for (int i = 0; i < socketHandlers()->size(); i++) {
72 if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketType, proxy, parent))
73 return ret;
74 }
75
76#ifndef QT_NO_NETWORKPROXY
77 // only NoProxy can have reached here
78 if (proxy.type() != QNetworkProxy::NoProxy)
79 return nullptr;
80#endif
81
82 return new QNativeSocketEngine(parent);
83}
84
85QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(qintptr socketDescripter, QObject *parent)
86{
87 QMutexLocker locker(&socketHandlers()->mutex);
88 for (int i = 0; i < socketHandlers()->size(); i++) {
89 if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketDescriptor: socketDescripter, parent))
90 return ret;
91 }
92 return new QNativeSocketEngine(parent);
93}
94
95QAbstractSocket::SocketError QAbstractSocketEngine::error() const
96{
97 return d_func()->socketError;
98}
99
100QString QAbstractSocketEngine::errorString() const
101{
102 return d_func()->socketErrorString;
103}
104
105void QAbstractSocketEngine::setError(QAbstractSocket::SocketError error, const QString &errorString) const
106{
107 Q_D(const QAbstractSocketEngine);
108 d->socketError = error;
109 d->socketErrorString = errorString;
110}
111
112void QAbstractSocketEngine::setReceiver(QAbstractSocketEngineReceiver *receiver)
113{
114 d_func()->receiver = receiver;
115}
116
117void QAbstractSocketEngine::readNotification()
118{
119 if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
120 receiver->readNotification();
121}
122
123void QAbstractSocketEngine::writeNotification()
124{
125 if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
126 receiver->writeNotification();
127}
128
129void QAbstractSocketEngine::exceptionNotification()
130{
131 if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
132 receiver->exceptionNotification();
133}
134
135void QAbstractSocketEngine::closeNotification()
136{
137 if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
138 receiver->closeNotification();
139}
140
141void QAbstractSocketEngine::connectionNotification()
142{
143 if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
144 receiver->connectionNotification();
145}
146
147#ifndef QT_NO_NETWORKPROXY
148void QAbstractSocketEngine::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
149{
150 if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
151 receiver->proxyAuthenticationRequired(proxy, authenticator);
152}
153#endif
154
155
156QAbstractSocket::SocketState QAbstractSocketEngine::state() const
157{
158 return d_func()->socketState;
159}
160
161void QAbstractSocketEngine::setState(QAbstractSocket::SocketState state)
162{
163 d_func()->socketState = state;
164}
165
166QAbstractSocket::SocketType QAbstractSocketEngine::socketType() const
167{
168 return d_func()->socketType;
169}
170
171void QAbstractSocketEngine::setSocketType(QAbstractSocket::SocketType socketType)
172{
173 d_func()->socketType = socketType;
174}
175
176QAbstractSocket::NetworkLayerProtocol QAbstractSocketEngine::protocol() const
177{
178 return d_func()->socketProtocol;
179}
180
181void QAbstractSocketEngine::setProtocol(QAbstractSocket::NetworkLayerProtocol protocol)
182{
183 d_func()->socketProtocol = protocol;
184}
185
186QHostAddress QAbstractSocketEngine::localAddress() const
187{
188 return d_func()->localAddress;
189}
190
191void QAbstractSocketEngine::setLocalAddress(const QHostAddress &address)
192{
193 d_func()->localAddress = address;
194}
195
196quint16 QAbstractSocketEngine::localPort() const
197{
198 return d_func()->localPort;
199}
200
201void QAbstractSocketEngine::setLocalPort(quint16 port)
202{
203 d_func()->localPort = port;
204}
205
206QHostAddress QAbstractSocketEngine::peerAddress() const
207{
208 return d_func()->peerAddress;
209}
210
211void QAbstractSocketEngine::setPeerAddress(const QHostAddress &address)
212{
213 d_func()->peerAddress = address;
214}
215
216quint16 QAbstractSocketEngine::peerPort() const
217{
218 return d_func()->peerPort;
219}
220
221void QAbstractSocketEngine::setPeerPort(quint16 port)
222{
223 d_func()->peerPort = port;
224}
225
226int QAbstractSocketEngine::inboundStreamCount() const
227{
228 return d_func()->inboundStreamCount;
229}
230
231int QAbstractSocketEngine::outboundStreamCount() const
232{
233 return d_func()->outboundStreamCount;
234}
235
236QT_END_NAMESPACE
237
238#include "moc_qabstractsocketengine_p.cpp"
239

source code of qtbase/src/network/socket/qabstractsocketengine.cpp