2018-03-02 11:46:59 +00:00
|
|
|
import asyncdispatch, asyncnet, json, tables
|
|
|
|
|
|
|
|
type
|
|
|
|
RpcProc* = proc (params: JsonNode): JsonNode
|
|
|
|
|
|
|
|
RpcServer* = ref object
|
|
|
|
socket*: AsyncSocket
|
|
|
|
port*: Port
|
|
|
|
address*: string
|
|
|
|
procs*: TableRef[string, RpcProc]
|
|
|
|
|
|
|
|
RpcProcError* = ref object of Exception
|
|
|
|
code*: int
|
|
|
|
data*: JsonNode
|
|
|
|
|
|
|
|
proc newRpcServer*(address: string, port: Port = Port(8545)): RpcServer =
|
|
|
|
RpcServer(
|
|
|
|
socket: newAsyncSocket(),
|
|
|
|
port: port,
|
|
|
|
address: address,
|
|
|
|
procs: newTable[string, RpcProc]()
|
|
|
|
)
|
|
|
|
|
|
|
|
proc register*(server: RpcServer, name: string, rpc: RpcProc) =
|
2018-03-22 17:29:31 +00:00
|
|
|
server.procs[name] = rpc
|
|
|
|
|
|
|
|
proc unRegisterAll*(server: RpcServer) = server.procs.clear
|