int main() { RPCServer server; server.registerService("sum", RPCService::shared_pointer(new SumServiceImpl())); // you can register as many services as you want here ... server.printInfo(); server.run(); return 0; }
int main(int argc,char *argv[]) { RPCServer server; // register our service as "helloService" server.registerService("helloService", RPCService::shared_pointer(new epics::helloService::HelloService())); server.printInfo(); server.run(); return 0; }
void RPCServer::run_callback(JSContextRef ctx, JSObjectRef this_object, size_t argc, const JSValueRef arguments[], jsc::ReturnValue &return_value) { RPCServer* server = get_rpc_server(JSContextGetGlobalContext(ctx)); if (!server) { return; } // The first argument was curried to be the callback id. RPCObjectID callback_id = jsc::Value::to_number(ctx, arguments[0]); JSObjectRef arguments_array = jsc::Object::create_array(ctx, uint32_t(argc - 1), argc == 1 ? nullptr : arguments + 1); json arguments_json = server->serialize_json_value(arguments_array); // The next task on the stack will instruct the JS to run this callback. // This captures references since it will be executed before exiting this function. server->m_worker.add_task([&]() -> json { return { {"callback", callback_id}, {"arguments", arguments_json}, }; }); // Wait for the next callback result to come off the result stack. while (server->m_callback_results.empty()) { // This may recursively bring us into another callback, hence the callback results being a stack. server->m_worker.try_run_task(); } json results = server->m_callback_results.pop_back(); json error = results["error"]; // The callback id should be identical! assert(callback_id == results["callback"].get<RPCObjectID>()); if (!error.is_null()) { throw jsc::Exception(ctx, error.get<std::string>()); } return_value.set(server->deserialize_json_value(results["result"])); }