2019-08-20 16:18:15 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-20 16:18:15 +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.
|
2020-11-01 22:23:26 +00:00
|
|
|
##
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-07-14 00:02:16 +00:00
|
|
|
import sequtils
|
2019-09-28 19:56:13 +00:00
|
|
|
import chronos, chronicles
|
2020-06-19 17:29:43 +00:00
|
|
|
import ../stream/connection,
|
2019-09-23 22:35:23 +00:00
|
|
|
../multiaddress,
|
2020-07-14 00:02:16 +00:00
|
|
|
../multicodec
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2020-12-01 17:34:27 +00:00
|
|
|
logScope:
|
|
|
|
topics = "libp2p transport"
|
|
|
|
|
2019-08-20 16:18:15 +00:00
|
|
|
type
|
2020-11-19 02:06:42 +00:00
|
|
|
TransportClosedError* = object of CatchableError
|
2019-08-20 16:18:15 +00:00
|
|
|
|
|
|
|
Transport* = ref object of RootObj
|
|
|
|
ma*: Multiaddress
|
|
|
|
multicodec*: MultiCodec
|
2020-11-19 02:06:42 +00:00
|
|
|
running*: bool
|
|
|
|
|
|
|
|
proc newTransportClosedError*(parent: ref Exception = nil): ref CatchableError =
|
|
|
|
newException(TransportClosedError,
|
|
|
|
"Transport closed, no more connections!", parent)
|
2020-05-08 16:08:38 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
method initTransport*(t: Transport) {.base, gcsafe, locks: "unknown".} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## perform protocol initialization
|
2020-11-19 02:06:42 +00:00
|
|
|
##
|
|
|
|
|
2019-08-20 16:18:15 +00:00
|
|
|
discard
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
method start*(t: Transport, ma: MultiAddress) {.base, async.} =
|
|
|
|
## start the transport
|
|
|
|
##
|
|
|
|
|
|
|
|
t.ma = ma
|
|
|
|
trace "starting transport", address = $ma
|
|
|
|
|
|
|
|
method stop*(t: Transport) {.base, async.} =
|
2019-08-21 22:53:16 +00:00
|
|
|
## stop and cleanup the transport
|
|
|
|
## including all outstanding connections
|
2020-11-19 02:06:42 +00:00
|
|
|
##
|
|
|
|
|
2020-05-18 17:05:34 +00:00
|
|
|
discard
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
method accept*(t: Transport): Future[Connection]
|
|
|
|
{.base, async, gcsafe.} =
|
|
|
|
## accept incoming connections
|
|
|
|
##
|
|
|
|
|
|
|
|
discard
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method dial*(t: Transport,
|
2020-11-19 02:06:42 +00:00
|
|
|
address: MultiAddress): Future[Connection]
|
|
|
|
{.base, async, gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## dial a peer
|
2020-11-19 02:06:42 +00:00
|
|
|
##
|
|
|
|
|
2019-08-20 16:18:15 +00:00
|
|
|
discard
|
|
|
|
|
2019-09-23 22:35:23 +00:00
|
|
|
method upgrade*(t: Transport) {.base, async, gcsafe.} =
|
2019-08-30 22:16:37 +00:00
|
|
|
## base upgrade method that the transport uses to perform
|
|
|
|
## transport specific upgrades
|
2020-11-19 02:06:42 +00:00
|
|
|
##
|
|
|
|
|
2019-08-30 22:16:37 +00:00
|
|
|
discard
|
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method handles*(t: Transport, address: MultiAddress): bool {.base, gcsafe.} =
|
2020-11-19 02:06:42 +00:00
|
|
|
## check if transport supports the multiaddress
|
|
|
|
##
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
# by default we skip circuit addresses to avoid
|
2019-12-04 04:44:54 +00:00
|
|
|
# having to repeat the check in every transport
|
2021-05-21 16:27:01 +00:00
|
|
|
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*(t: Transport): MultiAddress {.base, gcsafe.} =
|
|
|
|
## get the local address of the transport in case started with 0.0.0.0:0
|
2020-11-19 02:06:42 +00:00
|
|
|
##
|
|
|
|
|
2019-09-12 17:07:34 +00:00
|
|
|
discard
|