mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-26 06:50:06 +00:00
Use an std::vector for PollFDs instead of handling it manually
Lowered poll() timeout to 50, to fix (work around?) slider stutter.
This commit is contained in:
parent
ec7cba644c
commit
6b257887b7
@ -284,54 +284,24 @@ void CNServer::init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// poll() configuration
|
// poll() configuration
|
||||||
fdsSize = STARTFDSCOUNT * sizeof(PollFD); // won't overflow
|
fds.reserve(STARTFDSCOUNT);
|
||||||
fds = (PollFD*)xmalloc(fdsSize);
|
fds.push_back({sock, POLLIN});
|
||||||
|
|
||||||
nfds = 1;
|
|
||||||
fds[0].fd = sock;
|
|
||||||
fds[0].events = POLLIN;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CNServer::CNServer() {};
|
CNServer::CNServer() {};
|
||||||
CNServer::CNServer(uint16_t p): port(p) {}
|
CNServer::CNServer(uint16_t p): port(p) {}
|
||||||
|
|
||||||
void CNServer::addPollFD(SOCKET s) {
|
void CNServer::addPollFD(SOCKET s) {
|
||||||
// if the array is full, double its size
|
fds.push_back({s, POLLIN});
|
||||||
if (nfds == fdsSize / sizeof(PollFD)) {
|
|
||||||
size_t oldsize = fdsSize;
|
|
||||||
|
|
||||||
// check for multiplication overflow
|
|
||||||
assert(fdsSize < SIZE_MAX / 2);
|
|
||||||
fdsSize *= 2;
|
|
||||||
|
|
||||||
fds = (PollFD*)realloc(fds, fdsSize);
|
|
||||||
if (fds == NULL) {
|
|
||||||
std::cerr << "[FATAL] OpenFusion: out of memory!" << std::endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize newly allocated area
|
|
||||||
memset(((uint8_t*)fds) + oldsize, 0, oldsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
fds[nfds].fd = s;
|
|
||||||
fds[nfds].events = POLLIN;
|
|
||||||
nfds++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CNServer::removePollFD(int i) {
|
void CNServer::removePollFD(int i) {
|
||||||
nfds--;
|
auto it = fds.begin();
|
||||||
assert(nfds > 0);
|
while (it != fds.end() && it->fd != fds[i].fd)
|
||||||
|
it++;
|
||||||
|
assert(it != fds.end());
|
||||||
|
|
||||||
if (i != nfds) {
|
fds.erase(it);
|
||||||
// move the last entry to the new empty spot
|
|
||||||
fds[i].fd = fds[nfds].fd;
|
|
||||||
fds[i].events = fds[nfds].events; // redundant; events are always the same
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete the entry
|
|
||||||
fds[nfds].fd = 0;
|
|
||||||
fds[nfds].events = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CNServer::start() {
|
void CNServer::start() {
|
||||||
@ -340,13 +310,13 @@ void CNServer::start() {
|
|||||||
std::cout << "Starting server at *:" << port << std::endl;
|
std::cout << "Starting server at *:" << port << std::endl;
|
||||||
while (active) {
|
while (active) {
|
||||||
// the timeout is to ensure shard timers are ticking
|
// the timeout is to ensure shard timers are ticking
|
||||||
int n = poll(fds, nfds, 200);
|
int n = poll(fds.data(), fds.size(), 50);
|
||||||
if (SOCKETERROR(n)) {
|
if (SOCKETERROR(n)) {
|
||||||
std::cout << "[FATAL] poll() returned error" << std::endl;
|
std::cout << "[FATAL] poll() returned error" << std::endl;
|
||||||
terminate(0);
|
terminate(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
oldnfds = nfds;
|
oldnfds = fds.size();
|
||||||
|
|
||||||
activeCrit.lock();
|
activeCrit.lock();
|
||||||
|
|
||||||
|
@ -48,6 +48,8 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include "Defines.hpp"
|
#include "Defines.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
@ -197,9 +199,7 @@ protected:
|
|||||||
std::mutex activeCrit;
|
std::mutex activeCrit;
|
||||||
|
|
||||||
const size_t STARTFDSCOUNT = 8; // number of initial PollFD slots
|
const size_t STARTFDSCOUNT = 8; // number of initial PollFD slots
|
||||||
size_t fdsSize; // size of PollFD array in bytes
|
std::vector<PollFD> fds;
|
||||||
int nfds; // number of populated PollFD slots
|
|
||||||
PollFD *fds;
|
|
||||||
|
|
||||||
SOCKET sock;
|
SOCKET sock;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
|
Loading…
Reference in New Issue
Block a user