Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#include <QtCore/qpointer.h>
20
22
23Q_DECLARE_LOGGING_CATEGORY(lcSocketNotifierDeprecation)
24Q_LOGGING_CATEGORY(lcSocketNotifierDeprecation, "qt.core.socketnotifier_deprecation");
25
28
30{
31 Q_DECLARE_PUBLIC(QSocketNotifier)
32public:
35 bool snenabled = false;
36};
37
124 : QObject(*new QSocketNotifierPrivate, parent)
125{
126 Q_D(QSocketNotifier);
127
128 qRegisterMetaType<QSocketDescriptor>();
129 qRegisterMetaType<QSocketNotifier::Type>();
130
131 d->sntype = type;
132}
133
148 : QSocketNotifier(type, parent)
149{
150 Q_D(QSocketNotifier);
151
152 d->sockfd = socket;
153 d->snenabled = true;
154
155 auto thisThreadData = d->threadData.loadRelaxed();
156
157 if (!d->sockfd.isValid())
158 qWarning("QSocketNotifier: Invalid socket specified");
159 else if (!thisThreadData->hasEventDispatcher())
160 qWarning("QSocketNotifier: Can only be used with threads started with QThread");
161 else
162 thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
163}
164
173
174
214{
215 Q_D(QSocketNotifier);
216
217 setEnabled(false);
218 d->sockfd = socket;
219}
220
227{
228 Q_D(const QSocketNotifier);
229 return qintptr(d->sockfd);
230}
231
238{
239 Q_D(const QSocketNotifier);
240 return d->sntype;
241}
242
252{
253 Q_D(const QSocketNotifier);
254 return d->sockfd.isValid();
255}
256
263{
264 Q_D(const QSocketNotifier);
265 return d->snenabled;
266}
267
284{
285 Q_D(QSocketNotifier);
286 if (!d->sockfd.isValid())
287 return;
288 if (d->snenabled == enable) // no change
289 return;
290 d->snenabled = enable;
291
292
293 auto thisThreadData = d->threadData.loadRelaxed();
294
295 if (!thisThreadData->hasEventDispatcher()) // perhaps application/thread is shutting down
296 return;
298 qWarning("QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread");
299 return;
300 }
301 if (d->snenabled)
302 thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
303 else
304 thisThreadData->eventDispatcher.loadRelaxed()->unregisterSocketNotifier(this);
305}
306
307
311{
312 Q_D(QSocketNotifier);
313 // Emits the activated() signal when a QEvent::SockAct or QEvent::SockClose is
314 // received.
315 switch (e->type()) {
317 if (d->snenabled) {
319 Q_ARG(bool, d->snenabled));
320 setEnabled(false);
321 }
322 break;
323 case QEvent::SockAct:
325 {
326 QPointer<QSocketNotifier> alive(this);
327 emit activated(d->sockfd, d->sntype, QPrivateSignal());
328 // ### Qt7: Remove emission if the activated(int) signal is removed
329 if (alive)
330 emit activated(int(qintptr(d->sockfd)), QPrivateSignal());
331 }
332 return true;
333 default:
334 break;
335 }
336 return QObject::event(e);
337}
338
384
385#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
Type type() const
Returns the event type.
Definition qcoreevent.h:304
\inmodule QtCore
Definition qobject.h:103
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:1389
QThread * thread() const
Returns the thread in which the object lives.
Definition qobject.cpp:1598
\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:1039
Combined button and popup list for selecting options.
@ QueuedConnection
#define Q_UNLIKELY(x)
#define qWarning
Definition qlogging.h:166
#define Q_LOGGING_CATEGORY(name,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define QT_IMPL_METATYPE_EXTERN_TAGGED(TYPE, TAG)
Definition qmetatype.h:1384
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
#define Q_ARG(Type, data)
Definition qobjectdefs.h:63
GLenum type
GLboolean enable
#define emit
ptrdiff_t qintptr
Definition qtypes.h:166
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:23