简介
QAbstractSocketEngine是网络中的基础,QTcpSocket和QTcpServer底层都依赖socket引擎
结构
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 nowif (proxy.type() == QNetworkProxy::DefaultProxy)return 0;
#endifQMutexLocker 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 hereif (proxy.type() != QNetworkProxy::NoProxy)return 0;
#endifreturn 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();elseengine->writeNotification();return true;}return QSocketNotifier::event(e);
}bool QExceptionNotifier::event(QEvent *e)
{if (e->type() == QEvent::SockAct) {if (engine->state() == QAbstractSocket::ConnectingState)engine->connectionNotification();elseengine->exceptionNotification();return true;}return QSocketNotifier::event(e);
}