1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

update QQt Protocol and Protocol Manager

This commit is contained in:
tianduanrui 2018-04-29 14:44:20 +08:00
parent e036cdc00b
commit 1e7140f2c9
9 changed files with 96 additions and 27 deletions

View File

@ -1,4 +1,4 @@
#include "qqtbluetoothclient.h"
#include "qqtbluetoothclient.h"
#include <qqtcore.h>
QQtBluetoothClient::QQtBluetoothClient ( QBluetoothServiceInfo::Protocol socketType, QObject* parent ) :
@ -44,9 +44,10 @@ void QQtBluetoothClient::installProtocol ( QQtProtocol* stack )
if ( m_protocol )
return;
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
connect ( stack, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
stack->attach();
m_protocol = stack;
}
void QQtBluetoothClient::uninstallProtocol ( QQtProtocol* stack )
@ -58,6 +59,7 @@ void QQtBluetoothClient::uninstallProtocol ( QQtProtocol* stack )
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol->detach();
m_protocol = NULL;
}

View File

@ -19,7 +19,7 @@ class QQTSHARED_EXPORT QQtProtocol : public QObject
Q_OBJECT
public:
Q_INVOKABLE explicit QQtProtocol ( QObject* parent = 0 ) : QObject ( parent ) {
detach();
}
virtual ~QQtProtocol() {
@ -59,6 +59,9 @@ public:
*/
inline virtual bool dispatcher ( const QByteArray& ) { return 0; }
/*
*
*/
signals:
/**
* @brief notifyToProtocolManager
@ -72,6 +75,15 @@ signals:
* @param message
*/
void notifyToProtocolManager ( const QQtProtocol* self, const QQtMessage* message );
/**
* Socket和这个Protocol关联
*/
public:
void detach() { mIsDetached = true; }
void attach() { mIsDetached = false; }
bool detached() { return mIsDetached; }
private:
bool mIsDetached;
};
#endif // QQTPROTOCOL_H

View File

@ -24,13 +24,15 @@
*
* Protocl
* Protocol保存在句柄内部
*
*
* 1024
*/
class QQTSHARED_EXPORT QQtProtocolManager : public QObject
{
Q_OBJECT
public:
explicit QQtProtocolManager ( QObject* parent = 0 ) : QObject ( parent ) {
mProtocol = NULL;
}
virtual ~QQtProtocolManager() {
@ -65,9 +67,10 @@ public:
*/
template <typename T>
bool registerProtocol () {
if ( mProtocol )
return false;
mProtocol = new T ( this );
for ( int i = 0; i < 1024; i++ ) {
QQtProtocol* p0 = new T ( this );
m_protocol_list.push_back ( p0 );
}
return true;
}
@ -83,20 +86,34 @@ public:
* @return
*/
QQtProtocol* createProtocol () {
//如果不能生成,根本不返回,而是崩溃。
if ( !mProtocol )
if ( m_protocol_list.isEmpty() )
return NULL;
pmeta ( mProtocol );
QQtProtocol* p0 = ( QQtProtocol* ) mProtocol->staticMetaObject.newInstance ( Q_ARG ( QQtProtocolManager*, this ) );
//无论如何,使用对象工厂也一样,都不能正确生成。对象工厂崩溃退出。
//QQtProtocol* p0 = ( QQtProtocol* ) mProtocol->metaObject()->newInstance ( Q_ARG(QQtProtocolManager*, this) );
QQtProtocol* p0 = findDetachedInstance();
if ( p0 == 0 )
return NULL;
pmeta ( p0 ) << p0;
//帮助Protocol给用户发数据。
connect ( p0, SIGNAL ( notifyToProtocolManager ( const QQtProtocol*, const QQtMessage* ) ),
this, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ) );
return p0;
}
protected:
QQtProtocol* findDetachedInstance() {
QListIterator<QQtProtocol*> itor ( m_protocol_list );
while ( itor.hasNext() ) {
QQtProtocol* p = itor.next();
if ( p->detached() )
return p;
}
return NULL;
}
private:
QQtProtocol* mProtocol;
QList<QQtProtocol*> m_protocol_list;
};
#endif // QQTPROTOCOLMANAGER_H

View File

@ -1,4 +1,4 @@
#include "qqtserialport.h"
#include "qqtserialport.h"
#include "qqtcore.h"
QQtSerialPort::QQtSerialPort ( QObject* parent ) : QSerialPort ( parent )
@ -20,9 +20,10 @@ void QQtSerialPort::installProtocol ( QQtProtocol* stack )
if ( m_protocol )
return;
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
connect ( stack, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
stack->attach();
m_protocol = stack;
}
void QQtSerialPort::uninstallProtocol ( QQtProtocol* stack )
@ -34,6 +35,7 @@ void QQtSerialPort::uninstallProtocol ( QQtProtocol* stack )
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol->detach();
m_protocol = NULL;
}

