提交 3658b2de 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

Add ssl switch for the network socket code.

上级 45ab78e3
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
#include <assert.h> #include <assert.h>
#include <QFile> #include <QFile>
#include <QThread> #include <QThread>
QGHSslClient::QGHSslClient(QObject *parent,int nPayLoad)
: QSslSocket(parent),
QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad)
: QTcpSocket(parent),
m_nPayLoad(nPayLoad) m_nPayLoad(nPayLoad)
{ {
...@@ -11,16 +13,11 @@ QGHSslClient::QGHSslClient(QObject *parent,int nPayLoad) ...@@ -11,16 +13,11 @@ QGHSslClient::QGHSslClient(QObject *parent,int nPayLoad)
connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64))); connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
m_uuid = 0; m_uuid = 0;
} }
quint32 QGHTcpClient::uuid()
QGHSslClient::~QGHSslClient()
{
}
quint32 QGHSslClient::uuid()
{ {
return m_uuid; return m_uuid;
} }
void QGHSslClient::geneGlobalUUID(QString globalUuidFile) void QGHTcpClient::geneGlobalUUID(QString globalUuidFile)
{ {
QFile file(globalUuidFile); QFile file(globalUuidFile);
int ctp = 0; int ctp = 0;
...@@ -44,10 +41,12 @@ void QGHSslClient::geneGlobalUUID(QString globalUuidFile) ...@@ -44,10 +41,12 @@ void QGHSslClient::geneGlobalUUID(QString globalUuidFile)
file.write((char *)&oldUUID,sizeof(oldUUID)); file.write((char *)&oldUUID,sizeof(oldUUID));
file.flush(); file.flush();
file.close(); file.close();
} }
QGHTcpClient::~QGHTcpClient()
{
void QGHSslClient::some_data_sended(qint64 wsended) }
void QGHTcpClient::some_data_sended(qint64 wsended)
{ {
while (m_buffer_sending.empty()==false) while (m_buffer_sending.empty()==false)
{ {
...@@ -67,7 +66,7 @@ void QGHSslClient::some_data_sended(qint64 wsended) ...@@ -67,7 +66,7 @@ void QGHSslClient::some_data_sended(qint64 wsended)
} }
} }
void QGHSslClient::SendData(QByteArray dtarray) void QGHTcpClient::SendData(QByteArray dtarray)
{ {
if (dtarray.size()) if (dtarray.size())
{ {
...@@ -87,8 +86,12 @@ void QGHSslClient::SendData(QByteArray dtarray) ...@@ -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) m_nPayLoad(nPayLoad)
{ {
...@@ -96,11 +99,16 @@ QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad) ...@@ -96,11 +99,16 @@ QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad)
connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64))); connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
m_uuid = 0; m_uuid = 0;
} }
quint32 QGHTcpClient::uuid()
QGHSslClient::~QGHSslClient()
{
}
quint32 QGHSslClient::uuid()
{ {
return m_uuid; return m_uuid;
} }
void QGHTcpClient::geneGlobalUUID(QString globalUuidFile) void QGHSslClient::geneGlobalUUID(QString globalUuidFile)
{ {
QFile file(globalUuidFile); QFile file(globalUuidFile);
int ctp = 0; int ctp = 0;
...@@ -124,12 +132,10 @@ void QGHTcpClient::geneGlobalUUID(QString globalUuidFile) ...@@ -124,12 +132,10 @@ void QGHTcpClient::geneGlobalUUID(QString globalUuidFile)
file.write((char *)&oldUUID,sizeof(oldUUID)); file.write((char *)&oldUUID,sizeof(oldUUID));
file.flush(); file.flush();
file.close(); file.close();
}
QGHTcpClient::~QGHTcpClient()
{
} }
void QGHTcpClient::some_data_sended(qint64 wsended)
void QGHSslClient::some_data_sended(qint64 wsended)
{ {
while (m_buffer_sending.empty()==false) while (m_buffer_sending.empty()==false)
{ {
...@@ -149,7 +155,7 @@ void QGHTcpClient::some_data_sended(qint64 wsended) ...@@ -149,7 +155,7 @@ void QGHTcpClient::some_data_sended(qint64 wsended)
} }
} }
void QGHTcpClient::SendData(QByteArray dtarray) void QGHSslClient::SendData(QByteArray dtarray)
{ {
if (dtarray.size()) if (dtarray.size())
{ {
...@@ -169,3 +175,5 @@ void QGHTcpClient::SendData(QByteArray dtarray) ...@@ -169,3 +175,5 @@ void QGHTcpClient::SendData(QByteArray dtarray)
} }
} }
} }
#endif
#ifndef QGHTCPCLIENT_H #ifndef QGHTCPCLIENT_H
#define QGHTCPCLIENT_H #define QGHTCPCLIENT_H
#include <QSslSocket>
#include <QList> #include <QList>
class QGHSslClient : public QSslSocket #include <QTcpSocket>
#include "../ZoomPipeline_FuncSvr/network/ssl_config.h"
class QGHTcpClient : public QTcpSocket
{ {
Q_OBJECT Q_OBJECT
public: public:
QGHSslClient(QObject *parent,int nPayLoad = 4096); QGHTcpClient(QObject *parent,int nPayLoad = 4096);
~QGHSslClient(); ~QGHTcpClient();
quint32 uuid(); quint32 uuid();
void geneGlobalUUID(QString globalUuidFile); void geneGlobalUUID(QString globalUuidFile);
private: private:
quint32 m_uuid; quint32 m_uuid; int m_nPayLoad;
int m_nPayLoad;
QList<QByteArray> m_buffer_sending; QList<QByteArray> m_buffer_sending;
QList<qint64> m_buffer_sending_offset; QList<qint64> m_buffer_sending_offset;
public slots: public slots:
...@@ -24,18 +24,21 @@ public slots: ...@@ -24,18 +24,21 @@ public slots:
}; };
class QGHTcpClient : public QTcpSocket #if (ZP_WANTSSL!=0)
#include <QSslSocket>
class QGHSslClient : public QSslSocket
{ {
Q_OBJECT Q_OBJECT
public: public:
QGHTcpClient(QObject *parent,int nPayLoad = 4096); QGHSslClient(QObject *parent,int nPayLoad = 4096);
~QGHTcpClient(); ~QGHSslClient();
quint32 uuid(); quint32 uuid();
void geneGlobalUUID(QString globalUuidFile); void geneGlobalUUID(QString globalUuidFile);
private: private:
quint32 m_uuid; int m_nPayLoad; quint32 m_uuid;
int m_nPayLoad;
QList<QByteArray> m_buffer_sending; QList<QByteArray> m_buffer_sending;
QList<qint64> m_buffer_sending_offset; QList<qint64> m_buffer_sending_offset;
public slots: public slots:
...@@ -43,5 +46,5 @@ public slots: ...@@ -43,5 +46,5 @@ public slots:
void SendData(QByteArray dtarray); void SendData(QByteArray dtarray);
}; };
#endif
#endif // QGHTCPCLIENT_H #endif // QGHTCPCLIENT_H
...@@ -65,17 +65,24 @@ void QTcpClientTest::on_client_trasferred(qint64 dtw) ...@@ -65,17 +65,24 @@ void QTcpClientTest::on_client_trasferred(qint64 dtw)
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender()); QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
if (pSock) if (pSock)
{ {
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender()); QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender());
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender()); QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender());
if (pSockSsl) if (pSockSsl)
displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockSsl->uuid()).arg(dtw)); displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockSsl->uuid()).arg(dtw));
else if (pSockTcp) else if (pSockTcp)
displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockTcp->uuid()).arg(dtw)); displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockTcp->uuid()).arg(dtw));
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender());
if (pSockTcp)
displayMessage(QString("client %1 Transferrd %2 bytes.").arg(pSockTcp->uuid()).arg(dtw));
#endif
} }
} }
void QTcpClientTest::on_client_connected() void QTcpClientTest::on_client_connected()
{ {
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender()); QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender());
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender()); QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender());
...@@ -113,10 +120,30 @@ void QTcpClientTest::on_client_connected() ...@@ -113,10 +120,30 @@ void QTcpClientTest::on_client_connected()
pMsg->tmStamp = 0; pMsg->tmStamp = 0;
(pSockTcp)->SendData(array); (pSockTcp)->SendData(array);
} }
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender());
if (pSockTcp)
{
pSockTcp->geneGlobalUUID(ui.lineEdit_globalFile->text());
if (pSockTcp->uuid()>m_maxUUID)
m_maxUUID = pSockTcp->uuid();
if (pSockTcp->uuid()<m_minUUID)
m_minUUID = pSockTcp->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() void QTcpClientTest::on_client_disconnected()
{ {
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender()); QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender());
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender()); QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender());
if (pSockSsl) if (pSockSsl)
...@@ -143,18 +170,39 @@ void QTcpClientTest::on_client_disconnected() ...@@ -143,18 +170,39 @@ void QTcpClientTest::on_client_disconnected()
m_clients.remove(pSockTcp); m_clients.remove(pSockTcp);
pSockTcp->deleteLater(); pSockTcp->deleteLater();
} }
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(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*/) void QTcpClientTest::displayError(QAbstractSocket::SocketError /*err*/)
{ {
QTcpSocket * sock = qobject_cast<QTcpSocket *> (sender()); QTcpSocket * sock = qobject_cast<QTcpSocket *> (sender());
if (sock) if (sock)
{ {
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sock); QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sock); QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sock);
if (pSockSsl) if (pSockSsl)
displayMessage(QString("client %1 error msg:").arg(pSockSsl->uuid())+sock->errorString()); displayMessage(QString("client %1 error msg:").arg(pSockSsl->uuid())+sock->errorString());
else if (pSockTcp) else if (pSockTcp)
displayMessage(QString("client %1 error msg:").arg(pSockTcp->uuid())+sock->errorString()); displayMessage(QString("client %1 error msg:").arg(pSockTcp->uuid())+sock->errorString());
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sock);
if (pSockTcp)
displayMessage(QString("client %1 error msg:").arg(pSockTcp->uuid())+sock->errorString());
#endif
} }
} }
void QTcpClientTest::new_data_recieved() void QTcpClientTest::new_data_recieved()
...@@ -166,13 +214,18 @@ void QTcpClientTest::new_data_recieved() ...@@ -166,13 +214,18 @@ void QTcpClientTest::new_data_recieved()
if (array.size() <= sizeof(EXAMPLE_HEARTBEATING)) if (array.size() <= sizeof(EXAMPLE_HEARTBEATING))
return; return;
//in this example, we just do nothing but to display the byte size. //in this example, we just do nothing but to display the byte size.
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(pSock); QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(pSock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(pSock); QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(pSock);
if (pSockSsl) if (pSockSsl)
displayMessage(QString("client %1 Recieved %2 bytes.").arg(pSockSsl->uuid()).arg(array.size())); displayMessage(QString("client %1 Recieved %2 bytes.").arg(pSockSsl->uuid()).arg(array.size()));
else if (pSockTcp) else if (pSockTcp)
displayMessage(QString("client %1 Recieved %2 bytes.").arg(pSockTcp->uuid()).arg(array.size())); displayMessage(QString("client %1 Recieved %2 bytes.").arg(pSockTcp->uuid()).arg(array.size()));
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(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) ...@@ -191,6 +244,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
foreach(QTcpSocket * pSock,listObj) foreach(QTcpSocket * pSock,listObj)
{ {
quint32 uuid = 0; quint32 uuid = 0;
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(pSock); QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(pSock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(pSock); QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(pSock);
if (pSockSsl) if (pSockSsl)
...@@ -199,6 +253,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) ...@@ -199,6 +253,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
uuid = pSockTcp->uuid(); uuid = pSockTcp->uuid();
else else
continue; continue;
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(pSock);
if (pSockTcp)
uuid = pSockTcp->uuid();
else
continue;
#endif
QByteArray array(sizeof(EXAMPLE_HEARTBEATING),0); QByteArray array(sizeof(EXAMPLE_HEARTBEATING),0);
char * ptr = array.data(); char * ptr = array.data();
...@@ -206,10 +267,15 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) ...@@ -206,10 +267,15 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
pMsg->Mark = 0xBEBE; pMsg->Mark = 0xBEBE;
pMsg->source_id = uuid; pMsg->source_id = uuid;
pMsg->tmStamp = 0; pMsg->tmStamp = 0;
#if (ZP_WANTSSL!=0)
if (pSockSsl) if (pSockSsl)
pSockSsl->SendData(array); pSockSsl->SendData(array);
else if (pSockTcp) else if (pSockTcp)
pSockTcp->SendData(array); pSockTcp->SendData(array);
#else
if (pSockTcp)
pSockTcp->SendData(array);
#endif
} }
} }
foreach(QTcpSocket * sock,listObj) foreach(QTcpSocket * sock,listObj)
...@@ -217,6 +283,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) ...@@ -217,6 +283,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
if (rand()%1000<5) if (rand()%1000<5)
{ {
quint32 uuid = 0; quint32 uuid = 0;
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sock); QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sock); QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sock);
if (pSockSsl) if (pSockSsl)
...@@ -225,6 +292,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) ...@@ -225,6 +292,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
uuid = pSockTcp->uuid(); uuid = pSockTcp->uuid();
else else
continue; continue;
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sock);
if (pSockTcp)
uuid = pSockTcp->uuid();
else
continue;
#endif
quint16 nMsgLen = qrand()%(32)+nPayload-32-sizeof(EXAMPLE_TRANS_MSG); quint16 nMsgLen = qrand()%(32)+nPayload-32-sizeof(EXAMPLE_TRANS_MSG);
QByteArray array(sizeof(EXAMPLE_TRANS_MSG) + nMsgLen - 1,0); QByteArray array(sizeof(EXAMPLE_TRANS_MSG) + nMsgLen - 1,0);
char * ptr = array.data(); char * ptr = array.data();
...@@ -237,11 +311,15 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) ...@@ -237,11 +311,15 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
pMsg->data_length = nMsgLen; pMsg->data_length = nMsgLen;
for (int i=0;i<nMsgLen;i++) for (int i=0;i<nMsgLen;i++)
pMsg->data[i] = '0' + i%10; pMsg->data[i] = '0' + i%10;
#if (ZP_WANTSSL!=0)
if (pSockSsl) if (pSockSsl)
pSockSsl->SendData(array); pSockSsl->SendData(array);
else if (pSockTcp) else if (pSockTcp)
pSockTcp->SendData(array); pSockTcp->SendData(array);
#else
if (pSockTcp)
pSockTcp->SendData(array);
#endif
} }
} }
// //
...@@ -258,6 +336,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) ...@@ -258,6 +336,7 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
listObj.at(i)->abort(); listObj.at(i)->abort();
} }
} }
#if (ZP_WANTSSL!=0)
if (ui.checkBox_SSL->isChecked()==true) if (ui.checkBox_SSL->isChecked()==true)
{ {
QGHSslClient * client = new QGHSslClient(this,ui.horizontalSlider->value()); QGHSslClient * client = new QGHSslClient(this,ui.horizontalSlider->value());
...@@ -285,6 +364,16 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt) ...@@ -285,6 +364,16 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64))); connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64)));
client->connectToHost(ui.lineEdit_ip->text(),ui.lineEdit_Port->text().toUShort()); 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
} }
} }
} }
......
...@@ -53,7 +53,8 @@ HEADERS += zpmainframe.h \ ...@@ -53,7 +53,8 @@ HEADERS += zpmainframe.h \
cluster/zp_clusternode.h \ cluster/zp_clusternode.h \
smartlink/st_cross_svr_msg.h \ smartlink/st_cross_svr_msg.h \
smartlink/st_cross_svr_node.h \ smartlink/st_cross_svr_node.h \
logger/st_logger.h logger/st_logger.h \
network/ssl_config.h
FORMS += zpmainframe.ui \ FORMS += zpmainframe.ui \
dialogaddressinput.ui dialogaddressinput.ui
......
#ifndef CONFIG_H
#define CONFIG_H
#ifdef QT_NO_SSL
#define ZP_WANTSSL 0
#else
#define ZP_WANTSSL 1
#endif
#endif // CONFIG_H
#include "zp_nettransthread.h" #include "zp_nettransthread.h"
#include "ssl_config.h"
#include <QTcpSocket> #include <QTcpSocket>
#if (ZP_WANTSSL!=0)
#include <QSslSocket> #include <QSslSocket>
#endif
#include <assert.h> #include <assert.h>
#include <QDebug> #include <QDebug>
#include <QCoreApplication> #include <QCoreApplication>
...@@ -148,10 +151,14 @@ namespace ZPNetwork{ ...@@ -148,10 +151,14 @@ namespace ZPNetwork{
if (threadid!=this) if (threadid!=this)
return; return;
QTcpSocket * sock_client = 0; QTcpSocket * sock_client = 0;
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection) if (m_bSSLConnection)
sock_client = new QSslSocket(this); sock_client = new QSslSocket(this);
else else
sock_client = new QTcpSocket(this); sock_client = new QTcpSocket(this);
#else
sock_client = new QTcpSocket(this);
#endif
if (sock_client) if (sock_client)
{ {
//Initial content //Initial content
...@@ -164,6 +171,7 @@ namespace ZPNetwork{ ...@@ -164,6 +171,7 @@ namespace ZPNetwork{
m_mutex_protect.lock(); m_mutex_protect.lock();
m_clientList.insert(sock_client); m_clientList.insert(sock_client);
m_mutex_protect.unlock(); m_mutex_protect.unlock();
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection) if (m_bSSLConnection)
{ {
QSslSocket * psslsock = qobject_cast<QSslSocket *>(sock_client); QSslSocket * psslsock = qobject_cast<QSslSocket *>(sock_client);
...@@ -175,6 +183,7 @@ namespace ZPNetwork{ ...@@ -175,6 +183,7 @@ namespace ZPNetwork{
connect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted,Qt::QueuedConnection); connect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted,Qt::QueuedConnection);
psslsock->startServerEncryption(); psslsock->startServerEncryption();
} }
#endif
qDebug()<<sock_client->peerAddress().toString()<< qDebug()<<sock_client->peerAddress().toString()<<
sock_client->peerPort() <<tr("(%1)..Accepted.").arg((quint64)sock_client); sock_client->peerPort() <<tr("(%1)..Accepted.").arg((quint64)sock_client);
emit evt_NewClientConnected(sock_client); emit evt_NewClientConnected(sock_client);
...@@ -198,14 +207,20 @@ namespace ZPNetwork{ ...@@ -198,14 +207,20 @@ namespace ZPNetwork{
if (threadid!=this) if (threadid!=this)
return; return;
QTcpSocket * sock_client = 0; QTcpSocket * sock_client = 0;
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection) if (m_bSSLConnection)
sock_client = new QSslSocket(this); sock_client = new QSslSocket(this);
else else
sock_client = new QTcpSocket(this); sock_client = new QTcpSocket(this);
#else
sock_client = new QTcpSocket(this);
#endif
if (sock_client) if (sock_client)
{ {
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection==true) if (m_bSSLConnection==true)
{ {
QSslSocket * psslsock = qobject_cast<QSslSocket *>(sock_client); QSslSocket * psslsock = qobject_cast<QSslSocket *>(sock_client);
assert(psslsock!=NULL); assert(psslsock!=NULL);
QString strCerPath = QCoreApplication::applicationDirPath() + "/ca_cert.pem"; QString strCerPath = QCoreApplication::applicationDirPath() + "/ca_cert.pem";
...@@ -237,6 +252,17 @@ namespace ZPNetwork{ ...@@ -237,6 +252,17 @@ namespace ZPNetwork{
sock_client->connectToHost(addr,port); 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 else
assert(false); assert(false);
...@@ -264,12 +290,14 @@ namespace ZPNetwork{ ...@@ -264,12 +290,14 @@ namespace ZPNetwork{
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender()); QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
if (pSock) if (pSock)
{ {
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection) if (m_bSSLConnection)
{ {
QSslSocket * psslsock = qobject_cast<QSslSocket *>(pSock); QSslSocket * psslsock = qobject_cast<QSslSocket *>(pSock);
if (psslsock) if (psslsock)
disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted); disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted);
} }
#endif
disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved); disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved);
disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed); disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed);
disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
...@@ -347,13 +375,14 @@ namespace ZPNetwork{ ...@@ -347,13 +375,14 @@ namespace ZPNetwork{
qDebug()<<pSock->peerAddress().toString()<< qDebug()<<pSock->peerAddress().toString()<<
pSock->peerPort() <<tr("(%1)..Error :%2.").arg((quint64)pSock).arg(pSock->errorString()); pSock->peerPort() <<tr("(%1)..Error :%2.").arg((quint64)pSock).arg(pSock->errorString());
emit evt_SocketError(pSock,socketError); emit evt_SocketError(pSock,socketError);
//emit evt_Message(pSock,"Debug:" + pSock->errorString()); #if (ZP_WANTSSL!=0)
if (m_bSSLConnection) if (m_bSSLConnection)
{ {
QSslSocket * psslsock = qobject_cast<QSslSocket *>(pSock); QSslSocket * psslsock = qobject_cast<QSslSocket *>(pSock);
if (psslsock) if (psslsock)
disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted); disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted);
} }
#endif
disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved); disconnect(pSock, &QTcpSocket::readyRead,this, &zp_netTransThread::new_data_recieved);
disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed); disconnect(pSock, &QTcpSocket::disconnected,this,&zp_netTransThread::client_closed);
disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
...@@ -415,30 +444,10 @@ namespace ZPNetwork{ ...@@ -415,30 +444,10 @@ namespace ZPNetwork{
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(obj); QTcpSocket * pSock = qobject_cast<QTcpSocket*>(obj);
if (pSock) if (pSock)
{ {
// if (m_bSSLConnection)
// {
// QSslSocket * psslsock = qobject_cast<QSslSocket *>(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->disconnectFromHost();
// pSock->abort();
// emit evt_ClientDisconnected(pSock);
//emit evt_Message(pSock,"Info>" + QString(tr("Client Closed.")));
// qDebug()<<tr("(%1)..Closed.").arg((quint64)pSock);
// push_to_rabish_can(pSock);
} }
} }
//m_clientList.clear();
// m_mutex_protect.unlock();
} }
void zp_netTransThread::KickClient(QObject * objClient) void zp_netTransThread::KickClient(QObject * objClient)
...@@ -454,28 +463,7 @@ namespace ZPNetwork{ ...@@ -454,28 +463,7 @@ namespace ZPNetwork{
if (pSock) if (pSock)
{ {
// if (m_bSSLConnection)
// {
// QSslSocket * psslsock = qobject_cast<QSslSocket *>(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->disconnectFromHost();
// pSock->abort();
// emit evt_ClientDisconnected(pSock);
// //emit evt_Message(pSock,"Info>" + QString(tr("Client Closed.")));
// qDebug()<<tr("(%1)..Closed.").arg((quint64)pSock);
// push_to_rabish_can(pSock);
} }
} }
......
#include "zpmainframe.h" #include "zpmainframe.h"
#include "ui_zpmainframe.h" #include "ui_zpmainframe.h"
#include "network/ssl_config.h"
#include <QDateTime> #include <QDateTime>
#include <QDialog> #include <QDialog>
#include <QSettings> #include <QSettings>
...@@ -9,7 +10,9 @@ ...@@ -9,7 +10,9 @@
#include <QMap> #include <QMap>
#include <QTcpSocket> #include <QTcpSocket>
#include <QThread> #include <QThread>
#if (ZP_WANTSSL!=0)
#include <QSslSocket> #include <QSslSocket>
#endif
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include "smartlink/st_clientnode_basetrans.h" #include "smartlink/st_clientnode_basetrans.h"
#include "dialogaddressinput.h" #include "dialogaddressinput.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册