void ClientManager::ProcessDisconnect() { list<Client*>::iterator iter = clients.begin(); while(iter != clients.end()) { EQStream *c = (*iter)->GetConnection(); if(c->CheckClosed()) { server_log->Log(log_network, "Client disconnected from the server, removing client."); delete (*iter); iter = clients.erase(iter); } else { iter++; } } }
void EQStreamFactory::ReaderLoop() { fd_set readset; std::map<std::string,EQStream *>::iterator stream_itr; int num; int length; unsigned char buffer[2048]; sockaddr_in from; int socklen=sizeof(sockaddr_in); timeval sleep_time; //time_t now; ReaderRunning=true; while(sock!=-1) { MReaderRunning.lock(); if (!ReaderRunning) break; MReaderRunning.unlock(); FD_ZERO(&readset); FD_SET(sock,&readset); sleep_time.tv_sec=30; sleep_time.tv_usec=0; if ((num=select(sock+1,&readset,nullptr,nullptr,&sleep_time))<0) { // What do we wanna do? continue; } else if (num==0) continue; if(sock == -1) break; //somebody closed us while we were sleeping. if (FD_ISSET(sock,&readset)) { #ifdef _WINDOWS if ((length=recvfrom(sock,(char*)buffer,sizeof(buffer),0,(struct sockaddr*)&from,(int *)&socklen)) < 2) #else if ((length=recvfrom(sock,buffer,2048,0,(struct sockaddr *)&from,(socklen_t *)&socklen)) < 2) #endif { // What do we wanna do? } else { char temp[25]; sprintf(temp,"%u.%d",ntohl(from.sin_addr.s_addr),ntohs(from.sin_port)); MStreams.lock(); if ((stream_itr=Streams.find(temp))==Streams.end()) { if (buffer[1]==OP_SessionRequest) { EQStream *s = new EQStream(from); s->SetStreamType(StreamType); Streams[temp]=s; WriterWork.Signal(); Push(s); s->AddBytesRecv(length); s->Process(buffer,length); s->SetLastPacketTime(Timer::GetCurrentTime()); } MStreams.unlock(); } else { EQStream *curstream = stream_itr->second; //dont bother processing incoming packets for closed connections if(curstream->CheckClosed()) curstream = nullptr; else curstream->PutInUse(); MStreams.unlock(); //the in use flag prevents the stream from being deleted while we are using it. if(curstream) { curstream->AddBytesRecv(length); curstream->Process(buffer,length); curstream->SetLastPacketTime(Timer::GetCurrentTime()); curstream->ReleaseFromUse(); } } } } } }