Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsocketnotifier.cpp
Go to the documentation of this file.
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#define BUILDING_QSOCKETNOTIFIER
5#include "qsocketnotifier.h"
6#undef BUILDING_QSOCKETNOTIFIER
7
8#include "qplatformdefs.h"
9
11#include "qcoreapplication.h"
12
13#include "qmetatype.h"
14
15#include "qobject_p.h"
16#include <private/qthread_p.h>
17
18#include <QtCore/QLoggingCategory>
19
21
22Q_DECLARE_LOGGING_CATEGORY(lcSocketNotifierDeprecation)
23Q_LOGGING_CATEGORY(lcSocketNotifierDeprecation, "qt.core.socketnotifier_deprecation");
24
27
29{
30 Q_DECLARE_PUBLIC(QSocketNotifier)
31public:
34 bool snenabled = false;
35};
36
124{
125 Q_D(QSocketNotifier);
126
127 qRegisterMetaType<QSocketDescriptor>();
128 qRegisterMetaType<QSocketNotifier::Type>();
129
130 d->sntype = type;
131}
132
148{
149 Q_D(QSocketNotifier);
150
151 d->sockfd = socket;
152 d->snenabled = true;
153
154 auto thisThreadData = d->threadData.loadRelaxed();
155
156 if (!d->sockfd.isValid())
157 qWarning("QSocketNotifier: Invalid socket specified");
158 else if (!thisThreadData->hasEventDispatcher())
159 qWarning("QSocketNotifier: Can only be used with threads started with QThread");
160 else
161 thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
162}
163
169{
170 setEnabled(false);
171}
172
173
213{
214 Q_D(QSocketNotifier);
215
216 setEnabled(false);
217 d->sockfd = socket;
218}
219
226{
227 Q_D(const QSocketNotifier);
228 return qintptr(d->sockfd);
229}
230
237{
238 Q_D(const QSocketNotifier);
239 return d->sntype;
240}
241
251{
252 Q_D(const QSocketNotifier);
253 return d->sockfd.isValid();
254}
255
262{
263 Q_D(const QSocketNotifier);
264 return d->snenabled;
265}
266
283{
284 Q_D(QSocketNotifier);
285 if (!d->sockfd.isValid())
286 return;
287 if (d->snenabled == enable) // no change
288 return;
289 d->snenabled = enable;
290
291
292 auto thisThreadData = d->threadData.loadRelaxed();
293
294 if (!thisThreadData->hasEventDispatcher()) // perhaps application/thread is shutting down
295 return;
297 qWarning("QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread");
298 return;
299 }
300 if (d->snenabled)
301 thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
302 else
303 thisThreadData->eventDispatcher.loadRelaxed()->unregisterSocketNotifier(this);
304}
305
306
310{
311 Q_D(QSocketNotifier);
312 // Emits the activated() signal when a QEvent::SockAct or QEvent::SockClose is
313 // received.
314 switch (e->type()) {
316 if (d->snenabled) {
318 Q_ARG(bool, d->snenabled));
319 setEnabled(false);
320 }
321 break;
322 case QEvent::SockAct:
324 {
325 QPointer<QSocketNotifier> alive(this);
326 emit activated(d->sockfd, d->sntype, QPrivateSignal());
327 // ### Qt7: Remove emission if the activated(int) signal is removed
328 if (alive)
329 emit activated(int(qintptr(d->sockfd)), QPrivateSignal());
330 }
331 return true;
332 default:
333 break;
334 }
335 return QObject::event(e);
336}
337
383
384#include "moc_qsocketnotifier.cpp"
\inmodule QtCore
Definition qcoreevent.h:45
@ SockClose
Definition qcoreevent.h:268
@ SockAct
Definition qcoreevent.h:98
@ ThreadChange
Definition qcoreevent.h:82
\inmodule QtCore
Definition qobject.h:90
virtual bool event(QEvent *event)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition qobject.cpp:1363
QThread * thread() const
Returns the thread in which the object lives.
Definition qobject.cpp:1561
\inmodule QtCore
Definition qpointer.h:18
\inmodule QtCore
QSocketDescriptor sockfd
QSocketNotifier::Type sntype
\inmodule QtCore
void setSocket(qintptr socket)
bool isEnabled() const
Returns true if the notifier is enabled; otherwise returns false.
void activated(QSocketDescriptor socket, QSocketNotifier::Type activationEvent, QPrivateSignal)
Type type() const
Returns the socket event type specified to the constructor.
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
Type
This enum describes the various types of events that a socket notifier can recognize.
~QSocketNotifier()
Destroys this socket notifier.
qintptr socket() const
Returns the socket identifier assigned to this object.
QSocketNotifier(Type, QObject *parent=nullptr)
bool event(QEvent *) override
\reimp
static QThread * currentThread()
Definition qthread.cpp:966
double e
Combined button and popup list for selecting options.
@ QueuedConnection
#define Q_UNLIKELY(x)
#define qWarning
Definition qlogging.h:162
#define Q_LOGGING_CATEGORY(name,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define QT_IMPL_METATYPE_EXTERN_TAGGED(TYPE, TAG)
Definition qmetatype.h:1363
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1369
#define Q_ARG(Type, data)
Definition qobjectdefs.h:62
GLenum type
GLboolean enable
#define emit
ptrdiff_t qintptr
Definition qtypes.h:71
QTcpSocket * socket
[1]
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...
Definition moc.h:24
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent