nim-libp2p/libp2p/transports/transport.nim

108 lines
2.6 KiB
Nim
Raw Normal View History

## Nim-LibP2P
2019-09-24 17:48:23 +00: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.
##
{.push raises: [Defect].}
import sequtils
2019-09-28 19:56:13 +00:00
import chronos, chronicles
import ../stream/connection,
2019-09-23 22:35:23 +00:00
../multiaddress,
../multicodec,
../upgrademngrs/upgrade
2020-12-01 17:34:27 +00:00
logScope:
topics = "libp2p transport"
type
TransportError* = object of LPError
TransportClosedError* = object of TransportError
Transport* = ref object of RootObj
ma*: Multiaddress
running*: bool
upgrader*: Upgrade
multicodec*: MultiCodec
proc newTransportClosedError*(parent: ref Exception = nil): ref LPError =
newException(TransportClosedError,
"Transport closed, no more connections!", parent)
2020-05-08 16:08:38 +00:00
method initTransport*(self: Transport) {.base, gcsafe, locks: "unknown".} =
## perform protocol initialization
##
discard
method start*(
self: Transport,
ma: MultiAddress): Future[void] {.base, async.} =
## start the transport
##
self.ma = ma
trace "starting transport", address = $ma
method stop*(self: Transport): Future[void] {.base, async.} =
2019-08-21 22:53:16 +00:00
## stop and cleanup the transport
## including all outstanding connections
##
discard
method accept*(self: Transport): Future[Connection]
{.base, gcsafe.} =
## accept incoming connections
##
discard
method dial*(
self: Transport,
address: MultiAddress): Future[Connection] {.base, gcsafe.} =
## dial a peer
##
discard
method upgradeIncoming*(
self: Transport,
conn: Connection): Future[void] {.base, gcsafe.} =
2019-08-30 22:16:37 +00:00
## base upgrade method that the transport uses to perform
## transport specific upgrades
##
doAssert(false, "Not implemented!")
method upgradeOutgoing*(
self: Transport,
conn: Connection): Future[Connection] {.base, gcsafe.} =
## base upgrade method that the transport uses to perform
## transport specific upgrades
##
doAssert(false, "Not implemented!")
2019-08-30 22:16:37 +00:00
method handles*(
self: Transport,
address: MultiAddress): bool {.base, gcsafe.} =
## check if transport supports the multiaddress
##
# by default we skip circuit addresses to avoid
# having to repeat the check in every transport
if address.protocols.isOk:
return address.protocols.get().filterIt( it == multiCodec("p2p-circuit") ).len == 0
2019-09-12 17:07:34 +00:00
method localAddress*(self: Transport): MultiAddress {.base, gcsafe.} =
2019-09-12 17:07:34 +00:00
## get the local address of the transport in case started with 0.0.0.0:0
##
2019-09-12 17:07:34 +00:00
discard