Files
voice-cat/core/src/net/transport.h

247 lines
9.6 KiB
C
Raw Normal View History

/*
* net/transport.h TCP control channel + UDP media channel.
*
2026-06-15 23:48:44 +02:00
* Design: docs/architecture.md (Net thread), docs/protocol.md §1 (framing).
* Implementation uses standalone Asio for sockets and timers.
*
* The real classes are compiled only when VOICECAT_HAS_NET is defined (dev/release/
* server-release vcpkg deps on). The skeleton-preset stub definitions below keep the
* no-deps build green.
*/
#ifndef VOICECAT_NET_TRANSPORT_H
#define VOICECAT_NET_TRANSPORT_H
#include <cstdint>
#include <string>
2026-06-15 23:48:44 +02:00
#ifdef VOICECAT_HAS_NET
#define ASIO_STANDALONE 1
#include <asio.hpp>
#include <atomic>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "protocol/protocol.h"
// Forward-declare TlsContext so transport.h does not pull in mbedTLS headers.
namespace voicecat::crypto { class TlsContext; }
namespace voicecat::net {
2026-06-15 23:48:44 +02:00
// Callbacks delivered on the net thread. Callers must not block inside them.
struct TcpChannelCallbacks {
std::function<void()> on_connected;
std::function<void(std::error_code)> on_connect_error;
std::function<void(std::vector<uint8_t>)> on_frame; // one decoded frame payload
std::function<void(std::error_code)> on_error;
std::function<void()> on_disconnected;
// Called (on the handshake thread) right after TLS succeeds, before reads begin.
// Use to export keying material while the handshake context is still fresh.
std::function<void(voicecat::crypto::TlsContext&)> on_tls_ready;
2026-06-15 23:48:44 +02:00
};
// ── Client-side: owns an io_context + dedicated net thread ──────────────────
class TcpControlChannel {
public:
2026-06-15 23:48:44 +02:00
explicit TcpControlChannel(TcpChannelCallbacks cbs);
~TcpControlChannel();
// Async connect; calls on_connected or on_connect_error on the net thread.
void async_connect(const std::string& host, uint16_t port);
// Queue a framed send (thread-safe; callable from any thread).
void send_frame(std::vector<uint8_t> payload);
// Graceful close; safe to call from any thread. Waits for the net thread to join.
void close();
bool connected() const { return connected_.load(std::memory_order_acquire); }
// Access the io_context so callers can post work back to the net thread.
asio::io_context& io() { return io_; }
private:
2026-06-15 23:48:44 +02:00
void run_loop();
void start_read();
void handle_length(std::error_code ec, std::size_t n);
void handle_body(uint32_t length, std::error_code ec, std::size_t n);
void do_send();
asio::io_context io_;
asio::executor_work_guard<asio::io_context::executor_type> work_guard_;
asio::ip::tcp::socket socket_;
asio::strand<asio::io_context::executor_type> strand_;
std::thread net_thread_;
TcpChannelCallbacks cbs_;
protocol::FrameCodec codec_;
uint8_t len_buf_[4]{};
std::vector<uint8_t> body_buf_;
std::deque<std::vector<uint8_t>> send_queue_;
bool sending_{false};
std::atomic<bool> connected_{false};
std::atomic<bool> closing_{false};
};
2026-06-15 23:48:44 +02:00
// ── Server-side: one per accepted socket, shares the server's io_context ────
class TcpServerConn : public std::enable_shared_from_this<TcpServerConn> {
public:
// Plain TCP constructor (no TLS — for tests or future plaintext paths).
TcpServerConn(asio::ip::tcp::socket socket, TcpChannelCallbacks cbs);
// TLS constructor: takes ownership of a TlsContext; start() will run the
// handshake on a temporary thread then switch to a TLS I/O thread.
TcpServerConn(asio::ip::tcp::socket socket, TcpChannelCallbacks cbs,
std::unique_ptr<voicecat::crypto::TlsContext> tls);
~TcpServerConn();
// Begin reading; must be called once after construction (on the io thread).
void start();
// Thread-safe send (safe to call from the server's io thread or another strand).
void send_frame(std::vector<uint8_t> payload);
// Close the connection (safe from any thread).
void close();
feat(macos): validate dev + apple-dev presets on macOS, fix 3 cross-platform bugs macOS port groundwork — core, server, tools, and tests now build and run on macOS 26.5 / Apple Silicon. ctest --preset dev green 21/21 (2 consecutive runs). apple-dev produces valid arm64 libvoicecat.a + XCFramework for the Swift Package. Three real cross-platform bugs found and fixed (all latent on Windows/Linux): 1. test_m2_voice.cpp POSIX branch missing <netdb.h> — Linux glibc transitively includes it, macOS doesn't. Would fail on any strict POSIX system. 2. SIGPIPE killing processes on macOS — writing to a closed TCP socket raises SIGPIPE by default (doesn't exist on Windows, benign on Linux). Fixed by ignoring SIGPIPE in both core client init and server startup (POSIX-only, #ifndef _WIN32). Production fix, not just tests. 3. Use-after-free of Asio's kqueue reactor on server shutdown — the deterministic test_tofu_flow segfault. TcpServerConn's tls_read_loop runs on a blocking-I/O thread; when Server::run() returned, io_context was destroyed while those threads were still running. On macOS kqueue the reactor pointer is null'd immediately -> segfault in socket.close(). Latent on Windows IOCP and Linux epoll. Fix: TcpAcceptor now tracks connections; new shutdown() closes all + joins threads before io is destroyed; Server::stop() now closes acceptor + media_relay too (was just io.stop()). Verified: dev + apple-dev presets build green, 21/21 tests pass, server starts + two vccli text chat over TLS (M1 on Mac), vccli --voice starts MIC stream via CoreAudio (M2 protocol-level), vccli --list-devices enumerates CoreAudio devices, xcodebuild -create-xcframework produces valid VoiceCatCore.xcframework. No ABI or proto changes. Docs updated: building.md, clients/apple/README.md, PROGRESS.md, CLAUDE.md status line.
2026-06-18 13:24:42 +02:00
// Block until the TLS I/O thread (if any) has finished. Must be called after close().
// Safe to call from any thread except the TLS I/O thread itself.
void wait_closed();
2026-06-15 23:48:44 +02:00
bool connected() const { return connected_.load(std::memory_order_acquire); }
private:
// ── Asio path (no TLS) ───────────────────────────────────────────────────
void start_read();
void handle_length(std::error_code ec, std::size_t n);
void handle_body(uint32_t length, std::error_code ec, std::size_t n);
void do_send();
// ── TLS path ─────────────────────────────────────────────────────────────
void tls_read_loop();
void tls_drain_sends();
asio::ip::tcp::socket socket_;
asio::strand<asio::any_io_executor> strand_;
TcpChannelCallbacks cbs_;
protocol::FrameCodec codec_;
uint8_t len_buf_[4]{};
std::vector<uint8_t> body_buf_;
std::deque<std::vector<uint8_t>> send_queue_;
bool sending_{false};
std::atomic<bool> connected_{false};
std::atomic<bool> closing_{false};
// TLS members (null in plain-TCP mode)
std::unique_ptr<voicecat::crypto::TlsContext> tls_;
std::thread tls_thread_;
std::mutex tls_send_mutex_;
std::deque<std::vector<uint8_t>> tls_send_queue_;
};
// ── Server-side acceptor ─────────────────────────────────────────────────────
// Spawns a TcpServerConn (via factory) for each accepted TCP connection.
class TcpAcceptor {
public:
using ConnFactory = std::function<std::shared_ptr<TcpServerConn>(asio::ip::tcp::socket)>;
TcpAcceptor(asio::io_context& io, uint16_t port, ConnFactory factory);
// Start accepting. Call once; re-arms itself automatically.
void start();
feat(macos): validate dev + apple-dev presets on macOS, fix 3 cross-platform bugs macOS port groundwork — core, server, tools, and tests now build and run on macOS 26.5 / Apple Silicon. ctest --preset dev green 21/21 (2 consecutive runs). apple-dev produces valid arm64 libvoicecat.a + XCFramework for the Swift Package. Three real cross-platform bugs found and fixed (all latent on Windows/Linux): 1. test_m2_voice.cpp POSIX branch missing <netdb.h> — Linux glibc transitively includes it, macOS doesn't. Would fail on any strict POSIX system. 2. SIGPIPE killing processes on macOS — writing to a closed TCP socket raises SIGPIPE by default (doesn't exist on Windows, benign on Linux). Fixed by ignoring SIGPIPE in both core client init and server startup (POSIX-only, #ifndef _WIN32). Production fix, not just tests. 3. Use-after-free of Asio's kqueue reactor on server shutdown — the deterministic test_tofu_flow segfault. TcpServerConn's tls_read_loop runs on a blocking-I/O thread; when Server::run() returned, io_context was destroyed while those threads were still running. On macOS kqueue the reactor pointer is null'd immediately -> segfault in socket.close(). Latent on Windows IOCP and Linux epoll. Fix: TcpAcceptor now tracks connections; new shutdown() closes all + joins threads before io is destroyed; Server::stop() now closes acceptor + media_relay too (was just io.stop()). Verified: dev + apple-dev presets build green, 21/21 tests pass, server starts + two vccli text chat over TLS (M1 on Mac), vccli --voice starts MIC stream via CoreAudio (M2 protocol-level), vccli --list-devices enumerates CoreAudio devices, xcodebuild -create-xcframework produces valid VoiceCatCore.xcframework. No ABI or proto changes. Docs updated: building.md, clients/apple/README.md, PROGRESS.md, CLAUDE.md status line.
2026-06-18 13:24:42 +02:00
// Stop accepting and close all tracked connections (safe while io_context is alive).
2026-06-15 23:48:44 +02:00
void stop();
feat(macos): validate dev + apple-dev presets on macOS, fix 3 cross-platform bugs macOS port groundwork — core, server, tools, and tests now build and run on macOS 26.5 / Apple Silicon. ctest --preset dev green 21/21 (2 consecutive runs). apple-dev produces valid arm64 libvoicecat.a + XCFramework for the Swift Package. Three real cross-platform bugs found and fixed (all latent on Windows/Linux): 1. test_m2_voice.cpp POSIX branch missing <netdb.h> — Linux glibc transitively includes it, macOS doesn't. Would fail on any strict POSIX system. 2. SIGPIPE killing processes on macOS — writing to a closed TCP socket raises SIGPIPE by default (doesn't exist on Windows, benign on Linux). Fixed by ignoring SIGPIPE in both core client init and server startup (POSIX-only, #ifndef _WIN32). Production fix, not just tests. 3. Use-after-free of Asio's kqueue reactor on server shutdown — the deterministic test_tofu_flow segfault. TcpServerConn's tls_read_loop runs on a blocking-I/O thread; when Server::run() returned, io_context was destroyed while those threads were still running. On macOS kqueue the reactor pointer is null'd immediately -> segfault in socket.close(). Latent on Windows IOCP and Linux epoll. Fix: TcpAcceptor now tracks connections; new shutdown() closes all + joins threads before io is destroyed; Server::stop() now closes acceptor + media_relay too (was just io.stop()). Verified: dev + apple-dev presets build green, 21/21 tests pass, server starts + two vccli text chat over TLS (M1 on Mac), vccli --voice starts MIC stream via CoreAudio (M2 protocol-level), vccli --list-devices enumerates CoreAudio devices, xcodebuild -create-xcframework produces valid VoiceCatCore.xcframework. No ABI or proto changes. Docs updated: building.md, clients/apple/README.md, PROGRESS.md, CLAUDE.md status line.
2026-06-18 13:24:42 +02:00
// Stop accepting, close all connections, and block until every connection's
// I/O thread has finished. Call BEFORE the io_context is destroyed — the TLS read
// threads do blocking I/O (not async on the io_context) and will touch the reactor
// on socket close if the io_context is already gone (manifests as a null-kqueue-reactor
// segfault on macOS; latent on Windows IOCP / Linux epoll where timing is more forgiving).
void shutdown();
2026-06-15 23:48:44 +02:00
// Actual bound port (useful when bind_port=0 lets the OS pick).
uint16_t local_port() const { return static_cast<uint16_t>(acceptor_.local_endpoint().port()); }
private:
void do_accept();
feat(macos): validate dev + apple-dev presets on macOS, fix 3 cross-platform bugs macOS port groundwork — core, server, tools, and tests now build and run on macOS 26.5 / Apple Silicon. ctest --preset dev green 21/21 (2 consecutive runs). apple-dev produces valid arm64 libvoicecat.a + XCFramework for the Swift Package. Three real cross-platform bugs found and fixed (all latent on Windows/Linux): 1. test_m2_voice.cpp POSIX branch missing <netdb.h> — Linux glibc transitively includes it, macOS doesn't. Would fail on any strict POSIX system. 2. SIGPIPE killing processes on macOS — writing to a closed TCP socket raises SIGPIPE by default (doesn't exist on Windows, benign on Linux). Fixed by ignoring SIGPIPE in both core client init and server startup (POSIX-only, #ifndef _WIN32). Production fix, not just tests. 3. Use-after-free of Asio's kqueue reactor on server shutdown — the deterministic test_tofu_flow segfault. TcpServerConn's tls_read_loop runs on a blocking-I/O thread; when Server::run() returned, io_context was destroyed while those threads were still running. On macOS kqueue the reactor pointer is null'd immediately -> segfault in socket.close(). Latent on Windows IOCP and Linux epoll. Fix: TcpAcceptor now tracks connections; new shutdown() closes all + joins threads before io is destroyed; Server::stop() now closes acceptor + media_relay too (was just io.stop()). Verified: dev + apple-dev presets build green, 21/21 tests pass, server starts + two vccli text chat over TLS (M1 on Mac), vccli --voice starts MIC stream via CoreAudio (M2 protocol-level), vccli --list-devices enumerates CoreAudio devices, xcodebuild -create-xcframework produces valid VoiceCatCore.xcframework. No ABI or proto changes. Docs updated: building.md, clients/apple/README.md, PROGRESS.md, CLAUDE.md status line.
2026-06-18 13:24:42 +02:00
asio::ip::tcp::acceptor acceptor_;
ConnFactory factory_;
bool stopped_{false};
std::mutex conns_mu_;
std::vector<std::shared_ptr<TcpServerConn>> conns_;
2026-06-15 23:48:44 +02:00
};
// ── UDP media channel (M2) ───────────────────────────────────────────────────
// Thin async UDP socket. send_to() is thread-safe. Recv callbacks fire on the
// io_context's thread (same thread that runs the io_context::run() loop).
class UdpMediaChannel {
public:
using FrameCallback =
std::function<void(const uint8_t*, size_t, asio::ip::udp::endpoint)>;
UdpMediaChannel() = default;
~UdpMediaChannel() { close(); }
UdpMediaChannel(const UdpMediaChannel&) = delete;
UdpMediaChannel& operator=(const UdpMediaChannel&) = delete;
// Bind to 0.0.0.0:port (0 = OS-assigned). Must be called before start_recv/send_to.
bool bind(asio::io_context& io, uint16_t port = 0);
// Begin the async recv loop. cb is called on the io_context thread.
void start_recv(FrameCallback cb);
// Thread-safe fire-and-forget send. Copies data into a heap buffer.
void send_to(const uint8_t* data, size_t len, asio::ip::udp::endpoint dst);
// Cancel all async ops and close the socket. Safe to call from any thread.
void close();
asio::ip::udp::endpoint local_endpoint() const;
bool bound() const { return bound_.load(std::memory_order_acquire); }
private:
void do_recv();
// Socket + recv state live here; only accessed from the io_context thread after bind().
std::unique_ptr<asio::ip::udp::socket> socket_;
asio::ip::udp::endpoint sender_ep_;
std::array<uint8_t, 1500> recv_buf_{};
FrameCallback frame_cb_;
std::atomic<bool> bound_{false};
std::atomic<bool> closed_{false};
};
} // namespace voicecat::net
2026-06-15 23:48:44 +02:00
#else // !VOICECAT_HAS_NET — skeleton stubs for the dev preset
namespace voicecat::net {
class TcpControlChannel {
public:
bool connected() const { return false; }
};
class UdpMediaChannel {
public:
bool bound() const { return false; }
};
} // namespace voicecat::net
#endif // VOICECAT_HAS_NET
#endif // VOICECAT_NET_TRANSPORT_H