NanoPack RPC Format
The binary format of NanoPack RPC (NRPC) is heavily inspired by MessagePack’s implementation of RPC.
NPRC Request
An NRPC Request is sent whenever a remote function is called through an RPC client. The request is constructed on the client’s side and then sent over to the designated RPC server through the transport specified by the user, be it WebSocket, HTTP, or even stdio.
An NRPC request consists of the following four parts, in order of their appearance in the buffer:
- Message type (uint8): always the constant
1
to represent an NRPC request - Message ID (uint32): a unique ID that identifies this message.
- Function name (string): a length-prefixed UTF8 string that stores the name of the function that should be called. (More about string here)
- Serialized arguments: all of the arguments passed to the function serialized according to NanoPack’s specficiation, one after another.
As an example, let’s consider the following function:
and a call to the function like so:
The request will look like this in bytes, assuming the message ID is 10:
for readibility, the bytes are broken down line by line, but in practice they are all in one buffer.
NRPC Response
An NRPC Response is sent in response to an NRPC request. The response is constructed on the server’s side and then sent to the designated RPC client through the transport specified by the user.
An NRPC response consists of the following four parts, in order of their appearance in the buffer:
- Message type (uint8): always the constant
2
to represent an NRPC response - Message ID (uint32): the message ID of the request this response is for
- Error flag (uint8):
0
if the RPC request is successful;1
if an error has occurred - Serialized return value: omitted if void function was called; the return value if the call is successful; optional error value returned by the server if error flag is
1
.
Here is an example of what the response to the add
RPC above looks like: