o
odinpkg.dev
packages / library / RPC_Json_in_Odin

RPC_Json_in_Odin

c9e05b7library

A very simple RPC - Remote Procedure Calls, implemented in Odin with JSON marshaling/unmarshaling.

No license · updated 8 months ago

RPC Json in Odin

A very simple RPC - Remote Procedure Calls, implemented in Odin with JSON marshaling/unmarshaling.

Description

This is a RPC client and a RPC server made in Odin. The heavy lifthing is made by JSON module.

  • In this example there are 7 RPC calls:

    1. add - Sums 2 int
    2. mul - Multiply 2 f64
    3. add_vec - Sums 2 vectors element by element
    4. swap_vec - Swap vetors ( a = b, c = a )
    5. echo_string - Echoes a string
    6. process_person - Processes Person complex structure
    7. transform_data - Transforms a nexted structure
  • Suports for the parameters and return type:

    • Strut inside struct
    • Internal arrays and slices
    • Mensages of any size ( including megabytes )
    • Has return of errors.

Code structure

  • rpc_types.odin - types, structs, parameter and return types
  • rpc_server.odin - Implementation of RPC server
  • rpc_client.odin - Implementation of RPC client
  • main_server.odin - Main server program
  • main_client.odin - Main client programa

How to compile and run

Server

make all_server
make run_server

Terminal 2 - Cliente:

make all_client
make run_client

Protocol

  1. Client serialize parameters to JSON
  2. Client send size ( 4 bytes ) + JSON data
  3. Server receives, deserializes e executs funtion
  4. Server serializes result to JSON
  5. Server sends size ( 4 bytes ) + JSON data
  6. Client receives and deserializes result

Complex structurs

This simple RPC supports nexted structures like:

Person :: struct {
    
    name          : string,
    age           : int,
    address       : Address,
    phone_numbers : [ ]string,
}

Data_Container :: struct {
    
    id     : int,
    values : [ ]f64,
    nested : struct {
    
        flag  : bool,
        items : [ ]string,
    },
    person : Person,
}

License

MIT Open Source License

Have fun

Best regards,
Joao Carvalho