diff --git a/examples/qqtclientexample/main.cpp b/examples/qqtclientexample/main.cpp index 525aa6a2..b10e400d 100644 --- a/examples/qqtclientexample/main.cpp +++ b/examples/qqtclientexample/main.cpp @@ -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的功能。 //这是一个例子, diff --git a/src/network/qqtbluetoothserver.cpp b/src/network/qqtbluetoothserver.cpp index 577cef04..33f21358 100644 --- a/src/network/qqtbluetoothserver.cpp +++ b/src/network/qqtbluetoothserver.cpp @@ -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 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 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; } diff --git a/src/network/qqtbluetoothserver.h b/src/network/qqtbluetoothserver.h index b8319b82..0aa816b3 100644 --- a/src/network/qqtbluetoothserver.h +++ b/src/network/qqtbluetoothserver.h @@ -2,7 +2,8 @@ #define QQTBLUETOOTHSERVER_H #include -#include "qqtprotocol.h" +#include +#include "qqtprotocolmanager.h" #include 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& 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 m_clientList; }; #endif // QQTBLUETOOTHSERVER_H diff --git a/src/network/qqtethenetmanager.h b/src/network/qqtethenetmanager.h index 623267db..e2777eb1 100644 --- a/src/network/qqtethenetmanager.h +++ b/src/network/qqtethenetmanager.h @@ -7,7 +7,6 @@ #include #include "qqtcore.h" #include "qqt-local.h" -#include "qqtnetwork.h" class QQTSHARED_EXPORT QQtNetWorkClearThread : public QThread { diff --git a/src/network/qqtprotocolmanager.h b/src/network/qqtprotocolmanager.h index 791783ec..98fc200e 100644 --- a/src/network/qqtprotocolmanager.h +++ b/src/network/qqtprotocolmanager.h @@ -95,8 +95,6 @@ public: return p0; } -private slots: - void clientSocketDisConnected(); private: QQtProtocol* mProtocol; }; diff --git a/src/network/qqttcpclient.cpp b/src/network/qqttcpclient.cpp index 04a9d89d..8045c474 100644 --- a/src/network/qqttcpclient.cpp +++ b/src/network/qqttcpclient.cpp @@ -3,9 +3,6 @@ #include #include -#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; } diff --git a/src/network/qqttcpserver.cpp b/src/network/qqttcpserver.cpp index 36a45eab..935267e8 100644 --- a/src/network/qqttcpserver.cpp +++ b/src/network/qqttcpserver.cpp @@ -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 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 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; +} diff --git a/src/network/qqttcpserver.h b/src/network/qqttcpserver.h index 5bb54d59..9b164ca0 100644 --- a/src/network/qqttcpserver.h +++ b/src/network/qqttcpserver.h @@ -21,14 +21,20 @@ public: void uninstallProtocolManager ( QQtProtocolManager* stackGroup ); QQtProtocolManager* installedProtocolManager(); +public: + QQtTcpClient* findClientByProtocolInstance ( QQtProtocol* protocol ); + QQtTcpClient* findClientByIPAddress ( QString ip, quint16 port ); + QList& 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 m_clientList; }; diff --git a/src/network/qqtudpclient.cpp b/src/network/qqtudpclient.cpp index 35bb00db..69a6e247 100644 --- a/src/network/qqtudpclient.cpp +++ b/src/network/qqtudpclient.cpp @@ -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 ); diff --git a/src/network/qqtudpclient.h b/src/network/qqtudpclient.h index bb9fd4d1..e10ff170 100644 --- a/src/network/qqtudpclient.h +++ b/src/network/qqtudpclient.h @@ -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; diff --git a/src/network/qqtudpserver.h b/src/network/qqtudpserver.h index 609d52c7..a0f2e442 100644 --- a/src/network/qqtudpserver.h +++ b/src/network/qqtudpserver.h @@ -50,7 +50,7 @@ public: public: QQtUdpClient* findClientByProtocolInstance ( QQtProtocol* protocol ); QQtUdpClient* findClientByIPAddress ( QString ip, quint16 port ); - QList& clientList() const { return m_clientList; } + QList& clientList() { return m_clientList; } /** * 以下和用户无关 diff --git a/src/network/qqtwebsocketserver.cpp b/src/network/qqtwebsocketserver.cpp index 733f31d5..39425842 100644 --- a/src/network/qqtwebsocketserver.cpp +++ b/src/network/qqtwebsocketserver.cpp @@ -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 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 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; } diff --git a/src/network/qqtwebsocketserver.h b/src/network/qqtwebsocketserver.h index 51f866c4..f0096ffa 100644 --- a/src/network/qqtwebsocketserver.h +++ b/src/network/qqtwebsocketserver.h @@ -2,17 +2,41 @@ #define QQTWEBSOCKETSERVER_H #include +#include +#include #include -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& clientList() { return m_clientList; } + signals: +private slots: + void comingNewConnection(); public slots: + void clientSocketDisConnected(); +private: + QQtProtocolManager* m_protocolManager; + QList m_clientList; }; #endif // QQTWEBSOCKETSERVER_H