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-05-08 16:08:38 +00:00
|
|
|
import sequtils, tables
|
2019-09-28 19:56:13 +00:00
|
|
|
import chronos, chronicles
|
2019-12-04 04:44:54 +00:00
|
|
|
import ../connection,
|
2019-09-23 22:35:23 +00:00
|
|
|
../multiaddress,
|
2020-04-11 04:08:25 +00:00
|
|
|
../multicodec,
|
|
|
|
../errors
|
2019-08-20 16:18:15 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ConnHandler* = proc (conn: Connection): Future[void] {.gcsafe.}
|
|
|
|
|
2020-05-08 16:08:38 +00:00
|
|
|
TransportFlag* {.pure.} = enum
|
|
|
|
ReuseAddr
|
|
|
|
|
|
|
|
TransportFlags* = set[TransportFlag]
|
|
|
|
|
2019-08-20 16:18:15 +00:00
|
|
|
Transport* = ref object of RootObj
|
|
|
|
ma*: Multiaddress
|
2020-04-06 21:33:44 +00:00
|
|
|
connections*: seq[Connection]
|
2019-08-20 16:18:15 +00:00
|
|
|
handler*: ConnHandler
|
|
|
|
multicodec*: MultiCodec
|
2020-05-08 16:08:38 +00:00
|
|
|
flags*: TransportFlags
|
|
|
|
|
|
|
|
proc transportFlagsToServerFlags*(flags: TransportFlags): set[ServerFlags] {.gcsafe.} =
|
|
|
|
let transportFlagToServerFlagMapping = {
|
|
|
|
TransportFlag.ReuseAddr: ServerFlags.ReuseAddr,
|
|
|
|
}.toTable()
|
|
|
|
|
|
|
|
for flag in flags:
|
|
|
|
result.incl(transportFlagToServerFlagMapping[flag])
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method init*(t: Transport) {.base, gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## perform protocol initialization
|
|
|
|
discard
|
|
|
|
|
2020-05-08 16:08:38 +00:00
|
|
|
proc newTransport*(t: typedesc[Transport], flags: TransportFlags = {}): t {.gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
new result
|
2020-05-08 16:08:38 +00:00
|
|
|
result.flags = flags
|
2019-08-20 16:18:15 +00:00
|
|
|
result.init()
|
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method close*(t: Transport) {.base, async, gcsafe.} =
|
2019-08-21 22:53:16 +00:00
|
|
|
## stop and cleanup the transport
|
|
|
|
## including all outstanding connections
|
2020-04-11 04:08:25 +00:00
|
|
|
let futs = await allFinished(t.connections.mapIt(it.close()))
|
|
|
|
checkFutures(futs)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method listen*(t: Transport,
|
|
|
|
ma: MultiAddress,
|
2020-05-05 15:55:02 +00:00
|
|
|
handler: ConnHandler):
|
2019-09-12 00:15:04 +00:00
|
|
|
Future[Future[void]] {.base, async, gcsafe.} =
|
2019-08-21 22:53:16 +00:00
|
|
|
## listen for incoming connections
|
2019-08-21 23:13:20 +00:00
|
|
|
t.ma = ma
|
|
|
|
t.handler = handler
|
2019-11-06 18:25:33 +00:00
|
|
|
trace "starting node", address = $ma
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method dial*(t: Transport,
|
2019-09-23 22:35:23 +00:00
|
|
|
address: MultiAddress):
|
2019-08-30 15:28:07 +00:00
|
|
|
Future[Connection] {.base, async, gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## dial a peer
|
|
|
|
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
|
|
|
|
discard
|
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method handles*(t: Transport, address: MultiAddress): bool {.base, gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## check if transport supportes 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
|
|
|
|
address.protocols.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
|
|
|
|
discard
|