| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qsocketnotifier.cpp | 
| Source code | Switch to Preprocessed file | 
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 | #include "qsocketnotifier.h" | - | ||||||||||||
| 41 | - | |||||||||||||
| 42 | #include "qplatformdefs.h" | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | #include "qabstracteventdispatcher.h" | - | ||||||||||||
| 45 | #include "qcoreapplication.h" | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | #include "qobject_p.h" | - | ||||||||||||
| 48 | #include <private/qthread_p.h> | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | class QSocketNotifierPrivate : public QObjectPrivate | - | ||||||||||||
| 53 | { | - | ||||||||||||
| 54 | Q_DECLARE_PUBLIC(QSocketNotifier) | - | ||||||||||||
| 55 | public: | - | ||||||||||||
| 56 | qintptr sockfd; | - | ||||||||||||
| 57 | QSocketNotifier::Type sntype; | - | ||||||||||||
| 58 | bool snenabled; | - | ||||||||||||
| 59 | }; | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | /*! | - | ||||||||||||
| 62 | \class QSocketNotifier | - | ||||||||||||
| 63 | \inmodule QtCore | - | ||||||||||||
| 64 | \brief The QSocketNotifier class provides support for monitoring | - | ||||||||||||
| 65 | activity on a file descriptor. | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | \ingroup network | - | ||||||||||||
| 68 | \ingroup io | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | The QSocketNotifier makes it possible to integrate Qt's event | - | ||||||||||||
| 71 | loop with other event loops based on file descriptors. File | - | ||||||||||||
| 72 | descriptor action is detected in Qt's main event | - | ||||||||||||
| 73 | loop (QCoreApplication::exec()). | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | \target write notifiers | - | ||||||||||||
| 76 | - | |||||||||||||
| 77 | Once you have opened a device using a low-level (usually | - | ||||||||||||
| 78 | platform-specific) API, you can create a socket notifier to | - | ||||||||||||
| 79 | monitor the file descriptor. The socket notifier is enabled by | - | ||||||||||||
| 80 | default, i.e. it emits the activated() signal whenever a socket | - | ||||||||||||
| 81 | event corresponding to its type occurs. Connect the activated() | - | ||||||||||||
| 82 | signal to the slot you want to be called when an event | - | ||||||||||||
| 83 | corresponding to your socket notifier's type occurs. | - | ||||||||||||
| 84 | - | |||||||||||||
| 85 | There are three types of socket notifiers: read, write, and | - | ||||||||||||
| 86 | exception. The type is described by the \l Type enum, and must be | - | ||||||||||||
| 87 | specified when constructing the socket notifier. After | - | ||||||||||||
| 88 | construction it can be determined using the type() function. Note | - | ||||||||||||
| 89 | that if you need to monitor both reads and writes for the same | - | ||||||||||||
| 90 | file descriptor, you must create two socket notifiers. Note also | - | ||||||||||||
| 91 | that it is not possible to install two socket notifiers of the | - | ||||||||||||
| 92 | same type (\l Read, \l Write, \l Exception) on the same socket. | - | ||||||||||||
| 93 | - | |||||||||||||
| 94 | The setEnabled() function allows you to disable as well as enable | - | ||||||||||||
| 95 | the socket notifier. It is generally advisable to explicitly | - | ||||||||||||
| 96 | enable or disable the socket notifier, especially for write | - | ||||||||||||
| 97 | notifiers. A disabled notifier ignores socket events (the same | - | ||||||||||||
| 98 | effect as not creating the socket notifier). Use the isEnabled() | - | ||||||||||||
| 99 | function to determine the notifier's current status. | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | Finally, you can use the socket() function to retrieve the | - | ||||||||||||
| 102 | socket identifier. Although the class is called QSocketNotifier, | - | ||||||||||||
| 103 | it is normally used for other types of devices than sockets. | - | ||||||||||||
| 104 | QTcpSocket and QUdpSocket provide notification through signals, so | - | ||||||||||||
| 105 | there is normally no need to use a QSocketNotifier on them. | - | ||||||||||||
| 106 | - | |||||||||||||
| 107 | \sa QFile, QProcess, QTcpSocket, QUdpSocket | - | ||||||||||||
| 108 | */ | - | ||||||||||||
| 109 | - | |||||||||||||
| 110 | /*! | - | ||||||||||||
| 111 | \enum QSocketNotifier::Type | - | ||||||||||||
| 112 | - | |||||||||||||
| 113 | This enum describes the various types of events that a socket | - | ||||||||||||
| 114 | notifier can recognize. The type must be specified when | - | ||||||||||||
| 115 | constructing the socket notifier. | - | ||||||||||||
| 116 | - | |||||||||||||
| 117 | Note that if you need to monitor both reads and writes for the | - | ||||||||||||
| 118 | same file descriptor, you must create two socket notifiers. Note | - | ||||||||||||
| 119 | also that it is not possible to install two socket notifiers of | - | ||||||||||||
| 120 | the same type (Read, Write, Exception) on the same socket. | - | ||||||||||||
| 121 | - | |||||||||||||
| 122 | \value Read There is data to be read. | - | ||||||||||||
| 123 | \value Write Data can be written. | - | ||||||||||||
| 124 | \value Exception An exception has occurred. We recommend against using this. | - | ||||||||||||
| 125 | - | |||||||||||||
| 126 | \sa QSocketNotifier(), type() | - | ||||||||||||
| 127 | */ | - | ||||||||||||
| 128 | - | |||||||||||||
| 129 | /*! | - | ||||||||||||
| 130 | Constructs a socket notifier with the given \a parent. It enables | - | ||||||||||||
| 131 | the \a socket, and watches for events of the given \a type. | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | It is generally advisable to explicitly enable or disable the | - | ||||||||||||
| 134 | socket notifier, especially for write notifiers. | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | \b{Note for Windows users:} The socket passed to QSocketNotifier | - | ||||||||||||
| 137 | will become non-blocking, even if it was created as a blocking socket. | - | ||||||||||||
| 138 | - | |||||||||||||
| 139 | \sa setEnabled(), isEnabled() | - | ||||||||||||
| 140 | */ | - | ||||||||||||
| 141 | - | |||||||||||||
| 142 | QSocketNotifier::QSocketNotifier(qintptr socket, Type type, QObject *parent) | - | ||||||||||||
| 143 | : QObject(*new QSocketNotifierPrivate, parent) | - | ||||||||||||
| 144 | { | - | ||||||||||||
| 145 | Q_D(QSocketNotifier); | - | ||||||||||||
| 146 | d->sockfd = socket; | - | ||||||||||||
| 147 | d->sntype = type; | - | ||||||||||||
| 148 | d->snenabled = true; | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 |     if (socket < 0)
  | 0-25774 | ||||||||||||
| 151 |         qWarning("QSocketNotifier: Invalid socket specified"); never executed:  QMessageLogger(__FILE__, 151, __PRETTY_FUNCTION__).warning("QSocketNotifier: Invalid socket specified"); | 0 | ||||||||||||
| 152 |     else if (!d->threadData->eventDispatcher.load())
  | 0-25774 | ||||||||||||
| 153 |         qWarning("QSocketNotifier: Can only be used with threads started with QThread"); never executed:  QMessageLogger(__FILE__, 153, __PRETTY_FUNCTION__).warning("QSocketNotifier: Can only be used with threads started with QThread"); | 0 | ||||||||||||
| 154 | else | - | ||||||||||||
| 155 |         d->threadData->eventDispatcher.load()->registerSocketNotifier(this); executed 25774 times by 202 tests:  d->threadData->eventDispatcher.load()->registerSocketNotifier(this);Executed by: 
  | 25774 | ||||||||||||
| 156 | } | - | ||||||||||||
| 157 | - | |||||||||||||
| 158 | /*! | - | ||||||||||||
| 159 | Destroys this socket notifier. | - | ||||||||||||
| 160 | */ | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | QSocketNotifier::~QSocketNotifier() | - | ||||||||||||
| 163 | { | - | ||||||||||||
| 164 | setEnabled(false); | - | ||||||||||||
| 165 | } executed 25775 times by 236 tests:  end of blockExecuted by: 
  | 25775 | ||||||||||||
