Qt之QAbstractSocketEngine

简介

QAbstractSocketEngine是网络中的基础,QTcpSocket和QTcpServer底层都依赖socket引擎

结构

QAbstractSocketEngine
+QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent)
+QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent)
+void readNotification()
+void writeNotification()
+void closeNotification()
+void exceptionNotification()
+void connectionNotification()
QAbstractSocketEnginePrivate
+QAbstractSocketEngineReceiver *receiver
«Abstract»
QAbstractSocketEngineReceiver
+void readNotification()
+void writeNotification()
+void closeNotification()
+void exceptionNotification()
+void connectionNotification()
+void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
QTcpServerPrivate
QNativeSocketEngine
QNativeSocketEnginePrivate
+QSocketNotifier *readNotifier
+QSocketNotifier *writeNotifier
+QSocketNotifier *exceptNotifier
QReadNotifier
QWriteNotifier
QExceptionNotifier
«Abstract»
QSocketEngineHandler
+QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,const QNetworkProxy &, QObject *parent)
+QAbstractSocketEngine *createSocketEngine(qintptr socketDescriptor, QObject *parent)
QAbstractSocketPrivate
QSocks5SocketEngineHandler
QSocketNotifier

QAbstractSocketEngineReceiver :纯虚类
QSocketEngineHandler:在qt中会定义全局变量来存所有的QSocketEngineHandler

class QSocketEngineHandlerList : public QList<QSocketEngineHandler*>
{
public:
    QMutex mutex;
};

Q_GLOBAL_STATIC(QSocketEngineHandlerList, socketHandlers)

QAbstractSocketEngine :静态方法createSocketEngine会使用全局变量socketHandlers来创建socket引擎,如果全局变量中没有创建引擎,则默认创建QNativeSocketEngine

QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &proxy, QObject *parent)
{
#ifndef QT_NO_NETWORKPROXY
    // proxy type must have been resolved by now
    if (proxy.type() == QNetworkProxy::DefaultProxy)
        return 0;
#endif

    QMutexLocker locker(&socketHandlers()->mutex);
    for (int i = 0; i < socketHandlers()->size(); i++) {
        if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketType, proxy, parent))
            return ret;
    }

#ifndef QT_NO_NETWORKPROXY
    // only NoProxy can have reached here
    if (proxy.type() != QNetworkProxy::NoProxy)
        return 0;
#endif

    return new QNativeSocketEngine(parent);
}

QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(qintptr socketDescripter, QObject *parent)
{
    QMutexLocker locker(&socketHandlers()->mutex);
    for (int i = 0; i < socketHandlers()->size(); i++) {
        if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketDescripter, parent))
            return ret;
    }
    return new QNativeSocketEngine(parent);
}

读写异常通知是槽函数,其内部是调用QAbstractSocketEngineReceiver的方法

public Q_SLOTS:
    void readNotification();
    void writeNotification();
    void closeNotification();
    void exceptionNotification();
    void connectionNotification();

void QAbstractSocketEngine::readNotification()
{
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
        receiver->readNotification();
}

void QAbstractSocketEngine::writeNotification()
{
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
        receiver->writeNotification();
}

void QAbstractSocketEngine::exceptionNotification()
{
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
        receiver->exceptionNotification();
}

void QAbstractSocketEngine::closeNotification()
{
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
        receiver->closeNotification();
}

void QAbstractSocketEngine::connectionNotification()
{
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
        receiver->connectionNotification();
}

读写异常通知类

  • QReadNotifier
  • QWriteNotifier
  • QExceptionNotifier
    其事件处理中是调用QAbstractSocketEngine的通知方法
bool QReadNotifier::event(QEvent *e)
{
    if (e->type() == QEvent::SockAct) {
        engine->readNotification();
        return true;
    } else if (e->type() == QEvent::SockClose) {
        engine->closeNotification();
        return true;
    }
    return QSocketNotifier::event(e);
}

bool QWriteNotifier::event(QEvent *e)
{
    if (e->type() == QEvent::SockAct) {
        if (engine->state() == QAbstractSocket::ConnectingState)
            engine->connectionNotification();
        else
            engine->writeNotification();
        return true;
    }
    return QSocketNotifier::event(e);
}

bool QExceptionNotifier::event(QEvent *e)
{
    if (e->type() == QEvent::SockAct) {
        if (engine->state() == QAbstractSocket::ConnectingState)
            engine->connectionNotification();
        else
            engine->exceptionNotification();
        return true;
    }
    return QSocketNotifier::event(e);
}
  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值