提交 17d5bc4c 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

Create response

上级
######################################################################
# Automatically generated by qmake (2.01a) ??? ?? 8 11:26:23 2012
######################################################################
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ZoomPipeline_FuncSvr
TEMPLATE = app
# Input
HEADERS += qghtcpclient.h qtcpclienttest.h
FORMS += qtcpclienttest.ui
SOURCES += main.cpp qghtcpclient.cpp qtcpclienttest.cpp
RESOURCES += qtcpclienttest.qrc
#include "qtcpclienttest.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTcpClientTest w;
w.show();
return a.exec();
}
#include "qghtcpclient.h"
#include <assert.h>
QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad)
: QTcpSocket(parent),
m_nPayLoad(nPayLoad)
{
assert(m_nPayLoad>=256 && m_nPayLoad<=16*1024*1024);
connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
}
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;
}
}
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);
}
}
}
#ifndef QGHTCPCLIENT_H
#define QGHTCPCLIENT_H
#include <QTcpSocket>
#include <QList>
class QGHTcpClient : public QTcpSocket
{
Q_OBJECT
public:
QGHTcpClient(QObject *parent,int nPayLoad = 4096);
~QGHTcpClient();
private:
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);
};
#endif // QGHTCPCLIENT_H
#include "qtcpclienttest.h"
#include <QSettings>
QTcpClientTest::QTcpClientTest(QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
//Paramenters
QSettings settings("goldenhawking club","QTcpClientTest",this);
ui.lineEdit_ip->setText(settings.value("ip","localhost").toString());
ui.lineEdit_Port->setText(settings.value("port","23").toString());
ui.dial->setValue(settings.value("clientNum","2").toInt());
ui.lcdNumber->display(settings.value("clientNum","2").toInt());
ui.horizontalSlider->setValue(settings.value("Payload","4096").toInt());
ui.label_load->setText(QString("Payload = %1").arg(settings.value("Payload","4096").toInt()));
ui.listView_msg->setModel(&model);
}
QTcpClientTest::~QTcpClientTest()
{
}
void QTcpClientTest::on_horizontalSlider_valueChanged(int value)
{
ui.label_load->setText(QString("Payload = %1").arg(value));
}
void QTcpClientTest::on_action_Connect_triggered(bool bConn)
{
//connect to the server
QSettings settings("goldenhawking club","QTcpClientTest",this);
settings.setValue("ip",ui.lineEdit_ip->text());
settings.setValue("port",ui.lineEdit_Port->text());
settings.setValue("clientNum",ui.dial->value());
settings.setValue("Payload",ui.horizontalSlider->value());
if (bConn==true)
{
nTimer = startTimer(300);
}
else
killTimer(nTimer);
}
void QTcpClientTest::on_client_trasferred(qint64 dtw)
{
QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
if (pSock)
{
displayMessage(QString("client %1 Transferrd %2 bytes.").arg((quintptr)pSock).arg(dtw));
}
}
void QTcpClientTest::on_client_connected()
{
QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
if (pSock)
{
displayMessage(QString("client %1 disconnected.").arg((quintptr)pSock));
pSock->SendData(QByteArray(qrand()%1024+1024,qrand()%(128-32)+32));
}
}
void QTcpClientTest::on_client_disconnected()
{
QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
if (pSock)
{
displayMessage(QString("client %1 disconnected.").arg((quintptr)pSock));
//disconnect the signal immediately so that the system resource could be freed.
disconnect(pSock, SIGNAL(readyRead()),this, SLOT(new_data_recieved()));
disconnect(pSock, SIGNAL(connected()),this, SLOT(on_client_connected()));
disconnect(pSock, SIGNAL(disconnected()),this,SLOT(on_client_disconnected()));
disconnect(pSock, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
disconnect(pSock, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64)));
m_clients.remove(pSock);
pSock->deleteLater();
}
}
void QTcpClientTest::displayError(QAbstractSocket::SocketError err)
{
QGHTcpClient * sock = qobject_cast<QGHTcpClient *> (sender());
if (sock)
displayMessage(QString("client %1 error msg:").arg((quintptr)sock)+sock->errorString());
}
void QTcpClientTest::new_data_recieved()
{
QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
if (pSock)
{
QByteArray array =pSock->readAll();
//in this example, we just do nothing but to display the byte size.
displayMessage(QString("client %1 Recieved %2 bytes.").arg((quintptr)pSock).arg(array.size()));
}
}
void QTcpClientTest::timerEvent(QTimerEvent * evt)
{
if (evt->timerId()==nTimer)
{
int nTotalClients = ui.dial->value();
QList<QGHTcpClient*> listObj = m_clients.keys();
foreach(QGHTcpClient * sock,listObj)
{
if (rand()%10<3)
//3/10 possibility to send a data block to server
sock->SendData(QByteArray(qrand()%1024+1024,qrand()%(128-32)+32));
}
//
if (rand()%10 <1)
{
//1/10 chance to make new connections.
if (m_clients.size()>nTotalClients)
{
int nDel = m_clients.size()-nTotalClients;
QList<QGHTcpClient*> listObj = m_clients.keys();
for (int i=0;i<nDel;i++)
{
listObj.at(i)->disconnectFromHost();
}
}
QGHTcpClient * client = new QGHTcpClient(this,ui.horizontalSlider->value());
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(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)));
}
}
}
void QTcpClientTest::displayMessage(const QString &str)
{
model.insertRow(0,new QStandardItem(str));
while (model.rowCount()>=256)
model.removeRow(model.rowCount()-1);
}
#ifndef QTCPCLIENTTEST_H
#define QTCPCLIENTTEST_H
#include <QMainWindow>
#include "qghtcpclient.h"
#include "ui_qtcpclienttest.h"
#include <QMap>
#include <QDateTime>
#include <QStandardItemModel>
#include <QStandardItem>
class QTcpClientTest : public QMainWindow
{
Q_OBJECT
public:
QTcpClientTest(QWidget *parent = 0, Qt::WindowFlags flags = 0);
~QTcpClientTest();
virtual void timerEvent(QTimerEvent * evt);
private:
Ui::QTcpClientTestClass ui;
QMap<QGHTcpClient *, QDateTime> m_clients;
int nTimer;
QStandardItemModel model;
public slots:
void on_horizontalSlider_valueChanged(int);
void on_action_Connect_triggered(bool);
void new_data_recieved();
void on_client_trasferred(qint64);
void on_client_connected();
void on_client_disconnected();
void displayError(QAbstractSocket::SocketError);
void displayMessage(const QString &str);
};
#endif // QTCPCLIENTTEST_H
<RCC>
<qresource prefix="QTcpClientTest">
<file>Resources/_40Icon Silver Reverse.png</file>
<file>Resources/+_Sign.png</file>
<file>Resources/+_Sign_Alt.png</file>
<file>Resources/0_9.png</file>
<file>Resources/3floppy_mount-1.png</file>
<file>Resources/3floppy_mount-2.png</file>
<file>Resources/3floppy_mount-3.png</file>
<file>Resources/3floppy_mount.png</file>
<file>Resources/3floppy_unmount-1.png</file>
<file>Resources/3floppy_unmount-2.png</file>
<file>Resources/3floppy_unmount-3.png</file>
<file>Resources/3floppy_unmount-4.png</file>
<file>Resources/3floppy_unmount.png</file>
<file>Resources/010-3.png</file>
<file>Resources/10-3.png</file>
<file>Resources/019-1.png</file>
<file>Resources/19-1.png</file>
<file>Resources/019-3.png</file>
<file>Resources/27_Martin.png</file>
<file>Resources/033-1.png</file>
<file>Resources/37.png</file>
<file>Resources/049-1.png</file>
<file>Resources/0062.png</file>
<file>Resources/0098.png</file>
<file>Resources/0104.png</file>
<file>Resources/0109.png</file>
<file>Resources/0126.png</file>
<file>Resources/165c, 180c.png</file>
<file>Resources/3018.png</file>
<file>Resources/4003.png</file>
<file>Resources/4011.png</file>
<file>Resources/4013.png</file>
<file>Resources/4018.png</file>
<file>Resources/4021.png</file>
<file>Resources/A-1 009.png</file>
<file>Resources/Alienware (13).png</file>
<file>Resources/Alienware (27).png</file>
<file>Resources/Alienware (28).png</file>
<file>Resources/Alienware (29).png</file>
<file>Resources/Backup-1.png</file>
<file>Resources/BackUp-4.png</file>
<file>Resources/Backup drive.png</file>
<file>Resources/Battery (1).png</file>
<file>Resources/Battery Charged.png</file>
<file>Resources/Battery -No charge.png</file>
<file>Resources/Blizzard17.png</file>
<file>Resources/bluetooth256.png</file>
<file>Resources/Burn CD-1.png</file>
<file>Resources/cn1.png</file>
<file>Resources/cn2.png</file>
<file>Resources/cn3.png</file>
<file>Resources/cn4.png</file>
<file>Resources/cn5.png</file>
<file>Resources/cn6.png</file>
<file>Resources/cn7.png</file>
<file>Resources/cn8.png</file>
<file>Resources/cn9.png</file>
<file>Resources/cn10.png</file>
<file>Resources/cn11.png</file>
<file>Resources/cn12.png</file>
<file>Resources/cn13.png</file>
<file>Resources/coffee.png</file>
<file>Resources/Color Classic Green.png</file>
<file>Resources/Color Classic, Performa 250, 275.png</file>
<file>Resources/Color Classic.png</file>
<file>Resources/Color, Blueberry.png</file>
<file>Resources/Color, Bondi-1.png</file>
<file>Resources/Color, Bondi.png</file>
<file>Resources/Color, Grape.png</file>
<file>Resources/Color, Graphite.png</file>
<file>Resources/Color, Indigo.png</file>
<file>Resources/Color, Lemon.png</file>
<file>Resources/Color, Lime.png</file>
<file>Resources/Color, Ruby.png</file>
<file>Resources/Color, Sage.png</file>
<file>Resources/Color, Strawberry.png</file>
<file>Resources/Color, Tangerine.png</file>
<file>Resources/Color, Titanium.png</file>
<file>Resources/Crystal_folder09.png</file>
<file>Resources/Crystal_folder10.png</file>
<file>Resources/Crystal_folder18.png</file>
<file>Resources/Crystal_folder19.png</file>
<file>Resources/Digital Image Bmp.png</file>
<file>Resources/DimageViewer.png</file>
<file>Resources/Folder Graphite-1.png</file>
<file>Resources/Folder Online aqua.png</file>
<file>Resources/hanukkah_03.png</file>
<file>Resources/terminalserver.png</file>
</qresource>
</RCC>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QTcpClientTestClass</class>
<widget class="QMainWindow" name="QTcpClientTestClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>QTcpClientTest</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Message</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QListView" name="listView_msg">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>19</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="action_Connect"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
<addaction name="menu_File"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<property name="windowTitle">
<string>General</string>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="action_Connect"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<widget class="QDockWidget" name="dockWidget">
<property name="windowTitle">
<string>Settings</string>
</property>
<attribute name="dockWidgetArea">
<number>1</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>IP</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_ip"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Port</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_Port"/>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>模拟客户数</string>
</property>
</widget>
</item>
<item>
<widget class="QDial" name="dial">
<property name="minimumSize">
<size>
<width>128</width>
<height>128</height>
</size>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>128</number>
</property>
<property name="notchesVisible">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLCDNumber" name="lcdNumber">
<property name="frameShape">
<enum>QFrame::Panel</enum>
</property>
<property name="smallDecimalPoint">
<bool>false</bool>
</property>
<property name="segmentStyle">
<enum>QLCDNumber::Flat</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_load">
<property name="text">
<string>发射载荷</string>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider">
<property name="minimum">
<number>256</number>
</property>
<property name="maximum">
<number>8192</number>
</property>
<property name="value">
<number>4096</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="tickPosition">
<enum>QSlider::TicksBelow</enum>
</property>
<property name="tickInterval">
<number>256</number>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>232</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
<action name="action_Connect">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="qtcpclienttest.qrc">
<normaloff>:/QTcpClientTest/Resources/cn5.png</normaloff>
<normalon>:/QTcpClientTest/Resources/cn6.png</normalon>:/QTcpClientTest/Resources/cn5.png</iconset>
</property>
<property name="text">
<string>&amp;Connect</string>
</property>
</action>
<action name="actionExit">
<property name="icon">
<iconset resource="qtcpclienttest.qrc">
<normaloff>:/QTcpClientTest/Resources/27_Martin.png</normaloff>:/QTcpClientTest/Resources/27_Martin.png</iconset>
</property>
<property name="text">
<string>E&amp;xit</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="qtcpclienttest.qrc"/>
</resources>
<connections>
<connection>
<sender>actionExit</sender>
<signal>activated()</signal>
<receiver>QTcpClientTestClass</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>299</x>
<y>199</y>
</hint>
</hints>
</connection>
<connection>
<sender>dial</sender>
<signal>valueChanged(int)</signal>
<receiver>lcdNumber</receiver>
<slot>display(int)</slot>
<hints>
<hint type="sourcelabel">
<x>91</x>
<y>219</y>
</hint>
<hint type="destinationlabel">
<x>91</x>
<y>301</y>
</hint>
</hints>
</connection>
</connections>
</ui>
#-------------------------------------------------
#
# Project created by QtCreator 2013-12-13T08:12:31
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ZoomPipeline_FuncSvr
TEMPLATE = app
SOURCES += main.cpp\
zpmainframe.cpp \
network/zp_tcpserver.cpp \
network/zp_nettransthread.cpp \
network/zp_netlistenthread.cpp \
network/zp_net_threadpool.cpp
HEADERS += zpmainframe.h \
network/zp_tcpserver.h \
network/zp_nettransthread.h \
network/zp_netlistenthread.h \
network/zp_net_threadpool.h
FORMS += zpmainframe.ui
#include "zpmainframe.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ZPMainFrame w;
w.show();
int pp = a.exec();
return pp;
}
#include "zp_net_threadpool.h"
zp_net_ThreadPool::zp_net_ThreadPool(int nPayLoad,QObject *parent) :
QObject(parent)
{
m_nPayLoad = nPayLoad;
//The signals and slots will be reged.
if (false==QMetaType::isRegistered(QMetaType::type("qintptr")))
qRegisterMetaType<qintptr>("qintptr");
if (false==QMetaType::isRegistered(QMetaType::type("QAbstractSocket::SocketError")))
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
}
QStringList zp_net_ThreadPool::ListenerNames()
{
return m_map_netListenThreads.keys();
}
int zp_net_ThreadPool::TransThreadNum()
{
return m_vec_NetTransThreads.size();
}
int zp_net_ThreadPool::totalClients(int idxThread)
{
int nsz = m_vec_NetTransThreads.size();
if (idxThread >=0 && idxThread<nsz)
return m_vec_NetTransThreads[idxThread]->CurrentClients();
else
return 0;
}
//Begin a listening socket at special address and port. The socket will be activated as soon as possible
void zp_net_ThreadPool::AddListeningAddress(const QString & id,const QHostAddress & address , quint16 nPort)
{
if (m_map_netListenThreads.find(id)==m_map_netListenThreads.end())
{
//Start Thread
QThread * pThread = new QThread(this);
zp_netListenThread * pListenObj = new zp_netListenThread(id,address,nPort);
pThread->start();
//m_mutex_listen.lock();
m_map_netInternalListenThreads[id] = pThread;
m_map_netListenThreads[id] = pListenObj;
//m_mutex_listen.unlock();
//Bind Object to New thread
connect(this,&zp_net_ThreadPool::startListen,pListenObj,&zp_netListenThread::startListen);
connect(this,&zp_net_ThreadPool::stopListen,pListenObj,&zp_netListenThread::stopListen);
connect(pListenObj,&zp_netListenThread::evt_Message,this,&zp_net_ThreadPool::evt_Message);
connect(pListenObj,&zp_netListenThread::evt_ListenClosed,this,&zp_net_ThreadPool::on_ListenClosed);
connect(pListenObj,&zp_netListenThread::evt_NewClientArrived,this,&zp_net_ThreadPool::on_New_Arrived_Client);
pListenObj->moveToThread(pThread);
//Start Listen Immediately
emit startListen(id);
}
else
emit evt_Message("Warning>"+QString(tr("This ID has been used.")));
}
//Remove a listening socket at special address and port.The socket will be deactivated as soon as possible
void zp_net_ThreadPool::RemoveListeningAddress(const QString & id)
{
//m_mutex_listen.lock();
if (m_map_netListenThreads.find(id)!=m_map_netListenThreads.end())
emit stopListen(id);
//m_mutex_listen.unlock();
}
void zp_net_ThreadPool::RemoveAllAddresses()
{
//m_mutex_listen.lock();
foreach (QString id,m_map_netListenThreads.keys())
emit stopListen(id);
//m_mutex_listen.unlock();
}
void zp_net_ThreadPool::on_New_Arrived_Client(qintptr socketDescriptor)
{
emit evt_Message("Info>"+QString(tr("New Client Arriverd.")));
//m_mutex_trans.lock();
int nsz = m_vec_NetTransThreads.size();
int nMinPay = 0x7fffffff;
int nMinIdx = -1;
for (int i=0;i<nsz && nMinIdx!=0;i++)
{
if (m_vec_NetTransThreads[i]->isActive()==false)
continue;
int nPat = m_vec_NetTransThreads[i]->CurrentClients();
if (nPat<nMinPay)
{
nMinPay = nPat;
nMinIdx = i;
}
}
if (nMinIdx>=0 && nMinIdx<nsz)
emit evt_EstablishConnection(m_vec_NetTransThreads[nMinIdx],socketDescriptor);
//m_mutex_trans.unlock();
}
void zp_net_ThreadPool::on_ListenClosed(const QString & id)
{
//m_mutex_listen.lock();
if (m_map_netListenThreads.find(id)!=m_map_netListenThreads.end())
{
//Clean objects;
zp_netListenThread * pListenObj = m_map_netListenThreads[id];
QThread * pThread = m_map_netInternalListenThreads[id];
m_map_netListenThreads.remove(id);
m_map_netInternalListenThreads.remove(id);
//disconnect signals;
disconnect(this,&zp_net_ThreadPool::startListen,pListenObj,&zp_netListenThread::startListen);
disconnect(this,&zp_net_ThreadPool::stopListen,pListenObj,&zp_netListenThread::stopListen);
disconnect(pListenObj,&zp_netListenThread::evt_Message,this,&zp_net_ThreadPool::evt_Message);
disconnect(pListenObj,&zp_netListenThread::evt_ListenClosed,this,&zp_net_ThreadPool::on_ListenClosed);
disconnect(pListenObj,&zp_netListenThread::evt_NewClientArrived,this,&zp_net_ThreadPool::on_New_Arrived_Client);
pListenObj->deleteLater();
pThread->quit();
pThread->deleteLater();
}
//m_mutex_listen.unlock();
}
//Add n client-Trans Thread(s).
void zp_net_ThreadPool::AddClientTransThreads(int nThreads)
{
if (nThreads>0 && nThreads<256)
{
for (int i=0;i<nThreads;i++)
{
zp_netTransThread * clientTH = new zp_netTransThread(m_nPayLoad);
QThread * pThread = new QThread(this);
//m_mutex_trans.lock();
m_vec_netInternalTransThreads.push_back(pThread);
m_vec_NetTransThreads.push_back(clientTH);
//m_mutex_trans.unlock();
pThread->start();
//Connect signals
connect (clientTH,&zp_netTransThread::evt_ClientDisconnected,this,&zp_net_ThreadPool::evt_ClientDisconnected);
connect (clientTH,&zp_netTransThread::evt_Data_recieved,this,&zp_net_ThreadPool::evt_Data_recieved);
connect (clientTH,&zp_netTransThread::evt_Data_transferred,this,&zp_net_ThreadPool::evt_Data_transferred);
connect (clientTH,&zp_netTransThread::evt_NewClientConnected,this,&zp_net_ThreadPool::evt_NewClientConnected);
connect (clientTH,&zp_netTransThread::evt_SocketError,this,&zp_net_ThreadPool::evt_SocketError);
connect (this,&zp_net_ThreadPool::evt_EstablishConnection,clientTH,&zp_netTransThread::incomingConnection);
connect (this,&zp_net_ThreadPool::evt_BroadcastData,clientTH,&zp_netTransThread::BroadcastData);
connect (this,&zp_net_ThreadPool::evt_SendDataToClient,clientTH,&zp_netTransThread::SendDataToClient);
connect (this,&zp_net_ThreadPool::evt_KickAll,clientTH,&zp_netTransThread::KickAllClients);
connect (this,&zp_net_ThreadPool::evt_DeactivteImmediately,clientTH,&zp_netTransThread::DeactivateImmediately);
clientTH->moveToThread(pThread);
}
}
}
bool zp_net_ThreadPool::TransThreadDel(zp_netTransThread * pThreadObj)
{
if (pThreadObj->CanExit()==false)
return false;
int nsz = m_vec_NetTransThreads.size();
int idx = -1;
for (int i=0;i<nsz && idx<0;i++)
{
if (m_vec_NetTransThreads[i]==pThreadObj)
idx = i;
}
if (idx>=0 && idx <nsz)
{
zp_netTransThread * clientTH = m_vec_NetTransThreads[idx];
disconnect (clientTH,&zp_netTransThread::evt_ClientDisconnected,this,&zp_net_ThreadPool::evt_ClientDisconnected);
disconnect (clientTH,&zp_netTransThread::evt_Data_recieved,this,&zp_net_ThreadPool::evt_Data_recieved);
disconnect (clientTH,&zp_netTransThread::evt_Data_transferred,this,&zp_net_ThreadPool::evt_Data_transferred);
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_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);
disconnect (this,&zp_net_ThreadPool::evt_DeactivteImmediately,clientTH,&zp_netTransThread::DeactivateImmediately);
m_vec_netInternalTransThreads[idx]->quit();
m_vec_netInternalTransThreads[idx]->deleteLater();
m_vec_NetTransThreads[idx]->deleteLater();
m_vec_netInternalTransThreads.remove(idx);
m_vec_NetTransThreads.remove(idx);
}
return true;
}
void zp_net_ThreadPool::KickAllClients()
{
//m_mutex_trans.lock();
int nsz = m_vec_NetTransThreads.size();
for (int i=0;i<nsz;i++)
emit evt_KickAll(m_vec_NetTransThreads[i]);
//m_mutex_trans.unlock();
}
void zp_net_ThreadPool::DeactiveImmediately()
{
//m_mutex_trans.lock();
int nsz = m_vec_NetTransThreads.size();
for (int i=0;i<nsz;i++)
emit evt_DeactivteImmediately(m_vec_NetTransThreads[i]);
//m_mutex_trans.unlock();
}
//Remove n client-Trans Thread(s).a thread marked reomved will be terminated after its last client socket exited.
void zp_net_ThreadPool::RemoveClientTransThreads(int nThreads)
{
if (nThreads>0)
{
//m_mutex_trans.lock();
int nsz = m_vec_NetTransThreads.size();
for (int i=0;i<nsz && i<nThreads;i++)
m_vec_NetTransThreads[i]->Deactivate();
//m_mutex_trans.unlock();
}
}
void zp_net_ThreadPool::SendDataToClient(QObject * objClient,const QByteArray & dtarray)
{
emit evt_SendDataToClient(objClient,dtarray);
}
//向客户端广播数据,不包括 objFromClient
void zp_net_ThreadPool::BroadcastData(QObject * objFromClient,const QByteArray & dtarray)
{
emit evt_BroadcastData(objFromClient,dtarray);
}
bool zp_net_ThreadPool::CanExit()
{
bool res = true;
//m_mutex_trans.lock();
int nsz = m_vec_NetTransThreads.size();
for (int i=nsz-1;i>=0 && res==true;i--)
res = TransThreadDel( m_vec_NetTransThreads[i]);
//m_mutex_trans.unlock();
//m_mutex_listen.lock();
if (m_map_netListenThreads.size())
res= false;
//m_mutex_listen.unlock();
return res;
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册