QtBase  v6.3.1
qsocketnotifier.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #define BUILDING_QSOCKETNOTIFIER
41 #include "qsocketnotifier.h"
42 #undef BUILDING_QSOCKETNOTIFIER
43 
44 #include "qplatformdefs.h"
45 
47 #include "qcoreapplication.h"
48 
49 #include "qmetatype.h"
50 
51 #include "qobject_p.h"
52 #include <private/qthread_p.h>
53 
54 #include <QtCore/QLoggingCategory>
55 
57 
58 Q_DECLARE_LOGGING_CATEGORY(lcSocketNotifierDeprecation)
59 Q_LOGGING_CATEGORY(lcSocketNotifierDeprecation, "qt.core.socketnotifier_deprecation");
60 
63 
65 {
66  Q_DECLARE_PUBLIC(QSocketNotifier)
67 public:
70  bool snenabled = false;
71 };
72 
160 {
161  Q_D(QSocketNotifier);
162 
163  qRegisterMetaType<QSocketDescriptor>();
164  qRegisterMetaType<QSocketNotifier::Type>();
165 
166  d->sntype = type;
167 }
168 
184 {
185  Q_D(QSocketNotifier);
186 
187  d->sockfd = socket;
188  d->snenabled = true;
189 
190  auto thisThreadData = d->threadData.loadRelaxed();
191 
192  if (!d->sockfd.isValid())
193  qWarning("QSocketNotifier: Invalid socket specified");
194  else if (!thisThreadData->hasEventDispatcher())
195  qWarning("QSocketNotifier: Can only be used with threads started with QThread");
196  else
197  thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
198 }
199 
205 {
206  setEnabled(false);
207 }
208 
209 
249 {
250  Q_D(QSocketNotifier);
251 
252  setEnabled(false);
253  d->sockfd = socket;
254 }
255 
262 {
263  Q_D(const QSocketNotifier);
264  return qintptr(d->sockfd);
265 }
266 
273 {
274  Q_D(const QSocketNotifier);
275  return d->sntype;
276 }
277 
287 {
288  Q_D(const QSocketNotifier);
289  return d->sockfd.isValid();
290 }
291 
298 {
299  Q_D(const QSocketNotifier);
300  return d->snenabled;
301 }
302 
319 {
320  Q_D(QSocketNotifier);
321  if (!d->sockfd.isValid())
322  return;
323  if (d->snenabled == enable) // no change
324  return;
325  d->snenabled = enable;
326 
327 
328  auto thisThreadData = d->threadData.loadRelaxed();
329 
330  if (!thisThreadData->hasEventDispatcher()) // perhaps application/thread is shutting down
331  return;
333  qWarning("QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread");
334  return;
335  }
336  if (d->snenabled)
337  thisThreadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
338  else
339  thisThreadData->eventDispatcher.loadRelaxed()->unregisterSocketNotifier(this);
340 }
341 
342 
346 {
347  Q_D(QSocketNotifier);
348  // Emits the activated() signal when a QEvent::SockAct or QEvent::SockClose is
349  // received.
350  switch (e->type()) {
352  if (d->snenabled) {
354  Q_ARG(bool, d->snenabled));
355  setEnabled(false);
356  }
357  break;
358  case QEvent::SockAct:
359  case QEvent::SockClose:
360  {
361  QPointer<QSocketNotifier> alive(this);
362  emit activated(d->sockfd, d->sntype, QPrivateSignal());
363  // ### Qt7: Remove emission if the activated(int) signal is removed
364  if (alive)
365  emit activated(int(qintptr(d->sockfd)), QPrivateSignal());
366  }
367  return true;
368  default:
369  break;
370  }
371  return QObject::event(e);
372 }
373 
419 
420 #include "moc_qsocketnotifier.cpp"
The QEvent class is the base class of all event classes. Event objects contain event parameters.
Definition: qcoreevent.h:58
@ SockClose
Definition: qcoreevent.h:281
@ SockAct
Definition: qcoreevent.h:111
@ ThreadChange
Definition: qcoreevent.h:95
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
virtual bool event(QEvent *event)
Definition: qobject.cpp:1329
QThread * thread() const
Definition: qobject.cpp:1527
The QPointer class is a template class that provides guarded pointers to QObject.
Definition: qpointer.h:54
A class which holds a native socket descriptor.
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
void setSocket(qintptr socket)
bool isEnabled() const
void activated(QSocketDescriptor socket, QSocketNotifier::Type activationEvent, QPrivateSignal)
bool isValid() const
qintptr socket() const
QSocketNotifier(Type, QObject *parent=nullptr)
bool event(QEvent *) override
QSocketDescriptor sockfd
QSocketNotifier::Type sntype
static QThread * currentThread()
Definition: qthread.cpp:879
double e
@ QueuedConnection
Definition: qnamespace.h:1307
#define Q_UNLIKELY(x)
ptrdiff_t qintptr
Definition: qglobal.h:309
#define qWarning
Definition: qlogging.h:179
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition: qmetatype.h:1287
QT_BEGIN_NAMESPACE QT_IMPL_METATYPE_EXTERN_TAGGED(QNetworkAccessCache::CacheableObject *, QNetworkAccessCache__CacheableObject_ptr) enum ExpiryTimeEnum
#define Q_ARG(type, data)
Definition: qobjectdefs.h:98
GLenum type
Definition: qopengl.h:270
GLboolean enable
QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(lcSocketNotifierDeprecation, "qt.core.socketnotifier_deprecation")
#define emit
Definition: qtmetamacros.h:85
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())
Definition: moc.h:48
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent