#1 - quicr module
This commit is contained in:
@@ -1,42 +1,48 @@
|
||||
#include "ZoneClusterLink.hpp"
|
||||
|
||||
#include "Address.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace tw::net {
|
||||
|
||||
ZoneClusterLink::ZoneClusterLink(int port)
|
||||
: m_endpoint(quicr::QuicrEndpoint::create().value()),
|
||||
m_listener(quicr::QuicrConnectionListener::listen(m_endpoint.get()).value())
|
||||
{
|
||||
if (auto r = m_endpoint->bind(port); !r) {
|
||||
throw std::runtime_error("ZoneClusterLink: failed to bind to port " + std::to_string(port));
|
||||
namespace {
|
||||
|
||||
std::unique_ptr<msg::MessageEndpoint> bind_endpoint(int port) {
|
||||
auto endpoint_r = msg::MessageEndpoint::bind(port);
|
||||
if(!endpoint_r) {
|
||||
throw std::runtime_error("ZoneClusterLink: failed to bind to port " + std::to_string(port) +
|
||||
": " + endpoint_r.error().message());
|
||||
}
|
||||
|
||||
return std::move(endpoint_r.value());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ZoneClusterLink::ZoneClusterLink(int port) :
|
||||
m_endpoint(bind_endpoint(port)),
|
||||
m_messages(m_endpoint.get()) {
|
||||
m_endpoint->set_on_peer_connected([](msg::PeerId peer) {
|
||||
spdlog::info("Zone peer {} connected", peer);
|
||||
});
|
||||
|
||||
spdlog::info("ZoneClusterLink listening on port {}", port);
|
||||
}
|
||||
|
||||
void ZoneClusterLink::update() {
|
||||
m_endpoint->poll();
|
||||
|
||||
quicr::QuicrConnection* connection;
|
||||
while ((connection = m_listener->listen())) {
|
||||
spdlog::info("Zone peer connected from {}", connection->address().to_string());
|
||||
m_peers.push_back(connection);
|
||||
}
|
||||
m_endpoint->update();
|
||||
}
|
||||
|
||||
quicr::QuicrConnection* ZoneClusterLink::connect_to_peer(const std::string& host, int port) {
|
||||
auto result = m_endpoint->connect(Address(host, port));
|
||||
if (!result) {
|
||||
spdlog::error("ZoneClusterLink: failed to connect to peer {}:{}", host, port);
|
||||
msg::MessageConnection* ZoneClusterLink::connect_to_peer(const std::string& host, int port) {
|
||||
auto peer_r = m_endpoint->connect(host, port);
|
||||
if (!peer_r) {
|
||||
spdlog::error("ZoneClusterLink: failed to connect to peer {}:{}: {}",
|
||||
host, port, peer_r.error().message());
|
||||
return nullptr;
|
||||
}
|
||||
quicr::QuicrConnection* conn = result.value();
|
||||
m_peers.push_back(conn);
|
||||
|
||||
spdlog::info("ZoneClusterLink: connected to peer {}:{}", host, port);
|
||||
return conn;
|
||||
return peer_r.value();
|
||||
}
|
||||
|
||||
} // namespace tw::net
|
||||
|
||||
Reference in New Issue
Block a user