rules.list/rules.upsert: new store/rules.{hpp,cpp} (list in priority order; apply()
upserts+removes atomically, generating an id when absent, per the schema's own "never
leaves the table in a half-valid state"). Found and fixed along the way: migration
0001's rules table had no column for Rule.name at all — every rules.list/.upsert call
failed outright ("no such column: name"), unit tests included, since :memory: migrates
through the same path. Migration 0004 adds it.
queue.reorder: new store::Queues::reorder — taskIds must be an exact permutation of
the queue's current membership (compared as sorted sets) or nothing is written and
-32602 names the queue; a valid permutation rewrites every member's queue_position in
one transaction.
schedule.get/schedule.set: a thin wrapper over the queues.schedule column (already
read since D3b, never independently settable). nextRunAt is deliberately left unset —
computing it needs the same local-time, DST-aware window logic
sched/schedule_window.hpp's window_open() only has half of; called out rather than
approximated, and the field is optional.
limiter.get/limiter.set: backed by the same downloads.speedLimitEnabled/
downloads.speedLimitBps settings keys D9 already wired — one bag of truth, not two.
The new part is reaching the engine: EnginePort/TaskActionPort gain
set_global_speed_limit(bps) (0 = unlimited, TokenBucket's own convention), wired to
Engine::rate_limiter().set_global_limit(). Pushed live on every limiter.set *and* on
Scheduler::reload_config() so a limit from a previous run isn't silently unlimited
again after a restart. applyToRunning is accepted but has no lever to pull
differently — a single shared global bucket has no "next task only" variant. Also
found, not chased further: the schema's "globalBps:0 with enabled:true means 'stop
everything'" is the opposite of what TokenBucket does with rate_bps==0 (unlimited) —
a real discrepancy, but the schema says the GUI must not offer that combination.
download.update: "moving saveDir or filename moves the file on disk in the same
operation" — resolved and root-checked like download.add's destination, then the
.veloxpart/.veloxpart.meta pair (or the finished file, if complete) is moved via
rename, falling back to copy+remove across filesystems, only when the resolved
location actually differs. categoryId/queueId(appended to the new queue's run
order)/description/segments/bufferBytes/checksum apply through new
store::Tasks::apply_update.
download.refreshUrl: same async server-layer special-case as download.probe (a real
network round trip, same 30s deadline). Re-probes, flags contentChanged only when
size or validator are both known and actually differ, persists the new URL and probe
result, and swaps the URL on a live engine handle via a newly-widened
EnginePort::refresh_url (now takes headers too, matching DownloadHandle's real
signature — the seam had silently dropped them).
Found and documented, not fixed: the generated parser collapses "field absent" and
"field explicitly null" to the same nullopt for every optional<T> patch field
(DownloadUpdateParamsPatch, Settings) — both schemas document "an explicit null
clears the field" but neither handler can act on it because the wire distinction is
already gone by the time either sees the parsed struct. A generator-level gap
(PROTO's), not something to hand-route around locally.
Verified against real veloxd + tools/testserver: rules create/list, limiter.set
takes effect and reads back, schedule.set/get round-trips, queue.reorder against real
membership (and rejects a non-permutation), download.update renames+recategorizes a
task, download.refreshUrl swaps a paused task's URL and reports contentChanged
correctly. Full ctest: 55/55 (excluding the pre-existing, unrelated conformance
failure noted two commits back).
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GRDjHGgpYmMoPE2UFbe7pP
108 lines
3.6 KiB
C++
108 lines
3.6 KiB
C++
#pragma once
|
|
|
|
// The loopback WebSocket transport (docs/05 §4). Binds 127.0.0.1 only, on the first free
|
|
// port in 52000-52016, and writes the chosen port to <runtime>/ws.port. Any local process
|
|
// can connect, so a token is mandatory: session.pair mints one (behind a user prompt,
|
|
// rate-limited), session.hello must present it, and every other method is refused -32002
|
|
// until it does. Privileged methods are refused -32003 by the generated dispatch().
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <system_error>
|
|
#include <unordered_map>
|
|
|
|
#include <nlohmann/json_fwd.hpp>
|
|
|
|
#include "rpc/event_hub.hpp"
|
|
#include "rpc/pairing.hpp"
|
|
#include "rpc/runtime_dir.hpp"
|
|
#include "rpc/task_action_port.hpp"
|
|
#include "rpc/ws_frame.hpp"
|
|
#include "velox_proto.hpp"
|
|
|
|
namespace velox::daemon::store {
|
|
class Db;
|
|
}
|
|
|
|
namespace velox::daemon::rpc {
|
|
|
|
class EventLoop;
|
|
|
|
class WsServer {
|
|
public:
|
|
// `actions` is optional (nullptr in tests that don't need download.probe) — see
|
|
// handle_download_probe's own comment for why this method can't go through the
|
|
// generic dispatch() path like everything else.
|
|
WsServer(EventLoop& loop, velox::proto::Dispatcher& dispatcher, velox::daemon::store::Db& db,
|
|
PairingApprover& approver, EventHub& hub, RuntimeDir runtime,
|
|
TaskActionPort* actions = nullptr);
|
|
~WsServer();
|
|
|
|
WsServer(const WsServer&) = delete;
|
|
WsServer& operator=(const WsServer&) = delete;
|
|
|
|
// Pick a port, bind loopback, write ws.port, listen, register with the loop.
|
|
std::error_code start();
|
|
|
|
int port() const noexcept { return port_; }
|
|
std::size_t connection_count() const noexcept { return conns_.size(); }
|
|
|
|
static constexpr int kPortLo = 52000;
|
|
static constexpr int kPortHi = 52016;
|
|
|
|
private:
|
|
enum class Phase { Handshake, Open, Closing };
|
|
|
|
struct Conn {
|
|
int fd;
|
|
Phase phase = Phase::Handshake;
|
|
std::string in_raw; // bytes before the upgrade completes
|
|
WsFrameReader frames;
|
|
std::string outbuf;
|
|
std::size_t out_off = 0;
|
|
bool close_after_flush = false;
|
|
std::string origin;
|
|
bool authed = false;
|
|
std::string pairing_id;
|
|
std::string session_id;
|
|
std::optional<EventHub::SubId> sub_id;
|
|
};
|
|
|
|
void on_listener_readable();
|
|
void on_conn_event(int fd, unsigned events);
|
|
void progress_handshake(Conn& c);
|
|
void on_ws_bytes(Conn& c, std::string_view bytes);
|
|
void handle_rpc(Conn& c, const std::string& text);
|
|
bool handle_session_ws(Conn& c, const std::string& method, const nlohmann::json& request,
|
|
nlohmann::json& reply);
|
|
|
|
// See UdsServer::handle_download_probe — same reasoning, same pattern, duplicated per
|
|
// transport because each owns its own Conn/send mechanics.
|
|
void handle_download_probe(Conn& c, const nlohmann::json& request);
|
|
void handle_download_refreshUrl(Conn& c, const nlohmann::json& request);
|
|
|
|
void send_text(Conn& c, const nlohmann::json& value);
|
|
void send_frame(Conn& c, WsOpcode op, std::string_view payload);
|
|
void begin_close(Conn& c, std::uint16_t code, std::string_view reason);
|
|
void flush(Conn& c);
|
|
void close_conn(int fd);
|
|
|
|
EventLoop& loop_;
|
|
velox::proto::Dispatcher& dispatcher_;
|
|
velox::daemon::store::Db& db_;
|
|
PairingApprover& approver_;
|
|
EventHub& hub_;
|
|
TaskActionPort* actions_;
|
|
RuntimeDir runtime_;
|
|
PairingRateLimiter rate_limiter_;
|
|
|
|
int listen_fd_ = -1;
|
|
int port_ = 0;
|
|
bool wrote_port_file_ = false;
|
|
std::unordered_map<int, std::unique_ptr<Conn>> conns_;
|
|
};
|
|
|
|
} // namespace velox::daemon::rpc
|