提交 5065d4de 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

Switch the TCP socket to SSL Socket.

上级 a43abd8f
......@@ -2,12 +2,12 @@
#include <assert.h>
QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad)
: QTcpSocket(parent),
m_nPayLoad(nPayLoad)
: QSslSocket(parent),
m_nPayLoad(nPayLoad)
{
assert(m_nPayLoad>=256 && m_nPayLoad<=16*1024*1024);
connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
assert(m_nPayLoad>=256 && m_nPayLoad<=16*1024*1024);
connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
}
......@@ -17,41 +17,41 @@ QGHTcpClient::~QGHTcpClient()
}
void QGHTcpClient::some_data_sended(qint64 wsended)
{
while (m_buffer_sending.empty()==false)
{
QByteArray & arraySending = *m_buffer_sending.begin();
qint64 & currentOffset = *m_buffer_sending_offset.begin();
qint64 nTotalBytes = arraySending.size();
assert(nTotalBytes>=currentOffset);
qint64 nBytesWritten = write(arraySending.constData()+currentOffset,qMin((int)(nTotalBytes-currentOffset),m_nPayLoad));
currentOffset += nBytesWritten;
if (currentOffset>=nTotalBytes)
{
m_buffer_sending.pop_front();
m_buffer_sending_offset.pop_front();
}
else
break;
}
while (m_buffer_sending.empty()==false)
{
QByteArray & arraySending = *m_buffer_sending.begin();
qint64 & currentOffset = *m_buffer_sending_offset.begin();
qint64 nTotalBytes = arraySending.size();
assert(nTotalBytes>=currentOffset);
qint64 nBytesWritten = write(arraySending.constData()+currentOffset,qMin((int)(nTotalBytes-currentOffset),m_nPayLoad));
currentOffset += nBytesWritten;
if (currentOffset>=nTotalBytes)
{
m_buffer_sending.pop_front();
m_buffer_sending_offset.pop_front();
}
else
break;
}
}
void QGHTcpClient::SendData(QByteArray dtarray)
{
if (dtarray.size())
{
if (m_buffer_sending.empty()==true)
{
qint64 bytesWritten = write(dtarray.constData(),qMin(dtarray.size(),m_nPayLoad));
if (bytesWritten < dtarray.size())
{
m_buffer_sending.push_back(dtarray);
m_buffer_sending_offset.push_back(bytesWritten);
}
}
else
{
m_buffer_sending.push_back(dtarray);
m_buffer_sending_offset.push_back(0);
}
}
if (dtarray.size())
{
if (m_buffer_sending.empty()==true)
{
qint64 bytesWritten = write(dtarray.constData(),qMin(dtarray.size(),m_nPayLoad));
if (bytesWritten < dtarray.size())
{
m_buffer_sending.push_back(dtarray);
m_buffer_sending_offset.push_back(bytesWritten);
}
}
else
{
m_buffer_sending.push_back(dtarray);
m_buffer_sending_offset.push_back(0);
}
}
}
#ifndef QGHTCPCLIENT_H
#define QGHTCPCLIENT_H
#include <QTcpSocket>
#include <QSslSocket>
#include <QList>
class QGHTcpClient : public QTcpSocket
class QGHTcpClient : public QSslSocket
{
Q_OBJECT
Q_OBJECT
public:
QGHTcpClient(QObject *parent,int nPayLoad = 4096);
~QGHTcpClient();
QGHTcpClient(QObject *parent,int nPayLoad = 4096);
~QGHTcpClient();
private:
int m_nPayLoad;
QList<QByteArray> m_buffer_sending;
QList<qint64> m_buffer_sending_offset;
int m_nPayLoad;
QList<QByteArray> m_buffer_sending;
QList<qint64> m_buffer_sending_offset;
public slots:
void some_data_sended(qint64);
void SendData(QByteArray dtarray);
void some_data_sended(qint64);
void SendData(QByteArray dtarray);
};
......
#include "qtcpclienttest.h"
#include <QSettings>
#include <QCoreApplication>
QTcpClientTest::QTcpClientTest(QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
{
......@@ -116,13 +117,19 @@ void QTcpClientTest::timerEvent(QTimerEvent * evt)
}
}
QGHTcpClient * client = new QGHTcpClient(this,ui.horizontalSlider->value());
client->connectToHost(ui.lineEdit_ip->text(),ui.lineEdit_Port->text().toUShort());
//client->connectToHost(ui.lineEdit_ip->text(),ui.lineEdit_Port->text().toUShort());
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(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)));
connect(client, SIGNAL(encrypted()), this, SLOT(on_client_connected()));
QString strCerPath = QCoreApplication::applicationDirPath() + "/cert.pem";
client->setLocalCertificate(strCerPath);
client->setPrivateKey(strCerPath);
client->setPeerVerifyMode(QSslSocket::VerifyNone);
client->connectToHostEncrypted(ui.lineEdit_ip->text(),ui.lineEdit_Port->text().toUShort());
}
}
}
......
#include "zp_nettransthread.h"
#include <QTcpSocket>
#include <QSslSocket>
#include <assert.h>
#include <QDebug>
#include <QCoreApplication>
zp_netTransThread::zp_netTransThread(int nPayLoad,QObject *parent) :
QObject(parent)
{
m_nPayLoad = nPayLoad;
m_bActivated = true;
m_bSSLConnection = true;
assert(m_nPayLoad>=256 && m_nPayLoad<=16*1024*1024);
}
QList <QObject *> zp_netTransThread::clientsList()
......@@ -43,7 +47,11 @@ void zp_netTransThread::incomingConnection(QObject * threadid,qintptr socketDesc
{
if (threadid!=this)
return;
QTcpSocket * sock_client = new QTcpSocket(this);
QTcpSocket * sock_client = 0;
if (m_bSSLConnection)
sock_client = new QSslSocket(this);
else
sock_client = new QTcpSocket(this);
if (sock_client)
{
if (true ==sock_client->setSocketDescriptor(socketDescriptor))
......@@ -55,25 +63,56 @@ void zp_netTransThread::incomingConnection(QObject * threadid,qintptr socketDesc
m_mutex_protect.lock();
m_clientList[sock_client] = 0;
m_mutex_protect.unlock();
emit evt_NewClientConnected(sock_client);
if (m_bSSLConnection)
{
QSslSocket * psslsock = qobject_cast<QSslSocket *>(sock_client);
assert(psslsock!=nullptr);
QList<QSslCertificate> lstCerts = (QSslSocket::defaultCaCertificates());
if (lstCerts.size())
{
QString strCerPath = QCoreApplication::applicationDirPath() + "/cert.pem";
psslsock->setLocalCertificate(strCerPath);
psslsock->setPrivateKey(strCerPath);
}
connect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted);
psslsock->startServerEncryption();
}
else
emit evt_NewClientConnected(sock_client);
}
else
sock_client->deleteLater();
}
}
void zp_netTransThread::on_encrypted()
{
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
emit evt_NewClientConnected(pSock);
}
void zp_netTransThread::client_closed()
{
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
if (pSock)
{
emit evt_ClientDisconnected(pSock);
m_buffer_sending.remove(pSock);
if (m_bSSLConnection)
{
QSslSocket * psslsock = qobject_cast<QSslSocket *>(pSock);
if (psslsock)
disconnect(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted);
}
disconnect(pSock, SIGNAL(readyRead()),this, SLOT(new_data_recieved()));
disconnect(pSock, SIGNAL(disconnected()),this,SLOT(client_closed()));
disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
disconnect(pSock, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
m_buffer_sending.remove(pSock);
m_buffer_sending_offset.remove(pSock);
m_mutex_protect.lock();
m_clientList.remove(pSock);
m_mutex_protect.unlock();
pSock->deleteLater();
emit evt_ClientDisconnected(pSock);
}
}
void zp_netTransThread::new_data_recieved()
......@@ -114,6 +153,7 @@ void zp_netTransThread::displayError(QAbstractSocket::SocketError socketError)
if (pSock)
{
emit evt_SocketError(pSock,socketError);
qDebug()<<(pSock->errorString());
pSock->disconnectFromHost();
}
}
......
......@@ -18,9 +18,12 @@ public:
bool isActive(){return m_bActivated;}
bool CanExit();
bool SSLConnection(){return m_bSSLConnection ;}
void SetSSLConnection(bool bssl){ m_bSSLConnection = bssl;}
private:
bool m_bActivated;
bool m_bSSLConnection;
QMap<QObject *,QList<QByteArray> > m_buffer_sending;
QMap<QObject *,QList<qint64> > m_buffer_sending_offset;
QMap<QObject*,int> m_clientList;
......@@ -48,6 +51,8 @@ protected slots:
void some_data_sended(qint64);
//客户端错误
void displayError(QAbstractSocket::SocketError socketError);
//SSL加密开始
void on_encrypted();
signals:
//错误信息
void evt_SocketError(QObject * senderSock ,QAbstractSocket::SocketError socketError);
......
#include "zpmainframe.h"
#include "ui_zpmainframe.h"
ZPMainFrame::ZPMainFrame(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ZPMainFrame)
{
ui->setupUi(this);
m_pool = new zp_net_ThreadPool (4096);
connect (m_pool,&zp_net_ThreadPool::evt_Message,this,&ZPMainFrame::on_evt_Message);
......
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDRY4hWKwxi2Hz+snTuzcXg6KOH0Hv5Yz+B9pHc/J3V7J4uOERT
kjTWE8ef6WvNMqcEypWk3RKVZPm7sqU8mOdVJsY6z9gYGyBd6OIpISCzE5W/qRYg
Ld3Ze9oPaF8NZ+kmzgynwohtfvm5IKGWOq0B0Tjd/jJ+H1aGCgg1B7BU3QIDAQAB
AoGBALz9KUEc+cD9xVPdns0rzT6Cu5cKBdvCSL/03RKor402VE9DWehUPnw6viMH
e9VpsnYQo6B6KRvOV9miFKHtIF2HL16/IYG20LEAhUoAOL5jUi8D4D1b+a4TQMbB
oQsPS3ybQc4xUbuoipZjRnZ67HdiNNcbUoRxpfKrdQKboCYBAkEA/iT8gYXuSTnf
Mc6Xod9TTe2sEg23PMLqMZoqvmUrKoFcPGNs6+N/L1nay97r1WrHWV5XOD/NMe9N
rBU/bFeFWQJBANLq5Q4hIk4vcht0d14k65hv1tXpLMi0aHuOpq3R8YqnpzmExBG1
+nQos69qixc02qNs3uv9xXmmNvnbN3TVpyUCQQDRUfFPVNowIaQ9R6UIRAI+2xSw
Qe+78eYmOI4gBv+IoMcxTcq2dseYXm+hHtgrDSln6BX6VEJTcIAOzelQ+LzxAkAw
JvRYP3RHiUlxvyspUSCAUrgnae6Re66G1tByyoVsdE4XMZpZuM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wDwYDVQQIEwhTaGFuZ0hh
aTEQMA4GA1UEBxMHYmFvU2hhbjEbMBkGA1UEChMSZ29sZGVuaGF3a2luZyBDbHVi
MRAwDgYDVQQLEwdEZXYgU2VjMRYwFAYDVQQDEw1nb2xkZW5oYXdraW5nMSQwIgYJ
KoZIhvcNAQkBFhVnb2xkZW5oYXdraW5nQDE2My5jb22CCQCyrLiDt02izzAMBgNV
HRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAH8AWUZ5ee9mbkW/JEhuOAyBkfsQ
4VkYAXRCWG7+EXmAY54GnGogqUKnEq7x8ndRhFLQP9AiLIvQuCMP07U4x65K73I1
lvCKdQt4u3lvlP5RxrfDMzzMdsus834+mAsLKWIiYRwK89aOC60+PvyoyNcO3n1r
hxv6lPA3XuGYAcjo
-----END CERTIFICATE-----
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册