FastLED 3.9.15
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"
9#include "fl/stl/vector.h"
10#include "fl/stl/strstream.h" // IWYU pragma: keep
11#include "fl/remote/rpc/rpc.h"
14#include "fl/remote/types.h"
16#include "fl/stl/noexcept.h"
17
18namespace fl {
19
20// Forward declarations
21class ResponseSend;
22
40class Remote : public Server {
41public:
43 template<typename Callable>
45
46 // Import types from various sources
47 using RpcResult = fl::RpcResult; // from fl/remote/types.h
48 using ClearFlags = fl::RemoteClearFlags; // from fl/remote/types.h
49 using MethodInfo = fl::MethodInfo; // from fl/remote/rpc/rpc.h
50 using ParamInfo = fl::ParamInfo; // from fl/remote/rpc/rpc.h
51
71 Remote(RequestSource source, ResponseSink sink);
72
73 // Non-copyable, non-movable (lambda captures 'this')
74 Remote(const Remote&) FL_NOEXCEPT = delete;
76 Remote& operator=(const Remote&) FL_NOEXCEPT = delete;
77 Remote& operator=(Remote&&) FL_NOEXCEPT = delete;
78
79 // =========================================================================
80 // Method Registration
81 // =========================================================================
82
84 template<typename Callable>
85 void bind(const Config<Callable>& config) {
86 mRpc.bind(config);
87 }
88
90 template<typename Callable>
91 void bind(const char* name, Callable fn, fl::RpcMode mode = fl::RpcMode::SYNC) {
92 bind(fl::Rpc::Config<Callable>{name, fl::move(fn), mode});
93 }
94
98 void bindAsync(const char* name,
99 fl::function<void(fl::ResponseSend&, const fl::json&)> fn,
101 mRpc.bindAsync(name, fl::move(fn), mode);
102 }
103
105 template<class Sig>
106 fl::BindResult<Sig> get(const char* name) const {
107 return mRpc.get<Sig>(name);
108 }
109
111 bool has(const fl::string& name) const;
112
114 bool unbind(const fl::string& name);
115
116 // =========================================================================
117 // RPC Processing
118 // =========================================================================
119
122 fl::json processRpc(const fl::json& request);
123
124 // =========================================================================
125 // Server Coordination
126 // =========================================================================
127
129 size_t update(u32 currentTimeMs);
130
132 size_t tick(u32 currentTimeMs);
133
134 // Note: pull() and push() inherited from Server<fl::json, fl::json>
135
136 // =========================================================================
137 // Results and State
138 // =========================================================================
139
141 size_t pendingCount() const;
142
144 void clear(ClearFlags flags);
145
146 // =========================================================================
147 // Schema
148 // =========================================================================
149
152
155 fl::json schema() const {
156 return mRpc.schema();
157 }
158
160 fl::size count() const {
161 return mRpc.count();
162 }
163
164 // =========================================================================
165 // Async Response Support
166 // =========================================================================
167
170 void sendAsyncResponse(const char* method, const fl::json& result);
171
174 void sendStreamUpdate(const char* method, const fl::json& update);
175
178 void sendStreamFinal(const char* method, const fl::json& result);
179
180 // =========================================================================
181 // Error Reporting (Unsolicited Notifications)
182 // =========================================================================
183
189 void reportError(const fl::string& message);
190
193 void reportError(const fl::json& data);
194
195protected:
196 // Storage for async request IDs (method name -> request ID)
200 };
202 void scheduleFunction(u32 timestamp, u32 receivedAt, const fl::json& jsonRpcRequest);
203 void recordResult(const fl::string& funcName, const fl::json& result, u32 scheduledAt, u32 receivedAt, u32 executedAt, bool wasScheduled);
204
205 // Method registry and execution
207
208 // Generic task scheduler
210
211 // RPC-specific result tracking
213
214 // Note: mRequestSource, mResponseSink, mOutgoingQueue inherited from Server
215};
216
217} // namespace fl
ClearFlags
Flags for FastLED.clear() to control what state gets cleared/reset.
Definition FastLED.h:578
void sendStreamFinal(const char *method, const fl::json &result)
Send final stream response for a streaming async method (ASYNC_STREAM mode) The request ID is automat...
void bind(const Config< Callable > &config)
Register method with config (name, function, optional metadata)
Definition remote.h:85
void recordResult(const fl::string &funcName, const fl::json &result, u32 scheduledAt, u32 receivedAt, u32 executedAt, bool wasScheduled)
size_t tick(u32 currentTimeMs)
Process scheduled calls (call regularly)
typename fl::Rpc::Config< Callable > Config
Configuration for method registration (forwards to Rpc::Config)
Definition remote.h:44
fl::ParamInfo ParamInfo
Definition remote.h:50
fl::net::RpcScheduler mScheduler
Definition remote.h:209
fl::BindResult< Sig > get(const char *name) const
Get bound method by name for direct C++ invocation.
Definition remote.h:106
Remote(Remote &&) FL_NOEXCEPT=delete
bool has(const fl::string &name) const
Check if method is registered.
void scheduleFunction(u32 timestamp, u32 receivedAt, const fl::json &jsonRpcRequest)
Remote(const Remote &) FL_NOEXCEPT=delete
fl::RpcResult RpcResult
Definition remote.h:47
size_t pendingCount() const
Get number of pending scheduled calls.
fl::MethodInfo MethodInfo
Definition remote.h:49
fl::unordered_map< fl::string, AsyncRequest > mAsyncRequests
Definition remote.h:201
fl::json schema() const
Returns flat schema document Format: {"schema": [["methodName", "returnType", [["param1",...
Definition remote.h:155
fl::RemoteClearFlags ClearFlags
Definition remote.h:48
void bind(const char *name, Callable fn, fl::RpcMode mode=fl::RpcMode::SYNC)
Register method by name, function, and optional mode (default SYNC)
Definition remote.h:91
fl::vector< MethodInfo > methods() const
Get method information for all registered methods.
void bindAsync(const char *name, fl::function< void(fl::ResponseSend &, const fl::json &)> fn, fl::RpcMode mode=fl::RpcMode::ASYNC)
Register async method with ResponseSend& parameter (for ASYNC/ASYNC_STREAM modes) Signature: void(Res...
Definition remote.h:98
void reportError(const fl::string &message)
Send an error notification to the remote peer.
void sendAsyncResponse(const char *method, const fl::json &result)
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)
Send stream update for a streaming async method (ASYNC_STREAM mode) The request ID is automatically r...
fl::vector< RpcResult > mResults
Definition remote.h:212
bool unbind(const fl::string &name)
Unregister method by name.
fl::Rpc mRpc
Definition remote.h:206
fl::size count() const
Get number of registered methods.
Definition remote.h:160
void clear(ClearFlags flags)
Clear state (bitwise OR of ClearFlags)
fl::json processRpc(const fl::json &request)
Process JSON-RPC request (with optional "timestamp" field for scheduling) Returns JSON-RPC response: ...
Remote(RequestSource source, ResponseSink sink)
Construct with I/O callbacks.
Helper class for sending responses in async/streaming RPC methods.
Definition rpc.h:178
size_t update()
Main update: pull + push.
fl::function< fl::optional< fl::json >()> RequestSource
Definition server.h:29
Server() FL_NOEXCEPT
Default constructor.
fl::function< void(const fl::json &)> ResponseSink
Definition server.h:30
Generic time-based task scheduler.
FastLED's Elegant JSON Library: fl::json
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
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:32
RpcMode
Definition rpc_mode.h:9
Base definition for an LED controller.
Definition crgb.hpp:179
Method parameter information.
Definition rpc.h:112
Method information.
Definition rpc.h:120
Wraps the result of binding to a method by name.
Definition rpc.h:136
#define FL_NOEXCEPT
Configuration for method registration with optional metadata.
Definition rpc.h:183
Result metadata for executed RPC calls.
Definition types.h:14