int ManageRoom::getMaxFDAndSet() { int max = 0; FD_ZERO(&(this->read)); max = this->server->getSocket(); FD_SET(max, &(this->read)); int val; val = this->Udp->getSocket(); if (val > max) max = val; FD_SET(val, &(this->read)); for (std::list<Client *>::iterator it = this->clList.begin(); it != this->clList.end(); ++it) { ISocket *s = (*it)->getSocket(); if (s != 0) { int fd = s->getSocket(); FD_SET(fd, &(this->read)); if (max < fd) max = fd; } } return max + 1; }
bool TcpServer::processAccept(int efd) { if (this->stopping) return false; int socket; while (true) { ISocket *sock = this->listenSocket->accept(); if (sock == 0) { if (errno == EAGAIN) { return true; } else { LOG("error while accepting. returned " << errno); return false; } } socket = sock->getSocket(); int flags = fcntl(socket,F_GETFL,0); fcntl(socket, F_SETFL, flags | O_NONBLOCK); TcpClient* client = this->clientFactory->create(sock); clientList[socket] = client; client->setControlEventFd(this->controlEventFd); client->onConnected(); addFdToEpoll(efd,socket); } return true; }
void ManageRoom::check_list() { for (std::list<Client *>::iterator it = this->clList.begin(); it != this->clList.end(); ++it) { ISocket *socket; socket = (*it)->getSocket(); char *buf = new char[1025]; if (socket != NULL) { int fd = socket->getSocket(); if (FD_ISSET(fd, &(this->read))) { std::string buffer; int len; t_TCPHeader *header; if ((len = socket->recBinary(buf, 1024)) < 0) { std::map<std::string, Room *>::iterator it2; std::string ip = socket->getIp(); if ((it2 = this->_access.find(ip)) != this->_access.end()) { std::cout << "quittons la partie "<< (int)(*it)->getIdPlayer() << std::endl; Room * room = it2->second; room->deleteClient((*it)->getIdPlayer()); this->_access.erase(it2); } this->clList.erase(it++); socket->closeSocket(); } else { header = (t_TCPHeader *)buf; if (this->TCPActions.find(header->type) != this->TCPActions.end()) { ptr p; t_TCPConnection *con; con = (t_TCPConnection *)buf; p = this->TCPActions[header->type]; if ((*it)->getName().empty() == 1 && header->type == CONNECTION) (this->*p)(buf, *it); else if (!(*it)->getName().empty() == 1) (this->*p)(buf, *it); else this->sendErrorPacket(socket, NOT_LOGGED); } } } } } }