| 166 | - | |||||||||||||
| 167 | - | |||||||||||||
| 168 | /*! | - | ||||||||||||
| 169 | \fn void QSocketNotifier::activated(int socket) | - | ||||||||||||
| 170 | - | |||||||||||||
| 171 | This signal is emitted whenever the socket notifier is enabled and | - | ||||||||||||
| 172 | a socket event corresponding to its \l {Type}{type} occurs. | - | ||||||||||||
| 173 | - | |||||||||||||
| 174 | The socket identifier is passed in the \a socket parameter. | - | ||||||||||||
| 175 | - | |||||||||||||
| 176 | \sa type(), socket() | - | ||||||||||||
| 177 | */ | - | ||||||||||||
| 178 | - | |||||||||||||
| 179 | - | |||||||||||||
| 180 | /*! | - | ||||||||||||
| 181 | Returns the socket identifier specified to the constructor. | - | ||||||||||||
| 182 | - | |||||||||||||
| 183 | \sa type() | - | ||||||||||||
| 184 | */ | - | ||||||||||||
| 185 | qintptr QSocketNotifier::socket() const | - | ||||||||||||
| 186 | { | - | ||||||||||||
| 187 | Q_D(const QSocketNotifier); | - | ||||||||||||
| 188 |     return d->sockfd; executed 98722 times by 359 tests:  return d->sockfd;Executed by: 
  | 98722 | ||||||||||||
| 189 | } | - | ||||||||||||
| 190 | - | |||||||||||||
| 191 | /*! | - | ||||||||||||
| 192 | Returns the socket event type specified to the constructor. | - | ||||||||||||
| 193 | - | |||||||||||||
| 194 | \sa socket() | - | ||||||||||||
| 195 | */ | - | ||||||||||||
| 196 | QSocketNotifier::Type QSocketNotifier::type() const | - | ||||||||||||
| 197 | { | - | ||||||||||||
| 198 | Q_D(const QSocketNotifier); | - | ||||||||||||
| 199 |     return d->sntype; executed 49361 times by 202 tests:  return d->sntype;Executed by: 
  | 49361 | ||||||||||||
| 200 | } | - | ||||||||||||
| 201 | - | |||||||||||||
| 202 | /*! | - | ||||||||||||
| 203 | Returns \c true if the notifier is enabled; otherwise returns \c false. | - | ||||||||||||
| 204 | - | |||||||||||||
| 205 | \sa setEnabled() | - | ||||||||||||
| 206 | */ | - | ||||||||||||
| 207 | bool QSocketNotifier::isEnabled() const | - | ||||||||||||
| 208 | { | - | ||||||||||||
| 209 | Q_D(const QSocketNotifier); | - | ||||||||||||
| 210 |     return d->snenabled; executed 189139 times by 302 tests:  return d->snenabled;Executed by: 
  | 189139 | ||||||||||||
| 211 | } | - | ||||||||||||
| 212 | - | |||||||||||||
| 213 | /*! | - | ||||||||||||
| 214 | If \a enable is true, the notifier is enabled; otherwise the notifier | - | ||||||||||||
| 215 | is disabled. | - | ||||||||||||
| 216 | - | |||||||||||||
| 217 | The notifier is enabled by default, i.e. it emits the activated() | - | ||||||||||||
| 218 | signal whenever a socket event corresponding to its | - | ||||||||||||
| 219 | \l{type()}{type} occurs. If it is disabled, it ignores socket | - | ||||||||||||
| 220 | events (the same effect as not creating the socket notifier). | - | ||||||||||||
| 221 | - | |||||||||||||
| 222 | Write notifiers should normally be disabled immediately after the | - | ||||||||||||
| 223 | activated() signal has been emitted | - | ||||||||||||
| 224 | - | |||||||||||||
| 225 | \sa isEnabled(), activated() | - | ||||||||||||
| 226 | */ | - | ||||||||||||
| 227 | - | |||||||||||||
| 228 | void QSocketNotifier::setEnabled(bool enable) | - | ||||||||||||
| 229 | { | - | ||||||||||||
| 230 | Q_D(QSocketNotifier); | - | ||||||||||||
| 231 |     if (d->sockfd < 0)
  | 0-790819 | ||||||||||||
| 232 |         return; never executed:  return; | 0 | ||||||||||||
| 233 |     if (d->snenabled == enable)                        // no change
  | 72978-717841 | ||||||||||||
| 234 |         return; executed 717841 times by 360 tests:  return;Executed by: 
  | 717841 | ||||||||||||
| 235 | d->snenabled = enable; | - | ||||||||||||
| 236 | - | |||||||||||||
| 237 |     if (!d->threadData->eventDispatcher.load()) // perhaps application/thread is shutting down
  | 30-72948 | ||||||||||||
| 238 |         return; executed 30 times by 3 tests:  return;Executed by: 
  | 30 | ||||||||||||
| 239 |     if (Q_UNLIKELY(thread() != QThread::currentThread())) {
  | 0-72948 | ||||||||||||
| 240 | qWarning("QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread"); | - | ||||||||||||
| 241 |         return; never executed:  return; | 0 | ||||||||||||
| 242 | } | - | ||||||||||||
| 243 |     if (d->snenabled)
  | 23587-49361 | ||||||||||||
| 244 |         d->threadData->eventDispatcher.load()->registerSocketNotifier(this); executed 23587 times by 35 tests:  d->threadData->eventDispatcher.load()->registerSocketNotifier(this);Executed by: 
  | 23587 | ||||||||||||
| 245 | else | - | ||||||||||||
| 246 |         d->threadData->eventDispatcher.load()->unregisterSocketNotifier(this); executed 49361 times by 359 tests:  d->threadData->eventDispatcher.load()->unregisterSocketNotifier(this);Executed by: 
  | 49361 | ||||||||||||
| 247 | } | - | ||||||||||||
| 248 | - | |||||||||||||
| 249 | - | |||||||||||||
| 250 | /*!\reimp | - | ||||||||||||
| 251 | */ | - | ||||||||||||
| 252 | bool QSocketNotifier::event(QEvent *e) | - | ||||||||||||
| 253 | { | - | ||||||||||||
| 254 | Q_D(QSocketNotifier); | - | ||||||||||||
| 255 | // Emits the activated() signal when a QEvent::SockAct or QEvent::SockClose is | - | ||||||||||||
| 256 | // received. | - | ||||||||||||
| 257 |     if (e->type() == QEvent::ThreadChange) {
  | 35-5888 | ||||||||||||
| 258 |         if (d->snenabled) {
  | 4-31 | ||||||||||||
| 259 | QMetaObject::invokeMethod(this, "setEnabled", Qt::QueuedConnection, | - | ||||||||||||
| 260 | Q_ARG(bool, d->snenabled)); | - | ||||||||||||
| 261 | setEnabled(false); | - | ||||||||||||
| 262 |         } executed 31 times by 3 tests:  end of blockExecuted by: 
  | 31 | ||||||||||||
| 263 |     } executed 35 times by 3 tests:  end of blockExecuted by: 
  | 35 | ||||||||||||
| 264 | QObject::event(e); // will activate filters | - | ||||||||||||
| 265 |     if ((e->type() == QEvent::SockAct) || (e->type() == QEvent::SockClose)) {
 
  | 0-5763 | ||||||||||||
| 266 | emit activated(d->sockfd, QPrivateSignal()); | - | ||||||||||||
| 267 |         return true; executed 5763 times by 290 tests:  return true;Executed by: 
  | 5763 | ||||||||||||
| 268 | } | - | ||||||||||||
| 269 |     return false; executed 160 times by 8 tests:  return false;Executed by: 
  | 160 | ||||||||||||
| 270 | } | - | ||||||||||||
| 271 | - | |||||||||||||
| 272 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |