MOS Source Code
Loading...
Searching...
No Matches
rpc_server.c File Reference
#include "librpc/rpc_server.h"
#include "librpc/internal.h"
#include "librpc/rpc.h"
#include <libipc/ipc.h>
#include <mos/types.h>
#include <pb.h>
#include <pb_decode.h>
#include <pb_encode.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mos/syscall/usermode.h>
#include <pthread.h>
+ Include dependency graph for rpc_server.c:

Go to the source code of this file.

Classes

struct  rpc_server_t
 
struct  rpc_args_iter_t
 
struct  _rpc_reply_wrapper
 
struct  rpc_context_t
 

Macros

#define MOS_LIB_ASSERT_X(cond, msg)
 
#define MOS_LIB_ASSERT(cond)
 
#define memzero(ptr, size)
 
#define mutex_acquire(mutex)
 
#define mutex_release(mutex)
 
#define mos_warn(fmt, ...)
 
#define MOS_LIB_UNREACHABLE()
 
#define RPC_SERVER_MAX_PENDING_CALLS   32
 
#define RPC_ARG_NEXT_IMPL(type, TYPE)
 
#define RPC_GET_ARG_IMPL(type, TYPE)
 

Functions

static void start_thread (const char *name, thread_entry_t entry, void *arg)
 
static rpc_function_info_trpc_server_get_function (rpc_server_t *server, u32 function_id)
 
static void rpc_handle_client (void *arg)
 
rpc_server_trpc_server_create (const char *server_name, void *data)
 Create a new RPC server.
 
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.
 
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.
 
void rpc_server_close (rpc_server_t *server)
 Close the RPC server.
 
void rpc_server_destroy (rpc_server_t *server)
 Destroy the RPC server.
 
void rpc_server_set_data (rpc_server_t *server, void *data)
 Set the user data for the server.
 
voidrpc_server_get_data (rpc_server_t *server)
 Get the user data for the server.
 
void rpc_server_exec (rpc_server_t *server)
 Run the server, this function will not return until the server is destroyed.
 
bool rpc_server_register_functions (rpc_server_t *server, const rpc_function_info_t *functions, size_t count)
 Register multiple functions with the server.
 
voidrpc_context_get_data (const rpc_context_t *context)
 Get the context data for an RPC context.
 
voidrpc_context_set_data (rpc_context_t *context, void *data)
 Set the context data for an RPC client.
 
rpc_server_trpc_context_get_server (const rpc_context_t *context)
 Get the RPC server instance for an RPC call context.
 
MOSAPI int rpc_context_get_function_id (const rpc_context_t *context)
 Get the function ID for an RPC call context.
 
const voidrpc_arg_next (rpc_context_t *context, size_t *size)
 Iterate to the next argument.
 
const voidrpc_arg_sized_next (rpc_context_t *context, size_t expected_size)
 Iterate to the next argument, and check that the size is as expected.
 
u8 rpc_arg_next_u8 (rpc_context_t *context)
 
u16 rpc_arg_next_u16 (rpc_context_t *context)
 
u32 rpc_arg_next_u32 (rpc_context_t *context)
 
u64 rpc_arg_next_u64 (rpc_context_t *context)
 
s8 rpc_arg_next_s8 (rpc_context_t *context)
 
s16 rpc_arg_next_s16 (rpc_context_t *context)
 
s32 rpc_arg_next_s32 (rpc_context_t *context)
 
s64 rpc_arg_next_s64 (rpc_context_t *context)
 
const char * rpc_arg_next_string (rpc_context_t *context)
 
const voidrpc_arg (const rpc_context_t *context, size_t iarg, rpc_argtype_t type, size_t *argsize)
 
u8 rpc_arg_u8 (const rpc_context_t *context, size_t iarg)
 
u16 rpc_arg_u16 (const rpc_context_t *context, size_t iarg)
 
u32 rpc_arg_u32 (const rpc_context_t *context, size_t iarg)
 
u64 rpc_arg_u64 (const rpc_context_t *context, size_t iarg)
 
s8 rpc_arg_s8 (const rpc_context_t *context, size_t iarg)
 
s16 rpc_arg_s16 (const rpc_context_t *context, size_t iarg)
 
s32 rpc_arg_s32 (const rpc_context_t *context, size_t iarg)
 
s64 rpc_arg_s64 (const rpc_context_t *context, size_t iarg)
 
