CNServer::start() now uses an iterator instead of indexing repeatedly

This commit is contained in:
CPunch 2021-03-14 01:33:46 -06:00
parent 2024fb4969
commit ec67cc6527

View File

@ -363,15 +363,18 @@ void CNServer::start() {
} }
for (int i = 0; i < fds.size() && n > 0; i++) { for (int i = 0; i < fds.size() && n > 0; i++) {
if (fds[i].revents == 0) // about the same as fds[i], O(1)
std::vector<PollFD>::iterator iter = fds.begin() + i;
if ((*iter).revents == 0)
continue; // nothing in this one; don't decrement n continue; // nothing in this one; don't decrement n
n--; n--;
// is it the listener? // is it the listener?
if (fds[i].fd == sock) { if ((*iter).fd == sock) {
// any sort of error on the listener // any sort of error on the listener
if (fds[i].revents & ~POLLIN) { if ((*iter).revents & ~POLLIN) {
std::cout << "[FATAL] Error on listener socket" << std::endl; std::cout << "[FATAL] Error on listener socket" << std::endl;
terminate(0); terminate(0);
} }
@ -401,25 +404,25 @@ void CNServer::start() {
std::lock_guard<std::mutex> lock(activeCrit); // protect operations on connections std::lock_guard<std::mutex> lock(activeCrit); // protect operations on connections
// player sockets // player sockets
if (connections.find(fds[i].fd) == connections.end()) { if (connections.find((*iter).fd) == connections.end()) {
std::cout << "[WARN] Event on non-existant socket?" << std::endl; std::cout << "[WARN] Event on non-existant socket?" << std::endl;
continue; // just to be safe continue; // just to be safe
} }
CNSocket* cSock = connections[fds[i].fd]; CNSocket* cSock = connections[(*iter).fd];
// kill the socket on hangup/error // kill the socket on hangup/error
if (fds[i].revents & ~POLLIN) if ((*iter).revents & ~POLLIN)
cSock->kill(); cSock->kill();
if (cSock->isAlive()) { if (cSock->isAlive()) {
cSock->step(); cSock->step();
} else { } else {
killConnection(cSock); killConnection(cSock);
connections.erase(fds[i].fd); connections.erase((*iter).fd);
delete cSock; delete cSock;
removePollFD(i); fds.erase(iter);
// a new entry was moved to this position, so we check it again // a new entry was moved to this position, so we check it again
i--; i--;