Renamed newStreamClient to newSocketClient, added a check for nil transport

This commit is contained in:
coffeepots 2018-07-12 14:06:35 +01:00
parent e3a7ffd121
commit 8d3473e75b
1 changed files with 6 additions and 3 deletions

View File

@ -30,6 +30,9 @@ proc call*(self: RpcClient, name: string,
"method": %name, "method": %name,
"params": params, "params": params,
"id": %id} & "\c\l" "id": %id} & "\c\l"
if self.transport.isNil:
var connectStr = ""
raise newException(ValueError, "Transport is not initialised (missing a call to connect?)")
let res = await self.transport.write(value) let res = await self.transport.write(value)
# TODO: Add actions when not full packet was send, e.g. disconnect peer. # TODO: Add actions when not full packet was send, e.g. disconnect peer.
assert(res == len(value)) assert(res == len(value))
@ -100,15 +103,15 @@ proc processData(client: RpcClient) {.async.} =
# async loop reconnection and waiting # async loop reconnection and waiting
client.transport = await connect(client.address) client.transport = await connect(client.address)
type RpcStreamClient* = RpcClient[StreamTransport, TransportAddress] type RpcSocketClient* = RpcClient[StreamTransport, TransportAddress]
proc connect*(client: RpcStreamClient, address: string, port: Port) {.async.} = proc connect*(client: RpcSocketClient, address: string, port: Port) {.async.} =
let addresses = resolveTAddress(address, port) let addresses = resolveTAddress(address, port)
client.transport = await connect(addresses[0]) client.transport = await connect(addresses[0])
client.address = addresses[0] client.address = addresses[0]
asyncCheck processData(client) asyncCheck processData(client)
proc newRpcStreamClient*(): RpcStreamClient = proc newRpcSocketClient*(): RpcSocketClient =
## Create new server and assign it to addresses ``addresses``. ## Create new server and assign it to addresses ``addresses``.
result = newRpcClient[StreamTransport, TransportAddress]() result = newRpcClient[StreamTransport, TransportAddress]()