mirror of
https://gitee.com/drabel/LibQQt.git
synced 2025-01-04 10:18:44 +08:00
update qqt socket server
This commit is contained in:
parent
15104ce7c2
commit
d700db3bbb
@ -11,7 +11,6 @@ int main ( int argc, char* argv[] )
|
||||
//测试一下,数据字段不够,内部如何处理。
|
||||
QQtSubProtocolTest* p = new QQtSubProtocolTest();
|
||||
QByteArray bytes = "CCCCCCCC";
|
||||
p->translator ( bytes );
|
||||
|
||||
//这个例程里,讲解了QQt Application Framework里network module的功能。
|
||||
//这是一个例子,
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include "qqtbluetoothserver.h"
|
||||
#include "qqtnetwork.h"
|
||||
#include "qqtbluetoothclient.h"
|
||||
|
||||
QQtBluetoothServer::QQtBluetoothServer ( QBluetoothServiceInfo::Protocol serverType,
|
||||
@ -17,35 +16,88 @@ QQtBluetoothServer::~QQtBluetoothServer()
|
||||
|
||||
void QQtBluetoothServer::comingNewConnection()
|
||||
{
|
||||
if ( !hasPendingConnections() )
|
||||
while ( hasPendingConnections() )
|
||||
{
|
||||
QBluetoothSocket* comingSocket = nextPendingConnection();
|
||||
|
||||
if ( !m_protocolManager )
|
||||
{
|
||||
pline() << "please install protocol manager for your server.";
|
||||
comingSocket->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
QQtBluetoothClient* clientSocket = new QQtBluetoothClient ( this );
|
||||
connect ( clientSocket, SIGNAL ( disconnected() ), this, SLOT ( clientSocketDisConnected() ) );
|
||||
//如果崩溃,对这个操作进行加锁。
|
||||
QQtProtocol* protocol = m_protocolManager->createProtocol();
|
||||
clientSocket->installProtocol ( protocol );
|
||||
clientSocket->setSocketDescriptor ( comingSocket->socketDescriptor(), QBluetoothServiceInfo::RfcommProtocol );
|
||||
m_clientList.push_back ( clientSocket );
|
||||
}
|
||||
}
|
||||
|
||||
void QQtBluetoothServer::clientSocketDisConnected()
|
||||
{
|
||||
QObject* obj = sender();
|
||||
QQtBluetoothClient* clientSocket = ( QQtBluetoothClient* ) obj;
|
||||
QQtProtocol* protocol = clientSocket->installedProtocol();
|
||||
clientSocket->uninstallProtocol ( protocol );
|
||||
clientSocket->deleteLater();
|
||||
protocol->deleteLater();
|
||||
m_clientList.removeOne ( clientSocket );
|
||||
}
|
||||
|
||||
void QQtBluetoothServer::installProtocolManager ( QQtProtocolManager* stackGroup )
|
||||
{
|
||||
if ( m_protocolManager )
|
||||
return;
|
||||
|
||||
QBluetoothSocket* comingSocket = nextPendingConnection();
|
||||
QQtBluetoothClient* clientSocket = new QQtBluetoothClient ( this );
|
||||
connect ( clientSocket, SIGNAL ( disconnected() ), clientSocket, SLOT ( deleteLater() ) );
|
||||
clientSocket->installProtocol ( m_protocol );
|
||||
clientSocket->setSocketDescriptor ( comingSocket->socketDescriptor(), QBluetoothServiceInfo::RfcommProtocol );
|
||||
m_protocolManager = stackGroup;
|
||||
}
|
||||
|
||||
void QQtBluetoothServer::installProtocol ( QQtProtocol* stack )
|
||||
void QQtBluetoothServer::uninstallProtocolManager ( QQtProtocolManager* stackGroup )
|
||||
{
|
||||
if ( m_protocol )
|
||||
Q_UNUSED ( stackGroup )
|
||||
|
||||
if ( !m_protocolManager )
|
||||
return;
|
||||
|
||||
m_protocol = stack;
|
||||
m_protocolManager = NULL;
|
||||
}
|
||||
|
||||
void QQtBluetoothServer::uninstallProtocol ( QQtProtocol* stack )
|
||||
QQtProtocolManager* QQtBluetoothServer::installedProtocolManager()
|
||||
{
|
||||
Q_UNUSED ( stack )
|
||||
|
||||
if ( !m_protocol )
|
||||
return;
|
||||
|
||||
m_protocol = NULL;
|
||||
return m_protocolManager;
|
||||
}
|
||||
|
||||
QQtProtocol* QQtBluetoothServer::installedProtocol()
|
||||
QQtBluetoothClient* QQtBluetoothServer::findClientByProtocolInstance ( QQtProtocol* protocol )
|
||||
{
|
||||
return m_protocol;
|
||||
QListIterator<QQtBluetoothClient*> itor ( m_clientList );
|
||||
while ( itor.hasNext() )
|
||||
{
|
||||
QQtBluetoothClient* client = itor.next();
|
||||
QQtProtocol* cprotocol = client->installedProtocol();
|
||||
if ( cprotocol == protocol )
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QQtBluetoothClient* QQtBluetoothServer::findClientByIPAddress ( QString ip, quint16 port )
|
||||
{
|
||||
QListIterator<QQtBluetoothClient*> itor ( m_clientList );
|
||||
while ( itor.hasNext() )
|
||||
{
|
||||
QQtBluetoothClient* client = itor.next();
|
||||
QString aip = client->peerAddress().toString();
|
||||
quint16 aport = client->peerPort();
|
||||
if ( aip == ip && aport == port )
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -2,7 +2,8 @@
|
||||
#define QQTBLUETOOTHSERVER_H
|
||||
|
||||
#include <QBluetoothServer>
|
||||
#include "qqtprotocol.h"
|
||||
#include <qqtbluetoothclient.h>
|
||||
#include "qqtprotocolmanager.h"
|
||||
#include <qqt-local.h>
|
||||
|
||||
class QQTSHARED_EXPORT QQtBluetoothServer : public QBluetoothServer
|
||||
@ -12,9 +13,14 @@ public:
|
||||
explicit QQtBluetoothServer ( QBluetoothServiceInfo::Protocol serverType, QObject* parent = nullptr );
|
||||
~QQtBluetoothServer();
|
||||
|
||||
void installProtocol ( QQtProtocol* stack );
|
||||
void uninstallProtocol ( QQtProtocol* stack );
|
||||
QQtProtocol* installedProtocol();
|
||||
void installProtocolManager ( QQtProtocolManager* stackGroup );
|
||||
void uninstallProtocolManager ( QQtProtocolManager* stackGroup );
|
||||
QQtProtocolManager* installedProtocolManager();
|
||||
|
||||
public:
|
||||
QQtBluetoothClient* findClientByProtocolInstance ( QQtProtocol* protocol );
|
||||
QQtBluetoothClient* findClientByIPAddress ( QString ip, quint16 port );
|
||||
QList<QQtBluetoothClient*>& clientList() { return m_clientList; }
|
||||
|
||||
signals:
|
||||
|
||||
@ -24,9 +30,11 @@ signals:
|
||||
|
||||
private slots:
|
||||
void comingNewConnection();
|
||||
|
||||
public slots:
|
||||
void clientSocketDisConnected();
|
||||
private:
|
||||
QQtProtocol* m_protocol;
|
||||
QQtProtocolManager* m_protocolManager;
|
||||
QList<QQtBluetoothClient*> m_clientList;
|
||||
};
|
||||
|
||||
#endif // QQTBLUETOOTHSERVER_H
|
||||
|
@ -7,7 +7,6 @@
|
||||
#include <qthread.h>
|
||||
#include "qqtcore.h"
|
||||
#include "qqt-local.h"
|
||||
#include "qqtnetwork.h"
|
||||
|
||||
class QQTSHARED_EXPORT QQtNetWorkClearThread : public QThread
|
||||
{
|
||||
|
@ -95,8 +95,6 @@ public:
|
||||
return p0;
|
||||
}
|
||||
|
||||
private slots:
|
||||
void clientSocketDisConnected();
|
||||
private:
|
||||
QQtProtocol* mProtocol;
|
||||
};
|
||||
|
@ -3,9 +3,6 @@
|
||||
#include <QTcpSocket>
|
||||
#include <QHostInfo>
|
||||
|
||||
#include "qqtnetwork.h"
|
||||
#include "qqtcore.h"
|
||||
|
||||
QQtTcpClient::QQtTcpClient ( QObject* parent ) :
|
||||
QTcpSocket ( parent )
|
||||
{
|
||||
@ -145,12 +142,13 @@ void QQtTcpClient::socketStateChanged ( QAbstractSocket::SocketState eSocketStat
|
||||
break;
|
||||
|
||||
case QAbstractSocket::UnconnectedState:
|
||||
{
|
||||
eConType++;
|
||||
QSettings set;
|
||||
set.setValue ( "Network/eConType", eConType );
|
||||
set.sync();
|
||||
break;
|
||||
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ void QQtTcpServer::incomingConnection ( qintptr handle )
|
||||
//如果崩溃,对这个操作进行加锁。
|
||||
QQtProtocol* protocol = m_protocolManager->createProtocol();
|
||||
clientSocket->installProtocol ( protocol );
|
||||
m_clientList.push_back ( clientSocket );
|
||||
}
|
||||
|
||||
void QQtTcpServer::clientSocketDisConnected()
|
||||
@ -37,6 +38,7 @@ void QQtTcpServer::clientSocketDisConnected()
|
||||
clientSocket->uninstallProtocol ( protocol );
|
||||
clientSocket->deleteLater();
|
||||
protocol->deleteLater();
|
||||
m_clientList.removeOne ( clientSocket );
|
||||
}
|
||||
|
||||
void QQtTcpServer::installProtocolManager ( QQtProtocolManager* stackGroup )
|
||||
@ -61,3 +63,34 @@ QQtProtocolManager* QQtTcpServer::installedProtocolManager()
|
||||
{
|
||||
return m_protocolManager;
|
||||
}
|
||||
|
||||
QQtTcpClient* QQtTcpServer::findClientByProtocolInstance ( QQtProtocol* protocol )
|
||||
{
|
||||
QListIterator<QQtTcpClient*> itor ( m_clientList );
|
||||
while ( itor.hasNext() )
|
||||
{
|
||||
QQtTcpClient* client = itor.next();
|
||||
QQtProtocol* cprotocol = client->installedProtocol();
|
||||
if ( cprotocol == protocol )
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QQtTcpClient* QQtTcpServer::findClientByIPAddress ( QString ip, quint16 port )
|
||||
{
|
||||
QListIterator<QQtTcpClient*> itor ( m_clientList );
|
||||
while ( itor.hasNext() )
|
||||
{
|
||||
QQtTcpClient* client = itor.next();
|
||||
QString aip = client->peerAddress().toString();
|
||||
quint16 aport = client->peerPort();
|
||||
if ( aip == ip && aport == port )
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -21,14 +21,20 @@ public:
|
||||
void uninstallProtocolManager ( QQtProtocolManager* stackGroup );
|
||||
QQtProtocolManager* installedProtocolManager();
|
||||
|
||||
public:
|
||||
QQtTcpClient* findClientByProtocolInstance ( QQtProtocol* protocol );
|
||||
QQtTcpClient* findClientByIPAddress ( QString ip, quint16 port );
|
||||
QList<QQtTcpClient*>& clientList() { return m_clientList; }
|
||||
|
||||
signals:
|
||||
// QTcpServer interface
|
||||
protected:
|
||||
virtual void incomingConnection ( qintptr handle ) override;
|
||||
private:
|
||||
QQtProtocolManager* m_protocolManager;
|
||||
public slots:
|
||||
void clientSocketDisConnected();
|
||||
private:
|
||||
QQtProtocolManager* m_protocolManager;
|
||||
QList<QQtTcpClient*> m_clientList;
|
||||
};
|
||||
|
||||
|
||||
|
@ -188,7 +188,7 @@ void QQtUdpClient::slotWriteData ( const QByteArray& bytes )
|
||||
{
|
||||
#if QT_VERSION > QT_VERSION_DATAGRAM
|
||||
QNetworkDatagram datagram;
|
||||
datagram.setData ( dg );
|
||||
datagram.setData ( bytes );
|
||||
datagram.setDestination ( QHostAddress ( mIP ), mPort );
|
||||
datagram.setSender ( localAddress(), localPort() );
|
||||
writeDatagram ( datagram );
|
||||
|
@ -64,13 +64,15 @@ private slots:
|
||||
//Qt的元对象系统,解析信号和槽函数,不支持宏。
|
||||
//QtUdpSocket,writeDatagram不是个槽。
|
||||
|
||||
protected:
|
||||
public:
|
||||
/**
|
||||
* @brief translator
|
||||
* 用于拆分和分发数据报
|
||||
* @param bytes
|
||||
*/
|
||||
void translator ( const QByteArray& bytes );
|
||||
|
||||
protected:
|
||||
virtual void recvDatagram ( QByteArray& bytes, QHostAddress& address, quint16& port );
|
||||
private:
|
||||
QQtProtocol* m_protocol;
|
||||
|
@ -50,7 +50,7 @@ public:
|
||||
public:
|
||||
QQtUdpClient* findClientByProtocolInstance ( QQtProtocol* protocol );
|
||||
QQtUdpClient* findClientByIPAddress ( QString ip, quint16 port );
|
||||
QList<QQtUdpClient*>& clientList() const { return m_clientList; }
|
||||
QList<QQtUdpClient*>& clientList() { return m_clientList; }
|
||||
|
||||
/**
|
||||
* 以下和用户无关
|
||||
|
@ -1,6 +1,95 @@
|
||||
#include "qqtwebsocketserver.h"
|
||||
|
||||
QQtWebSocketServer::QQtWebSocketServer ( QObject* parent ) : QObject ( parent )
|
||||
QQtWebSocketServer::QQtWebSocketServer ( const QString& serverName, SslMode secureMode,
|
||||
QObject* parent ) : QWebSocketServer ( serverName, secureMode, parent )
|
||||
{
|
||||
connect ( this, SIGNAL ( newConnection() ),
|
||||
this, SLOT ( comingNewConnection() ) );
|
||||
|
||||
m_protocolManager = NULL;
|
||||
}
|
||||
|
||||
void QQtWebSocketServer::installProtocolManager ( QQtProtocolManager* stackGroup )
|
||||
{
|
||||
if ( m_protocolManager )
|
||||
return;
|
||||
|
||||
m_protocolManager = stackGroup;
|
||||
}
|
||||
|
||||
void QQtWebSocketServer::uninstallProtocolManager ( QQtProtocolManager* stackGroup )
|
||||
{
|
||||
Q_UNUSED ( stackGroup )
|
||||
|
||||
if ( !m_protocolManager )
|
||||
return;
|
||||
|
||||
m_protocolManager = NULL;
|
||||
}
|
||||
|
||||
QQtWebSocketClient* QQtWebSocketServer::findClientByProtocolInstance ( QQtProtocol* protocol )
|
||||
{
|
||||
QListIterator<QQtWebSocketClient*> itor ( m_clientList );
|
||||
while ( itor.hasNext() )
|
||||
{
|
||||
QQtWebSocketClient* client = itor.next();
|
||||
QQtProtocol* cprotocol = client->installedProtocol();
|
||||
if ( cprotocol == protocol )
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QQtWebSocketClient* QQtWebSocketServer::findClientByIPAddress ( QString ip, quint16 port )
|
||||
{
|
||||
QListIterator<QQtWebSocketClient*> itor ( m_clientList );
|
||||
while ( itor.hasNext() )
|
||||
{
|
||||
QQtWebSocketClient* client = itor.next();
|
||||
QString aip = client->peerAddress().toString();
|
||||
quint16 aport = client->peerPort();
|
||||
if ( aip == ip && aport == port )
|
||||
{
|
||||
return client;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void QQtWebSocketServer::comingNewConnection()
|
||||
{
|
||||
while ( hasPendingConnections() )
|
||||
{
|
||||
QQtWebSocketClient* clientSocket = ( QQtWebSocketClient* ) nextPendingConnection();
|
||||
if ( !m_protocolManager )
|
||||
{
|
||||
pline() << "please install protocol manager for your server.";
|
||||
clientSocket->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
connect ( clientSocket, SIGNAL ( disconnected() ), this, SLOT ( clientSocketDisConnected() ) );
|
||||
//如果崩溃,对这个操作进行加锁。
|
||||
QQtProtocol* protocol = m_protocolManager->createProtocol();
|
||||
clientSocket->installProtocol ( protocol );
|
||||
m_clientList.push_back ( clientSocket );
|
||||
}
|
||||
}
|
||||
|
||||
void QQtWebSocketServer::clientSocketDisConnected()
|
||||
{
|
||||
QObject* obj = sender();
|
||||
QQtWebSocketClient* clientSocket = ( QQtWebSocketClient* ) obj;
|
||||
QQtProtocol* protocol = clientSocket->installedProtocol();
|
||||
clientSocket->uninstallProtocol ( protocol );
|
||||
clientSocket->deleteLater();
|
||||
protocol->deleteLater();
|
||||
m_clientList.removeOne ( clientSocket );
|
||||
}
|
||||
|
||||
QQtProtocolManager* QQtWebSocketServer::installedProtocolManager()
|
||||
{
|
||||
return m_protocolManager;
|
||||
}
|
||||
|
@ -2,17 +2,41 @@
|
||||
#define QQTWEBSOCKETSERVER_H
|
||||
|
||||
#include <QWebSocketServer>
|
||||
#include <qqtwebsocketclient.h>
|
||||
#include <qqtprotocolmanager.h>
|
||||
#include <qqtcore.h>
|
||||
|
||||
class QQtWebSocketServer : public QWebSocketServer
|
||||
/**
|
||||
* @brief The QQtWebSocketServer class
|
||||
* please handleConnection
|
||||
*/
|
||||
class QQTSHARED_EXPORT QQtWebSocketServer : public QWebSocketServer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QQtWebSocketServer ( QObject* parent = nullptr );
|
||||
explicit QQtWebSocketServer ( const QString& serverName, SslMode secureMode,
|
||||
QObject* parent = nullptr );
|
||||
virtual ~QQtWebSocketServer() {}
|
||||
|
||||
void installProtocolManager ( QQtProtocolManager* stackGroup );
|
||||
void uninstallProtocolManager ( QQtProtocolManager* stackGroup );
|
||||
QQtProtocolManager* installedProtocolManager();
|
||||
|
||||
public:
|
||||
QQtWebSocketClient* findClientByProtocolInstance ( QQtProtocol* protocol );
|
||||
QQtWebSocketClient* findClientByIPAddress ( QString ip, quint16 port );
|
||||
QList<QQtWebSocketClient*>& clientList() { return m_clientList; }
|
||||
|
||||
|
||||
signals:
|
||||
|
||||
private slots:
|
||||
void comingNewConnection();
|
||||
public slots:
|
||||
void clientSocketDisConnected();
|
||||
private:
|
||||
QQtProtocolManager* m_protocolManager;
|
||||
QList<QQtWebSocketClient*> m_clientList;
|
||||
};
|
||||
|
||||
#endif // QQTWEBSOCKETSERVER_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user