diff --git a/libp2p/standard_setup.nim b/libp2p/standard_setup.nim index 9df26f0..cd0328b 100644 --- a/libp2p/standard_setup.nim +++ b/libp2p/standard_setup.nim @@ -6,7 +6,6 @@ const import options, tables, - chronos, switch, peer, peerinfo, connection, multiaddress, crypto/crypto, transports/[transport, tcptransport], muxers/[muxer, mplex/mplex, mplex/types], @@ -19,7 +18,7 @@ else: import protocols/secure/secio export - switch, peer, peerinfo, connection, multiaddress, crypto, ServerFlags + switch, peer, peerinfo, connection, multiaddress, crypto proc newStandardSwitch*(privKey = none(PrivateKey), address = MultiAddress.init("/ip4/127.0.0.1/tcp/0"), @@ -27,7 +26,7 @@ proc newStandardSwitch*(privKey = none(PrivateKey), gossip = false, verifySignature = libp2p_pubsub_verify, sign = libp2p_pubsub_sign, - serverFlags: set[ServerFlags] = {}): Switch = + transportFlags: TransportFlags = {}): Switch = proc createMplex(conn: Connection): Muxer = result = newMplex(conn) @@ -35,7 +34,7 @@ proc newStandardSwitch*(privKey = none(PrivateKey), seckey = privKey.get(otherwise = PrivateKey.random(ECDSA)) peerInfo = PeerInfo.init(seckey, [address]) mplexProvider = newMuxerProvider(createMplex, MplexCodec) - transports = @[Transport(newTransport(TcpTransport, serverFlags))] + transports = @[Transport(newTransport(TcpTransport, transportFlags))] muxers = {MplexCodec: mplexProvider}.toTable identify = newIdentify(peerInfo) when libp2p_secure == "noise": diff --git a/libp2p/transports/tcptransport.nim b/libp2p/transports/tcptransport.nim index 7ab5104..a28ef07 100644 --- a/libp2p/transports/tcptransport.nim +++ b/libp2p/transports/tcptransport.nim @@ -127,7 +127,7 @@ method listen*(t: TcpTransport, discard await procCall Transport(t).listen(ma, handler) # call base ## listen on the transport - t.server = createStreamServer(t.ma, connCb, t.serverFlags, t) + t.server = createStreamServer(t.ma, connCb, transportFlagsToServerFlags(t.flags), t) t.server.start() # always get the resolved address in case we're bound to 0.0.0.0:0 diff --git a/libp2p/transports/transport.nim b/libp2p/transports/transport.nim index fd703c8..6919f72 100644 --- a/libp2p/transports/transport.nim +++ b/libp2p/transports/transport.nim @@ -7,32 +7,43 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import sequtils +import sequtils, tables import chronos, chronicles import ../connection, ../multiaddress, ../multicodec, ../errors -export ServerFlags - type ConnHandler* = proc (conn: Connection): Future[void] {.gcsafe.} + TransportFlag* {.pure.} = enum + ReuseAddr + + TransportFlags* = set[TransportFlag] + Transport* = ref object of RootObj ma*: Multiaddress connections*: seq[Connection] handler*: ConnHandler multicodec*: MultiCodec - serverFlags*: set[ServerFlags] + flags*: TransportFlags + +proc transportFlagsToServerFlags*(flags: TransportFlags): set[ServerFlags] {.gcsafe.} = + let transportFlagToServerFlagMapping = { + TransportFlag.ReuseAddr: ServerFlags.ReuseAddr, + }.toTable() + + for flag in flags: + result.incl(transportFlagToServerFlagMapping[flag]) method init*(t: Transport) {.base, gcsafe.} = ## perform protocol initialization discard -proc newTransport*(t: typedesc[Transport], serverFlags: set[ServerFlags] = {}): t {.gcsafe.} = +proc newTransport*(t: typedesc[Transport], flags: TransportFlags = {}): t {.gcsafe.} = new result - result.serverFlags = serverFlags + result.flags = flags result.init() method close*(t: Transport) {.base, async, gcsafe.} =