17 #error "Hosted::Client can be used only on the Host architecture!"
23 #include <simpleRPC.h>
24 #include <simpleRPC/parser.h>
29 using namespace simpleRPC;
40 Client(
Stream& stream,
char methodEndsWith =
':') : stream(stream), methodEndsWith(methodEndsWith)
60 template <
typename... Args>
bool send(
const String& functionName, Args... args)
62 int functionId = getFunctionId(functionName);
67 rpcPrint(stream, uint8_t(functionId), args...);
77 template <
typename R> R
wait()
79 size_t neededBytes =
sizeof(R);
81 while(stream.available() < int(neededBytes)) {
87 stream.readBytes(
reinterpret_cast<char*
>(&result),
sizeof(result));
102 if(name.indexOf(
'(') != -1 && name.indexOf(
')') != -1) {
107 int id = commands.indexOf(name);
112 return commands[name];
121 host_debug_i(
"Getting remote RPC commands \033[5m...\033[0m");
124 stream.write(&head, 1);
126 ParserSettings settings;
127 settings.startMethods = ParserSettings::SimpleMethod(&Client::startMethods,
this);
128 settings.startMethod = ParserSettings::SimpleMethod(&Client::startMethod,
this);
129 settings.methodSignature = ParserSettings::CharMethod(&Client::methodSignature,
this);
130 settings.methodName = ParserSettings::CharMethod(&Client::methodName,
this);
131 settings.endMethod = ParserSettings::SimpleMethod(&Client::endMethod,
this);
132 settings.endMethods = ParserSettings::SimpleMethod(&Client::endMethods,
this);
133 settings.state = ParserState::ready;
139 size_t length = stream.readBytes(buffer, 512);
144 ParserResult result = parse(settings, buffer, length, methodEndsWith);
145 if(result == ParserResult::finished) {
149 if(result == ParserResult::error) {
157 for(
auto cmd : commands) {
158 host_debug_i(
"\t%s => %d", cmd.key().c_str(), cmd.value());
168 bool fetchCommands{
true};
169 RemoteCommands commands;
170 uint8_t methodPosition = 0;
187 void methodSignature(
char ch)
192 void methodName(
char ch)
199 if(!commands.contains(name) || signature ==
":") {
200 commands[
name] = methodPosition;
202 commands[
name +
"(" + signature +
")"] = methodPosition++;
207 fetchCommands =
false;
Definition: Components/Hosted/include/Hosted/Client.h:36
Client(Stream &stream, char methodEndsWith=':')
Definition: Components/Hosted/include/Hosted/Client.h:40
bool getRemoteCommands()
Gets list of remote command names and their ids.
Definition: Components/Hosted/include/Hosted/Client.h:119
bool send(const String &functionName, Args... args)
Method to send commands to the remote server.
Definition: Components/Hosted/include/Hosted/Client.h:60
int getFunctionId(String name)
Fetches a list of commands supported on the RPC server and gives back the id of the desired command.
Definition: Components/Hosted/include/Hosted/Client.h:96
R wait()
This method will block the execution until a message is detected.
Definition: Components/Hosted/include/Hosted/Client.h:77
Base Stream class.
Definition: Wiring/Stream.h:33
The String class.
Definition: WString.h:137
int host_main_loop()
Executing this function will run once the main emulator loop.
#define host_debug_i(fmt,...)
Definition: hostmsg.h:51
#define host_debug_e(fmt,...)
Definition: hostmsg.h:49
Definition: Components/Hosted/include/Hosted/Client.h:32
constexpr int COMMAND_NOT_FOUND
Definition: Components/Hosted/include/Hosted/Client.h:33
String convertFQN(const String &name)