提交 418075c4 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

Add possive connection , which meet the demand of server-to-server connections.

上级 08f404e1
......@@ -176,6 +176,7 @@ void zp_net_ThreadPool::AddClientTransThreads(int nThreads,bool bSSL)
connect (clientTH,&zp_netTransThread::evt_NewClientConnected,this,&zp_net_ThreadPool::evt_NewClientConnected,Qt::QueuedConnection);
connect (clientTH,&zp_netTransThread::evt_SocketError,this,&zp_net_ThreadPool::evt_SocketError,Qt::QueuedConnection);
connect (this,&zp_net_ThreadPool::evt_EstablishConnection,clientTH,&zp_netTransThread::incomingConnection,Qt::QueuedConnection);
connect (this,&zp_net_ThreadPool::evt_FireConnection,clientTH,&zp_netTransThread::startConnection,Qt::QueuedConnection);
connect (this,&zp_net_ThreadPool::evt_BroadcastData,clientTH,&zp_netTransThread::BroadcastData,Qt::QueuedConnection);
connect (this,&zp_net_ThreadPool::evt_SendDataToClient,clientTH,&zp_netTransThread::SendDataToClient,Qt::QueuedConnection);
connect (this,&zp_net_ThreadPool::evt_KickAll,clientTH,&zp_netTransThread::KickAllClients,Qt::QueuedConnection);
......@@ -206,6 +207,7 @@ bool zp_net_ThreadPool::TransThreadDel(zp_netTransThread * pThreadObj)
disconnect (clientTH,&zp_netTransThread::evt_NewClientConnected,this,&zp_net_ThreadPool::evt_NewClientConnected);
disconnect (clientTH,&zp_netTransThread::evt_SocketError,this,&zp_net_ThreadPool::evt_SocketError);
disconnect (this,&zp_net_ThreadPool::evt_EstablishConnection,clientTH,&zp_netTransThread::incomingConnection);
disconnect (this,&zp_net_ThreadPool::evt_FireConnection,clientTH,&zp_netTransThread::startConnection);
disconnect (this,&zp_net_ThreadPool::evt_BroadcastData,clientTH,&zp_netTransThread::BroadcastData);
disconnect (this,&zp_net_ThreadPool::evt_SendDataToClient,clientTH,&zp_netTransThread::SendDataToClient);
disconnect (this,&zp_net_ThreadPool::evt_KickAll,clientTH,&zp_netTransThread::KickAllClients);
......@@ -288,4 +290,45 @@ bool zp_net_ThreadPool::CanExit()
//m_mutex_listen.unlock();
return res;
}
bool zp_net_ThreadPool::connectTo (const QHostAddress & address , quint16 nPort,bool bSSLConn)
{
bool res= false;
//m_mutex_trans.lock();
int nsz = m_vec_NetTransThreads.size();
int nMinPay = 0x7fffffff;
int nMinIdx = -1;
for (int i=0;i<nsz && nMinPay!=0;i++)
{
if (m_vec_NetTransThreads[i]->isActive()==false ||
m_vec_NetTransThreads[i]->SSLConnection()!=bSSLConn
)
continue;
int nPat = m_vec_NetTransThreads[i]->CurrentClients();
if (nPat<nMinPay)
{
nMinPay = nPat;
nMinIdx = i;
}
//qDebug()<<i<<" "<<nPat<<" "<<nMinIdx;
}
for (int i=0;i<nsz;i++)
if (m_vec_NetTransThreads[i]->isActive()==false )
TransThreadDel(m_vec_NetTransThreads[i]);
if (nMinIdx>=0 && nMinIdx<nsz)
{
res = true;
emit evt_FireConnection(m_vec_NetTransThreads[nMinIdx],address,nPort);
}
else
{
emit evt_Message("Waring>"+QString(tr("Need Trans Thread Object for clients.")));
}
//m_mutex_trans.unlock();
return res;
}
}
......@@ -83,6 +83,7 @@ signals:
void startListen(const QString & id);
void stopListen(const QString & id);
void evt_EstablishConnection(QObject * threadid,qintptr socketDescriptor);
void evt_FireConnection(QObject * threadid,const QHostAddress & hostAddr, quint16 port);
//Trans Control,for intenal thread usage
void evt_SendDataToClient(QObject * objClient,const QByteArray & dtarray);
void evt_BroadcastData(QObject * objFromClient,const QByteArray & dtarray);
......@@ -102,6 +103,9 @@ public slots:
//Close client Immediatele
void KickClients(QObject * object);
//Possive Connection Methods
bool connectTo (const QHostAddress & address , quint16 nPort,bool bSSLConn = true);
};
}
#endif // ZP_NET_THREADPOOL_H
......@@ -4,6 +4,7 @@
#include <assert.h>
#include <QDebug>
#include <QCoreApplication>
#include <QHostAddress>
namespace ZPNetwork{
zp_netTransThread::zp_netTransThread(zp_net_ThreadPool *pThreadPool,int nPayLoad,QObject *parent) :
QObject(parent)
......@@ -58,10 +59,10 @@ void zp_netTransThread::incomingConnection(QObject * threadid,qintptr socketDesc
//Initial content
if (true ==sock_client->setSocketDescriptor(socketDescriptor))
{
connect(sock_client, SIGNAL(readyRead()),this, SLOT(new_data_recieved()),Qt::QueuedConnection);
connect(sock_client, SIGNAL(disconnected()),this,SLOT(client_closed()),Qt::QueuedConnection);
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, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)),Qt::QueuedConnection);
connect(sock_client, &QTcpSocket::bytesWritten, this, &zp_netTransThread::some_data_sended,Qt::QueuedConnection);
m_mutex_protect.lock();
m_clientList[sock_client] = 0;
m_mutex_protect.unlock();
......@@ -84,6 +85,52 @@ void zp_netTransThread::incomingConnection(QObject * threadid,qintptr socketDesc
}
}
void zp_netTransThread::startConnection(QObject * threadid,const QHostAddress & addr, quint16 port)
{
if (threadid!=this)
return;
QTcpSocket * sock_client = 0;
if (m_bSSLConnection)
sock_client = new QSslSocket(this);
else
sock_client = new QTcpSocket(this);
if (sock_client)
{
if (m_bSSLConnection==true)
{
QSslSocket * psslsock = qobject_cast<QSslSocket *>(sock_client);
assert(psslsock!=NULL);
QString strCerPath = QCoreApplication::applicationDirPath() + "/ca_cert.pem";
QList<QSslCertificate> lstCas = QSslCertificate::fromPath(strCerPath);
psslsock->setCaCertificates(lstCas);
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(psslsock, &QSslSocket::encrypted,this, &zp_netTransThread::on_encrypted,Qt::QueuedConnection);
m_mutex_protect.lock();
m_clientList[sock_client] = 0;
m_mutex_protect.unlock();
psslsock->connectToHostEncrypted(addr.toString(),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_encrypted,Qt::QueuedConnection);
sock_client->connectToHost(addr,port);
}
}
else
assert(false);
}
void zp_netTransThread::on_encrypted()
{
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
......@@ -101,10 +148,10 @@ void zp_netTransThread::client_closed()
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, &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, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
disconnect(pSock, &QTcpSocket::bytesWritten, this, &zp_netTransThread::some_data_sended);
m_buffer_sending.remove(pSock);
m_buffer_sending_offset.remove(pSock);
m_mutex_protect.lock();
......
......@@ -37,6 +37,8 @@ private:
public slots:
//This slot dealing with multi-thread client socket accept.
void incomingConnection(QObject * threadid,qintptr socketDescriptor);
//This slot dealing with possive connect to method.
void startConnection(QObject * threadid,const QHostAddress & addr, quint16 port);
//sending dtarray to objClient. dtarray will be pushed into m_buffer_sending
void SendDataToClient(QObject * objClient,const QByteArray & dtarray);
//Broadcast dtarray to every client except objFromClient itself
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册