diff --git a/QTcpClientTest/qghtcpclient.cpp b/QTcpClientTest/qghtcpclient.cpp index 9b926997ab5d4d0b2027b8e9b22b4ab07f3a2075..7682727cbf77c011ce1103b0e0d14771ba7b6306 100644 --- a/QTcpClientTest/qghtcpclient.cpp +++ b/QTcpClientTest/qghtcpclient.cpp @@ -2,8 +2,10 @@ #include #include #include -QGHSslClient::QGHSslClient(QObject *parent,int nPayLoad) - : QSslSocket(parent), + + +QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad) + : QTcpSocket(parent), m_nPayLoad(nPayLoad) { @@ -11,16 +13,11 @@ QGHSslClient::QGHSslClient(QObject *parent,int nPayLoad) connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64))); m_uuid = 0; } - -QGHSslClient::~QGHSslClient() -{ - -} -quint32 QGHSslClient::uuid() +quint32 QGHTcpClient::uuid() { return m_uuid; } -void QGHSslClient::geneGlobalUUID(QString globalUuidFile) +void QGHTcpClient::geneGlobalUUID(QString globalUuidFile) { QFile file(globalUuidFile); int ctp = 0; @@ -44,10 +41,12 @@ void QGHSslClient::geneGlobalUUID(QString globalUuidFile) file.write((char *)&oldUUID,sizeof(oldUUID)); file.flush(); file.close(); - } +QGHTcpClient::~QGHTcpClient() +{ -void QGHSslClient::some_data_sended(qint64 wsended) +} +void QGHTcpClient::some_data_sended(qint64 wsended) { while (m_buffer_sending.empty()==false) { @@ -67,7 +66,7 @@ void QGHSslClient::some_data_sended(qint64 wsended) } } -void QGHSslClient::SendData(QByteArray dtarray) +void QGHTcpClient::SendData(QByteArray dtarray) { if (dtarray.size()) { @@ -87,8 +86,12 @@ void QGHSslClient::SendData(QByteArray dtarray) } } } -QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad) - : QTcpSocket(parent), + + + +#if (ZP_WANTSSL!=0) +QGHSslClient::QGHSslClient(QObject *parent,int nPayLoad) + : QSslSocket(parent), m_nPayLoad(nPayLoad) { @@ -96,11 +99,16 @@ QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad) connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64))); m_uuid = 0; } -quint32 QGHTcpClient::uuid() + +QGHSslClient::~QGHSslClient() +{ + +} +quint32 QGHSslClient::uuid() { return m_uuid; } -void QGHTcpClient::geneGlobalUUID(QString globalUuidFile) +void QGHSslClient::geneGlobalUUID(QString globalUuidFile) { QFile file(globalUuidFile); int ctp = 0; @@ -124,12 +132,10 @@ void QGHTcpClient::geneGlobalUUID(QString globalUuidFile) file.write((char *)&oldUUID,sizeof(oldUUID)); file.flush(); file.close(); -} -QGHTcpClient::~QGHTcpClient() -{ } -void QGHTcpClient::some_data_sended(qint64 wsended) + +void QGHSslClient::some_data_sended(qint64 wsended) { while (m_buffer_sending.empty()==false) { @@ -149,7 +155,7 @@ void QGHTcpClient::some_data_sended(qint64 wsended) } } -void QGHTcpClient::SendData(QByteArray dtarray) +void QGHSslClient::SendData(QByteArray dtarray) { if (dtarray.size()) { @@ -169,3 +175,5 @@ void QGHTcpClient::SendData(QByteArray dtarray) } } } + +#endif diff --git a/QTcpClientTest/qghtcpclient.h b/QTcpClientTest/qghtcpclient.h index e462eb3e83f29086ae294844bc32e8e5a660e813..12360ea4ac029adf1a489f0dd6a3b7a2eff022f5 100644 --- a/QTcpClientTest/qghtcpclient.h +++ b/QTcpClientTest/qghtcpclient.h @@ -1,21 +1,21 @@ #ifndef QGHTCPCLIENT_H #define QGHTCPCLIENT_H - -#include #include -class QGHSslClient : public QSslSocket +#include +#include "../ZoomPipeline_FuncSvr/network/ssl_config.h" + +class QGHTcpClient : public QTcpSocket { Q_OBJECT public: - QGHSslClient(QObject *parent,int nPayLoad = 4096); - ~QGHSslClient(); + QGHTcpClient(QObject *parent,int nPayLoad = 4096); + ~QGHTcpClient(); quint32 uuid(); void geneGlobalUUID(QString globalUuidFile); private: - quint32 m_uuid; - int m_nPayLoad; + quint32 m_uuid; int m_nPayLoad; QList m_buffer_sending; QList m_buffer_sending_offset; public slots: @@ -24,18 +24,21 @@ public slots: }; -class QGHTcpClient : public QTcpSocket +#if (ZP_WANTSSL!=0) +#include +class QGHSslClient : public QSslSocket { Q_OBJECT public: - QGHTcpClient(QObject *parent,int nPayLoad = 4096); - ~QGHTcpClient(); + QGHSslClient(QObject *parent,int nPayLoad = 4096); + ~QGHSslClient(); quint32 uuid(); void geneGlobalUUID(QString globalUuidFile); private: - quint32 m_uuid; int m_nPayLoad; + quint32 m_uuid; + int m_nPayLoad; QList m_buffer_sending; QList m_buffer_sending_offset; public slots: @@ -43,5 +46,5 @@ public slots: void SendData(QByteArray dtarray); }; - +#endif #endif // QGHTCPCLIENT_H diff --git a/QTcpClientTest/qtcpclienttest.cpp b/QTcpClientTest/qtcpclienttest.cpp index 21e7db3bdd2b430149ec61e3fa32f40d1c747201..07a043acf91ccc45f5f22b8ff5aba86167638646 100644 --- a/QTcpClientTest/qtcpclienttest.cpp +++ b/QTcpClientTest/qtcpclienttest.cpp @@ -65,17 +65,24 @@ void QTcpClientTest::on_client_trasferred(qint64 dtw) QTcpSocket * pSock = qobject_cast(sender()); if (pSock) { +#if (ZP_WANTSSL!=0) QGHSslClient * pSockSsl = qobject_cast(sender()); QGHTcpClient * pSockTcp = qobject_cast(sender()); if (pSockSsl) displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockSsl->uuid()).arg(dtw)); else if (pSockTcp) displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockTcp->uuid()).arg(dtw)); +#else + QGHTcpClient * pSockTcp = qobject_cast(sender()); + if (pSockTcp) + displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockTcp->uuid()).arg(dtw)); +#endif } } void QTcpClientTest::on_client_connected() { +#if (ZP_WANTSSL!=0) QGHSslClient * pSockSsl = qobject_cast(sender()); QGHTcpClient * pSockTcp = qobject_cast(sender()); @@ -113,10 +120,30 @@ void QTcpClientTest::on_client_connected() pMsg->tmStamp = 0; (pSockTcp)->SendData(array); } +#else + QGHTcpClient * pSockTcp = qobject_cast(sender()); + if (pSockTcp) + { + pSockTcp->geneGlobalUUID(ui.lineEdit_globalFile->text()); + if (pSockTcp->uuid()>m_maxUUID) + m_maxUUID = pSockTcp->uuid(); + if (pSockTcp->uuid()uuid(); + displayMessage(QString("client %1 connected.").arg(pSockTcp->uuid())); + QByteArray array(sizeof(EXAMPLE_HEARTBEATING),0); + char * ptr = array.data(); + EXAMPLE_HEARTBEATING * pMsg = (EXAMPLE_HEARTBEATING *)ptr; + pMsg->Mark = 0xBEBE; + pMsg->source_id = pSockTcp->uuid(); + pMsg->tmStamp = 0; + (pSockTcp)->SendData(array); + } +#endif } void QTcpClientTest::on_client_disconnected() { +#if (ZP_WANTSSL!=0) QGHSslClient * pSockSsl = qobject_cast(sender()); QGHTcpClient * pSockTcp = qobject_cast(sender()); if (pSockSsl) @@ -143,18 +170,39 @@ void QTcpClientTest::on_client_disconnected() m_clients.remove(pSockTcp); pSockTcp->deleteLater(); } +#else + QGHTcpClient * pSockTcp = qobject_cast(sender()); + if (pSockTcp) + { + displayMessage(QString("client %1 disconnected.").arg(pSockTcp->uuid())); + //disconnect the signal immediately so that the system resource could be freed. + disconnect(pSockTcp, SIGNAL(readyRead()),this, SLOT(new_data_recieved())); + disconnect(pSockTcp, SIGNAL(connected()),this, SLOT(on_client_connected())); + disconnect(pSockTcp, SIGNAL(disconnected()),this,SLOT(on_client_disconnected())); + disconnect(pSockTcp, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); + disconnect(pSockTcp, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64))); + m_clients.remove(pSockTcp); + pSockTcp->deleteLater(); + } +#endif } void QTcpClientTest::displayError(QAbstractSocket::SocketError /*err*/) { QTcpSocket * sock = qobject_cast (sender()); if (sock) { +#if (ZP_WANTSSL!=0) QGHSslClient * pSockSsl = qobject_cast(sock); QGHTcpClient * pSockTcp = qobject_cast(sock); if (pSockSsl) displayMessage(QString("client %1 error msg:").arg(pSockSsl->uuid())+sock->errorString()); else if (pSockTcp) displayMessage(QString("client %1 error msg:").arg(pSockTcp->uuid())+sock->errorString()); +#else + QGHTcpClient * pSockTcp = qobject_cast(sock); + if (pSockTcp) + displayMessage(QString("client %1 error msg:").arg(pSockTcp->uuid())+sock->errorString()); +#endif } } void QTcpClientTest::new_data_recieved() @@ -166,13 +214,18 @@ void QTcpClientTest::new_data_recieved() if (array.size() <= sizeof(EXAMPLE_HEARTBEATING)) return; //in this example, we just do nothing but to display the byte size. +#if (ZP_WANTSSL!=0) QGHSslClient * pSockSsl = qobject_cast(pSock); QGHTcpClient * pSockTcp = qobject_cast(pSock); if (pSockSsl) displayMessage(QString("client %1 Recieved %2 bytes.").arg(pSockSsl->uuid()).arg(array.size())); else if (pSockTcp) displayMessage(QString("client %1 Recieved %2 bytes.").arg(pSockTcp->uuid()).arg(array.size())); - +#else + QGHTcpClient * pSockTcp = qobject_cast(pSock); + if (pSockTcp) + displayMessage(QString("client %1 Recieved %2 bytes.").arg(pSockTcp->uuid()).arg(array.size())); +#endif } } @@ -191,6 +244,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) foreach(QTcpSocket * pSock,listObj) { quint32 uuid = 0; +#if (ZP_WANTSSL!=0) QGHSslClient * pSockSsl = qobject_cast(pSock); QGHTcpClient * pSockTcp = qobject_cast(pSock); if (pSockSsl) @@ -199,6 +253,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) uuid = pSockTcp->uuid(); else continue; +#else + QGHTcpClient * pSockTcp = qobject_cast(pSock); + if (pSockTcp) + uuid = pSockTcp->uuid(); + else + continue; +#endif QByteArray array(sizeof(EXAMPLE_HEARTBEATING),0); char * ptr = array.data(); @@ -206,10 +267,15 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) pMsg->Mark = 0xBEBE; pMsg->source_id = uuid; pMsg->tmStamp = 0; +#if (ZP_WANTSSL!=0) if (pSockSsl) pSockSsl->SendData(array); else if (pSockTcp) pSockTcp->SendData(array); +#else + if (pSockTcp) + pSockTcp->SendData(array); +#endif } } foreach(QTcpSocket * sock,listObj) @@ -217,6 +283,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) if (rand()%1000<5) { quint32 uuid = 0; +#if (ZP_WANTSSL!=0) QGHSslClient * pSockSsl = qobject_cast(sock); QGHTcpClient * pSockTcp = qobject_cast(sock); if (pSockSsl) @@ -225,6 +292,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) uuid = pSockTcp->uuid(); else continue; +#else + QGHTcpClient * pSockTcp = qobject_cast(sock); + if (pSockTcp) + uuid = pSockTcp->uuid(); + else + continue; +#endif quint16 nMsgLen = qrand()%(32)+nPayload-32-sizeof(EXAMPLE_TRANS_MSG); QByteArray array(sizeof(EXAMPLE_TRANS_MSG) + nMsgLen - 1,0); char * ptr = array.data(); @@ -237,11 +311,15 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) pMsg->data_length = nMsgLen; for (int i=0;idata[i] = '0' + i%10; - +#if (ZP_WANTSSL!=0) if (pSockSsl) pSockSsl->SendData(array); else if (pSockTcp) pSockTcp->SendData(array); +#else + if (pSockTcp) + pSockTcp->SendData(array); +#endif } } // @@ -258,6 +336,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) listObj.at(i)->abort(); } } +#if (ZP_WANTSSL!=0) if (ui.checkBox_SSL->isChecked()==true) { QGHSslClient * client = new QGHSslClient(this,ui.horizontalSlider->value()); @@ -285,6 +364,16 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64))); client->connectToHost(ui.lineEdit_ip->text(),ui.lineEdit_Port->text().toUShort()); } +#else + QGHTcpClient * client = new QGHTcpClient(this,ui.horizontalSlider->value()); + m_clients[client] = QDateTime::currentDateTime(); + connect(client, SIGNAL(readyRead()),this, SLOT(new_data_recieved())); + connect(client, SIGNAL(connected()),this, SLOT(on_client_connected())); + connect(client, SIGNAL(disconnected()),this,SLOT(on_client_disconnected())); + connect(client, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); + connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64))); + client->connectToHost(ui.lineEdit_ip->text(),ui.lineEdit_Port->text().toUShort()); +#endif } } } diff --git a/ZoomPipeline_FuncSvr/ZoomPipeline_FuncSvr.pro b/ZoomPipeline_FuncSvr/ZoomPipeline_FuncSvr.pro index 3ddd461769decbdc23cd443541c3bb18ea8c5d6f..5f2b7c2b6915ecce8fb1ed729936ed62156e4902 100644 --- a/ZoomPipeline_FuncSvr/ZoomPipeline_FuncSvr.pro +++ b/ZoomPipeline_FuncSvr/ZoomPipeline_FuncSvr.pro @@ -53,7 +53,8 @@ HEADERS += zpmainframe.h \ cluster/zp_clusternode.h \ smartlink/st_cross_svr_msg.h \ smartlink/st_cross_svr_node.h \ - logger/st_logger.h + logger/st_logger.h \ + network/ssl_config.h FORMS += zpmainframe.ui \ dialogaddressinput.ui diff --git a/ZoomPipeline_FuncSvr/network/ssl_config.h b/ZoomPipeline_FuncSvr/network/ssl_config.h new file mode 100644 index 0000000000000000000000000000000000000000..4f9f9e22a76ff26828e108ccb38a6593d34216dd --- /dev/null +++ b/ZoomPipeline_FuncSvr/network/ssl_config.h @@ -0,0 +1,10 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#ifdef QT_NO_SSL +#define ZP_WANTSSL 0 +#else +#define ZP_WANTSSL 1 +#endif + +#endif // CONFIG_H diff --git a/ZoomPipeline_FuncSvr/network/zp_nettransthread.cpp b/ZoomPipeline_FuncSvr/network/zp_nettransthread.cpp index f69d3ba66d468144388d2074e8d6def7a27c733f..2aa70297dc5fc40bcec2332405797883e78b47ec 100644 --- a/ZoomPipeline_FuncSvr/network/zp_nettransthread.cpp +++ b/ZoomPipeline_FuncSvr/network/zp_nettransthread.cpp @@ -1,6 +1,9 @@ #include "zp_nettransthread.h" +#include "ssl_config.h" #include +#if (ZP_WANTSSL!=0) #include +#endif #include #include #include @@ -148,10 +151,14 @@ namespace ZPNetwork{ if (threadid!=this) return; QTcpSocket * sock_client = 0; +#if (ZP_WANTSSL!=0) if (m_bSSLConnection) sock_client = new QSslSocket(this); else sock_client = new QTcpSocket(this); +#else + sock_client = new QTcpSocket(this); +#endif if (sock_client) { //Initial content @@ -164,6 +171,7 @@ namespace ZPNetwork{ m_mutex_protect.lock(); m_clientList.insert(sock_client); m_mutex_protect.unlock(); +#if (ZP_WANTSSL!=0) if (m_bSSLConnection) { QSslSocket * psslsock = qobject_cast(sock_client); @@ -175,6 +183,7 @@ namespace ZPNetwork{ connect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted,Qt::QueuedConnection); psslsock->startServerEncryption(); } +#endif qDebug()<peerAddress().toString()<< sock_client->peerPort() <(sock_client); assert(psslsock!=NULL); QString strCerPath = QCoreApplication::applicationDirPath() + "/ca_cert.pem"; @@ -237,6 +252,17 @@ namespace ZPNetwork{ sock_client->connectToHost(addr,port); } +#else + connect(sock_client, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved,Qt::QueuedConnection); + connect(sock_client, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed,Qt::QueuedConnection); + connect(sock_client, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)),Qt::QueuedConnection); + connect(sock_client, &QTcpSocket::bytesWritten, this,&zp_netTransThread::some_data_sended,Qt::QueuedConnection); + connect(sock_client, &QTcpSocket::connected,this, &zp_netTransThread::on_connected,Qt::QueuedConnection); + m_mutex_protect.lock(); + m_clientList.insert(sock_client); + m_mutex_protect.unlock(); + sock_client->connectToHost(addr,port); +#endif } else assert(false); @@ -264,12 +290,14 @@ namespace ZPNetwork{ QTcpSocket * pSock = qobject_cast(sender()); if (pSock) { +#if (ZP_WANTSSL!=0) if (m_bSSLConnection) { QSslSocket * psslsock = qobject_cast(pSock); if (psslsock) disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted); } +#endif disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved); disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed); disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); @@ -347,13 +375,14 @@ namespace ZPNetwork{ qDebug()<peerAddress().toString()<< pSock->peerPort() <errorString()); emit evt_SocketError(pSock,socketError); - //emit evt_Message(pSock,"Debug:" + pSock->errorString()); +#if (ZP_WANTSSL!=0) if (m_bSSLConnection) { QSslSocket * psslsock = qobject_cast(pSock); if (psslsock) disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted); } +#endif disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved); disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed); disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); @@ -415,30 +444,10 @@ namespace ZPNetwork{ QTcpSocket * pSock = qobject_cast(obj); if (pSock) { -// if (m_bSSLConnection) -// { -// QSslSocket * psslsock = qobject_cast(pSock); -// if (psslsock) -// disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted); -// } -// disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved); -// disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed); -// disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); -// disconnect(pSock, &QTcpSocket::bytesWritten, this, &zp_netTransThread::some_data_sended); -// disconnect(pSock, &QTcpSocket::connected,this, &zp_netTransThread::on_connected); -// m_buffer_sending.erase(pSock); -// m_buffer_sending_offset.erase(pSock); pSock->disconnectFromHost(); -// pSock->abort(); -// emit evt_ClientDisconnected(pSock); - //emit evt_Message(pSock,"Info>" + QString(tr("Client Closed."))); -// qDebug()<(pSock); -// if (psslsock) -// disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted); -// } -// disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved); -// disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed); -// disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); -// disconnect(pSock, &QTcpSocket::bytesWritten, this, &zp_netTransThread::some_data_sended); -// disconnect(pSock, &QTcpSocket::connected,this, &zp_netTransThread::on_connected); -// m_buffer_sending.erase(pSock); -// m_buffer_sending_offset.erase(pSock); -// m_mutex_protect.lock(); -// m_clientList.remove(pSock); -// m_mutex_protect.unlock(); pSock->disconnectFromHost(); -// pSock->abort(); -// emit evt_ClientDisconnected(pSock); -// //emit evt_Message(pSock,"Info>" + QString(tr("Client Closed."))); -// qDebug()< #include #include @@ -9,7 +10,9 @@ #include #include #include +#if (ZP_WANTSSL!=0) #include +#endif #include #include "smartlink/st_clientnode_basetrans.h" #include "dialogaddressinput.h"