nim-json-rpc/json_rpc/server.nim
Jacek Sieka 4eb39203eb
fixes (#95)
* fixes

* fix https://github.com/status-im/nimbus-eth2/issues/1650
* only one of `result` and `error` allowed in response
* fix invalid `string` instances being created from byte sequences
* fix large int64 parsing on 32-bit
* fix exception inheritance
* fix some dangling results
* some cleanups

* annotate exception issues, fix cancellation

* more error handling cleanup

* add rudimentary error tests

* cleanups

* simplify init
* use nextId -> lastId to avoid =1 init
* remove obsolete tests
2021-02-15 13:45:51 +01:00

38 lines
969 B
Nim

import
std/[json, macros],
chronos, chronicles,
./router,
./jsonmarshal
export chronos, json, jsonmarshal, router, chronicles
type
RpcServer* = ref object of RootRef
router*: RpcRouter
proc new(T: type RpcServer): T =
T(router: RpcRouter.init())
proc newRpcServer*(): RpcServer {.deprecated.} = RpcServer.new()
template rpc*(server: RpcServer, path: string, body: untyped): untyped =
server.router.rpc(path, body)
template hasMethod*(server: RpcServer, methodName: string): bool =
server.router.hasMethod(methodName)
# Wrapper for message processing
proc route*(server: RpcServer, line: string): Future[string] {.gcsafe.} =
server.router.route(line)
# Server registration
proc register*(server: RpcServer, name: string, rpc: RpcProc) =
## Add a name/code pair to the RPC server.
server.router.register(name, rpc)
proc unRegisterAll*(server: RpcServer) =
# Remove all remote procedure calls from this server.
server.router.clear