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 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | class QSocketEngineHandlerList : public QList<QSocketEngineHandler*> |
14 | { |
15 | public: |
16 | QMutex mutex; |
17 | }; |
18 | |
19 | Q_GLOBAL_STATIC(QSocketEngineHandlerList, socketHandlers) |
20 | |
21 | QSocketEngineHandler::QSocketEngineHandler() |
22 | { |
23 | if (!socketHandlers()) |
24 | return; |
25 | QMutexLocker locker(&socketHandlers()->mutex); |
26 | socketHandlers()->prepend(t: this); |
27 | } |
28 | |
29 | QSocketEngineHandler::~QSocketEngineHandler() |
30 | { |
31 | if (!socketHandlers()) |
32 | return; |
33 | QMutexLocker locker(&socketHandlers()->mutex); |
34 | socketHandlers()->removeAll(t: this); |
35 | } |
36 | |
37 | QAbstractSocketEnginePrivate::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 | |
52 | QAbstractSocketEngine::QAbstractSocketEngine(QObject *parent) |
53 | : QObject(*new QAbstractSocketEnginePrivate(), parent) |
54 | { |
55 | } |
56 | |
57 | QAbstractSocketEngine::QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent) |
58 | : QObject(dd, parent) |
59 | { |
60 | } |
61 | |
62 | QAbstractSocketEngine *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 | |
85 | QAbstractSocketEngine *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 | |
95 | QAbstractSocket::SocketError QAbstractSocketEngine::error() const |
96 | { |
97 | return d_func()->socketError; |
98 | } |
99 | |
100 | QString QAbstractSocketEngine::errorString() const |
101 | { |
102 | return d_func()->socketErrorString; |
103 | } |
104 | |
105 | void 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 | |
112 | void QAbstractSocketEngine::setReceiver(QAbstractSocketEngineReceiver *receiver) |
113 | { |
114 | d_func()->receiver = receiver; |
115 | } |
116 | |
117 | void QAbstractSocketEngine::readNotification() |
118 | { |
119 | if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver) |
120 | receiver->readNotification(); |
121 | } |
122 | |
123 | void QAbstractSocketEngine::writeNotification() |
124 | { |
125 | if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver) |
126 | receiver->writeNotification(); |
127 | } |
128 | |
129 | void QAbstractSocketEngine::exceptionNotification() |
130 | { |
131 | if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver) |
132 | receiver->exceptionNotification(); |
133 | } |
134 | |
135 | void QAbstractSocketEngine::closeNotification() |
136 | { |
137 | if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver) |
138 | receiver->closeNotification(); |
139 | } |
140 | |
141 | void QAbstractSocketEngine::connectionNotification() |
142 | { |
143 | if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver) |
144 | receiver->connectionNotification(); |
145 | } |
146 | |
147 | #ifndef QT_NO_NETWORKPROXY |
148 | void 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 | |
156 | QAbstractSocket::SocketState QAbstractSocketEngine::state() const |
157 | { |
158 | return d_func()->socketState; |
159 | } |
160 | |
161 | void QAbstractSocketEngine::setState(QAbstractSocket::SocketState state) |
162 | { |
163 | d_func()->socketState = state; |
164 | } |
165 | |
166 | QAbstractSocket::SocketType QAbstractSocketEngine::socketType() const |
167 | { |
168 | return d_func()->socketType; |
169 | } |
170 | |
171 | void QAbstractSocketEngine::setSocketType(QAbstractSocket::SocketType socketType) |
172 | { |
173 | d_func()->socketType = socketType; |
174 | } |
175 | |
176 | QAbstractSocket::NetworkLayerProtocol QAbstractSocketEngine::protocol() const |
177 | { |
178 | return d_func()->socketProtocol; |
179 | } |
180 | |
181 | void QAbstractSocketEngine::setProtocol(QAbstractSocket::NetworkLayerProtocol protocol) |
182 | { |
183 | d_func()->socketProtocol = protocol; |
184 | } |
185 | |
186 | QHostAddress QAbstractSocketEngine::localAddress() const |
187 | { |
188 | return d_func()->localAddress; |
189 | } |
190 | |
191 | void QAbstractSocketEngine::setLocalAddress(const QHostAddress &address) |
192 | { |
193 | d_func()->localAddress = address; |
194 | } |
195 | |
196 | quint16 QAbstractSocketEngine::localPort() const |
197 | { |
198 | return d_func()->localPort; |
199 | } |
200 | |
201 | void QAbstractSocketEngine::setLocalPort(quint16 port) |
202 | { |
203 | d_func()->localPort = port; |
204 | } |
205 | |
206 | QHostAddress QAbstractSocketEngine::peerAddress() const |
207 | { |
208 | return d_func()->peerAddress; |
209 | } |
210 | |
211 | void QAbstractSocketEngine::setPeerAddress(const QHostAddress &address) |
212 | { |
213 | d_func()->peerAddress = address; |
214 | } |
215 | |
216 | quint16 QAbstractSocketEngine::peerPort() const |
217 | { |
218 | return d_func()->peerPort; |
219 | } |
220 | |
221 | void QAbstractSocketEngine::setPeerPort(quint16 port) |
222 | { |
223 | d_func()->peerPort = port; |
224 | } |
225 | |
226 | int QAbstractSocketEngine::inboundStreamCount() const |
227 | { |
228 | return d_func()->inboundStreamCount; |
229 | } |
230 | |
231 | int QAbstractSocketEngine::outboundStreamCount() const |
232 | { |
233 | return d_func()->outboundStreamCount; |
234 | } |
235 | |
236 | QT_END_NAMESPACE |
237 | |
238 | #include "moc_qabstractsocketengine_p.cpp" |
239 | |