const char * rpc_arg_string (const rpc_context_t *context, size_t iarg)
 
void rpc_write_result (rpc_context_t *context, const void *data, size_t size)
 Write a result to the reply.
 
bool rpc_arg_pb (rpc_context_t *context, const pb_msgdesc_t *fields, void *val, size_t argid)
 
void rpc_write_result_pb (rpc_context_t *context, const pb_msgdesc_t *type_fields, const void *val)
 

Macro Definition Documentation

◆ MOS_LIB_ASSERT_X

#define MOS_LIB_ASSERT_X ( cond,
msg )

◆ MOS_LIB_ASSERT

#define MOS_LIB_ASSERT ( cond)
Value:
assert(cond)

Definition at line 26 of file rpc_server.c.

Referenced by hashmap_init(), printf_cs(), printf_diouxX(), rpc_arg(), and tree_add_child().

◆ memzero

#define memzero ( ptr,
size )
Value:
memset(ptr, 0, size)
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
size_t size
Definition slab.c:30

Definition at line 48 of file rpc_server.c.

Referenced by rpc_server_create().

◆ mutex_acquire

#define mutex_acquire ( mutex)
Value:
pthread_mutex_lock(mutex)

Definition at line 49 of file rpc_server.c.

◆ mutex_release

#define mutex_release ( mutex)
Value:
pthread_mutex_unlock(mutex)

Definition at line 50 of file rpc_server.c.

◆ mos_warn

#define mos_warn ( fmt,
... )
Value:
fprintf(stderr, fmt "\n", ##__VA_ARGS__)
u32 const char * fmt
Definition assert.h:36
#define stderr
Definition mos_stdio.h:32

Definition at line 51 of file rpc_server.c.

Referenced by rpc_handle_client().

◆ MOS_LIB_UNREACHABLE

#define MOS_LIB_UNREACHABLE ( )
Value:
__builtin_unreachable()

Definition at line 52 of file rpc_server.c.

◆ RPC_SERVER_MAX_PENDING_CALLS

#define RPC_SERVER_MAX_PENDING_CALLS   32

Definition at line 66 of file rpc_server.c.

Referenced by rpc_server_create().

◆ RPC_ARG_NEXT_IMPL

#define RPC_ARG_NEXT_IMPL ( type,
TYPE )
Value:
type rpc_arg_next_##type(rpc_context_t *context) \
{ \
return *(type *) rpc_arg_next(context, NULL); \
}
#define NULL
Definition pb_syshdr.h:46
const void * rpc_arg_next(rpc_context_t *context, size_t *size)
Iterate to the next argument.
Definition rpc_server.c:332

Definition at line 363 of file rpc_server.c.

◆ RPC_GET_ARG_IMPL

