FastLED 3.10.8
Loading...
Searching...
No Matches
remote.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/json.h"
4#include "fl/stl/stdint.h"
5#include "fl/stl/cstddef.h"
6#include "fl/stl/move.h"
7#include "fl/stl/string.h"
8#include "fl/system/sketch_macros.h" // FL_PLATFORM_HAS_LARGE_MEMORY -- gates RAM fields
10#include "fl/stl/vector.h"
11#include "fl/stl/strstream.h" // IWYU pragma: keep
12#include "fl/remote/rpc/rpc.h"
16#include "fl/remote/types.h"
18#include "fl/stl/noexcept.h"
19
20namespace fl {
21
22// Forward declarations
23class ResponseSend;
24
42class Remote : public Server {
43public:
45 template<typename Callable>
47
48 // Import types from various sources
49 using RpcResult = fl::RpcResult; // from fl/remote/types.h
50 using ClearFlags = fl::RemoteClearFlags; // from fl/remote/types.h
51 using MethodInfo = fl::MethodInfo; // from fl/remote/rpc/rpc.h
52 using ParamInfo = fl::ParamInfo; // from fl/remote/rpc/rpc.h
53
74
79
80 // Non-copyable, non-movable (lambda captures 'this')
81 Remote(const Remote&) FL_NO_EXCEPT = delete;
83 Remote& operator=(const Remote&) FL_NO_EXCEPT = delete;
84 Remote& operator=(Remote&&) FL_NO_EXCEPT = delete;
85
86 // =========================================================================
87 // Method Registration
88 // =========================================================================
89
91 template<typename Callable>
92 void bind(const Config<Callable>& config) FL_NO_EXCEPT {
93 mRpc.bind(config);
94 }
95
97 template<typename Callable>
98 void bind(const char* name, Callable fn, fl::RpcMode mode = fl::RpcMode::SYNC) FL_NO_EXCEPT {
99 bind(fl::Rpc::Config<Callable>{name, fl::move(fn), mode});
100 }
101
105 void bindAsync(const char* name,
106 fl::function<void(fl::ResponseSend&, const fl::json&)> fn,
108 mRpc.bindAsync(name, fl::move(fn), mode);
109 }
110
113 mRpc.bindStreaming(name, fl::move(fn));
114 }
115
117 template<class Sig>
118 fl::BindResult<Sig> get(const char* name) const FL_NO_EXCEPT {
119 return mRpc.get<Sig>(name);
120 }
121
123 bool has(const fl::string& name) const FL_NO_EXCEPT;
124
126 bool unbind(const fl::string& name) FL_NO_EXCEPT;
127
128 // =========================================================================
129 // RPC Processing
130 // =========================================================================
131
135
136 // =========================================================================
137 // Server Coordination
138 // =========================================================================
139
141 size_t update(u32 currentTimeMs) FL_NO_EXCEPT;
142
144 size_t tick(u32 currentTimeMs) FL_NO_EXCEPT;
145
146 // Note: pull() and push() inherited from Server<fl::json, fl::json>
147
148 // =========================================================================
149 // Results and State
150 // =========================================================================
151
153 size_t pendingCount() const FL_NO_EXCEPT;
154
156 void clear(ClearFlags flags) FL_NO_EXCEPT;
157
158 // =========================================================================
159 // Schema
160 // =========================================================================
161
164
168 return mRpc.schema();
169 }
170
172 fl::size count() const FL_NO_EXCEPT {
173 return mRpc.count();
174 }
175
176 // =========================================================================
177 // Async Response Support
178 // =========================================================================
179
182 void sendAsyncResponse(const char* method, const fl::json& result) FL_NO_EXCEPT;
183
186 void sendStreamUpdate(const char* method, const fl::json& update) FL_NO_EXCEPT;
187
190 void sendStreamFinal(const char* method, const fl::json& result) FL_NO_EXCEPT;
191
192 // =========================================================================
193 // Error Reporting (Unsolicited Notifications)
194 // =========================================================================
195
201 void reportError(const fl::string& message) FL_NO_EXCEPT;
202
205 void reportError(const fl::json& data) FL_NO_EXCEPT;
206
207protected:
208 // Storage for async request IDs (method name -> request ID).
209 // Gated on Low-memory per #3224 Tier 1B: the LowMemory async-tracking +
210 // scheduling + result-vector paths are all `#if`'d out of processRpc, so
211 // the corresponding instance state is dead RAM weight. Drop the field
212 // declarations entirely on Low-memory to recover several hundred bytes
213 // of .bss per Remote instance.
217 };
218#if FL_PLATFORM_HAS_LARGE_MEMORY
220 void scheduleFunction(u32 timestamp, u32 receivedAt, const fl::json& jsonRpcRequest) FL_NO_EXCEPT;
221 void recordResult(const fl::string& funcName, const fl::json& result, u32 scheduledAt, u32 receivedAt, u32 executedAt, bool wasScheduled) FL_NO_EXCEPT;
222#endif
223
224 // Method registry and execution
226
227#if FL_PLATFORM_HAS_LARGE_MEMORY
228 // Generic task scheduler
229 fl::net::RpcScheduler<> mScheduler;
230
231 // RPC-specific result tracking
232 fl::vector<RpcResult> mResults;
233#endif
234
235 // Note: mRequestSource, mResponseSink, mOutgoingQueue inherited from Server
236};
237
238} // namespace fl
Remote(RequestSource source, ResponseSink sink) FL_NO_EXCEPT
Construct with I/O callbacks.
fl::size count() const FL_NO_EXCEPT
Get number of registered methods.
Definition remote.h:172
typename fl::Rpc::Config< Callable > Config
Configuration for method registration (forwards to Rpc::Config)
Definition remote.h:46
fl::ParamInfo ParamInfo
Definition remote.h:52
void bind(const char *name, Callable fn, fl::RpcMode mode=fl::RpcMode::SYNC) FL_NO_EXCEPT
Register method by name, function, and optional mode (default SYNC)
Definition remote.h:98
fl::json schema() const FL_NO_EXCEPT
Returns flat schema document Format: {"schema": [["methodName", "returnType", [["param1",...
Definition remote.h:167
fl::BindResult< Sig > get(const char *name) const FL_NO_EXCEPT
Get bound method by name for direct C++ invocation.
Definition remote.h:118
Remote(Remote &&) FL_NO_EXCEPT=delete
void bindAsync(const char *name, fl::function< void(fl::ResponseSend &, const fl::json &)> fn, fl::RpcMode mode=fl::RpcMode::ASYNC) FL_NO_EXCEPT
Register async method with ResponseSend& parameter (for ASYNC/ASYNC_STREAM modes) Signature: void(Res...
Definition remote.h:105
fl::json processRpc(const fl::json &request) FL_NO_EXCEPT
Process JSON-RPC request (with optional "timestamp" field for scheduling) Returns JSON-RPC response: ...
fl::RpcResult RpcResult
Definition remote.h:49
fl::MethodInfo MethodInfo
Definition remote.h:51
size_t pendingCount() const FL_NO_EXCEPT
Get number of pending scheduled calls.
bool has(const fl::string &name) const FL_NO_EXCEPT
Check if method is registered.
fl::RemoteClearFlags ClearFlags
Definition remote.h:50
bool unbind(const fl::string &name) FL_NO_EXCEPT
Unregister method by name.
fl::vector< MethodInfo > methods() const FL_NO_EXCEPT
Get method information for all registered methods.
void sendAsyncResponse(const char *method, const fl::json &result) FL_NO_EXCEPT
Send async response for a previously-called async method The request ID is automatically retrieved fr...
void sendStreamUpdate(const char *method, const fl::json &update) FL_NO_EXCEPT
Send stream update for a streaming async method (ASYNC_STREAM mode) The request ID is automatically r...
void sendStreamFinal(const char *method, const fl::json &result) FL_NO_EXCEPT
Send final stream response for a streaming async method (ASYNC_STREAM mode) The request ID is automat...
void bindStreaming(const char *name, fl::StreamingRpcHandler fn) FL_NO_EXCEPT
Register method that streams its JSON-RPC result directly to the transport.
Definition remote.h:112
void reportError(const fl::string &message) FL_NO_EXCEPT
Send an error notification to the remote peer.
fl::Rpc mRpc
Definition remote.h:225
Remote(const Remote &) FL_NO_EXCEPT=delete
size_t tick(u32 currentTimeMs) FL_NO_EXCEPT
Process scheduled calls (call regularly)
void bind(const Config< Callable > &config) FL_NO_EXCEPT
Register method with config (name, function, optional metadata)
Definition remote.h:92
Helper class for sending responses in async/streaming RPC methods.
Definition rpc.h:181
fl::function< fl::optional< fl::json >()> RequestSource
Definition server.h:31
size_t update() FL_NO_EXCEPT
Main update: pull + push.
Server() FL_NO_EXCEPT
Default constructor.
fl::function< void(const fl::json &)> ResponseSink
Definition server.h:32
Generic time-based task scheduler.
ClearFlags
Flags for FastLED.clear() to control what state gets cleared/reset.
Definition FastLED.h:666
FastLED's Elegant JSON Library: fl::json
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:30
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
void clear(CRGB(&arr)[N]) FL_NO_EXCEPT
Definition clear.h:14
fl::function< void(fl::JsonStreamWriter &, const fl::json &, const fl::json &)> StreamingRpcHandler
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
RemoteClearFlags
Flags for clearing Remote state (can be OR'd together)
Definition types.h:33
fl::function< void(fl::JsonStreamCallback)> ResponseStreamSink
RpcMode
Definition rpc_mode.h:9
Base definition for an LED controller.
Definition crgb.hpp:179
Method parameter information.
Definition rpc.h:115
Method information.
Definition rpc.h:123
Wraps the result of binding to a method by name.
Definition rpc.h:139
Configuration for method registration with optional metadata.
Definition rpc.h:186
Result metadata for executed RPC calls.
Definition types.h:15