Files
Towards/modules/network/include/protocol/quicr/QuicrEndpoint.hpp
T

71 lines
1.9 KiB
C++
Raw Normal View History

2026-07-18 14:31:15 +02:00
#pragma once
#include "Address.hpp"
#include "NetworkError.hpp"
#include "protocol/quicr/QuicrConnection.hpp"
#include <tl/expected.hpp>
#include <memory>
#include <unordered_map>
#include <unistd.h>
namespace tw::net::quicr {
class QuicrConnection;
class QuicrConnectionListener;
class QuicrEndpoint {
int32_t m_socket_fd;
std::unordered_map<uint64_t, std::shared_ptr<QuicrConnection>> m_connections;
std::vector<std::byte> m_inbound_buffer;
QuicrConnectionListener* m_new_connection_handler;
void process_datagram(std::span<std::byte> datagram, Address from);
QuicrEndpoint(int socket_fd);
public:
QuicrEndpoint(const QuicrEndpoint&) = delete;
QuicrEndpoint& operator=(const QuicrEndpoint&) = delete;
QuicrEndpoint(QuicrEndpoint&&) = delete;
QuicrEndpoint& operator=(QuicrEndpoint&&) = delete;
~QuicrEndpoint() {
::close(m_socket_fd);
m_socket_fd = -1;
}
std::vector<std::pair<uint64_t, std::shared_ptr<QuicrConnection>>> clients() const {
std::vector<std::pair<uint64_t, std::shared_ptr<QuicrConnection>>> result;
for (const auto& [id, connection] : m_connections) {
result.emplace_back(id, connection);
}
return result;
}
static tl::expected<std::unique_ptr<QuicrEndpoint>, NetworkError> create();
/**
* Creates the QUICr endpoint and binds it to a port.
*/
static tl::expected<std::unique_ptr<QuicrEndpoint>, NetworkError> create_and_bind(int16_t port);
void assign_listener(QuicrConnectionListener* listener) {
m_new_connection_handler = listener;
}
tl::expected<void, NetworkError> bind(int port);
tl::expected<QuicrConnection*, NetworkError> connect(Address address);
tl::expected<size_t, NetworkError> send_to(std::span<std::byte> data, Address to);
tl::expected<size_t, NetworkError> read_from_into(std::span<std::byte> data, Address* out_from);
void poll();
};
}