2019-08-20 16:18:15 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2018 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.
|
|
|
|
|
2019-08-22 21:35:47 +00:00
|
|
|
import chronos
|
|
|
|
import peerinfo, multiaddress, readerwriter, peerinfo
|
2019-08-20 16:18:15 +00:00
|
|
|
|
|
|
|
const DefaultReadSize = 1024
|
|
|
|
|
|
|
|
type
|
2019-08-22 21:35:47 +00:00
|
|
|
Connection* = ref object of ReadWrite
|
|
|
|
peerInfo*: PeerInfo
|
|
|
|
stream: ReadWrite
|
|
|
|
|
|
|
|
proc newConnection*(stream: ReadWrite): Connection =
|
|
|
|
## create a new Connection for the specified async reader/writer
|
2019-08-20 16:18:15 +00:00
|
|
|
new result
|
2019-08-22 21:35:47 +00:00
|
|
|
result.stream = stream
|
|
|
|
|
|
|
|
method read*(s: Connection, n = -1): Future[seq[byte]] {.async.} =
|
|
|
|
result = await s.stream.read(n)
|
|
|
|
|
|
|
|
method readExactly*(s: Connection, pbytes: pointer, nbytes: int): Future[void] {.async.} =
|
|
|
|
result = s.stream.readExactly(pbytes, nbytes)
|
|
|
|
|
|
|
|
method readLine*(s: Connection, limit = 0, sep = "\r\n"): Future[string] {.async.} =
|
|
|
|
result = await s.stream.readLine(limit, sep)
|
2019-08-21 19:15:51 +00:00
|
|
|
|
2019-08-22 21:35:47 +00:00
|
|
|
method readOnce*(s: Connection, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
|
|
|
result = await s.stream.readOnce(pbytes, nbytes)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-22 21:35:47 +00:00
|
|
|
method readUntil*(s: Connection, pbytes: pointer, nbytes: int, sep: seq[byte]): Future[int] {.async.} =
|
|
|
|
result = await s.stream.readUntil(pbytes, nbytes, sep)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-22 21:35:47 +00:00
|
|
|
method write*(s: Connection, pbytes: pointer, nbytes: int) {.async.} =
|
|
|
|
result = s.stream.write(pbytes, nbytes)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-22 21:35:47 +00:00
|
|
|
method write*(s: Connection, msg: string, msglen = -1) {.async.} =
|
|
|
|
result = s.stream.write(msg, msglen)
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2019-08-23 00:03:29 +00:00
|
|
|
method write*(s: Connection, msg: seq[byte], msglen = -1) {.async.} =
|
2019-08-22 21:35:47 +00:00
|
|
|
result = s.stream.write(msg, msglen)
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2019-08-22 21:35:47 +00:00
|
|
|
method close*(s: Connection) {.async.} =
|
|
|
|
result = s.stream.close()
|
2019-08-20 16:18:15 +00:00
|
|
|
|
|
|
|
method getPeerInfo* (c: Connection): Future[PeerInfo] {.base, async.} =
|
|
|
|
## get up to date peer info
|
|
|
|
## TODO: implement PeerInfo refresh over identify
|
|
|
|
discard
|
|
|
|
|
|
|
|
method getObservedAddrs(c: Connection): Future[seq[MultiAddress]] {.base, async.} =
|
|
|
|
## get resolved multiaddresses for the connection
|
|
|
|
discard
|