qt网络事件之QSocketNotifier

简介

QSocketNotifier用于处理网络事件的,即事件处理器

结构

QSocketNotifier +qintptr socket() +Type type() +bool isEnabled() +void setEnabled(bool) +void activated(int socket, QPrivateSignal) #bool event(QEvent *) QSocketNotifierPrivate +qintptr sockfd +QSocketNotifier::Type sntype +bool snenabled

qintptr socket():表示对应的网络套接字

Type type():表示关注的事件类型,使用枚举表示

cpp 复制代码
enum Type { Read, Write, Exception }

bool isEnabled():表示对应的是否注册到事件分发顺器中

void setEnabled(bool):是槽函数,表示是否将套接字注册到事件分发器中

cpp 复制代码
void QSocketNotifier::setEnabled(bool enable)
{
    Q_D(QSocketNotifier);
    if (d->sockfd < 0)
        return;
    if (d->snenabled == enable)                        
        return;
    d->snenabled = enable;

    if (!d->threadData->hasEventDispatcher())
        return;
    if (Q_UNLIKELY(thread() != QThread::currentThread())) {
        return;
    }
    if (d->snenabled)
        d->threadData->eventDispatcher.loadRelaxed()->registerSocketNotifier(this);
    else
        d->threadData->eventDispatcher.loadRelaxed()->unregisterSocketNotifier(this);
}

void activated(int socket, QPrivateSignal):信号,在event事件处理函数中,如果事件类型为QEvent::SockAct或者QEvent::SockClose,会触发该信号

bool event(QEvent *):事件处理函数

cpp 复制代码
bool QSocketNotifier::event(QEvent *e)
{
    Q_D(QSocketNotifier);
    // Emits the activated() signal when a QEvent::SockAct or QEvent::SockClose is
    // received.
    if (e->type() == QEvent::ThreadChange) {
        if (d->snenabled) {
            QMetaObject::invokeMethod(this, "setEnabled", Qt::QueuedConnection,
                                      Q_ARG(bool, d->snenabled));
            setEnabled(false);
        }
    }
    QObject::event(e);                        // will activate filters
    if ((e->type() == QEvent::SockAct) || (e->type() == QEvent::SockClose)) {
        emit activated(d->sockfd, QPrivateSignal());
        return true;
    }
    return false;
}

触发网络事件

QEventDispatcherUNIX QEventDispatcherUNIXPrivate QSocketNotifier processEvents qt_safe_poll activateSocketNotifiers event emit activated QEventDispatcherUNIX QEventDispatcherUNIXPrivate QSocketNotifier

相关推荐
未来之窗软件服务1 小时前
自己平台接入国家网络身份认证公共服务接入
网络·仙盟创梦ide·东方仙盟
要做一个小太阳1 小时前
华为Atlas 900 A3 SuperPoD 超节点网络架构
运维·服务器·网络·华为·架构
vx-bot5556661 小时前
企业微信接口在混合云环境下的集成架构与网络互联方案企业微信接口在混合云环境下的集成架构与网络互联方案
网络·架构·企业微信
蒸蒸yyyyzwd1 小时前
c网络编程学习笔记
c语言·网络·学习
时艰.2 小时前
Java 并发编程:Callable、Future 与 CompletableFuture
java·网络
创客小邓2 小时前
Qt对SQLite数据库的操作
数据库·qt·sqlite
强风7942 小时前
Linux—应用层自定义协议与序列化
运维·服务器·网络
小小码农Come on2 小时前
QT布局介绍
开发语言·qt
云中飞鸿2 小时前
QTCreator error: C3861: “_mm_loadu_si64”: 找不到标识符
qt
科技块儿2 小时前
如何定期向监管部门报送IP属地统计报表?
网络·网络协议·tcp/ip