mirror of
https://gitee.com/drabel/LibQQt.git
synced 2025-01-04 10:18:44 +08:00
升级QQtProtocol
升级QQtProtocolManager
This commit is contained in:
parent
255aacb3b6
commit
cad81495c5
@ -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 )
|
||||
|
@ -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 )
|
||||
|
@ -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 )
|
||||
|
@ -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 )
|
||||
|
@ -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() {}
|
||||
/*
|
||||
* 以下函数,与用户无关。
|
||||
*/
|
||||
|
@ -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<QQtProtocol*> 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<QQtProtocol*> itor ( m_protocol_list );
|
||||
while ( itor.hasNext() )
|
||||
|
@ -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:
|
||||
|
@ -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 )
|
||||
|
@ -42,6 +42,7 @@ void QQtTcpServer::clientSocketDisConnected()
|
||||
clientSocket->uninstallProtocol ( protocol );
|
||||
clientSocket->deleteLater();
|
||||
m_clientList.removeOne ( clientSocket );
|
||||
m_protocolManager->deleteProtocol ( protocol );
|
||||
}
|
||||
|
||||
void QQtTcpServer::comingNewConnection()
|
||||
|
@ -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 )
|
||||
|
@ -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 )
|
||||
|
@ -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 )
|
||||
|
@ -86,6 +86,7 @@ void QQtWebSocketServer::clientSocketDisConnected()
|
||||
clientSocket->uninstallProtocol ( protocol );
|
||||
clientSocket->deleteLater();
|
||||
m_clientList.removeOne ( clientSocket );
|
||||
m_protocolManager->deleteProtocol ( protocol );
|
||||
}
|
||||
|
||||
QQtProtocolManager* QQtWebSocketServer::installedProtocolManager()
|
||||
|
Loading…
x
Reference in New Issue
Block a user