2020-04-14 11:51:26 -06:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2020 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
import chronos
|
|
|
|
import stream,
|
|
|
|
../peerinfo,
|
|
|
|
../multiaddress
|
|
|
|
|
|
|
|
type
|
2020-04-14 17:14:27 -06:00
|
|
|
Connection* = ref object of Stream
|
2020-04-14 11:51:26 -06:00
|
|
|
peerInfo*: PeerInfo
|
|
|
|
observedAddr*: MultiAddress
|
2020-04-14 17:14:27 -06:00
|
|
|
stream*: Stream
|
2020-04-14 11:51:26 -06:00
|
|
|
|
2020-04-14 17:14:27 -06:00
|
|
|
proc init*(C: type[Connection],
|
|
|
|
stream: Stream,
|
|
|
|
peerInfo: PeerInfo = nil): C =
|
2020-04-14 11:51:26 -06:00
|
|
|
Connection(stream: stream, peerInfo: peerInfo)
|
|
|
|
|
|
|
|
proc source*(s: Connection): Source[seq[byte]] =
|
2020-04-14 17:14:27 -06:00
|
|
|
source(s.stream)
|
2020-04-14 11:51:26 -06:00
|
|
|
|
|
|
|
proc sink*(s: Connection): Sink[seq[byte]] =
|
2020-04-14 17:14:27 -06:00
|
|
|
sink(s.stream)
|
2020-04-14 11:51:26 -06:00
|
|
|
|
|
|
|
proc close*(s: Connection) {.async.} =
|
2020-04-14 17:14:27 -06:00
|
|
|
result = close(s.stream)
|
2020-04-14 11:51:26 -06:00
|
|
|
|
|
|
|
proc closed*(s: Connection): bool =
|
|
|
|
s.stream.isClosed
|
|
|
|
|
|
|
|
proc getObservedAddrs*(c: Connection): Future[MultiAddress] {.async.} =
|
|
|
|
## get resolved multiaddresses for the connection
|
|
|
|
result = c.observedAddr
|
|
|
|
|
|
|
|
proc `$`*(c: Connection): string =
|
|
|
|
if not isNil(c.peerInfo):
|
|
|
|
result = $(c.peerInfo)
|