Cleanup net/ comments
Some checks failed
Build Linux Binaries / linux/amd64 (push) Has been cancelled
Build Linux Binaries / linux/arm64 (push) Has been cancelled

This commit is contained in:
2026-07-03 15:49:32 +01:00
parent 158a2df062
commit eafa5eb90c
3 changed files with 15 additions and 20 deletions

View File

@@ -6,7 +6,6 @@
namespace voicecat::net { namespace voicecat::net {
// ── TcpControlChannel ────────────────────────────────────────────────────────
TcpControlChannel::TcpControlChannel(TcpChannelCallbacks cbs) TcpControlChannel::TcpControlChannel(TcpChannelCallbacks cbs)
: work_guard_(asio::make_work_guard(io_)), : work_guard_(asio::make_work_guard(io_)),
@@ -129,7 +128,7 @@ void TcpControlChannel::close() {
if (net_thread_.joinable()) net_thread_.join(); if (net_thread_.joinable()) net_thread_.join();
} }
// ── TcpServerConn ────────────────────────────────────────────────────────────
TcpServerConn::TcpServerConn(asio::ip::tcp::socket socket, TcpChannelCallbacks cbs) TcpServerConn::TcpServerConn(asio::ip::tcp::socket socket, TcpChannelCallbacks cbs)
: socket_(std::move(socket)), : socket_(std::move(socket)),
@@ -323,7 +322,7 @@ void TcpServerConn::close() {
void TcpServerConn::wait_closed() { void TcpServerConn::wait_closed() {
if (tls_thread_.joinable()) { if (tls_thread_.joinable()) {
if (std::this_thread::get_id() == tls_thread_.get_id()) { if (std::this_thread::get_id() == tls_thread_.get_id()) {
// Being called from our own TLS thread detach to avoid self-join deadlock. // Being called from our own TLS thread, detach to avoid self-join deadlock.
tls_thread_.detach(); tls_thread_.detach();
} else { } else {
tls_thread_.join(); tls_thread_.join();
@@ -331,7 +330,7 @@ void TcpServerConn::wait_closed() {
} }
} }
// ── TcpAcceptor ─────────────────────────────────────────────────────────────
namespace { namespace {
// Try IPv6 dual-stack first (one socket handles both ::1 and 127.0.0.1 — fixes the common // Try IPv6 dual-stack first (one socket handles both ::1 and 127.0.0.1 — fixes the common
@@ -392,7 +391,7 @@ void TcpAcceptor::shutdown() {
for (auto& conn : to_join) { for (auto& conn : to_join) {
conn->wait_closed(); conn->wait_closed();
} }
// to_join drops here if a thread captured shared_from_this, the TcpServerConn stays // 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 // 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. // closed) and tls_thread_ is already joined, so no reactor access occurs.
} }
@@ -419,7 +418,7 @@ void TcpAcceptor::do_accept() {
}); });
} }
// ── UdpMediaChannel ──────────────────────────────────────────────────────────
bool UdpMediaChannel::bind(asio::io_context& io, uint16_t port) { bool UdpMediaChannel::bind(asio::io_context& io, uint16_t port) {
if (bound_.load()) return false; if (bound_.load()) return false;

View File

@@ -1,8 +1,6 @@
/* /*
* net/transport.h TCP control channel + UDP media channel. * net/transport.h: TCP control channel + UDP media channel.
* *
* Design: docs/architecture.md (Net thread), docs/protocol.md §1 (framing).
* Implementation uses standalone Asio for sockets and timers.
*/ */
#ifndef VOICECAT_NET_TRANSPORT_H #ifndef VOICECAT_NET_TRANSPORT_H
#define VOICECAT_NET_TRANSPORT_H #define VOICECAT_NET_TRANSPORT_H
@@ -40,7 +38,7 @@ struct TcpChannelCallbacks {
std::function<void(voicecat::crypto::TlsContext&)> on_tls_ready; std::function<void(voicecat::crypto::TlsContext&)> on_tls_ready;
}; };
// ── Client-side: owns an io_context + dedicated net thread ────────────────── // Client-side: owns an io_context + dedicated net thread
class TcpControlChannel { class TcpControlChannel {
public: public:
explicit TcpControlChannel(TcpChannelCallbacks cbs); explicit TcpControlChannel(TcpChannelCallbacks cbs);
@@ -84,7 +82,7 @@ class TcpControlChannel {
std::atomic<bool> closing_{false}; std::atomic<bool> closing_{false};
}; };
// ── Server-side: one per accepted socket, shares the server's io_context ──── // Server-side: one per accepted socket, shares the server's io_context
class TcpServerConn : public std::enable_shared_from_this<TcpServerConn> { class TcpServerConn : public std::enable_shared_from_this<TcpServerConn> {
public: public:
// Plain TCP constructor (no TLS — for tests or future plaintext paths). // Plain TCP constructor (no TLS — for tests or future plaintext paths).
@@ -113,13 +111,13 @@ class TcpServerConn : public std::enable_shared_from_this<TcpServerConn> {
bool connected() const { return connected_.load(std::memory_order_acquire); } bool connected() const { return connected_.load(std::memory_order_acquire); }
private: private:
// ── Asio path (no TLS) ─────────────────────────────────────────────────── // Asio path (no TLS)
void start_read(); void start_read();
void handle_length(std::error_code ec, std::size_t n); 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 handle_body(uint32_t length, std::error_code ec, std::size_t n);
void do_send(); void do_send();
// ── TLS path ───────────────────────────────────────────────────────────── // TLS path
void tls_read_loop(); void tls_read_loop();
void tls_drain_sends(); void tls_drain_sends();
@@ -142,7 +140,7 @@ class TcpServerConn : public std::enable_shared_from_this<TcpServerConn> {
std::deque<std::vector<uint8_t>> tls_send_queue_; std::deque<std::vector<uint8_t>> tls_send_queue_;
}; };
// ── Server-side acceptor ───────────────────────────────────────────────────── // Server-side acceptor
// Spawns a TcpServerConn (via factory) for each accepted TCP connection. // Spawns a TcpServerConn (via factory) for each accepted TCP connection.
class TcpAcceptor { class TcpAcceptor {
public: public:
@@ -177,7 +175,7 @@ class TcpAcceptor {
std::vector<std::shared_ptr<TcpServerConn>> conns_; std::vector<std::shared_ptr<TcpServerConn>> conns_;
}; };
// ── UDP media channel ──────────────────────────────────────────────────────── // UDP media channel
// Thin async UDP socket. send_to() is thread-safe. Recv callbacks fire on the // 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). // io_context's thread (same thread that runs the io_context::run() loop).
class UdpMediaChannel { class UdpMediaChannel {

View File

@@ -1,7 +1,7 @@
/* /*
* net/voice_frame.h UDP media frame wire format (header-only). * net/voice_frame.h: UDP media frame wire format (header-only).
* *
* Design: docs/voice.md §2. The 14-byte fixed header is also used as AEAD associated data. * The 14-byte fixed header is also used as AEAD associated data.
* Multi-byte fields are big-endian. The payload (Opus packet) is AEAD-encrypted. * Multi-byte fields are big-endian. The payload (Opus packet) is AEAD-encrypted.
*/ */
#ifndef VOICECAT_NET_VOICE_FRAME_H #ifndef VOICECAT_NET_VOICE_FRAME_H
@@ -44,9 +44,7 @@ inline constexpr size_t kVoiceHeaderSize = 20;
* The 20-byte header is the AEAD AAD (authenticated, not encrypted). * The 20-byte header is the AEAD AAD (authenticated, not encrypted).
* The payload region is the AEAD ciphertext + 16-byte Poly1305 MAC. * The payload region is the AEAD ciphertext + 16-byte Poly1305 MAC.
* *
* Protocol v2 widened `seq` from u16 to u64: the receiver reconstructs the AEAD
* nonce counter directly from this field, so the full 64-bit counter must be on the
* wire (a 16-bit field wrapped after 65,536 frames and desynced the nonce).
*/ */
struct VoiceFrame { struct VoiceFrame {
uint8_t type = kFrameVoice; uint8_t type = kFrameVoice;