View File

@ -1,4 +1,4 @@
#include "qqttcpclient.h"
#include "qqttcpclient.h"
#include <QTcpSocket>
#include <QHostInfo>
@ -49,9 +49,10 @@ void QQtTcpClient::installProtocol ( QQtProtocol* stack )
if ( m_protocol )
return;
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
connect ( stack, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
stack->attach();
m_protocol = stack;
}
void QQtTcpClient::uninstallProtocol ( QQtProtocol* stack )
@ -63,6 +64,7 @@ void QQtTcpClient::uninstallProtocol ( QQtProtocol* stack )
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol->detach();
m_protocol = NULL;
}

View File

@ -1,8 +1,11 @@
#include "qqttcpserver.h"
#include "qqttcpserver.h"
QQtTcpServer::QQtTcpServer ( QObject* parent ) :
QTcpServer ( parent )
{
connect ( this, SIGNAL ( newConnection() ),
this, SLOT ( comingNewConnection() ),
Qt::QueuedConnection );
m_protocolManager = NULL;
}
@ -14,6 +17,7 @@ QQtTcpServer::~QQtTcpServer()
void QQtTcpServer::incomingConnection ( qintptr handle )
{
return QTcpServer::incomingConnection ( handle );
QQtTcpClient* clientSocket = new QQtTcpClient ( this );
clientSocket->setSocketDescriptor ( handle );
if ( !m_protocolManager )
@ -41,6 +45,30 @@ void QQtTcpServer::clientSocketDisConnected()
m_clientList.removeOne ( clientSocket );
}
void QQtTcpServer::comingNewConnection()
{
while ( hasPendingConnections() )
{
QTcpSocket* comingSocket = nextPendingConnection();
if ( !m_protocolManager )
{
pline() << "please install protocol manager for your server.";
comingSocket->deleteLater();
return;
}
QQtTcpClient* clientSocket = new QQtTcpClient ( this );
clientSocket->setSocketDescriptor ( comingSocket->socketDescriptor() );
connect ( clientSocket, SIGNAL ( disconnected() ),
this, SLOT ( clientSocketDisConnected() ) );
//如果崩溃,对这个操作进行加锁。
QQtProtocol* protocol = m_protocolManager->createProtocol();
clientSocket->installProtocol ( protocol );
m_clientList.push_back ( clientSocket );
}
}
void QQtTcpServer::installProtocolManager ( QQtProtocolManager* stackGroup )
{
if ( m_protocolManager )

View File

@ -1,4 +1,4 @@
#ifndef QQTTCPSERVER_H
#ifndef QQTTCPSERVER_H
#define QQTTCPSERVER_H
#include <QTcpServer>
@ -32,6 +32,8 @@ protected:
virtual void incomingConnection ( qintptr handle ) override;
public slots:
void clientSocketDisConnected();
private slots:
void comingNewConnection();
private:
QQtProtocolManager* m_protocolManager;
QList<QQtTcpClient*> m_clientList;

View File

@ -1,4 +1,4 @@
#include "qqtudpclient.h"
#include "qqtudpclient.h"
QQtUdpClient::QQtUdpClient ( QObject* parent ) : QUdpSocket ( parent )
{
@ -28,9 +28,10 @@ void QQtUdpClient::installProtocol ( QQtProtocol* stack )
if ( m_protocol )
return;
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
connect ( stack, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
stack->attach();
m_protocol = stack;
}
void QQtUdpClient::uninstallProtocol ( QQtProtocol* stack )
@ -42,6 +43,7 @@ void QQtUdpClient::uninstallProtocol ( QQtProtocol* stack )
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol->detach();
m_protocol = NULL;
}

View File

@ -1,4 +1,4 @@
#include "qqtwebsocketclient.h"
#include "qqtwebsocketclient.h"
QQtWebSocketClient::QQtWebSocketClient ( QObject* parent, const QString& origin, QWebSocketProtocol::Version version ) :
QWebSocket ( origin, version, parent )
@ -12,9 +12,10 @@ void QQtWebSocketClient::installProtocol ( QQtProtocol* stack )
if ( m_protocol )
return;
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
connect ( stack, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
stack->attach();
m_protocol = stack;
}
void QQtWebSocketClient::uninstallProtocol ( QQtProtocol* stack )
@ -26,6 +27,7 @@ void QQtWebSocketClient::uninstallProtocol ( QQtProtocol* stack )
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( slotWriteData ( const QByteArray& ) ) );
m_protocol->detach();
m_protocol = NULL;
}