mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-01-07 08:03:07 +00:00
Added reviews requirements.
This commit is contained in:
parent
7607cc4106
commit
2b12f3699a
@ -22,6 +22,7 @@ proc call*(self: RpcClient, name: string,
|
||||
let msg = $ %{"jsonrpc": %"2.0", "method": %name, "params": params,
|
||||
"id": %id} & "\c\l"
|
||||
let res = await self.transp.write(msg)
|
||||
# TODO: Add actions when not full packet was send, e.g. disconnect peer.
|
||||
assert(res == len(msg))
|
||||
|
||||
# completed by processMessage.
|
||||
|
||||
@ -12,7 +12,7 @@ type
|
||||
# Procedure signature accepted as an RPC call by server
|
||||
RpcProc* = proc (params: JsonNode): Future[JsonNode]
|
||||
|
||||
RpcServer* = ref object of RootRef
|
||||
RpcServer* = ref object
|
||||
servers*: seq[StreamServer]
|
||||
procs*: TableRef[string, RpcProc]
|
||||
|
||||
@ -43,8 +43,6 @@ else:
|
||||
template ifDebug*(actions: untyped): untyped =
|
||||
discard
|
||||
|
||||
proc `$`*(port: Port): string = $int(port)
|
||||
|
||||
# Json state checking
|
||||
|
||||
template jsonValid*(jsonString: string, node: var JsonNode): (bool, string) =
|
||||
@ -139,6 +137,57 @@ proc processClient(server: StreamServer, client: StreamTransport) {.async.} =
|
||||
await client.sendError(SERVER_ERROR,
|
||||
"Error: Unknown error occurred", %"")
|
||||
|
||||
proc newRpcServer*(addresses: openarray[TransportAddress]): RpcServer =
|
||||
## Create new server and assign it to addresses ``addresses``.
|
||||
result = RpcServer()
|
||||
result.procs = newTable[string, RpcProc]()
|
||||
result.servers = newSeq[StreamServer]()
|
||||
|
||||
for item in addresses:
|
||||
try:
|
||||
ifDebug: echo "Create server on " & $item
|
||||
var server = createStreamServer(item, processClient, {ReuseAddr},
|
||||
udata = result)
|
||||
result.servers.add(server)
|
||||
except:
|
||||
ifDebug: echo "Failed to create server on " & $item
|
||||
|
||||
if len(result.servers) == 0:
|
||||
# Server was not bound, critical error.
|
||||
# TODO: Custom RpcException error
|
||||
raise newException(ValueError, "Unable to create server!")
|
||||
|
||||
proc newRpcServer*(addresses: openarray[string]): RpcServer =
|
||||
## Create new server and assign it to addresses ``addresses``.
|
||||
var
|
||||
tas4: seq[TransportAddress]
|
||||
tas6: seq[TransportAddress]
|
||||
baddrs: seq[TransportAddress]
|
||||
|
||||
for a in addresses:
|
||||
# Attempt to resolve `address` for IPv4 address space.
|
||||
try:
|
||||
tas4 = resolveTAddress(a, IpAddressFamily.IPv4)
|
||||
except:
|
||||
discard
|
||||
|
||||
# Attempt to resolve `address` for IPv6 address space.
|
||||
try:
|
||||
tas6 = resolveTAddress(a, IpAddressFamily.IPv6)
|
||||
except:
|
||||
discard
|
||||
|
||||
for r in tas4:
|
||||
baddrs.add(r)
|
||||
for r in tas6:
|
||||
baddrs.add(r)
|
||||
|
||||
if len(baddrs) == 0:
|
||||
# Addresses could not be resolved, critical error.
|
||||
raise newException(ValueError, "Unable to get address!")
|
||||
|
||||
result = newRpcServer(baddrs)
|
||||
|
||||
proc newRpcServer*(address = "localhost", port: Port = Port(8545)): RpcServer =
|
||||
var
|
||||
tas4: seq[TransportAddress]
|
||||
|
||||
@ -27,7 +27,7 @@ let
|
||||
},
|
||||
"c": %1.23}
|
||||
|
||||
var s = newRpcServer("localhost")
|
||||
var s = newRpcServer(["localhost:8545"])
|
||||
|
||||
# RPC definitions
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import unittest, json
|
||||
import ../rpcclient, ../rpcserver
|
||||
|
||||
var srv = newRpcServer()
|
||||
var srv = newRpcServer(["localhost:8545"])
|
||||
var client = newRpcClient()
|
||||
|
||||
# Create RPC on server
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user