ObzLib networking

obz::transport

Small RAII wrappers for moving bytes between IPv4 TCP and UDP endpoints.

The Clean-Code Idea

Native sockets are easy to leak or reuse accidentally. Transport wraps the handle in move-only objects so ownership is clear, closing is automatic, and byte-moving APIs stay small.

class tcp_socket {
public:
    void connect(const endpoint& remote_endpoint);

    std::size_t send(std::span<const std::byte> data);
    void send_all(std::span<const std::byte> data);
    std::vector<std::byte> receive(std::size_t max_bytes = 4096);

    void close();

    bool is_open() const;
    native_socket_handle native_handle() const;
    endpoint local_endpoint() const;
};

Why This Shape?

UDP and TCP are different enough to deserve different types. TCP has listeners and streams. UDP has datagrams and remote endpoints on each send. Keeping those differences visible makes misuse harder.

struct endpoint {
    std::string host;
    std::uint16_t port;
};

The library also keeps platform-specific details behind a small implementation boundary, while the public API talks in terms of endpoints, byte spans, and owned socket handles.

More Libraries