nim-libp2p/libp2p/connection.nim

121 lines
3.5 KiB
Nim
Raw Normal View History

## Nim-LibP2P
2019-09-24 11:48:23 -06:00
## Copyright (c) 2019 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-09-09 17:08:30 -06:00
import chronos, options, chronicles
import peerinfo,
multiaddress,
stream/lpstream,
peerinfo,
varint,
vbuffer
2019-09-03 14:39:42 -06:00
const DefaultReadSize*: uint = 64 * 1024
type
2019-08-27 15:45:21 -06:00
Connection* = ref object of LPStream
2019-09-11 13:06:22 -06:00
peerInfo*: PeerInfo
2019-08-29 23:15:43 -06:00
stream*: LPStream
2019-09-12 11:07:34 -06:00
observedAddrs*: Multiaddress
InvalidVarintException = object of LPStreamError
proc newInvalidVarintException*(): ref InvalidVarintException =
result = newException(InvalidVarintException, "unable to prase varint")
2019-08-27 15:45:21 -06:00
proc newConnection*(stream: LPStream): Connection =
## create a new Connection for the specified async reader/writer
new result
result.stream = stream
2019-09-04 15:21:12 -06:00
2019-08-30 15:55:07 -06:00
method read*(s: Connection, n = -1): Future[seq[byte]] {.gcsafe.} =
result = s.stream.read(n)
2019-08-26 09:37:15 -06:00
method readExactly*(s: Connection,
pbytes: pointer,
2019-09-02 14:45:00 -06:00
nbytes: int):
Future[void] {.gcsafe.} =
2019-09-25 13:34:01 -06:00
s.stream.readExactly(pbytes, nbytes)
2019-08-26 09:37:15 -06:00
method readLine*(s: Connection,
limit = 0,
2019-09-02 14:45:00 -06:00
sep = "\r\n"):
Future[string] {.gcsafe.} =
2019-09-25 13:34:01 -06:00
s.stream.readLine(limit, sep)
2019-08-21 13:15:51 -06:00
2019-08-26 09:37:15 -06:00
method readOnce*(s: Connection,
pbytes: pointer,
2019-09-02 14:45:00 -06:00
nbytes: int):
Future[int] {.gcsafe.} =
2019-09-25 13:34:01 -06:00
s.stream.readOnce(pbytes, nbytes)
2019-08-26 09:37:15 -06:00
method readUntil*(s: Connection,
pbytes: pointer,
nbytes: int,
2019-09-02 14:45:00 -06:00
sep: seq[byte]):
Future[int] {.gcsafe.} =
2019-09-25 13:34:01 -06:00
s.stream.readUntil(pbytes, nbytes, sep)
2019-09-02 14:45:00 -06:00
method write*(s: Connection,
pbytes: pointer,
nbytes: int):
Future[void] {.gcsafe.} =
2019-09-25 13:34:01 -06:00
s.stream.write(pbytes, nbytes)
2019-09-02 14:45:00 -06:00
method write*(s: Connection,
msg: string,
msglen = -1):
Future[void] {.gcsafe.} =
2019-09-25 13:34:01 -06:00
s.stream.write(msg, msglen)
2019-08-21 16:53:16 -06:00
2019-09-02 14:45:00 -06:00
method write*(s: Connection,
msg: seq[byte],
msglen = -1):
Future[void] {.gcsafe.} =
2019-09-25 13:34:01 -06:00
s.stream.write(msg, msglen)
2019-08-21 16:53:16 -06:00
2019-08-30 09:28:07 -06:00
method close*(s: Connection) {.async, gcsafe.} =
await s.stream.close()
s.closed = true
2019-08-30 09:28:07 -06:00
proc readLp*(s: Connection): Future[seq[byte]] {.async, gcsafe.} =
## read lenght prefixed msg
var
size: uint
length: int
res: VarintStatus
2019-09-25 14:13:22 -06:00
result = newSeq[byte](10)
try:
2019-09-25 14:13:22 -06:00
for i in 0..<len(result):
await s.readExactly(addr result[i], 1)
res = LP.getUVarint(result.toOpenArray(0, i), length, size)
if res == VarintStatus.Success:
break
if res != VarintStatus.Success or size > DefaultReadSize:
raise newInvalidVarintException()
2019-09-25 14:13:22 -06:00
result.setLen(size)
if size > 0.uint:
2019-09-25 14:13:22 -06:00
await s.readExactly(addr result[0], int(size))
2019-09-09 17:08:30 -06:00
except LPStreamIncompleteError, LPStreamReadError:
error "readLp: could not read from remote", exception = getCurrentExceptionMsg()
2019-09-08 01:43:33 -06:00
proc writeLp*(s: Connection, msg: string | seq[byte]): Future[void] {.gcsafe.} =
## write lenght prefixed
var buf = initVBuffer()
buf.writeSeq(msg)
buf.finish()
result = s.write(buf.buffer)
2019-08-30 09:28:07 -06:00
method getObservedAddrs*(c: Connection): Future[MultiAddress] {.base, async, gcsafe.} =
## get resolved multiaddresses for the connection
2019-09-12 11:07:34 -06:00
result = c.observedAddrs
2019-09-09 17:08:30 -06:00
proc `$`*(conn: Connection): string =
2019-09-11 13:06:22 -06:00
if conn.peerInfo.peerId.isSome:
result = $(conn.peerInfo.peerId.get())