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

484 lines
17 KiB
C++
Raw Normal View History

#include "net/transport.h"
2026-06-15 23:48:44 +02:00
#include <cstring>
#include "crypto/crypto.h"
namespace voicecat::net {
2026-06-15 23:48:44 +02:00
// ── TcpControlChannel ────────────────────────────────────────────────────────
TcpControlChannel::TcpControlChannel(TcpChannelCallbacks cbs)
: work_guard_(asio::make_work_guard(io_)),
socket_(io_),
strand_(io_.get_executor()),
cbs_(std::move(cbs)) {
net_thread_ = std::thread([this] { run_loop(); });
}
TcpControlChannel::~TcpControlChannel() { close(); }
void TcpControlChannel::run_loop() { io_.run(); }
void TcpControlChannel::async_connect(const std::string& host, uint16_t port) {
auto resolver = std::make_shared<asio::ip::tcp::resolver>(io_);
resolver->async_resolve(
host, std::to_string(port),
[this, resolver](std::error_code ec, asio::ip::tcp::resolver::results_type eps) {
if (ec) {
if (cbs_.on_connect_error) cbs_.on_connect_error(ec);
return;
}
asio::async_connect(socket_, eps,
[this](std::error_code ec2, const asio::ip::tcp::endpoint&) {
if (ec2) {
if (cbs_.on_connect_error) cbs_.on_connect_error(ec2);
return;
}
connected_.store(true, std::memory_order_release);
if (cbs_.on_connected) cbs_.on_connected();
start_read();
});
});
}
void TcpControlChannel::send_frame(std::vector<uint8_t> payload) {
std::vector<uint8_t> wire;
protocol::FrameCodec::emit(payload, wire);
asio::post(strand_, [this, w = std::move(wire)]() mutable {
send_queue_.push_back(std::move(w));
if (!sending_) do_send();
});
}
void TcpControlChannel::do_send() {
if (send_queue_.empty()) { sending_ = false; return; }
sending_ = true;
auto& front = send_queue_.front();
asio::async_write(socket_,
asio::buffer(front),
asio::bind_executor(strand_,
[this](std::error_code ec, std::size_t) {
if (ec) {
connected_.store(false, std::memory_order_release);
if (cbs_.on_error) cbs_.on_error(ec);
return;
}
send_queue_.pop_front();
do_send();
}));
}
void TcpControlChannel::start_read() {
asio::async_read(socket_, asio::buffer(len_buf_, 4),
[this](std::error_code ec, std::size_t n) { handle_length(ec, n); });
}
void TcpControlChannel::handle_length(std::error_code ec, std::size_t) {
if (ec) {
connected_.store(false, std::memory_order_release);
if (ec == asio::error::eof || ec == asio::error::connection_reset) {
if (cbs_.on_disconnected) cbs_.on_disconnected();
} else {
if (cbs_.on_error) cbs_.on_error(ec);
}
return;
}
uint32_t length =
(static_cast<uint32_t>(len_buf_[0]) << 24) |
(static_cast<uint32_t>(len_buf_[1]) << 16) |
(static_cast<uint32_t>(len_buf_[2]) << 8) |
static_cast<uint32_t>(len_buf_[3]);
if (length > protocol::kMaxFrameBytes) {
if (cbs_.on_error) cbs_.on_error(asio::error::message_size);
return;
}
if (length == 0) {
if (cbs_.on_frame) cbs_.on_frame({});
start_read();
return;
}
body_buf_.resize(length);
asio::async_read(socket_, asio::buffer(body_buf_),
[this, length](std::error_code ec, std::size_t n) { handle_body(length, ec, n); });
}
void TcpControlChannel::handle_body(uint32_t, std::error_code ec, std::size_t) {
if (ec) {
connected_.store(false, std::memory_order_release);
if (ec == asio::error::eof || ec == asio::error::connection_reset) {
if (cbs_.on_disconnected) cbs_.on_disconnected();
} else {
if (cbs_.on_error) cbs_.on_error(ec);
}
return;
}
if (cbs_.on_frame) cbs_.on_frame(body_buf_);
start_read();
}
void TcpControlChannel::close() {
if (closing_.exchange(true)) return;
asio::post(io_, [this] {
std::error_code ignored;
socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ignored);
socket_.close(ignored);
});
work_guard_.reset();
if (net_thread_.joinable()) net_thread_.join();
}
// ── TcpServerConn ────────────────────────────────────────────────────────────
TcpServerConn::TcpServerConn(asio::ip::tcp::socket socket, TcpChannelCallbacks cbs)
: socket_(std::move(socket)),
strand_(asio::make_strand(socket_.get_executor())),
cbs_(std::move(cbs)) {}
TcpServerConn::TcpServerConn(asio::ip::tcp::socket socket, TcpChannelCallbacks cbs,
std::unique_ptr<crypto::TlsContext> tls)
: socket_(std::move(socket)),
strand_(asio::make_strand(socket_.get_executor())),
cbs_(std::move(cbs)),
tls_(std::move(tls)) {}
TcpServerConn::~TcpServerConn() {
close();
if (tls_thread_.joinable()) {
if (std::this_thread::get_id() == tls_thread_.get_id()) {
tls_thread_.detach(); // being destroyed from our own TLS thread — detach safely
} else {
tls_thread_.join();
}
}
}
void TcpServerConn::start() {
if (tls_) {
// Run TLS handshake on a temporary thread so we don't block the io_context.
auto self = shared_from_this();
std::thread([self] {
std::string err;
int fd = static_cast<int>(self->socket_.native_handle());
if (!self->tls_->handshake(fd, err)) {
if (!self->closing_.exchange(true)) {
if (self->cbs_.on_error) {
asio::post(self->strand_, [self] {
self->cbs_.on_error(
std::make_error_code(std::errc::connection_reset));
});
}
}
return;
}
self->connected_.store(true, std::memory_order_release);
// Export media keying material before the read loop starts.
if (self->cbs_.on_tls_ready) self->cbs_.on_tls_ready(*self->tls_);
2026-06-15 23:48:44 +02:00
// 50 ms timeout so tls_read_loop can drain the send queue between reads.
self->tls_->set_read_timeout(50);
self->tls_thread_ = std::thread([self] { self->tls_read_loop(); });
}).detach();
} else {
connected_.store(true, std::memory_order_release);
start_read();
}
}
void TcpServerConn::tls_read_loop() {
std::vector<uint8_t> buf(16384);
while (!closing_.load(std::memory_order_acquire)) {
tls_drain_sends();
int n = tls_->read(buf.data(), buf.size());
if (crypto::TlsContext::is_timeout_error(n)) continue;
if (n <= 0) break;
std::vector<std::vector<uint8_t>> frames;
if (!codec_.feed(buf.data(), static_cast<size_t>(n), frames)) break;
for (auto& frame : frames) {
if (cbs_.on_frame) cbs_.on_frame(std::move(frame));
}
}
connected_.store(false, std::memory_order_release);
if (cbs_.on_disconnected) cbs_.on_disconnected();
}
void TcpServerConn::tls_drain_sends() {
while (true) {
std::vector<uint8_t> frame;
{
std::lock_guard lk(tls_send_mutex_);
if (tls_send_queue_.empty()) return;
frame = std::move(tls_send_queue_.front());
tls_send_queue_.pop_front();
}
size_t off = 0;
while (off < frame.size()) {
int n = tls_->write(frame.data() + off, frame.size() - off);
if (n <= 0) { closing_.store(true, std::memory_order_release); return; }
off += static_cast<size_t>(n);
}
}
}
void TcpServerConn::start_read() {
auto self = shared_from_this();
asio::async_read(socket_, asio::buffer(len_buf_, 4),
asio::bind_executor(strand_,
[this, self](std::error_code ec, std::size_t n) { handle_length(ec, n); }));
}
void TcpServerConn::handle_length(std::error_code ec, std::size_t) {
if (ec) {
connected_.store(false, std::memory_order_release);
if (ec == asio::error::eof || ec == asio::error::connection_reset) {
if (cbs_.on_disconnected) cbs_.on_disconnected();
} else {
if (cbs_.on_error) cbs_.on_error(ec);
}
return;
}
uint32_t length =
(static_cast<uint32_t>(len_buf_[0]) << 24) |
(static_cast<uint32_t>(len_buf_[1]) << 16) |
(static_cast<uint32_t>(len_buf_[2]) << 8) |
static_cast<uint32_t>(len_buf_[3]);
if (length > protocol::kMaxFrameBytes) {
if (cbs_.on_error) cbs_.on_error(asio::error::message_size);
return;
}
if (length == 0) {
if (cbs_.on_frame) cbs_.on_frame({});
start_read();
return;
}
body_buf_.resize(length);
auto self = shared_from_this();
asio::async_read(socket_, asio::buffer(body_buf_),
asio::bind_executor(strand_,
[this, self, length](std::error_code ec, std::size_t n) {
handle_body(length, ec, n);
}));
}
void TcpServerConn::handle_body(uint32_t, std::error_code ec, std::size_t) {
if (ec) {
connected_.store(false, std::memory_order_release);
if (ec == asio::error::eof || ec == asio::error::connection_reset) {
if (cbs_.on_disconnected) cbs_.on_disconnected();
} else {
if (cbs_.on_error) cbs_.on_error(ec);
}
return;
}
if (cbs_.on_frame) cbs_.on_frame(body_buf_);
start_read();
}
void TcpServerConn::send_frame(std::vector<uint8_t> payload) {
std::vector<uint8_t> wire;
protocol::FrameCodec::emit(payload, wire);
if (tls_) {
std::lock_guard lk(tls_send_mutex_);
tls_send_queue_.push_back(std::move(wire));
} else {
auto self = shared_from_this();
asio::post(strand_, [this, self, w = std::move(wire)]() mutable {
send_queue_.push_back(std::move(w));
if (!sending_) do_send();
});
}
}
void TcpServerConn::do_send() {
if (send_queue_.empty()) { sending_ = false; return; }
sending_ = true;
auto self = shared_from_this();
auto& front = send_queue_.front();
asio::async_write(socket_,
asio::buffer(front),
asio::bind_executor(strand_,
[this, self](std::error_code ec, std::size_t) {
if (ec) {
connected_.store(false, std::memory_order_release);
if (cbs_.on_error) cbs_.on_error(ec);
return;
}
send_queue_.pop_front();
do_send();
}));
}
void TcpServerConn::close() {
if (closing_.exchange(true)) return;
std::error_code ignored;
socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ignored);
socket_.close(ignored);
connected_.store(false, std::memory_order_release);
// In non-TLS mode, the Asio async chain will naturally stop when the socket closes.
}
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
void TcpServerConn::wait_closed() {
if (tls_thread_.joinable()) {
if (std::this_thread::get_id() == tls_thread_.get_id()) {
// Being called from our own TLS thread — detach to avoid self-join deadlock.
tls_thread_.detach();
} else {
tls_thread_.join();
}
}
}
2026-06-15 23:48:44 +02:00
// ── TcpAcceptor ─────────────────────────────────────────────────────────────
feat(M4): Windows WinForms client, TOFU identity pinning, VAD threshold + always-on mode Core ABI extensions (voicecat.h): - vc_list_channels / vc_list_users / vc_list_user_streams — pull-based snapshot getters for the channel-tree and user-list UI; session_model_mu_ guards cross-thread reads - VC_EVENT_JOIN_RESULT / vc_join_channel — channel join with optional password - VC_EVENT_SERVER_IDENTITY + vc_confirm_server_identity — TOFU gate that blocks io_thread_ until the UI approves or rejects; pins TLS leaf-cert SHA-256 (not declared Ed25519) - vc_get_server_identity_display — Ed25519 fingerprint for human-readable display only - VC_INPUT_ALWAYS_ON = 2 in vc_input_mode — transmit unconditionally, no VAD gate - vc_set_vad_threshold — live RMS threshold update (0.0–1.0); EnergyVadProcessor stores it atomically so the audio RT path reads without a lock C++ implementation: - SessionModel::apply_snapshot / apply_channel_event fixed to populate parent_id, password_protected, and max_users (were permanently zeroed) - TlsContext::peer_cert_fingerprint — SHA-256 of peer leaf cert DER via mbedTLS - TofuStore split into peek (read-only) + pin (write) so first-connect only persists after user approval; tofu_store_path in vc_config for per-user pin file location - TcpAcceptor uses dual-stack IPv6+IPv4 fallback (fixes localhost → ::1 on Windows) - windows-client CMake preset: Release shared DLL, static MinGW runtime, no tools/tests - New C++ tests: test_channel_user_list_abi, test_tofu_flow (14/14 green) Windows client (clients/windows/ — .NET 10 WinForms): - VoiceCat.Interop: LibraryImport P/Invoke surface, UnmanagedCallersOnly callbacks, Channel<VoiceCatEvent> event delivery drained by 30ms WinForms Timer - VoiceCat.App: ConnectDialog (saved servers, DPAPI password storage), ServerIdentity- Dialog (TOFU first-connect / mismatch warning), MainForm (channel TreeView, user ListBox, RichTextBox chat, voice controls, device pickers, VAD/PTT/always-on mode, per-user gain/mute/NR tuning, VAD sensitivity TrackBar, level meter ProgressBar) - PttKeyCaptureDialog — focus-scoped PTT key capture (documented limitation) - PerUserTuningDialog — real-time gain/mute/NR applied to all of a user's streams - Accessibility: explicit AccessibleName/Description on every control, & mnemonics, Activity log ListBox as durable screen-reader record, AutomationNotification for curated live announcements Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-17 00:35:16 +02:00
namespace {
// Try IPv6 dual-stack first (one socket handles both ::1 and 127.0.0.1 — fixes the common
// Windows case where `localhost` resolves to ::1 before 127.0.0.1). Falls back to IPv4-only
// if the OS has IPv6 disabled or the dual-stack bind fails for any reason.
asio::ip::tcp::acceptor make_acceptor(asio::io_context& io, uint16_t port) {
asio::ip::tcp::acceptor acc(io);
std::error_code ec;
acc.open(asio::ip::tcp::v6(), ec);
if (!ec) {
acc.set_option(asio::ip::v6_only(false), ec); // dual-stack
acc.set_option(asio::ip::tcp::acceptor::reuse_address(true));
acc.bind(asio::ip::tcp::endpoint(asio::ip::tcp::v6(), port), ec);
if (!ec) acc.listen(asio::socket_base::max_listen_connections, ec);
}
if (ec) {
if (acc.is_open()) { std::error_code ignored; acc.close(ignored); }
acc.open(asio::ip::tcp::v4());
acc.set_option(asio::ip::tcp::acceptor::reuse_address(true));
acc.bind(asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port));
acc.listen(asio::socket_base::max_listen_connections);
}
return acc;
2026-06-15 23:48:44 +02:00
}
feat(M4): Windows WinForms client, TOFU identity pinning, VAD threshold + always-on mode Core ABI extensions (voicecat.h): - vc_list_channels / vc_list_users / vc_list_user_streams — pull-based snapshot getters for the channel-tree and user-list UI; session_model_mu_ guards cross-thread reads - VC_EVENT_JOIN_RESULT / vc_join_channel — channel join with optional password - VC_EVENT_SERVER_IDENTITY + vc_confirm_server_identity — TOFU gate that blocks io_thread_ until the UI approves or rejects; pins TLS leaf-cert SHA-256 (not declared Ed25519) - vc_get_server_identity_display — Ed25519 fingerprint for human-readable display only - VC_INPUT_ALWAYS_ON = 2 in vc_input_mode — transmit unconditionally, no VAD gate - vc_set_vad_threshold — live RMS threshold update (0.0–1.0); EnergyVadProcessor stores it atomically so the audio RT path reads without a lock C++ implementation: - SessionModel::apply_snapshot / apply_channel_event fixed to populate parent_id, password_protected, and max_users (were permanently zeroed) - TlsContext::peer_cert_fingerprint — SHA-256 of peer leaf cert DER via mbedTLS - TofuStore split into peek (read-only) + pin (write) so first-connect only persists after user approval; tofu_store_path in vc_config for per-user pin file location - TcpAcceptor uses dual-stack IPv6+IPv4 fallback (fixes localhost → ::1 on Windows) - windows-client CMake preset: Release shared DLL, static MinGW runtime, no tools/tests - New C++ tests: test_channel_user_list_abi, test_tofu_flow (14/14 green) Windows client (clients/windows/ — .NET 10 WinForms): - VoiceCat.Interop: LibraryImport P/Invoke surface, UnmanagedCallersOnly callbacks, Channel<VoiceCatEvent> event delivery drained by 30ms WinForms Timer - VoiceCat.App: ConnectDialog (saved servers, DPAPI password storage), ServerIdentity- Dialog (TOFU first-connect / mismatch warning), MainForm (channel TreeView, user ListBox, RichTextBox chat, voice controls, device pickers, VAD/PTT/always-on mode, per-user gain/mute/NR tuning, VAD sensitivity TrackBar, level meter ProgressBar) - PttKeyCaptureDialog — focus-scoped PTT key capture (documented limitation) - PerUserTuningDialog — real-time gain/mute/NR applied to all of a user's streams - Accessibility: explicit AccessibleName/Description on every control, & mnemonics, Activity log ListBox as durable screen-reader record, AutomationNotification for curated live announcements Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-17 00:35:16 +02:00
} // namespace
TcpAcceptor::TcpAcceptor(asio::io_context& io, uint16_t port, ConnFactory factory)
: acceptor_(make_acceptor(io, port)),
factory_(std::move(factory)) {}
2026-06-15 23:48:44 +02:00
void TcpAcceptor::start() { do_accept(); }
void TcpAcceptor::stop() {
stopped_ = true;
std::error_code ignored;
acceptor_.close(ignored);
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
// Close all tracked connections so their TLS read threads exit. The socket close
// happens while the io_context (and its reactor) is still alive, preventing the
// null-reactor use-after-free that manifests on macOS kqueue.
std::vector<std::shared_ptr<TcpServerConn>> to_close;
{
std::lock_guard lk(conns_mu_);
to_close = conns_;
}
for (auto& conn : to_close) conn->close();
}
void TcpAcceptor::shutdown() {
stop();
// Wait for every connection's TLS I/O thread to finish. close() (called by stop())
// set closing_=true and closed the socket, so tls_read_loop is already exiting or has
// exited; the join is brief. This must complete BEFORE the io_context is destroyed.
std::vector<std::shared_ptr<TcpServerConn>> to_join;
{
std::lock_guard lk(conns_mu_);
to_join = std::move(conns_);
}
for (auto& conn : to_join) {
conn->wait_closed();
}
// to_join drops here — if a thread captured shared_from_this, the TcpServerConn stays
// alive until that thread releases it; the destructor's close() is a no-op (already
// closed) and tls_thread_ is already joined, so no reactor access occurs.
2026-06-15 23:48:44 +02:00
}
void TcpAcceptor::do_accept() {
if (stopped_) return;
acceptor_.async_accept(
[this](std::error_code ec, asio::ip::tcp::socket socket) {
if (ec) {
if (!stopped_) do_accept();
return;
}
socket.set_option(asio::ip::tcp::no_delay(true));
auto conn = factory_(std::move(socket));
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
if (conn) {
conn->start();
// Track so shutdown() can close + join before the io_context is destroyed.
{
std::lock_guard lk(conns_mu_);
conns_.push_back(conn);
}
}
2026-06-15 23:48:44 +02:00
do_accept();
});
}
// ── UdpMediaChannel ──────────────────────────────────────────────────────────
bool UdpMediaChannel::bind(asio::io_context& io, uint16_t port) {
if (bound_.load()) return false;
try {
socket_ = std::make_unique<asio::ip::udp::socket>(io);
socket_->open(asio::ip::udp::v4());
socket_->set_option(asio::socket_base::reuse_address(true));
socket_->bind(asio::ip::udp::endpoint(asio::ip::udp::v4(), port));
bound_.store(true, std::memory_order_release);
return true;
} catch (...) {
socket_.reset();
return false;
}
}
void UdpMediaChannel::start_recv(FrameCallback cb) {
frame_cb_ = std::move(cb);
do_recv();
}
void UdpMediaChannel::do_recv() {
if (!socket_ || closed_.load()) return;
socket_->async_receive_from(
asio::buffer(recv_buf_), sender_ep_,
[this](std::error_code ec, std::size_t n) {
if (ec || closed_.load()) return;
if (frame_cb_ && n > 0)
frame_cb_(recv_buf_.data(), n, sender_ep_);
do_recv();
});
}
void UdpMediaChannel::send_to(const uint8_t* data, size_t len,
asio::ip::udp::endpoint dst) {
if (!socket_ || closed_.load() || len == 0) return;
auto buf = std::make_shared<std::vector<uint8_t>>(data, data + len);
asio::post(socket_->get_executor(), [this, buf, dst]() mutable {
if (closed_.load()) return;
socket_->async_send_to(
asio::buffer(*buf), dst,
[buf](std::error_code, std::size_t) {});
});
}
void UdpMediaChannel::close() {
if (closed_.exchange(true)) return;
if (socket_) {
std::error_code ec;
socket_->cancel(ec);
socket_->close(ec);
}
}
asio::ip::udp::endpoint UdpMediaChannel::local_endpoint() const {
if (!socket_) return {};
std::error_code ec;
return socket_->local_endpoint(ec);
}
} // namespace voicecat::net