MOS Source Code
Loading...
Searching...
No Matches
rpc_server++.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#if defined(__MOS_KERNEL__) || !defined(__cplusplus)
6#error "This file is only for use in C++ userspace code"
7#endif
8
9#include "librpc/rpc_server.h"
10
11#include <pb_decode.h> // RPC_DECL_SERVER_INTERFACE_CLASS needs pb_decode.h
12#include <string>
13
15{
16 public:
17 explicit RPCServer(const std::string &server_name, const rpc_function_info_t *functions, size_t count) : server_name(server_name)
18 {
19 const auto redirector = [](rpc_context_t *context)
20 {
21 const auto fid = rpc_context_get_function_id(context);
22 const auto userdata = rpc_server_get_data(rpc_context_get_server(context));
23 return ((RPCServer *) userdata)->dispatcher(context, fid);
24 };
25
26 const auto redirector_on_connect = [](rpc_context_t *context)
27 {
28 const auto userdata = rpc_server_get_data(rpc_context_get_server(context));
29 ((RPCServer *) userdata)->on_connect(context);
30 };
31
32 const auto redirector_on_disconnect = [](rpc_context_t *context)
33 {
34 const auto userdata = rpc_server_get_data(rpc_context_get_server(context));
35 ((RPCServer *) userdata)->on_disconnect(context);
36 };
37
38 rpc_function_info_t *redirect_functions = (rpc_function_info_t *) alloca(sizeof(rpc_function_info_t) * count);
39 for (size_t i = 0; i < count; i++)
40 {
41 redirect_functions[i] = functions[i];
42 redirect_functions[i].func = redirector;
43 }
44
45 server = rpc_server_create(server_name.c_str(), this);
46 rpc_server_set_on_connect(server, redirector_on_connect);
47 rpc_server_set_on_disconnect(server, redirector_on_disconnect);
48 rpc_server_register_functions(server, redirect_functions, count);
49 }
50
51 virtual ~RPCServer()
52 {
54 }
55
56 void run()
57 {
59 }
60
61 std::string get_name() const
62 {
63 return server_name;
64 }
65
66 protected:
67 virtual rpc_result_code_t dispatcher(rpc_context_t *context, u32 funcid) = 0;
68
69 virtual void on_connect(rpc_context_t *)
70 {
71 }
72
73 virtual void on_disconnect(rpc_context_t *)
74 {
75 }
76
77 protected:
78 template<typename T>
79 T *get_data(rpc_context_t *context)
80 {
81 return (T *) rpc_context_get_data(context);
82 }
83
84 template<typename T>
85 void set_data(rpc_context_t *context, T *data)
86 {
87 rpc_context_set_data(context, data);
88 }
89
90 private:
92 std::string server_name;
93};
std::string get_name() const
virtual void on_disconnect(rpc_context_t *)
void set_data(rpc_context_t *context, T *data)
virtual ~RPCServer()
RPCServer(const std::string &server_name, const rpc_function_info_t *functions, size_t count)
T * get_data(rpc_context_t *context)
virtual rpc_result_code_t dispatcher(rpc_context_t *context, u32 funcid)=0
std::string server_name
virtual void on_connect(rpc_context_t *)
rpc_server_t * server
rpc_result_code_t
Definition rpc.h:25
MOSAPI rpc_server_t * rpc_server_create(const char *server_name, void *data)
Create a new RPC server.
Definition rpc_server.c:213
MOSAPI void * rpc_context_set_data(rpc_context_t *context, void *data)
Set the context data for an RPC client.
Definition rpc_server.c:313
MOSAPI bool rpc_server_register_functions(rpc_server_t *server, const rpc_function_info_t *functions, size_t count)
Register multiple functions with the server.
Definition rpc_server.c:299
MOSAPI void rpc_server_close(rpc_server_t *server)
Close the RPC server.
Definition rpc_server.c:246
MOSAPI void * rpc_server_get_data(rpc_server_t *server)
Get the user data for the server.
Definition rpc_server.c:266
MOSAPI int rpc_context_get_function_id(const rpc_context_t *context)
Get the function ID for an RPC call context.
Definition rpc_server.c:325
MOSAPI void * rpc_context_get_data(const rpc_context_t *context)
Get the context data for an RPC context.
Definition rpc_server.c:308
MOSAPI void rpc_server_exec(rpc_server_t *server)
Run the server, this function will not return until the server is destroyed.
Definition rpc_server.c:271
MOSAPI void rpc_server_set_on_connect(rpc_server_t *server, rpc_server_on_connect_t on_connect)
Sets the callback function to be called when a client connects to the RPC server.
Definition rpc_server.c:236
MOSAPI rpc_server_t * rpc_context_get_server(const rpc_context_t *context)
Get the RPC server instance for an RPC call context.
Definition rpc_server.c:320
MOSAPI void rpc_server_set_on_disconnect(rpc_server_t *server, rpc_server_on_disconnect_t on_disconnect)
Sets the callback function to be called when a client disconnects from the RPC server.
Definition rpc_server.c:241
rpc_function_t func
Definition rpc_server.h:22
unsigned int u32
Definition types.h:21