No description
Find a file
Roger Shi 4dbde25da2 Update README.md
Add installation guide
2015-09-06 17:14:25 +08:00
example Re-organize project structure 2015-08-27 16:58:43 +08:00
src/nimrpc Re-organize project structure 2015-08-27 16:58:43 +08:00
.gitignore init commit 2015-08-26 17:02:00 +08:00
.travis.yml Re-organize project structure 2015-08-27 16:58:43 +08:00
nimrpc.nimble Re-organize project structure 2015-08-27 16:58:43 +08:00
README.md Update README.md 2015-09-06 17:14:25 +08:00
todo.txt add todo.txt 2015-08-26 17:05:06 +08:00

nim-rpc

Build Status

RPC implementation for Nim based on msgpack4nim created by @jangko.

Install

Pre Requirement

Nimble package manager installed

Install Command

nimble install nimrpc

Example

RPC Server

import nimrpc/rpc_server, nimrpc/rpc_type, asyncnet, asyncdispatch, msgpack

# Define your remote proc
# Remote porc must have two params, first is input, second is output (so it's var param).
# Return value must be an int as error code, for example, 0 for success, -1 for error.
proc remoteAdd(param: tuple[v1: int, v2: int], ret: var int): int =
  ret = param.v1 + param.v2
  result = 0

# Create the server
# Currently procs must be resgistered before rpc server starts running.
# Running time proc register will be added in later version
var server = newRpcServer("127.0.0.1", Port(4343)) # server listen to 127.0.0.1:4343
server.register("add", remoteAdd) # register remoteAdd to RPC server binding name "add"
asyncCheck server.run() # run RPC server
runForever()

RPC Client

import nimrpc/rpc_client, nimrpc/rpc_type, net, asyncdispatch

proc main =
  var client = newRpcClient("127.0.0.1", Port(4343)) # client send request to 127.0.0.1:4343
  var ret: int
  var state = client.call("add", (1, 2), ret) # client remote call add
  
  # Check remote call state first, only process ret value when state == Correct
  if state == Correct:
    echo($state) # output: Correct
    echo($ret) # output: 3
  else:
    echo($state)

if isMainModule == true:
  main()

RPC Async Client

Due to nim compiler bug, async client is not supported currently. It will be added in later version once the bug gets fixed.