nim-libp2p/libp2p/transports/transport.nim

106 lines
2.7 KiB
Nim
Raw Normal View History

2022-07-01 18:19:57 +00:00
# Nim-LibP2P
# Copyright (c) 2023-2024 Status Research & Development GmbH
2022-07-01 18:19:57 +00:00
# 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.
##
2023-06-07 11:12:49 +00:00
{.push raises: [].}
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,
2023-03-08 11:30:19 +00:00
../muxers/muxer,
../upgrademngrs/upgrade,
../protocols/connectivity/autonat/core
export core.NetworkReachability
2020-12-01 17:34:27 +00:00
logScope:
topics = "libp2p transport"
type
TransportError* = object of LPError
TransportInvalidAddrError* = object of TransportError
TransportClosedError* = object of TransportError
Transport* = ref object of RootObj
2021-12-16 10:05:20 +00:00
addrs*: seq[MultiAddress]
running*: bool
upgrader*: Upgrade
networkReachability*: NetworkReachability
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 start*(
self: Transport,
addrs: seq[MultiAddress]) {.base, async.} =
## start the transport
##
trace "starting transport on addrs", address = $addrs
self.addrs = addrs
self.running = true
method stop*(self: Transport) {.base, async.} =
2019-08-21 22:53:16 +00:00
## stop and cleanup the transport
## including all outstanding connections
##
trace "stopping transport", address = $self.addrs
self.running = false
method accept*(self: Transport): Future[Connection]
{.base, gcsafe.} =
## accept incoming connections
##
doAssert(false, "Not implemented!")
method dial*(
self: Transport,
hostname: string,
address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.base, gcsafe.} =
## dial a peer
##
doAssert(false, "Not implemented!")
proc dial*(
self: Transport,
address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.gcsafe.} =
self.dial("", address)
method upgrade*(
self: Transport,
conn: Connection,
peerId: Opt[PeerId]
): Future[Muxer] {.base, async: (raises: [
CancelledError, LPError], raw: true).} =
## base upgrade method that the transport uses to perform
## transport specific upgrades
##
self.upgrader.upgrade(conn, peerId)
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
let protocols = address.protocols.valueOr: return false
protocols
.filterIt(
it == multiCodec("p2p-circuit")
).len == 0