mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2026-04-01 17:20:04 +00:00
Clean up polling logic
* Extracted PollFD manipulation and nonblocking socket configuration into helper functions * Replaced the connections list with an unordered_map * Dynamically grow the number of PollFD structures with realloc() With these changes done, the server's CPU usage is completely diminished from its old average of ~47% to ~0.07%, with occasional spikes up to ~14%.
This commit is contained in:
@@ -47,6 +47,7 @@
|
||||
#include <csignal>
|
||||
#include <list>
|
||||
#include <queue>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Defines.hpp"
|
||||
#include "settings.hpp"
|
||||
@@ -71,7 +72,7 @@ inline void* xmalloc(size_t sz) {
|
||||
void* res = calloc(1, sz);
|
||||
|
||||
if (res == NULL) {
|
||||
std::cerr << "[FATAL] OpenFusion: calloc failed to allocate memory!" << std::endl;
|
||||
std::cerr << "[FATAL] OpenFusion: out of memory!" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -192,9 +193,14 @@ struct TimerEvent {
|
||||
// in charge of accepting new connections and making sure each connection is kept alive
|
||||
class CNServer {
|
||||
protected:
|
||||
std::list<CNSocket*> connections;
|
||||
std::unordered_map<SOCKET, CNSocket*> connections;
|
||||
std::mutex activeCrit;
|
||||
|
||||
const size_t STARTFDSCOUNT = 8; // number of initial PollFD slots
|
||||
size_t fdsSize; // size of PollFD array in bytes
|
||||
int nfds; // number of populated PollFD slots
|
||||
PollFD *fds;
|
||||
|
||||
SOCKET sock;
|
||||
uint16_t port;
|
||||
socklen_t addressSize;
|
||||
@@ -209,6 +215,9 @@ public:
|
||||
CNServer();
|
||||
CNServer(uint16_t p);
|
||||
|
||||
void addPollFD(SOCKET s);
|
||||
void removePollFD(int i);
|
||||
|
||||
void start();
|
||||
void kill();
|
||||
static void printPacket(CNPacketData *data, int type);
|
||||
|
||||
Reference in New Issue
Block a user