#define RPC_GET_ARG_IMPL ( type,
TYPE )
Value:
type rpc_arg_##type(const rpc_context_t *context, size_t iarg) \
{ \
return *(type *) rpc_arg(context, iarg, RPC_ARGTYPE_##TYPE, NULL); \
}
const void * rpc_arg(const rpc_context_t *context, size_t iarg, rpc_argtype_t type, size_t *argsize)
Definition rpc_server.c:383

Definition at line 402 of file rpc_server.c.

Function Documentation

◆ start_thread()

static void start_thread ( const char * name,
thread_entry_t entry,
void * arg )
static

Definition at line 53 of file rpc_server.c.

Referenced by rpc_server_exec().

◆ rpc_server_get_function()

static rpc_function_info_t * rpc_server_get_function ( rpc_server_t * server,
u32 function_id )
inlinestatic

Definition at line 100 of file rpc_server.c.

Referenced by rpc_handle_client().

◆ rpc_handle_client()

static void rpc_handle_client ( void * arg)
static

Definition at line 108 of file rpc_server.c.

Referenced by rpc_server_exec().

+ Here is the call graph for this function:

◆ rpc_server_create()

rpc_server_t * rpc_server_create ( const char * server_name,
void * data )

Create a new RPC server.

Parameters
server_nameThe name of the server
dataA pointer to user data, which will be passed to the function
Returns
rpc_server_t* A pointer to the new server

Definition at line 213 of file rpc_server.c.

Referenced by RPCServer::RPCServer(), and userfs_manager_server_exec().

+ Here is the call graph for this function:

◆ rpc_server_set_on_connect()

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.

Parameters
serverThe RPC server instance.
on_connectThe callback function to be called when a client connects.

Definition at line 236 of file rpc_server.c.

Referenced by RPCServer::RPCServer().

◆ rpc_server_set_on_disconnect()

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.

Parameters
serverThe RPC server instance.
on_disconnectThe callback function to be called when a client disconnects.

Definition at line 241 of file rpc_server.c.

Referenced by RPCServer::RPCServer().

◆ rpc_server_close()

void rpc_server_close ( rpc_server_t * server)

Close the RPC server.

Parameters
serverThe server to close

Definition at line 246 of file rpc_server.c.

Referenced by RPCServer::~RPCServer().

+ Here is the call graph for this function:

◆ rpc_server_destroy()

void rpc_server_destroy ( rpc_server_t * server)

Destroy the RPC server.

Parameters
serverThe server to destroy

Definition at line 252 of file rpc_server.c.

+ Here is the call graph for this function:

◆ rpc_server_set_data()

void rpc_server_set_data ( rpc_server_t * server,
void * data )

Set the user data for the server.

Parameters
serverThe server to set the data for
dataThe data to set

Definition at line 261 of file rpc_server.c.

◆ rpc_server_get_data()

void * rpc_server_get_data ( rpc_server_t * server)

Get the user data for the server.

Parameters
serverThe server to get the data for

Definition at line 266 of file rpc_server.c.

Referenced by RPCServer::RPCServer().

◆ rpc_server_exec()

void rpc_server_exec ( rpc_server_t * server)

Run the server, this function will not return until the server is destroyed.

Parameters
serverThe server to run
Note
The incoming RPC calls will be handled in a separate thread.

Definition at line 271 of file rpc_server.c.

Referenced by RPCServer::run(), and userfs_manager_server_exec().

+ Here is the call graph for this function:

◆ rpc_server_register_functions()

bool rpc_server_register_functions ( rpc_server_t * server,
const rpc_function_info_t * functions,
size_t count )

Register multiple functions with the server.

Parameters
serverThe server to register the functions with
functionsAn array of function info structures
countThe number of functions to register
Returns
true The functions were registered successfully
false The functions could not be registered

Definition at line 299 of file rpc_server.c.

Referenced by RPCServer::RPCServer(), and userfs_manager_server_exec().

+ Here is the call graph for this function:

◆ rpc_context_get_data()

void * rpc_context_get_data ( const rpc_context_t * context)

Get the context data for an RPC context.

Parameters
contextThe context to get the data for
Returns
MOSAPI*

Definition at line 308 of file rpc_server.c.

Referenced by RPCServer::get_data().

◆ rpc_context_set_data()

void * rpc_context_set_data ( rpc_context_t * context,
void * data )

Set the context data for an RPC client.

Parameters
contextThe context to set the data for
dataThe data to set
Returns
void* The previous data, or NULL if there was no previous data

Definition at line 313 of file rpc_server.c.

Referenced by RPCServer::set_data().

◆ rpc_context_get_server()

rpc_server_t * rpc_context_get_server ( const rpc_context_t * context)

Get the RPC server instance for an RPC call context.

Parameters
contextThe context to get the server for
Returns
rpc_server_t* The server

Definition at line 320 of file rpc_server.c.

Referenced by RPCServer::RPCServer().

◆ rpc_context_get_function_id()

MOSAPI int rpc_context_get_function_id ( const rpc_context_t * context)

Get the function ID for an RPC call context.

Parameters
contextThe context to get the function ID for
Returns
int The function ID

Definition at line 325 of file rpc_server.c.

Referenced by RPCServer::RPCServer().

◆ rpc_arg_next()

const void * rpc_arg_next ( rpc_context_t * args,
size_t * size )

Iterate to the next argument.

Parameters
argsThe argument iterator
sizeA pointer to the size of the argument
Returns
const void* A pointer to the next argument
Note
Do not modify any of the data in the returned pointer.

Definition at line 332 of file rpc_server.c.

Referenced by rpc_arg_next_string(), and rpc_arg_sized_next().

◆ rpc_arg_sized_next()

const void * rpc_arg_sized_next ( rpc_context_t * context,
size_t expected_size )

Iterate to the next argument, and check that the size is as expected.

Parameters
contextThe RPC call context
expected_sizeThe expected size of the argument
Returns
const void* A pointer to the next argument, or NULL if the size is incorrect.
Note
Do not modify any of the data in the returned pointer.

Definition at line 354 of file rpc_server.c.

+ Here is the call graph for this function:

◆ rpc_arg_next_u8()

u8 rpc_arg_next_u8 ( rpc_context_t * context)

Definition at line 369 of file rpc_server.c.

◆ rpc_arg_next_u16()

u16 rpc_arg_next_u16 ( rpc_context_t * context)

Definition at line 370 of file rpc_server.c.

◆ rpc_arg_next_u32()

u32 rpc_arg_next_u32 ( rpc_context_t * context)

Definition at line 371 of file rpc_server.c.

◆ rpc_arg_next_u64()

u64 rpc_arg_next_u64 ( rpc_context_t * context)

Definition at line 372 of file rpc_server.c.

◆ rpc_arg_next_s8()

s8 rpc_arg_next_s8 ( rpc_context_t * context)

Definition at line 373 of file rpc_server.c.

◆ rpc_arg_next_s16()

s16 rpc_arg_next_s16 ( rpc_context_t * context)

Definition at line 374 of file rpc_server.c.

◆ rpc_arg_next_s32()

s32 rpc_arg_next_s32 ( rpc_context_t * context)

Definition at line 375 of file rpc_server.c.

◆ rpc_arg_next_s64()

s64 rpc_arg_next_s64 ( rpc_context_t * context)

Definition at line 376 of file rpc_server.c.

◆ rpc_arg_next_string()

const char * rpc_arg_next_string ( rpc_context_t * context)

Definition at line 378 of file rpc_server.c.

+ Here is the call graph for this function:

◆ rpc_arg()

const void * rpc_arg ( const rpc_context_t * context,
size_t iarg,
rpc_argtype_t type,
size_t * argsize )

Definition at line 383 of file rpc_server.c.

Referenced by rpc_arg_pb(), and rpc_arg_string().

◆ rpc_arg_u8()

u8 rpc_arg_u8 ( const rpc_context_t * context,
size_t iarg )

Definition at line 408 of file rpc_server.c.

◆ rpc_arg_u16()

u16 rpc_arg_u16 ( const rpc_context_t * context,
size_t iarg )

Definition at line 409 of file rpc_server.c.

◆ rpc_arg_u32()

u32 rpc_arg_u32 ( const rpc_context_t * context,
size_t iarg )

Definition at line 410 of file rpc_server.c.

◆ rpc_arg_u64()

u64 rpc_arg_u64 ( const rpc_context_t * context,
size_t iarg )

Definition at line 411 of file rpc_server.c.

◆ rpc_arg_s8()

s8 rpc_arg_s8 ( const rpc_context_t * context,
size_t iarg )

Definition at line 412 of file rpc_server.c.

◆ rpc_arg_s16()

s16 rpc_arg_s16 ( const rpc_context_t * context,
size_t iarg )

Definition at line 413 of file rpc_server.c.

◆ rpc_arg_s32()

s32 rpc_arg_s32 ( const rpc_context_t * context,
size_t iarg )

Definition at line 414 of file rpc_server.c.

◆ rpc_arg_s64()

s64 rpc_arg_s64 ( const rpc_context_t * context,
size_t iarg )

Definition at line 415 of file rpc_server.c.

◆ rpc_arg_string()

const char * rpc_arg_string ( const rpc_context_t * context,
size_t iarg )

Definition at line 417 of file rpc_server.c.

+ Here is the call graph for this function:

◆ rpc_write_result()

void rpc_write_result ( rpc_context_t * context,
const void * data,
size_t size )

Write a result to the reply.

Parameters
contextThe RPC call context
dataThe data to write
sizeThe size of the data to write

Definition at line 422 of file rpc_server.c.

Referenced by rpc_write_result_pb().

+ Here is the call graph for this function:

◆ rpc_arg_pb()

bool rpc_arg_pb ( rpc_context_t * context,
const pb_msgdesc_t * fields,
void * val,
size_t argid )

Definition at line 435 of file rpc_server.c.

+ Here is the call graph for this function:

◆ rpc_write_result_pb()

void rpc_write_result_pb ( rpc_context_t * context,
const pb_msgdesc_t * type_fields,
const void * val )

Definition at line 443 of file rpc_server.c.

+ Here is the call graph for this function: