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
|
2019-09-01 17:31:24 +00:00
|
|
|
import peerinfo, multiaddress, stream/lpstream, peerinfo, varint, vbuffer
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-23 22:16:46 +00:00
|
|
|
const DefaultReadSize: uint = 64*1024
|
2019-08-20 16:18:15 +00:00
|
|
|
|
|
|
|
type
|
2019-08-27 21:45:21 +00:00
|
|
|
Connection* = ref object of LPStream
|
2019-08-22 21:35:47 +00:00
|
|
|
peerInfo*: PeerInfo
|
2019-08-30 05:15:43 +00:00
|
|
|
stream*: LPStream
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-08-27 21:45:21 +00:00
|
|
|
proc newConnection*(stream: LPStream): Connection =
|
2019-08-22 21:35:47 +00:00
|
|
|
## 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
|
|
|
|
|
2019-08-30 21:55:07 +00:00
|
|
|
method read*(s: Connection, n = -1): Future[seq[byte]] {.gcsafe.} =
|
|
|
|
result = s.stream.read(n)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method readExactly*(s: Connection,
|
|
|
|
pbytes: pointer,
|
2019-08-30 21:55:07 +00:00
|
|
|
nbytes: int): Future[void] {.gcsafe.} =
|
|
|
|
result = s.stream.readExactly(pbytes, nbytes)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method readLine*(s: Connection,
|
|
|
|
limit = 0,
|
2019-08-30 21:55:07 +00:00
|
|
|
sep = "\r\n"): Future[string] {.gcsafe.} =
|
|
|
|
result = s.stream.readLine(limit, sep)
|
2019-08-21 19:15:51 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method readOnce*(s: Connection,
|
|
|
|
pbytes: pointer,
|
2019-08-30 21:55:07 +00:00
|
|
|
nbytes: int): Future[int] {.gcsafe.} =
|
|
|
|
result = s.stream.readOnce(pbytes, nbytes)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method readUntil*(s: Connection,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int,
|
2019-08-30 21:55:07 +00:00
|
|
|
sep: seq[byte]): Future[int] {.gcsafe.} =
|
|
|
|
result = s.stream.readUntil(pbytes, nbytes, sep)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-30 21:55:07 +00:00
|
|
|
method write*(s: Connection, pbytes: pointer, nbytes: int): Future[void] {.gcsafe.} =
|
|
|
|
result = s.stream.write(pbytes, nbytes)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-30 21:55:07 +00:00
|
|
|
method write*(s: Connection, msg: string, msglen = -1): Future[void] {.gcsafe.} =
|
|
|
|
result = s.stream.write(msg, msglen)
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2019-08-30 21:55:07 +00:00
|
|
|
method write*(s: Connection, msg: seq[byte], msglen = -1): Future[void] {.gcsafe.} =
|
|
|
|
result = s.stream.write(msg, msglen)
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method close*(s: Connection) {.async, gcsafe.} =
|
2019-08-24 19:15:05 +00:00
|
|
|
await s.stream.close()
|
|
|
|
s.closed = true
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
proc readLp*(s: Connection): Future[seq[byte]] {.async, gcsafe.} =
|
2019-08-23 22:16:46 +00:00
|
|
|
## read lenght prefixed msg
|
|
|
|
var
|
|
|
|
size: uint
|
|
|
|
length: int
|
|
|
|
res: VarintStatus
|
|
|
|
var buffer = newSeq[byte](10)
|
|
|
|
try:
|
|
|
|
for i in 0..<len(buffer):
|
|
|
|
await s.readExactly(addr buffer[i], 1)
|
|
|
|
res = LP.getUVarint(buffer.toOpenArray(0, i), length, size)
|
|
|
|
if res == VarintStatus.Success:
|
|
|
|
break
|
|
|
|
if res != VarintStatus.Success or size > DefaultReadSize:
|
|
|
|
buffer.setLen(0)
|
|
|
|
buffer.setLen(size)
|
|
|
|
await s.readExactly(addr buffer[0], int(size))
|
|
|
|
except TransportIncompleteError:
|
|
|
|
buffer.setLen(0)
|
|
|
|
|
|
|
|
result = buffer
|
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
proc writeLp*(s: Connection, msg: string | seq[byte]) {.async, gcsafe.} =
|
2019-08-23 22:16:46 +00:00
|
|
|
## write lenght prefixed
|
|
|
|
var buf = initVBuffer()
|
|
|
|
buf.writeSeq(msg)
|
|
|
|
buf.finish()
|
|
|
|
result = s.write(buf.buffer)
|
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method getPeerInfo*(c: Connection): Future[PeerInfo] {.base, async, gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## get up to date peer info
|
|
|
|
## TODO: implement PeerInfo refresh over identify
|
|
|
|
discard
|
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method getObservedAddrs*(c: Connection): Future[MultiAddress] {.base, async, gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## get resolved multiaddresses for the connection
|
|
|
|
discard
|