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

相关推荐
大师兄666813 分钟前
Qt-for-鸿蒙PC-Electron应用鸿蒙平台白屏问题修复实战
qt·electron·harmonyos
牢七1 小时前
12345W
网络
小刘摸鱼中2 小时前
高频电子电路-振荡器的频率稳定度
网络·人工智能
00后程序员张2 小时前
全面解析网络抓包工具使用:Wireshark和TCPDUMP教程
网络·ios·小程序·uni-app·wireshark·iphone·tcpdump
濊繵3 小时前
Linux网络--应用层自定义协议与序列化
linux·服务器·网络
沫儿笙3 小时前
镀锌板焊接中库卡机器人是如何省气的
网络·人工智能·机器人
网安小白的进阶之路4 小时前
B模块 安全通信网络 第一门课 园区网实现与安全-2-ACL
网络·安全
爬山算法4 小时前
Redis(135)Redis的网络模型是什么?
网络·数据库·redis
海域云-罗鹏4 小时前
电商掘金日本:SDWAN专线刚需原因
服务器·网络·网络协议
雲烟4 小时前
Qt SQLite在I.mx8上使用问题
数据库·qt·i.mx8