📄 communicate.cpp
字号:
// YsCommunicate.cpp: implementation of the YsCommunicate class.////////////////////////////////////////////////////////////////////////#include "Communicate.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////YsCommunicate::YsCommunicate(){ m_hListen = NULL; m_iTimeout = 30 * 1000;}YsCommunicate::~YsCommunicate(){ m_ClientList.setShared(true);//线程间共享数据,需加锁 Close(); #ifdef WIN32 if(m_hListen != NULL) { TerminateThread(m_hListen,0); } #else if(m_hListen != NULL) { pthread_cancel(m_hListen); pthread_join(m_hListen,NULL); } #endif //release free client socket m_Lock.lock(); int i,cnt=m_ClientList.count(); for(i=0;i<cnt;i++) { YsCommunicate *p=m_ClientList[i]; if(p == NULL) continue; m_ClientList.remove(i); p->Close(); delete p; } m_Lock.unlock();}YsCommunicate::Status YsCommunicate::getStat() { if(!IsConnected()) return ERROR; else return WORKING;}string YsCommunicate::peerName(){ int port=GetPeerPort(); char buf[16]; memset(buf,0,16); sprintf(buf,"%d",port); string peer = GetPeerName()+":"+buf; return peer;}bool YsCommunicate::beginComm(YsCommunicate::CommType type,string addr,WORD port){ if(type == CLIENT) return beginCommClt(addr,port); else if(type == SERVER) return beginCommSvr(addr,port); else return false;}bool YsCommunicate::beginCommClt(string addr,WORD port){ if(!this->Create()) return false; this->SetTimeout(m_iTimeout,m_iTimeout); if(!this->Connect(addr.c_str(),(int)port)) return false; m_sPeerName = this->GetPeerName(); return true;}bool YsCommunicate::beginCommSvr(string addr,WORD port){ if(!this->Create()) return false; if(!this->Bind((int)port,(char *)addr.c_str())) return false; if(!this->Listen()) return false; m_LsnParam.pListenSocket = this; m_LsnParam.pClientList = &m_ClientList; #ifdef WIN32 m_hListen = CreateThread(NULL, 0, ThreadListenPort, (LPVOID)&m_LsnParam, 0, 0) ; #else pthread_t t; pthread_create(&t, NULL, ThreadListenPort,(void*) &m_LsnParam); #endif return true;}#ifdef WIN32DWORD WINAPI YsCommunicate::ThreadListenPort(void * lp)#elsevoid* YsCommunicate::ThreadListenPort(void * lp)#endif{ #ifndef WIN32 pthread_detach(pthread_self()); #endif stuThreadSocketLsnParm *p=(stuThreadSocketLsnParm*) lp; YsCommunicate *pSkt = p->pListenSocket; while(true) { YsCommunicate *service=new YsCommunicate(); if(!pSkt->Accept(*service)) { #ifdef _YSDEBUG printf("Accept error, exit this thread!\n"); #endif break; } #ifdef _YSDEBUG printf("get a new connection!\n"); #endif service->m_sPeerName = service->GetPeerName(); pSkt->m_Lock.lock(); p->pClientList->append(service); pSkt->m_Lock.unlock(); } return 0;}YsCommunicate* YsCommunicate::getPeerConnection(string addr){ if(m_ClientList.count() <= 0) return NULL; m_Lock.lock(); int i,cnt=m_ClientList.count(); for(i=0; i<cnt; i++) { YsCommunicate *p=m_ClientList[i]; if(p == NULL) break; if(addr.size() == 0 || p->m_sPeerName == addr) { m_ClientList.remove(i); m_Lock.unlock(); return p; } } m_Lock.unlock(); return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -