46 lines
1.6 KiB
Nim
46 lines
1.6 KiB
Nim
|
## 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.
|
||
|
|
||
|
import chronos, peerinfo, multiaddress
|
||
|
|
||
|
const DefaultReadSize = 1024
|
||
|
|
||
|
type
|
||
|
Connection* = ref object of RootObj
|
||
|
reader: AsyncStreamReader
|
||
|
writter: AsyncStreamWriter
|
||
|
|
||
|
proc newConnection*(reader: AsyncStreamReader, writter: AsyncStreamWriter): Connection =
|
||
|
## create a new Connection for the specified async stream reader/writter
|
||
|
new result
|
||
|
result.reader = reader
|
||
|
result.writter = writter
|
||
|
|
||
|
method read* (c: Connection, size: int = DefaultReadSize): Future[seq[byte]] {.base, async.} =
|
||
|
## read DefaultReadSize (1024) bytes or `size` bytes if specified
|
||
|
result = await c.reader.read(size)
|
||
|
|
||
|
method write* (c: Connection, data: pointer, size: int): Future[void] {.base, async.} =
|
||
|
## write bytes pointed to by `data` up to `size` size
|
||
|
result = c.writter.write(data, size)
|
||
|
|
||
|
method close* (c: Connection): Future[void] {.base, async.} =
|
||
|
## close connection
|
||
|
## TODO: figure out how to correctly close the streams and underlying resource
|
||
|
discard
|
||
|
|
||
|
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
|