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

Add ssl switch for the network socket code.

上级 45ab78e3
......@@ -2,8 +2,10 @@
#include <assert.h>
#include <QFile>
#include <QThread>
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
#ifndef QGHTCPCLIENT_H
#define QGHTCPCLIENT_H
#include <QSslSocket>
#include <QList>
class QGHSslClient : public QSslSocket
#include <QTcpSocket>
#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<QByteArray> m_buffer_sending;
QList<qint64> m_buffer_sending_offset;
public slots:
......@@ -24,18 +24,21 @@ public slots:
};
class QGHTcpClient : public QTcpSocket
#if (ZP_WANTSSL!=0)
#include <QSslSocket>
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<QByteArray> m_buffer_sending;
QList<qint64> m_buffer_sending_offset;
public slots:
......@@ -43,5 +46,5 @@ public slots:
void SendData(QByteArray dtarray);
};
#endif
#endif // QGHTCPCLIENT_H
......@@ -65,17 +65,24 @@ void QTcpClientTest::on_client_trasferred(qint64 dtw)
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
if (pSock)
{
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender());
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(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<QGHTcpClient*>(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<QGHSslClient*>(sender());
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender());
......@@ -113,10 +120,30 @@ void QTcpClientTest::on_client_connected()
pMsg->tmStamp = 0;
(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()
{
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sender());
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sender());
if (pSockSsl)
......@@ -143,18 +170,39 @@ void QTcpClientTest::on_client_disconnected()
m_clients.remove(pSockTcp);
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*/)
{
QTcpSocket * sock = qobject_cast<QTcpSocket *> (sender());
if (sock)
{
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(sock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(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<QGHTcpClient*>(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<QGHSslClient*>(pSock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(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<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)
foreach(QTcpSocket * pSock,listObj)
{
quint32 uuid = 0;
#if (ZP_WANTSSL!=0)
QGHSslClient * pSockSsl = qobject_cast<QGHSslClient*>(pSock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(pSock);
if (pSockSsl)
......@@ -199,6 +253,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
uuid = pSockTcp->uuid();
else
continue;
#else
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(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<QGHSslClient*>(sock);
QGHTcpClient * pSockTcp = qobject_cast<QGHTcpClient*>(sock);
if (pSockSsl)
......@@ -225,6 +292,13 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
uuid = pSockTcp->uuid();
else
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);
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;i<nMsgLen;i++)
pMsg->data[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
}
}
}
......
......@@ -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
......
#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 "ssl_config.h"
#include <QTcpSocket>
#if (ZP_WANTSSL!=0)
#include <QSslSocket>
#endif
#include <assert.h>
#include <QDebug>
#include <QCoreApplication>
......@@ -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<QSslSocket *>(sock_client);
......@@ -175,6 +183,7 @@ namespace ZPNetwork{
connect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted,Qt::QueuedConnection);
psslsock->startServerEncryption();
}
#endif
qDebug()<<sock_client->peerAddress().toString()<<
sock_client->peerPort() <<tr("(%1)..Accepted.").arg((quint64)sock_client);
emit evt_NewClientConnected(sock_client);
......@@ -198,14 +207,20 @@ 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)
{
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection==true)
{
QSslSocket * psslsock = qobject_cast<QSslSocket *>(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<QTcpSocket*>(sender());
if (pSock)
{
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection)
{
QSslSocket * psslsock = qobject_cast<QSslSocket *>(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()<<pSock->peerAddress().toString()<<
pSock->peerPort() <<tr("(%1)..Error :%2.").arg((quint64)pSock).arg(pSock->errorString());
emit evt_SocketError(pSock,socketError);
//emit evt_Message(pSock,"Debug:" + pSock->errorString());
#if (ZP_WANTSSL!=0)
if (m_bSSLConnection)
{
QSslSocket * psslsock = qobject_cast<QSslSocket *>(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<QTcpSocket*>(obj);
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->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)
......@@ -454,28 +463,7 @@ namespace ZPNetwork{
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->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 "ui_zpmainframe.h"
#include "network/ssl_config.h"
#include <QDateTime>
#include <QDialog>
#include <QSettings>
......@@ -9,7 +10,9 @@
#include <QMap>
#include <QTcpSocket>
#include <QThread>
#if (ZP_WANTSSL!=0)
#include <QSslSocket>
#endif
#include <QSystemTrayIcon>
#include "smartlink/st_clientnode_basetrans.h"
#include "dialogaddressinput.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册