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

相关推荐
ssswywywht44 分钟前
OSPF实验
网络
FCM661 小时前
HCIA第一次实验报告:静态路由综合实验
网络·tcp/ip·信息与通信
apihz1 小时前
VM虚拟机全版本网盘+免费本地网络穿透端口映射实时同步动态家庭IP教程
android·服务器·开发语言·网络·数据库·网络协议·tcp/ip
dog2502 小时前
TCP 传输时 sk_buff 的 clone 和 unclone
网络·网络协议·tcp/ip
学习溢出2 小时前
【网络安全】理解安全事件的“三分法”流程:应对警报的第一道防线
网络·安全·web安全·网络安全·ids
智慧化智能化数字化方案3 小时前
华为IPD(集成产品开发)流程是其研发管理的核心体系
网络·华为ipd流程·ipd流程体系·ipd产品研发
云空4 小时前
《QtPy:Python与Qt的完美桥梁》
开发语言·python·qt·pyqt
kfepiza4 小时前
Linux的NetworkManager的nmcli配置网桥(bridge) 笔记250712
linux·运维·网络·笔记·tcp/ip·ip·tcp
墨月白4 小时前
【QT】多线程相关教程
数据库·qt
cui_win4 小时前
【网络】Linux 内核优化实战 - net.netfilter.nf_conntrack_buckets
linux·网络·.net