mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-22 21:40:05 +00:00
Integrate the monitor's listener socket into the shard's poll()
This removes the need for a separate monitor thread.
This commit is contained in:
parent
53d8cb67ba
commit
92307063fc
@ -219,7 +219,7 @@ bool setSockNonblocking(SOCKET listener, SOCKET newSock) {
|
|||||||
unsigned long mode = 1;
|
unsigned long mode = 1;
|
||||||
if (ioctlsocket(newSock, FIONBIO, &mode) != 0) {
|
if (ioctlsocket(newSock, FIONBIO, &mode) != 0) {
|
||||||
#else
|
#else
|
||||||
if (fcntl(newSock, F_SETFL, (fcntl(listener, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
if (fcntl(newSock, F_SETFL, (fcntl(newSock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
||||||
#endif
|
#endif
|
||||||
std::cerr << "[WARN] OpenFusion: fcntl failed on new connection" << std::endl;
|
std::cerr << "[WARN] OpenFusion: fcntl failed on new connection" << std::endl;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -351,6 +351,9 @@ void CNServer::start() {
|
|||||||
connections[newConnectionSocket] = tmp;
|
connections[newConnectionSocket] = tmp;
|
||||||
newConnection(tmp);
|
newConnection(tmp);
|
||||||
|
|
||||||
|
} else if (checkExtraSockets(i)) {
|
||||||
|
// no-op. handled in checkExtraSockets().
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// player sockets
|
// player sockets
|
||||||
if (connections.find(fds[i].fd) == connections.end()) {
|
if (connections.find(fds[i].fd) == connections.end()) {
|
||||||
@ -419,6 +422,7 @@ void CNServer::printPacket(CNPacketData *data, int type) {
|
|||||||
std::cout << "OpenFusion: received " << Defines::p2str(type, data->type) << " (" << data->type << ")" << std::endl;
|
std::cout << "OpenFusion: received " << Defines::p2str(type, data->type) << " (" << data->type << ")" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CNServer::checkExtraSockets(int i) { return false; } // stubbed
|
||||||
void CNServer::newConnection(CNSocket* cns) {} // stubbed
|
void CNServer::newConnection(CNSocket* cns) {} // stubbed
|
||||||
void CNServer::killConnection(CNSocket* cns) {} // stubbed
|
void CNServer::killConnection(CNSocket* cns) {} // stubbed
|
||||||
void CNServer::onStep() {} // stubbed
|
void CNServer::onStep() {} // stubbed
|
||||||
|
@ -223,6 +223,7 @@ public:
|
|||||||
void start();
|
void start();
|
||||||
void kill();
|
void kill();
|
||||||
static void printPacket(CNPacketData *data, int type);
|
static void printPacket(CNPacketData *data, int type);
|
||||||
|
virtual bool checkExtraSockets(int i);
|
||||||
virtual void newConnection(CNSocket* cns);
|
virtual void newConnection(CNSocket* cns);
|
||||||
virtual void killConnection(CNSocket* cns);
|
virtual void killConnection(CNSocket* cns);
|
||||||
virtual void onStep();
|
virtual void onStep();
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "CNShared.hpp"
|
#include "CNShared.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
#include "Database.hpp"
|
#include "Database.hpp"
|
||||||
|
#include "Monitor.hpp"
|
||||||
#include "TableData.hpp" // for flush()
|
#include "TableData.hpp" // for flush()
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -21,6 +22,9 @@ CNShardServer::CNShardServer(uint16_t p) {
|
|||||||
REGISTER_SHARD_TIMER(keepAliveTimer, 4000);
|
REGISTER_SHARD_TIMER(keepAliveTimer, 4000);
|
||||||
REGISTER_SHARD_TIMER(periodicSaveTimer, settings::DBSAVEINTERVAL*1000);
|
REGISTER_SHARD_TIMER(periodicSaveTimer, settings::DBSAVEINTERVAL*1000);
|
||||||
init();
|
init();
|
||||||
|
|
||||||
|
if (settings::MONITORENABLED)
|
||||||
|
fds.push_back({Monitor::init(), POLLIN});
|
||||||
}
|
}
|
||||||
|
|
||||||
void CNShardServer::handlePacket(CNSocket* sock, CNPacketData* data) {
|
void CNShardServer::handlePacket(CNSocket* sock, CNPacketData* data) {
|
||||||
@ -62,6 +66,10 @@ void CNShardServer::periodicSaveTimer(CNServer* serv, time_t currTime) {
|
|||||||
std::cout << "[INFO] Done." << std::endl;
|
std::cout << "[INFO] Done." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CNShardServer::checkExtraSockets(int i) {
|
||||||
|
return Monitor::acceptConnection(fds[i].fd, fds[i].revents);
|
||||||
|
}
|
||||||
|
|
||||||
void CNShardServer::newConnection(CNSocket* cns) {
|
void CNShardServer::newConnection(CNSocket* cns) {
|
||||||
cns->setActiveKey(SOCKETKEY_E); // by default they accept keys encrypted with the default key
|
cns->setActiveKey(SOCKETKEY_E); // by default they accept keys encrypted with the default key
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ public:
|
|||||||
|
|
||||||
static void _killConnection(CNSocket *cns);
|
static void _killConnection(CNSocket *cns);
|
||||||
|
|
||||||
|
bool checkExtraSockets(int i);
|
||||||
void newConnection(CNSocket* cns);
|
void newConnection(CNSocket* cns);
|
||||||
void killConnection(CNSocket* cns);
|
void killConnection(CNSocket* cns);
|
||||||
void kill();
|
void kill();
|
||||||
|
@ -6,16 +6,12 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
static int listener;
|
static SOCKET listener;
|
||||||
static std::mutex sockLock; // guards socket list
|
static std::mutex sockLock; // guards socket list
|
||||||
static std::list<SOCKET> sockets;
|
static std::list<SOCKET> sockets;
|
||||||
static sockaddr_in address;
|
static sockaddr_in address;
|
||||||
|
|
||||||
// runs during init
|
SOCKET Monitor::init() {
|
||||||
void Monitor::init() {
|
|
||||||
if (!settings::MONITORENABLED)
|
|
||||||
return;
|
|
||||||
|
|
||||||
listener = socket(AF_INET, SOCK_STREAM, 0);
|
listener = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (SOCKETERROR(listener)) {
|
if (SOCKETERROR(listener)) {
|
||||||
std::cout << "Failed to create monitor socket" << std::endl;
|
std::cout << "Failed to create monitor socket" << std::endl;
|
||||||
@ -59,6 +55,8 @@ void Monitor::init() {
|
|||||||
std::cout << "Monitor listening on *:" << settings::MONITORPORT << std::endl;
|
std::cout << "Monitor listening on *:" << settings::MONITORPORT << std::endl;
|
||||||
|
|
||||||
REGISTER_SHARD_TIMER(tick, settings::MONITORINTERVAL);
|
REGISTER_SHARD_TIMER(tick, settings::MONITORINTERVAL);
|
||||||
|
|
||||||
|
return listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool transmit(std::list<SOCKET>::iterator& it, char *buff, int len) {
|
static bool transmit(std::list<SOCKET>::iterator& it, char *buff, int len) {
|
||||||
@ -86,7 +84,6 @@ static bool transmit(std::list<SOCKET>::iterator& it, char *buff, int len) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// runs in shard thread
|
|
||||||
void Monitor::tick(CNServer *serv, time_t delta) {
|
void Monitor::tick(CNServer *serv, time_t delta) {
|
||||||
std::lock_guard<std::mutex> lock(sockLock);
|
std::lock_guard<std::mutex> lock(sockLock);
|
||||||
char buff[256];
|
char buff[256];
|
||||||
@ -115,17 +112,23 @@ void Monitor::tick(CNServer *serv, time_t delta) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// runs in monitor thread
|
bool Monitor::acceptConnection(SOCKET fd, uint16_t revents) {
|
||||||
void Monitor::start(void *unused) {
|
|
||||||
socklen_t len = sizeof(address);
|
socklen_t len = sizeof(address);
|
||||||
|
|
||||||
if (!settings::MONITORENABLED)
|
if (!settings::MONITORENABLED)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
|
if (fd != listener)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (revents & ~POLLIN) {
|
||||||
|
std::cout << "[FATAL] Error on monitor listener?" << std::endl;
|
||||||
|
terminate(0);
|
||||||
|
}
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
int sock = accept(listener, (struct sockaddr*)&address, &len);
|
int sock = accept(listener, (struct sockaddr*)&address, &len);
|
||||||
if (SOCKETERROR(sock))
|
if (SOCKETERROR(sock))
|
||||||
continue;
|
return true;
|
||||||
|
|
||||||
setSockNonblocking(listener, sock);
|
setSockNonblocking(listener, sock);
|
||||||
|
|
||||||
@ -136,5 +139,6 @@ void Monitor::start(void *unused) {
|
|||||||
|
|
||||||
sockets.push_back(sock);
|
sockets.push_back(sock);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
namespace Monitor {
|
namespace Monitor {
|
||||||
void init();
|
SOCKET init();
|
||||||
void tick(CNServer *, time_t);
|
void tick(CNServer *, time_t);
|
||||||
void start(void *);
|
bool acceptConnection(SOCKET, uint16_t);
|
||||||
};
|
};
|
||||||
|
@ -38,8 +38,6 @@
|
|||||||
CNShardServer *shardServer = nullptr;
|
CNShardServer *shardServer = nullptr;
|
||||||
std::thread *shardThread = nullptr;
|
std::thread *shardThread = nullptr;
|
||||||
|
|
||||||
std::thread *monitorThread = nullptr;
|
|
||||||
|
|
||||||
void startShard(CNShardServer* server) {
|
void startShard(CNShardServer* server) {
|
||||||
server->start();
|
server->start();
|
||||||
}
|
}
|
||||||
@ -104,7 +102,6 @@ int main() {
|
|||||||
TransportManager::init();
|
TransportManager::init();
|
||||||
BuddyManager::init();
|
BuddyManager::init();
|
||||||
GroupManager::init();
|
GroupManager::init();
|
||||||
Monitor::init();
|
|
||||||
Database::open();
|
Database::open();
|
||||||
|
|
||||||
switch (settings::EVENTMODE) {
|
switch (settings::EVENTMODE) {
|
||||||
@ -123,7 +120,6 @@ int main() {
|
|||||||
shardServer = new CNShardServer(settings::SHARDPORT);
|
shardServer = new CNShardServer(settings::SHARDPORT);
|
||||||
|
|
||||||
shardThread = new std::thread(startShard, (CNShardServer*)shardServer);
|
shardThread = new std::thread(startShard, (CNShardServer*)shardServer);
|
||||||
monitorThread = new std::thread(Monitor::start, nullptr);
|
|
||||||
|
|
||||||
loginServer.start();
|
loginServer.start();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user