feat: simplify udp listen
This commit is contained in:
@@ -15,7 +15,7 @@ ClusterManager::ClusterManager(NodeContext& ctx, TaskManager& taskMgr) : ctx(ctx
|
||||
|
||||
void ClusterManager::registerTasks() {
|
||||
taskManager.registerTask("discovery_send", ctx.config.discovery_interval_ms, [this]() { sendDiscovery(); });
|
||||
taskManager.registerTask("discovery_listen", ctx.config.discovery_interval_ms / 10, [this]() { listenForDiscovery(); });
|
||||
taskManager.registerTask("cluster_listen", ctx.config.discovery_interval_ms / 10, [this]() { listen(); });
|
||||
taskManager.registerTask("status_update", ctx.config.status_update_interval_ms, [this]() { updateAllNodeStatuses(); removeDeadNodes(); });
|
||||
taskManager.registerTask("print_members", ctx.config.print_interval_ms, [this]() { printMemberList(); });
|
||||
taskManager.registerTask("heartbeat", ctx.config.heartbeat_interval_ms, [this]() { heartbeatTaskCallback(); });
|
||||
@@ -30,37 +30,19 @@ void ClusterManager::sendDiscovery() {
|
||||
ctx.udp->endPacket();
|
||||
}
|
||||
|
||||
void ClusterManager::listenForDiscovery() {
|
||||
switch (listenState) {
|
||||
case ListenState::WAITING_FOR_PACKET: {
|
||||
int packetSize = ctx.udp->parsePacket();
|
||||
if (packetSize) {
|
||||
listenState = ListenState::MESSAGE_RECEIVED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ListenState::MESSAGE_RECEIVED: {
|
||||
char incoming[ClusterProtocol::UDP_BUF_SIZE];
|
||||
int len = ctx.udp->read(incoming, ClusterProtocol::UDP_BUF_SIZE);
|
||||
if (len > 0) {
|
||||
incoming[len] = 0;
|
||||
listenState = ListenState::DISPATCHING;
|
||||
handleIncomingMessage(incoming);
|
||||
} else {
|
||||
listenState = ListenState::DONE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ListenState::DISPATCHING: {
|
||||
// handled synchronously
|
||||
listenState = ListenState::DONE;
|
||||
break;
|
||||
}
|
||||
case ListenState::DONE: {
|
||||
listenState = ListenState::WAITING_FOR_PACKET;
|
||||
break;
|
||||
}
|
||||
void ClusterManager::listen() {
|
||||
int packetSize = ctx.udp->parsePacket();
|
||||
if (!packetSize) {
|
||||
return;
|
||||
}
|
||||
|
||||
char incoming[ClusterProtocol::UDP_BUF_SIZE];
|
||||
int len = ctx.udp->read(incoming, ClusterProtocol::UDP_BUF_SIZE);
|
||||
if (len <= 0) {
|
||||
return;
|
||||
}
|
||||
incoming[len] = 0;
|
||||
handleIncomingMessage(incoming);
|
||||
}
|
||||
|
||||
void ClusterManager::initMessageHandlers() {
|
||||
|
||||
Reference in New Issue
Block a user