From cad81495c54d295b90ab8e878f6ea3694d2ae24c Mon Sep 17 00:00:00 2001 From: tianduanrui Date: Sun, 1 Jul 2018 13:24:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7QQtProtocol=20=E5=8D=87?= =?UTF-8?q?=E7=BA=A7QQtProtocolManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/network/qqtbluetoothclient.cpp | 1 + src/network/qqtbluetoothserver.cpp | 1 + src/network/qqtnfcclient.cpp | 1 + src/network/qqtnfcserver.cpp | 1 + src/network/qqtprotocol.h | 15 ++++++++++++--- src/network/qqtprotocolmanager.cpp | 26 ++++++++++++++++++++++++++ src/network/qqtprotocolmanager.h | 19 +++++++++++++++++++ src/network/qqttcpclient.cpp | 1 + src/network/qqttcpserver.cpp | 1 + src/network/qqtudpclient.cpp | 1 + src/network/qqtudpserver.cpp | 1 + src/network/qqtwebsocketclient.cpp | 1 + src/network/qqtwebsocketserver.cpp | 1 + 13 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/network/qqtbluetoothclient.cpp b/src/network/qqtbluetoothclient.cpp index a5662f07..176470e0 100644 --- a/src/network/qqtbluetoothclient.cpp +++ b/src/network/qqtbluetoothclient.cpp @@ -49,6 +49,7 @@ void QQtBluetoothClient::installProtocol ( QQtProtocol* stack ) connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ), this, SLOT ( slotWriteData ( const QByteArray& ) ) ); m_protocol->attach(); + m_protocol->initializer(); } void QQtBluetoothClient::uninstallProtocol ( QQtProtocol* stack ) diff --git a/src/network/qqtbluetoothserver.cpp b/src/network/qqtbluetoothserver.cpp index 2a54a74a..ed9421b0 100644 --- a/src/network/qqtbluetoothserver.cpp +++ b/src/network/qqtbluetoothserver.cpp @@ -45,6 +45,7 @@ void QQtBluetoothServer::clientSocketDisConnected() clientSocket->uninstallProtocol ( protocol ); clientSocket->deleteLater(); m_clientList.removeOne ( clientSocket ); + m_protocolManager->deleteProtocol ( protocol ); } void QQtBluetoothServer::installProtocolManager ( QQtProtocolManager* stackGroup ) diff --git a/src/network/qqtnfcclient.cpp b/src/network/qqtnfcclient.cpp index 51dfc705..8890b8c7 100644 --- a/src/network/qqtnfcclient.cpp +++ b/src/network/qqtnfcclient.cpp @@ -21,6 +21,7 @@ void QQtNfcClient::installProtocol ( QQtProtocol* stack ) connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ), this, SLOT ( slotWriteData ( const QByteArray& ) ) ); m_protocol->attach(); + m_protocol->initializer(); } void QQtNfcClient::uninstallProtocol ( QQtProtocol* stack ) diff --git a/src/network/qqtnfcserver.cpp b/src/network/qqtnfcserver.cpp index c50024e0..fd835a86 100644 --- a/src/network/qqtnfcserver.cpp +++ b/src/network/qqtnfcserver.cpp @@ -71,6 +71,7 @@ void QQtNfcServer::hasTargetLosted ( QNearFieldTarget* target ) clientSocket->uninstallProtocol ( protocol ); clientSocket->deleteLater(); m_clientList.removeOne ( clientSocket ); + m_protocolManager->deleteProtocol ( protocol ); } QQtNfcClient* QQtNfcServer::findInstanceByUid ( QByteArray uid ) diff --git a/src/network/qqtprotocol.h b/src/network/qqtprotocol.h index 081cf3e1..cb9a8dbd 100644 --- a/src/network/qqtprotocol.h +++ b/src/network/qqtprotocol.h @@ -83,14 +83,14 @@ signals: /** * 如果Socket和这个Protocol关联,就会设置关联。 - * 如果用户需要对Protocol初始化和清理,可以从这里override。 + * 表示是否在使用中。 */ public: - virtual void detach() { + inline virtual void detach() { mIsDetached = true; statusChanged ( 0 ); } - virtual void attach() { + inline virtual void attach() { mIsDetached = false; statusChanged ( 1 ); } @@ -99,6 +99,15 @@ public: protected: bool mIsDetached; + /** + * 用户使用 + * optional + * reset函数 = clear = clean = init函数 + * 用于对Protocol句柄进行初始化成员变量。 + * 这个函数会在被安装到客户端句柄的时候使用,会有作用。 + */ +public: + inline virtual void initializer() {} /* * 以下函数,与用户无关。 */ diff --git a/src/network/qqtprotocolmanager.cpp b/src/network/qqtprotocolmanager.cpp index ff382c82..4bb2d689 100644 --- a/src/network/qqtprotocolmanager.cpp +++ b/src/network/qqtprotocolmanager.cpp @@ -28,8 +28,33 @@ QQtProtocol* QQtProtocolManager::createProtocol() return p0; } +void QQtProtocolManager::deleteProtocol ( QQtProtocol* stack ) +{ + //决定不删除句柄 + return; + + if ( m_protocol_list.isEmpty() ) + return; + + QQtProtocol* p0 = stack; + + if ( !p0 ) + return; + + disconnect ( p0, SIGNAL ( notifyToProtocolManager ( const QQtProtocol*, const QQtMessage* ) ), + this, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ) ); + + m_protocol_list.removeOne ( p0 ); + p0->deleteLater(); + + return; +} + QQtProtocol* QQtProtocolManager::findDetachedInstance() { + //但凡attached就是在使用的。createProtocol,attach. + //但凡deattached的都是准备删除的。deattach,deleteProtocol. + //初始deattached的呢?不会删除。删除操作发生在deattached阶段。 QListIterator itor0 ( m_protocol_list ); int index = 0; while ( itor0.hasNext() ) @@ -39,6 +64,7 @@ QQtProtocol* QQtProtocolManager::findDetachedInstance() index++; } emit remanentProtocolChanged ( index ); + emit remainingProtocolChanged ( index ); QListIterator itor ( m_protocol_list ); while ( itor.hasNext() ) diff --git a/src/network/qqtprotocolmanager.h b/src/network/qqtprotocolmanager.h index bdf2d764..3f948f96 100644 --- a/src/network/qqtprotocolmanager.h +++ b/src/network/qqtprotocolmanager.h @@ -88,6 +88,7 @@ signals: //内部每次协议句柄列表发生变动都会发射这个信号。 //剩余的可用句柄数目 void remanentProtocolChanged ( int num ); + void remainingProtocolChanged ( int num ); /** * 以下和用户无关 @@ -95,12 +96,30 @@ signals: public: /** * @brief createProtocol + * 创建一个Protocol句柄,开始使用。 + * * 这个函数给QQtSocketServer用的,不是给用户用的。 * 用于生成用户协议对象实例。 + * * @param protocolTypeName * @return */ QQtProtocol* createProtocol (); + /** + * 使用完,删除。 + * + * Protocol的deattach和这个功能不同,它表示使用状态切换为不使用。 + * 而deleteProtocol表示,这个句柄要删除了。 + * 如果只是deattach,那么这个句柄重新被attach以后,用户把内部的变量都reset了吗?其实如果reset了,不必要删除句柄。 + * 但是,内部实现的,支持reset,在attach的时候调用。可是没有太多作用,因为,这个句柄在不用了的时候会在这里被delete掉。 + * + * 这个机制会导致初始注册了一些句柄以后,使用使用就变少了,逐渐的就用完了。所以必须频繁根据句柄数量变动进行注册。 + * 经过考虑,我决定不删除句柄,用户继承QQtProtocol句柄的时候override init函数初始化句柄,可以避免句柄逐渐变少引起的频繁注册。 + * Protocol的init函数,只需要在必要初始化句柄的时候重写。 + * + * 内部没有对QList加锁,所以不要movetothread里用。 + */ + void deleteProtocol ( QQtProtocol* stack ); protected: QQtProtocol* findDetachedInstance(); private: diff --git a/src/network/qqttcpclient.cpp b/src/network/qqttcpclient.cpp index 6cb17980..b9df64c3 100644 --- a/src/network/qqttcpclient.cpp +++ b/src/network/qqttcpclient.cpp @@ -53,6 +53,7 @@ void QQtTcpClient::installProtocol ( QQtProtocol* stack ) connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ), this, SLOT ( slotWriteData ( const QByteArray& ) ) ); m_protocol->attach(); + m_protocol->initializer(); } void QQtTcpClient::uninstallProtocol ( QQtProtocol* stack ) diff --git a/src/network/qqttcpserver.cpp b/src/network/qqttcpserver.cpp index 72545720..0286740b 100644 --- a/src/network/qqttcpserver.cpp +++ b/src/network/qqttcpserver.cpp @@ -42,6 +42,7 @@ void QQtTcpServer::clientSocketDisConnected() clientSocket->uninstallProtocol ( protocol ); clientSocket->deleteLater(); m_clientList.removeOne ( clientSocket ); + m_protocolManager->deleteProtocol ( protocol ); } void QQtTcpServer::comingNewConnection() diff --git a/src/network/qqtudpclient.cpp b/src/network/qqtudpclient.cpp index d6a72d58..61f28546 100644 --- a/src/network/qqtudpclient.cpp +++ b/src/network/qqtudpclient.cpp @@ -34,6 +34,7 @@ void QQtUdpClient::installProtocol ( QQtProtocol* stack ) connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ), this, SLOT ( slotWriteData ( const QByteArray& ) ) ); m_protocol->attach(); + m_protocol->initializer(); } void QQtUdpClient::uninstallProtocol ( QQtProtocol* stack ) diff --git a/src/network/qqtudpserver.cpp b/src/network/qqtudpserver.cpp index 9644efdd..3fa3b2fc 100644 --- a/src/network/qqtudpserver.cpp +++ b/src/network/qqtudpserver.cpp @@ -37,6 +37,7 @@ void QQtUdpServer::clientSocketDisConnected() clientSocket->uninstallProtocol ( protocol ); clientSocket->deleteLater(); m_clientList.removeOne ( clientSocket ); + m_protocolManager->deleteProtocol ( protocol ); } QQtUdpClient* QQtUdpServer::findClientByProtocolInstance ( QQtProtocol* protocol ) diff --git a/src/network/qqtwebsocketclient.cpp b/src/network/qqtwebsocketclient.cpp index 404a768c..0778fe04 100644 --- a/src/network/qqtwebsocketclient.cpp +++ b/src/network/qqtwebsocketclient.cpp @@ -16,6 +16,7 @@ void QQtWebSocketClient::installProtocol ( QQtProtocol* stack ) connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ), this, SLOT ( slotWriteData ( const QByteArray& ) ) ); m_protocol->attach(); + m_protocol->initializer(); } void QQtWebSocketClient::uninstallProtocol ( QQtProtocol* stack ) diff --git a/src/network/qqtwebsocketserver.cpp b/src/network/qqtwebsocketserver.cpp index bf3efc5d..2dfe2ae1 100644 --- a/src/network/qqtwebsocketserver.cpp +++ b/src/network/qqtwebsocketserver.cpp @@ -86,6 +86,7 @@ void QQtWebSocketServer::clientSocketDisConnected() clientSocket->uninstallProtocol ( protocol ); clientSocket->deleteLater(); m_clientList.removeOne ( clientSocket ); + m_protocolManager->deleteProtocol ( protocol ); } QQtProtocolManager* QQtWebSocketServer::installedProtocolManager()