In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of an example analysis of multithreading on the Qt server. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
This example uses only two threads, one for listening for new connections and one for handling client events that have established connections (here a thread pool can be used to improve performance). Message receiving adds a simple subpacket mechanism. The first four bytes of each message store the uint32_t type, which refers to the entire length of the message, so that each message can be well distinguished. The code is somewhat imprecise in many details, for reference only.
/ / tcpserver.hclass CClientSocket;class CTcpServer final: public QTcpServer {Q_OBJECTpublic: explicit CTcpServer (QObject * parent = nullptr); virtual ~ CTcpServer () override; void Listen (int _ iPort); void Nortify (const QByteArray & _ csMessage); protected: virtual void incomingConnection (qintptr socketDescriptor) override; virtual void timerEvent (QTimerEvent * event) override;public slots: void SLOT_ClientDisconnect (); private: void _ PackageMessage (QByteArray & _ baMsg); private: QList masked lstSocket; / /
< 连接的客户端 QThread * m_pEventThd; ///< 事件线程};// tcpserver.cppCTcpServer::CTcpServer(QObject *parent) : QTcpServer(parent), m_pEventThd(new QThread()){ m_lstSocket.clear();}CTcpServer::~CTcpServer(){}void CTcpServer::Listen(int _iPort){ this->Listen (QHostAddress::Any, static_cast (_ iPort)); start (); QObject::startTimer (5 * 1000);} void CTcpServer::Nortify (const QByteArray & _ csMessage) {QByteArray baSendMsg = _ csMessage; _ PackageMessage (baSendMsg); for (auto pClientSocket: m_lstSocket) {pClientSocket- > SendMsg (baSendMsg);} QThread::msleep (50);} void CTcpServer::incomingConnection (qintptr socketDescriptor) {qDebug () setSocketDescriptor (_ iFd) Connect (this, SIGNAL (error (QAbstractSocket::SocketError)), this, SLOT (SLOT_SocketError (QAbstractSocket::SocketError)); connect (this, & QTcpSocket::readyRead, this, & CClientSocket::SLOT_ReadyRead); connect (this, & QTcpSocket::disconnected, this, & CClientSocket::SLOT_Disconnect); m_iOldResponseTimeStamp = QDateTime::currentDateTime (). ToSecsSinceEpoch ();} CClientSocket::~CClientSocket () {this- > close () QDebug () moveToThread (_ pThread); QMetaObject::invokeMethod (this, & CClientSocket::_StartCheckTimer);} void CClientSocket::SendMsg (const QByteArray& _ baMessage) {QMetaObject::invokeMethod (this, "_ SendMessage", Q_ARG (const QByteArray&, _ baMessage)) } void CClientSocket::timerEvent (QTimerEvent * event) {if (m_iTimeId = = event- > timerId ()) {if (abs (QDateTime::currentSecsSinceEpoch ()-m_iOldResponseTimeStamp) > 60) {this- > disconnectFromHost ();} void CClientSocket::_DeInitSocket () {this- > close (); QObject::killTimer (m_iTimeId); m_iTimeId = 0 } void CClientSocket::_UpdateHeartTime () {m_iOldResponseTimeStamp = QDateTime::currentDateTime (). ToSecsSinceEpoch ();} void CClientSocket::_StartCheckTimer () {qDebug () isWritable () {this- > write (_ baMessage); this- > flush ();}} void CClientSocket::SLOT_ReadyRead () {char cBuffer [read _ MAX_SIZE]; qint64 iReadSize = 0; QByteArray baNewCache; do {iReadSize = this- > read (cBuffer, READ_MAX_SIZE) If (iReadSize =-1) / /
< 网络异常 { qDebug() peerAddress().toString()) .arg(this->PeerPort (); this- > SLOT_Disconnect (); return;} if (iReadSize! = 0) {baNewCache.append (cBuffer, static_cast (iReadSize));}} while (iReadSize! = 0); if (m_tMsgCache.baPacket.size ()! = 0) {baNewCache = m_tMsgCache.baPacket + baNewCache } while (baNewCache.size () > 0) {if (baNewCache.size () > 4) {uint32_t iSize; if (m_tMsgCache.iSize = = 0) {QByteArray baSize = baNewCache.mid (0,4);:: memcpy (& iSize, baSize.data (), sizeof (iSize)) ISize =: ntohl (iSize);} else {iSize = MsgCache.iSize;} if (baNewCache.size () > = static_cast (iSize)) / / decompose a complete message packet {m_tMsgCache.baPacket = baNewCache.mid (4, static_cast (iSize-4)) / Action: push to the execution thread queue / / Push pHandleMessageThd-> Push (m_tMsgCache.baPacket); / / reset cache status m_tMsgCache.Clear (); / / detect the next new message packet baNewCache = baNewCache.mid (static_cast (iSize)) } else {m_tMsgCache.iSize = iSize; m_tMsgCache.baPacket = baNewCache; break;}} else {/ / there is no complete 4-byte length value m_tMsgCache.iSize = 0; m_tMsgCache.baPacket = baNewCache; break }} this- > _ UpdateHeartTime ();} void CClientSocket::SLOT_SocketError (QAbstractSocket::SocketError _ eError) {qDebug () peerAddress (). ToString () .arg (this- > peerPort ()). Arg (_ eError); _ DeInitSocket (); emit SIGNAL_Disconneted ();} void CClientSocket::SLOT_Disconnect () {qDebug ()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.