daemon: rpc/ — Unix-socket transport + generated dispatch wiring (build step 1)

First real code in daemon/. veloxd now listens on
$XDG_RUNTIME_DIR/velox/velox.sock (0600, SO_PEERCRED same-UID check),
frames NDJSON, and routes every method through the generated
velox::proto::dispatch(). The CLI and GUI have a server to talk to.

Modules:
- rpc/ndjson.hpp    — newline-delimited framing, 8 MiB frame cap, CRLF-
                       tolerant, partial-tail buffering. Header-only, tested.
- rpc/event_loop    — single-threaded poll(2) reactor; never blocks the
                       loop. stop()/wake() are async-signal-safe (eventfd).
- rpc/runtime_dir   — $XDG_RUNTIME_DIR/velox resolution, 0700, owner-checked;
                       refuses an insecure fallback rather than using /tmp.
- rpc/uds_server    — listener + non-blocking per-conn read/write with
                       backpressure; handles session.hello (protocol-major
                       check -> -32001, sessionId, transport=uds) and
                       session.subscribe in the server layer; routes the
                       rest through dispatch().
- rpc/dispatcher    — VeloxDispatcher : proto::Dispatcher, all 39 methods.
                       download.list answers an empty table; the rest return
                       "not implemented" (-> -32603) until the store lands.
- main.cpp          — abstract-namespace single-instance lock, signal ->
                       clean shutdown, socket unlinked on exit.

Tests (ASan+UBSan and TSan clean):
- veloxd.ndjson         — framing edge cases
- veloxd.uds_roundtrip  — real socket: hello ok / version mismatch / empty
                          list / -32601 / -32700 / pipelined requests, and a
                          guard on the -32603 collapse documented in P1.

Known gap, filed not worked around: daemon/docs/proto-requests-m1.md P1 —
the generated Dispatcher has no error channel below -32603, so handlers
cannot yet return -32010/-32011/-32013 with their data payloads. The
server layer handles -32001/-32002/-32003 around dispatch(); genuine
in-handler errors collapse to -32603 until PROTO gives handlers a real
error return. Three error fixtures are non-conformant until then.

