1<style> 2 .table td { 3 text-align: left; 4 } 5 6 .table td:first-child { 7 text-align: right; 8 width: 230px; 9 } 10</style> 11This document is a detailed and (hopefully) complete reference of the public interface of `rpclib`. 12For a tutorial, take a look at the [Primer](primer.md). Also, you can find many examples in the [Cookbook](cookbook.md). 13 14 15 16## rpc::client 17 18```cpp 19#include "rpc/client.h" 20``` 21### Description 22 23Implements a client that connects to a msgpack-rpc server and is able to call functions synchronously or asynchronously. This is the main interfacing point for implementing client applications. 24 25Use this class to connect to msgpack-rpc servers and call their exposed functions. This class supports calling functions synchronously and asynchronously. When the client object is created, it initiates connecting to the given server asynchronically and disconnects when it is destroyed. 26 27### Public functions 28 29| | | 30|---------|-------------| 31| | [client](#classrpc_1_1client_1aafbcbb90607bb189bf75a2020ee14eb8)(std::string const &addr, uint16_t port) 32| | [~client](#classrpc_1_1client_1a87e0788aaa7a95dfc0e1b9c15dfe4163)() 33| RPCLIB_MSGPACK::object_handle | [call](#classrpc_1_1client_1aedc166b5a80820be198ef134f522b049)(std::string const &func_name, Args... args) 34| std::future< RPCLIB_MSGPACK::object_handle > | [async_call](#classrpc_1_1client_1a2e3702a314c8c0a00bfac652b82d16cc)(std::string const &func_name, Args... args) 35| void | [send](#classrpc_1_1client_1a5f5ad1d1d0630178a51ae219cd831b44)(std::string const &func_name, Args... args) 36| nonstd::optional< int64_t > | [get_timeout](#classrpc_1_1client_1a2c264af4d7169574452b9f968ffde87d)() const 37| void | [set_timeout](#classrpc_1_1client_1af890e3067745861642e2ba1a65bebce6)(int64_t value) 38| void | [clear_timeout](#classrpc_1_1client_1a89eeffaf87bf0470a65c9c8ca40562bb)() 39| connection_state | [get_connection_state](#classrpc_1_1client_1a710037bce0d9b80127a98eb6cd54caf1)() const 40| void | [wait_all_responses](#classrpc_1_1client_1ac37437bc05b70588c079079b957eb15f)() 41 42 43<h4 id="classrpc_1_1client_1aafbcbb90607bb189bf75a2020ee14eb8" class="doxy">rpc::client::client</h4> 44```cpp 45 rpc::client::client(std::string const &addr, uint16_t port); 46``` 47 48Constructs a client. 49 50##### Parameters 51`addr` The address of the server to connect to. This might be an IP address or a host name, too. 52 53`port` The port on the server to connect to. 54 55##### Details 56When a client is constructed, it initiates a connection asynchronically. This means that it will not block while the connection is established. However, when the first call is performed, it *might* block if the connection was not already established. 57 58 59<h4 id="classrpc_1_1client_1a87e0788aaa7a95dfc0e1b9c15dfe4163" class="doxy">rpc::client::~client</h4> 60```cpp 61 rpc::client::~client(); 62``` 63 64Destructor. 65 66##### Details 67During destruction, the connection to the server is gracefully closed. This means that any outstanding reads and writes are completed first. 68 69 70<h4 id="classrpc_1_1client_1aedc166b5a80820be198ef134f522b049" class="doxy">rpc::client::call</h4> 71```cpp 72RPCLIB_MSGPACK::object_handle rpc::client::call(std::string const &func_name, Args... args); 73``` 74 75Calls a function with the given name and arguments (if any). 76 77##### Template parameters 78`Args` The types of the arguments. Each type in this parameter pack have to be serializable by msgpack. 79 80##### Parameters 81`func_name` The name of the function to call on the server. 82 83`args` A variable number of arguments to pass to the called function. 84 85##### Return value 86A RPCLIB_MSGPACK::object containing the result of the function (if any). To obtain a typed value, use the msgpack API. 87 88 89<h4 id="classrpc_1_1client_1a2e3702a314c8c0a00bfac652b82d16cc" class="doxy">rpc::client::async_call</h4> 90```cpp 91std::future< RPCLIB_MSGPACK::object_handle > rpc::client::async_call(std::string const &func_name, Args... args); 92``` 93 94Calls a function asynchronously with the given name and arguments. 95 96##### Template parameters 97`Args` The types of the arguments. 98 99##### Parameters 100`func_name` The name of the function to call. 101 102`args` The arguments to pass to the function. 103 104##### Details 105A call is performed asynchronously in the context of the client, i.e. this is not to be confused with parallel execution on the server. This function differs from `call` in that it does not wait for the result of the function. Instead, it returns a std::future that can be used to retrieve the result later. 106 107##### Return value 108A std::future, possibly holding a future result (which is a RPCLIB_MSGPACK::object). 109 110 111<h4 id="classrpc_1_1client_1a5f5ad1d1d0630178a51ae219cd831b44" class="doxy">rpc::client::send</h4> 112```cpp 113void rpc::client::send(std::string const &func_name, Args... args); 114``` 115 116Sends a notification with the given name and arguments (if any). 117 118##### Template parameters 119`Args` THe types of the arguments. 120 121##### Parameters 122`func_name` The name of the notification to call. 123 124`args` The arguments to pass to the function. 125 126##### Details 127Notifications are a special kind of calls. They can be used to notify the server, while not expecting a response. In `rpclib` terminology, a notification is like an `async_call` without a return value. 128 129!!! warn 130 This function returns immediately (possibly before the notification is written to the socket). 131 132<h4 id="classrpc_1_1client_1a2c264af4d7169574452b9f968ffde87d" class="doxy">rpc::client::get_timeout</h4> 133```cpp 134nonstd::optional< int64_t > rpc::client::get_timeout() const; 135``` 136 137Returns the timeout setting of this client in milliseconds. 138 139##### Details 140The timeout is applied to synchronous calls. If the timeout expires without receiving a response from the server, 141 142!!! warn 143 The timeout has no effect on async calls. For those, the preferred timeout mechanism remains using std::future. 144 145<h4 id="classrpc_1_1client_1af890e3067745861642e2ba1a65bebce6" class="doxy">rpc::client::set_timeout</h4> 146```cpp 147void rpc::client::set_timeout(int64_t value); 148``` 149 150Sets the timeout for synchronous calls. For more information, see 151 152 153<h4 id="classrpc_1_1client_1a89eeffaf87bf0470a65c9c8ca40562bb" class="doxy">rpc::client::clear_timeout</h4> 154```cpp 155void rpc::client::clear_timeout(); 156``` 157 158Clears the timeout for synchronous calls. For more information, see 159 160 161<h4 id="classrpc_1_1client_1a710037bce0d9b80127a98eb6cd54caf1" class="doxy">rpc::client::get_connection_state</h4> 162```cpp 163connection_state rpc::client::get_connection_state() const; 164``` 165 166Returns the current connection state. 167 168 169<h4 id="classrpc_1_1client_1ac37437bc05b70588c079079b957eb15f" class="doxy">rpc::client::wait_all_responses</h4> 170```cpp 171void rpc::client::wait_all_responses(); 172``` 173 174Waits for the completion of all ongoing calls. 175 176 177 178## rpc::rpc_error 179 180```cpp 181#include "rpc/rpc_error.h" 182``` 183### Description 184 185This exception is thrown by the client when the server signals an error during a call. 186 187This type allows clients to handle arbitrary error objects as the msgpack-rpc specification allows. In client code you probably don't want to throw it, hence its constructor is private. 188 189### Public functions 190 191| | | 192|---------|-------------| 193| std::string | [get_function_name](#classrpc_1_1rpc__error_1ac45388bcde0a436b888c907015df01e2)() const 194| RPCLIB_MSGPACK::object_handle & | [get_error](#classrpc_1_1rpc__error_1a88ab8f211393ae62813042a797c08663)() 195 196 197<h4 id="classrpc_1_1rpc__error_1ac45388bcde0a436b888c907015df01e2" class="doxy">rpc::rpc_error::get_function_name</h4> 198```cpp 199std::string rpc::rpc_error::get_function_name() const; 200``` 201 202Returns the name of the function that was called on the server while the error occurred. 203 204 205<h4 id="classrpc_1_1rpc__error_1a88ab8f211393ae62813042a797c08663" class="doxy">rpc::rpc_error::get_error</h4> 206```cpp 207RPCLIB_MSGPACK::object_handle & rpc::rpc_error::get_error(); 208``` 209 210Returns the error object that the server provided. 211 212 213 214## rpc::server 215 216```cpp 217#include "rpc/server.h" 218``` 219### Description 220 221Implements a msgpack-rpc server. This is the main interfacing point with the library for creating servers. 222 223The server maintains a registry of function bindings that it uses to dispatch calls. It also takes care of managing worker threads and TCP connections. The server does not start listening right after construction in order to allow binding functions before that. Use the `run` or `async_run` functions to start listening on the port. This class is not copyable, but moveable. 224 225### Public functions 226 227| | | 228|---------|-------------| 229| | [server](#classrpc_1_1server_1ac406b44f73cf2ff17240c0cd926a9c1e)(uint16_t port) 230| | [server](#classrpc_1_1server_1a744b961d3b151b3449a4e0da37bd471a)(server &&other) noexcept 231| | [server](#classrpc_1_1server_1ad71ffce076d752e116cefa3672e5d188)(std::string const &address, uint16_t port) 232| | [~server](#classrpc_1_1server_1a20b9197e5ef1a22371e6fbdb7f58b330)() 233| server | [operator=](#classrpc_1_1server_1ac3cf4848fc3969cd26ba5e3bd2dc411b)(server &&other) 234| void | [run](#classrpc_1_1server_1a981d7e4a08d04d05cbac6770fab0dff8)() 235| void | [async_run](#classrpc_1_1server_1a462e032fa21cad78eeacc27da103a2b7)(std::size_t worker_threads=1) 236| void | [bind](#classrpc_1_1server_1a072135629430df6d5576416806f7b02c)(std::string const &name, F func) 237| void | [suppress_exceptions](#classrpc_1_1server_1a95d336322c9c24cf014404a3b4f70df5)(bool suppress) 238| void | [stop](#classrpc_1_1server_1a7df94a496caf38b3d679113c1f62082b)() 239| void | [close_sessions](#classrpc_1_1server_1abf6bebbbeea52451aef2126d29240094)() 240 241 242<h4 id="classrpc_1_1server_1ac406b44f73cf2ff17240c0cd926a9c1e" class="doxy">rpc::server::server</h4> 243```cpp 244 rpc::server::server(uint16_t port); 245``` 246 247Constructs a server that listens on the localhost on the specified port. 248 249##### Parameters 250`port` The port number to listen on. 251 252 253<h4 id="classrpc_1_1server_1a744b961d3b151b3449a4e0da37bd471a" class="doxy">rpc::server::server</h4> 254```cpp 255 rpc::server::server(server &&other) noexcept; 256``` 257 258Move constructor. This is implemented by calling the move assignment operator. 259 260##### Parameters 261`other` The other instance to move from. 262 263 264<h4 id="classrpc_1_1server_1ad71ffce076d752e116cefa3672e5d188" class="doxy">rpc::server::server</h4> 265```cpp 266 rpc::server::server(std::string const &address, uint16_t port); 267``` 268 269Constructs a server that listens on the specified address on the specified port. 270 271##### Parameters 272`address` The address to bind to. This only works if oee of your network adapaters control the given address. 273 274`port` The port number to listen on. 275 276 277<h4 id="classrpc_1_1server_1a20b9197e5ef1a22371e6fbdb7f58b330" class="doxy">rpc::server::~server</h4> 278```cpp 279 rpc::server::~server(); 280``` 281 282Destructor. 283 284##### Details 285When the server is destroyed, all ongoin sessions are closed gracefully. 286 287 288<h4 id="classrpc_1_1server_1ac3cf4848fc3969cd26ba5e3bd2dc411b" class="doxy">rpc::server::operator=</h4> 289```cpp 290server rpc::server::operator=(server &&other); 291``` 292 293Move assignment operator. 294 295##### Parameters 296`other` The other instance to move from. 297 298##### Return value 299The result of the assignment. 300 301 302<h4 id="classrpc_1_1server_1a981d7e4a08d04d05cbac6770fab0dff8" class="doxy">rpc::server::run</h4> 303```cpp 304void rpc::server::run(); 305``` 306 307Starts the server loop. This is a blocking call. 308 309##### Details 310First and foremost, running the event loop causes the server to start listening on the specified port. Also, as connections are established and calls are made by clients, the server executes the calls as part of this call. This means that the handlers are executed on the thread that calls `run`. Reads and writes are initiated by this function internally as well. 311 312 313<h4 id="classrpc_1_1server_1a462e032fa21cad78eeacc27da103a2b7" class="doxy">rpc::server::async_run</h4> 314```cpp 315void rpc::server::async_run(std::size_t worker_threads=1); 316``` 317 318Starts the server loop on one or more threads. This is a non-blocking call. 319 320##### Parameters 321`worker_threads` The number of worker threads to start. 322 323##### Details 324This function behaves similarly to `run`, except the event loop is optionally started on different threads. Effectively this sets up a worker thread pool for the server. Handlers will be executed on one of the threads. 325 326 327<h4 id="classrpc_1_1server_1a072135629430df6d5576416806f7b02c" class="doxy">rpc::server::bind</h4> 328```cpp 329void rpc::server::bind(std::string const &name, F func); 330``` 331 332Binds a functor to a name so it becomes callable via RPC. 333 334##### Template parameters 335`F` The type of the functor. 336 337##### Parameters 338`name` The name of the functor. 339 340`func` The functor to bind. 341 342##### Details 343This function template accepts a wide range of callables. The arguments and return types of these callables should be serializable by msgpack. `bind` effectively generates a suitable, light-weight compile-time wrapper for the functor. 344 345 346<h4 id="classrpc_1_1server_1a95d336322c9c24cf014404a3b4f70df5" class="doxy">rpc::server::suppress_exceptions</h4> 347```cpp 348void rpc::server::suppress_exceptions(bool suppress); 349``` 350 351Sets the exception behavior in handlers. By default, handlers throwing will crash the server. If suppressing is on, the server will try to gather textual data and return it to the client as an error response. 352 353!!! warn 354 Setting this flag only affects subsequent connections. 355 356<h4 id="classrpc_1_1server_1a7df94a496caf38b3d679113c1f62082b" class="doxy">rpc::server::stop</h4> 357```cpp 358void rpc::server::stop(); 359``` 360 361Stops the server. 362 363!!! warn 364 This should not be called from worker threads. 365 366<h4 id="classrpc_1_1server_1abf6bebbbeea52451aef2126d29240094" class="doxy">rpc::server::close_sessions</h4> 367```cpp 368void rpc::server::close_sessions(); 369``` 370 371Closes all sessions gracefully. 372 373 374 375## rpc::this_handler_t 376 377```cpp 378#include "rpc/this_handler.h" 379``` 380### Description 381 382Encapsulates information about the currently executing handler. This is the interface through which bound functions may return errors, arbitrary type responses or prohibit sending a response. 383 384 385 386### Public functions 387 388| | | 389|---------|-------------| 390| void | [respond_error](#classrpc_1_1this__handler__t_1a6cfb57fa89c28bd49c6ec82c42c32e87)(T &&err_obj) 391| void | [respond](#classrpc_1_1this__handler__t_1ab825539a615b55772ebc1513cf2b44c7)(T &&resp_obj) 392| void | [disable_response](#classrpc_1_1this__handler__t_1a179997ea536a93f1b8054e0a480876a5)() 393| void | [enable_response](#classrpc_1_1this__handler__t_1aac9c17130dea7ded4bf020a370a0b9aa)() 394| void | [clear](#classrpc_1_1this__handler__t_1a0a53e27b4d8d5b542790b218029d26f4)() 395 396 397<h4 id="classrpc_1_1this__handler__t_1a6cfb57fa89c28bd49c6ec82c42c32e87" class="doxy">rpc::this_handler_t::respond_error</h4> 398```cpp 399void rpc::this_handler_t::respond_error(T &&err_obj); 400``` 401 402Sets an arbitrary object to be sent back as an error response to the client. 403 404##### Template parameters 405`T` The type of the error object. 406 407##### Parameters 408`err_obj` The error object. This can be anything that is possible to encode with messagepack (even custom structures). 409 410 411<h4 id="classrpc_1_1this__handler__t_1ab825539a615b55772ebc1513cf2b44c7" class="doxy">rpc::this_handler_t::respond</h4> 412```cpp 413void rpc::this_handler_t::respond(T &&resp_obj); 414``` 415 416Sets an arbitrary object to be sent back as the response to the call. 417 418##### Template parameters 419`T` The type of the response object. 420 421##### Parameters 422`resp_obj` The response object. This can be anything that is possible to encode with messagepack (even custom structures). 423 424!!! warn 425 The normal return value of the function (if any) will be ignored if a special response is set. 426 427<h4 id="classrpc_1_1this__handler__t_1a179997ea536a93f1b8054e0a480876a5" class="doxy">rpc::this_handler_t::disable_response</h4> 428```cpp 429void rpc::this_handler_t::disable_response(); 430``` 431 432Instructs the server to not send a response to the client (ignoring any errors and return values). 433 434!!! warn 435 It is unusual to not send a response to requests, and doing so might cause problems in the client (depending on its implementation). 436 437<h4 id="classrpc_1_1this__handler__t_1aac9c17130dea7ded4bf020a370a0b9aa" class="doxy">rpc::this_handler_t::enable_response</h4> 438```cpp 439void rpc::this_handler_t::enable_response(); 440``` 441 442Enables sending a response to the call. Sending the response is by default enabled. Enabling the response multiple times have no effect. 443 444 445<h4 id="classrpc_1_1this__handler__t_1a0a53e27b4d8d5b542790b218029d26f4" class="doxy">rpc::this_handler_t::clear</h4> 446```cpp 447void rpc::this_handler_t::clear(); 448``` 449 450Sets all state of the object to default. 451 452 453 454## rpc::this_server_t 455 456```cpp 457#include "rpc/this_server.h" 458``` 459### Description 460 461Allows controlling the server instance from the currently executing handler. 462 463 464 465### Public functions 466 467| | | 468|---------|-------------| 469| void | [stop](#classrpc_1_1this__server__t_1a7e522ccc489d7376e2d795f030f3890f)() 470| void | [cancel_stop](#classrpc_1_1this__server__t_1a127035c6f2281a5a1bfadf19f4dfe451)() 471 472 473<h4 id="classrpc_1_1this__server__t_1a7e522ccc489d7376e2d795f030f3890f" class="doxy">rpc::this_server_t::stop</h4> 474```cpp 475void rpc::this_server_t::stop(); 476``` 477 478Gracefully stops the server. 479 480 481<h4 id="classrpc_1_1this__server__t_1a127035c6f2281a5a1bfadf19f4dfe451" class="doxy">rpc::this_server_t::cancel_stop</h4> 482```cpp 483void rpc::this_server_t::cancel_stop(); 484``` 485 486Cancels a requested stop operation. 487 488 489 490## rpc::this_session_t 491 492```cpp 493#include "rpc/this_session.h" 494``` 495### Description 496 497Encapsulates information about the server session/connection this handler is running in. This is the interface through which bound functions may interact with the session. 498 499 500 501### Public functions 502 503| | | 504|---------|-------------| 505| void | [post_exit](#classrpc_1_1this__session__t_1ae0551bc44674bdd43e33a4d4a36781b0)() 506| session_id_t | [id](#classrpc_1_1this__session__t_1aada2250ec9dd3d88781ca16eb4352047)() const 507 508 509<h4 id="classrpc_1_1this__session__t_1ae0551bc44674bdd43e33a4d4a36781b0" class="doxy">rpc::this_session_t::post_exit</h4> 510```cpp 511void rpc::this_session_t::post_exit(); 512``` 513 514Gracefully exits the session (i.e. ongoing writes and reads are completed; queued writes and reads are not). 515 516!!! warn 517 Use this function if you need to close the connection from a handler. 518 519<h4 id="classrpc_1_1this__session__t_1aada2250ec9dd3d88781ca16eb4352047" class="doxy">rpc::this_session_t::id</h4> 520```cpp 521session_id_t rpc::this_session_t::id() const; 522``` 523 524Returns an ID that uniquely identifies a session. 525 526!!! warn 527 This is not an ID for the client. If the client disconnects and reconnects, this ID may change. That being said, you can use this ID to store client-specific information *for the duration of the session. 528 529 530## rpc::timeout 531 532```cpp 533#include "rpc/rpc_error.h" 534``` 535### Description 536 537This exception is thrown by the client when either the connection or a call takes more time than it is set in set_timeout. 538 539 540 541### Public functions 542 543| | | 544|---------|-------------| 545| const char * | [what](#classrpc_1_1timeout_1ad782f083798c650188b5927a226c3b04)() const noexcept override 546 547 548<h4 id="classrpc_1_1timeout_1ad782f083798c650188b5927a226c3b04" class="doxy">rpc::timeout::what</h4> 549```cpp 550const char * rpc::timeout::what() const noexcept override; 551``` 552 553Describes the exception. 554 555 556 557