QTcpserver 服务器

  • K1_228332
    了解作者
  • 7.1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 07:03
    上传日期
qt4.8的开发环境,qtcpserver,qtcpsocket开发的服务器端。
QTcpServrt.rar
  • QTcpServrt
  • tcpsession.cpp
    1.8KB
  • tcpserver.cpp
    1KB
  • QTcpServrt.pro
    636B
  • QTcpServrt.pro.user
    17.3KB
  • tcpthread.h
    242B
  • datadecoder.h
    216B
  • sessionthreads.h
    1020B
  • datadecoder.cpp
    633B
  • sessionthreads.cpp
    2.9KB
  • main.cpp
    202B
  • tcpsession.h
    1KB
  • tcpserver.h
    690B
  • tcpthread.cpp
    103B
内容介绍
#include "sessionthreads.h" SessionThreads::SessionThreads() { } SessionThreads::~SessionThreads() { this->Stop(); } bool SessionThreads::Start(int threadNum) { if(IsRunning_) { return true; } for(int i = 0; i < threadNum; ++i) { TcpThread *thread = new TcpThread(); ThreadList_.push_back(thread); thread->start(); } IsRunning_ = true; return true; } void SessionThreads::Stop() { if(!IsRunning_) { return; } QMutexLocker locker(&Lock_); std::unordered_map<void*, std::shared_ptr<TcpSession>>::iterator itor = SessionList_.begin(); //关闭连接 for(itor = SessionList_.begin(); itor != SessionList_.end(); ++itor) { std::shared_ptr<TcpSession> session = itor->second; if(session.get()) { disconnect(session.get(),SIGNAL(SignalDisConnected()),session.get(),SLOT(SlotDisConnected())); session.get()->Disconnect(); } } for(TcpThread *thread : this->ThreadList_) { thread->exit(); thread->wait(); } for(TcpThread *thread : this->ThreadList_) { delete thread; } this->ThreadList_.clear(); this->SessionList_.clear(); IsRunning_ = false; } TcpThread *SessionThreads::PickMinThread() { TcpThread *thread = nullptr; uint32_t mincount = 0; for(TcpThread *tmpthread : this->ThreadList_) { uint32_t tmpcount = tmpthread->SessionCount; if(mincount == 0 || tmpcount < mincount) { mincount = tmpcount; thread = tmpthread; } } return thread; } std::vector<uint32_t> SessionThreads::GetSessionSize() const { std::vector<uint32_t> vec; for(TcpThread *thread : this->ThreadList_) { vec.push_back(thread->SessionCount); } return vec; } std::shared_ptr<TcpSession> SessionThreads::CreateSession(int handle) { TcpThread *thread = this->PickMinThread(); std::shared_ptr<TcpSession> session = std::make_shared<TcpSession> (thread); connect((TcpSession*)session.get(), SIGNAL(SignalDisConnected(void*)),this, SLOT(SlotSessionDisConnected(void*))); session->setSocketDescriptor(handle); session->moveToThread(thread); ++thread->SessionCount; //加锁 QMutexLocker locker(&Lock_); this->SessionList_[session.get()] = session; return session; } void SessionThreads::SlotSessionDisConnected(void *id) { //加锁 QMutexLocker locker(&Lock_); std::unordered_map<void*, std::shared_ptr<TcpSession>>::iterator itor = SessionList_.begin(); itor = SessionList_.find(id); if(itor != SessionList_.end()) { qDebug()<< "SessionThreads::SlotSessionDisConnected id:"<<id; SessionList_.erase(itor); } } void SessionThreads::SlotReadData(const char *data, int len) { //emit SignalDoWrite(data,len); }
评论