Not in this drop: rpc/ws_server (next; needs the store for hashed pairing
tokens), store/, sched/, cli/. WS reuses this event loop.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Upd9WhG9oppieig5nRDLig
This commit is contained in:
2026-09-10 15:27:20 +04:00
co-authored by Claude Sonnet 5
parent 170bcfdb3e
commit e60d6669d8
18 changed files with 1563 additions and 0 deletions
+103
View File
@@ -0,0 +1,103 @@
#pragma once
// VeloxDispatcher implements the generated velox::proto::Dispatcher — one virtual per RPC
// method. The generated dispatch() does the envelope, the transport check and the param
// parse; a method here only ever sees a validated, typed params struct and returns a
// typed result.
//
// Scope of this drop (AGENT-DAEMON.md build order): the transport is real, the store is
// not. session.hello / session.pair / session.subscribe are handled in the server layer
// (they are connection- and transport-stateful) and never reach this class in the running
// daemon. download.list answers with an empty table so a client can connect and render.
// Every other method returns "not implemented in this build" — which the generated
// dispatch() surfaces as -32603 — until the store and scheduler land.
//
// The -32603 collapse for genuine in-handler errors (-32010 / -32011 / -32013) is a known
// codegen gap, filed as P1 in daemon/docs/proto-requests-m1.md. Not worked around here.
#include "velox_proto.hpp"
namespace velox::daemon::rpc {
class VeloxDispatcher final : public velox::proto::Dispatcher {
public:
velox::proto::Result<velox::proto::CaptureRules>
on_capture_getRules(const velox::proto::CaptureGetRulesParams&) override;
velox::proto::Result<velox::proto::CaptureOfferResult>
on_capture_offer(const velox::proto::CaptureOfferParams&) override;
velox::proto::Result<velox::proto::CategoryListResult>
on_category_list(const velox::proto::CategoryListParams&) override;
velox::proto::Result<velox::proto::CategoryRemoveResult>
on_category_remove(const velox::proto::CategoryRemoveParams&) override;
velox::proto::Result<velox::proto::CategoryUpsertResult>
on_category_upsert(const velox::proto::CategoryUpsertParams&) override;
velox::proto::Result<velox::proto::DownloadAddResult>
on_download_add(const velox::proto::DownloadSpec&) override;
velox::proto::Result<velox::proto::DownloadAddBatchResult>
on_download_addBatch(const velox::proto::DownloadAddBatchParams&) override;
velox::proto::Result<velox::proto::BulkTaskResult>
on_download_cancel(const velox::proto::DownloadCancelParams&) override;
velox::proto::Result<velox::proto::TaskDetail>
on_download_get(const velox::proto::DownloadGetParams&) override;
velox::proto::Result<velox::proto::DownloadListResult>
on_download_list(const velox::proto::DownloadListParams&) override;
velox::proto::Result<velox::proto::BulkTaskResult>
on_download_pause(const velox::proto::DownloadPauseParams&) override;
velox::proto::Result<velox::proto::DownloadProbeResult>
on_download_probe(const velox::proto::DownloadProbeParams&) override;
velox::proto::Result<velox::proto::DownloadProvideAuthResult>
on_download_provideAuth(const velox::proto::DownloadProvideAuthParams&) override;
velox::proto::Result<velox::proto::DownloadRefreshUrlResult>
on_download_refreshUrl(const velox::proto::DownloadRefreshUrlParams&) override;
velox::proto::Result<velox::proto::DownloadRemoveResult>
on_download_remove(const velox::proto::DownloadRemoveParams&) override;
velox::proto::Result<velox::proto::BulkTaskResult>
on_download_resume(const velox::proto::DownloadResumeParams&) override;
velox::proto::Result<velox::proto::BulkTaskResult>
on_download_start(const velox::proto::DownloadStartParams&) override;
velox::proto::Result<velox::proto::TaskSummary>
on_download_update(const velox::proto::DownloadUpdateParams&) override;
velox::proto::Result<velox::proto::GrabberHarvestResult>
on_grabber_harvest(const velox::proto::GrabberHarvestParams&) override;
velox::proto::Result<velox::proto::GrabberStartResult>
on_grabber_start(const velox::proto::GrabberStartParams&) override;
velox::proto::Result<velox::proto::GrabberStatusResult>
on_grabber_status(const velox::proto::GrabberStatusParams&) override;
velox::proto::Result<velox::proto::Limiter>
on_limiter_get(const velox::proto::LimiterGetParams&) override;
velox::proto::Result<velox::proto::Limiter> on_limiter_set(const velox::proto::Limiter&) override;
velox::proto::Result<velox::proto::MediaAddVariantResult>
on_media_addVariant(const velox::proto::MediaAddVariantParams&) override;
velox::proto::Result<velox::proto::MediaListVariantsResult>
on_media_listVariants(const velox::proto::MediaListVariantsParams&) override;
velox::proto::Result<velox::proto::QueueListResult>
on_queue_list(const velox::proto::QueueListParams&) override;
velox::proto::Result<velox::proto::QueueReorderResult>
on_queue_reorder(const velox::proto::QueueReorderParams&) override;
velox::proto::Result<velox::proto::QueueStartResult>
on_queue_start(const velox::proto::QueueStartParams&) override;
velox::proto::Result<velox::proto::QueueStopResult>
on_queue_stop(const velox::proto::QueueStopParams&) override;
velox::proto::Result<velox::proto::QueueUpsertResult>
on_queue_upsert(const velox::proto::QueueUpsertParams&) override;
velox::proto::Result<velox::proto::RulesListResult>
on_rules_list(const velox::proto::RulesListParams&) override;
velox::proto::Result<velox::proto::RulesUpsertResult>
on_rules_upsert(const velox::proto::RulesUpsertParams&) override;
velox::proto::Result<velox::proto::ScheduleGetResult>
on_schedule_get(const velox::proto::ScheduleGetParams&) override;
velox::proto::Result<velox::proto::ScheduleSetResult>
on_schedule_set(const velox::proto::ScheduleSetParams&) override;
velox::proto::Result<velox::proto::SessionHelloResult>
on_session_hello(const velox::proto::SessionHelloParams&) override;
velox::proto::Result<velox::proto::SessionPairResult>
on_session_pair(const velox::proto::SessionPairParams&) override;
velox::proto::Result<velox::proto::SessionSubscribeResult>
on_session_subscribe(const velox::proto::SessionSubscribeParams&) override;
velox::proto::Result<velox::proto::SettingsGetResult>
on_settings_get(const velox::proto::SettingsGetParams&) override;
velox::proto::Result<velox::proto::SettingsSetResult>
on_settings_set(const velox::proto::SettingsSetParams&) override;
};
} // namespace velox::daemon::rpc