Files
Towards/modules/server/src/ZoneClusterLink.hpp
T

59 lines
1.6 KiB
C++
Raw Normal View History

2026-07-18 14:31:15 +02:00
#pragma once
2026-07-22 17:34:44 +02:00
#include "ProtobufMessages.hpp"
#include "message_protocol/MessageConnection.hpp"
#include "message_protocol/MessageEndpoint.hpp"
2026-07-18 14:31:15 +02:00
#include <memory>
#include <string>
#include <vector>
#include <spdlog/spdlog.h>
namespace tw::net {
2026-07-22 17:34:44 +02:00
// Listens on a dedicated port for incoming zone-server peer connections and
// opens outgoing connections to known peers.
2026-07-18 14:31:15 +02:00
//
2026-07-22 17:34:44 +02:00
// Incoming and outgoing peers are held together so that broadcast messages
// (ZoneHello, ZoneBye) reach every peer uniformly.
2026-07-18 14:31:15 +02:00
class ZoneClusterLink {
2026-07-22 17:34:44 +02:00
std::unique_ptr<msg::MessageEndpoint> m_endpoint;
ProtobufMessages m_messages;
2026-07-18 14:31:15 +02:00
public:
explicit ZoneClusterLink(int port);
2026-07-22 17:34:44 +02:00
// Poll for messages and accept any pending peer connections.
2026-07-18 14:31:15 +02:00
void update();
// Connect to a remote zone peer and register it in the peer list.
2026-07-22 17:34:44 +02:00
msg::MessageConnection* connect_to_peer(const std::string& host, int port);
2026-07-18 14:31:15 +02:00
2026-07-22 17:34:44 +02:00
// Send a protobuf message to a single peer.
2026-07-18 14:31:15 +02:00
template<typename T>
2026-07-22 17:34:44 +02:00
void send_mesg(msg::MessageConnection* peer, const T& msg) {
auto send_r = m_messages.send(peer, msg, false);
if(!send_r) {
spdlog::error("ZoneClusterLink: failed to send to peer {}: {}",
peer->peer_id(), send_r.error().message());
2026-07-18 14:31:15 +02:00
}
}
2026-07-22 17:34:44 +02:00
// Send a protobuf message to all connected peers.
2026-07-18 14:31:15 +02:00
template<typename T>
void broadcast(const T& msg) {
2026-07-22 17:34:44 +02:00
m_messages.broadcast(msg);
}
std::vector<msg::MessageConnection*> peers() const {
return m_endpoint->peers();
2026-07-18 14:31:15 +02:00
}
2026-07-22 17:34:44 +02:00
msg::MessageEndpoint& endpoint() {
return *m_endpoint;
}
2026-07-18 14:31:15 +02:00
};
} // namespace tw::net