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

相关推荐
平生不喜凡桃李5 小时前
Linux网络:UDP
linux·网络·udp
weixiao04305 小时前
Linux网络 网络层
linux·网络·智能路由器
静若繁花_jingjing5 小时前
面试_项目问题_RPC调用异常
网络·网络协议·rpc
せいしゅん青春之我6 小时前
[JavaEE初阶] 防止网络传输中的中间人入侵---证书
服务器·网络·网络协议·java-ee
友友马6 小时前
『 QT 』QT控件属性全解析 (一)
开发语言·前端·qt
RTC老炮7 小时前
webrtc弱网-ReceiveSideCongestionController类源码分析及算法原理
网络·算法·webrtc
python百炼成钢8 小时前
3.Linux 网络相关
linux·运维·网络·stm32·单片机
2503_930123938 小时前
Kubernetes (四)网络插件详解:Flannel 与 Calico 的原理、数据流向与实战对比
网络·容器·kubernetes
星哥说事9 小时前
网络安全设备:入侵检测系统(IDS)、入侵防御系统(IPS)的配置与使用
网络·安全·web安全
问道飞鱼9 小时前
【HTTP知识】HTTP OPTIONS 预检请求深度解析与优化策略
网络·网络协议·http·option·预检