diff --git a/libp2p/builders.nim b/libp2p/builders.nim index 8e5f722..203b633 100644 --- a/libp2p/builders.nim +++ b/libp2p/builders.nim @@ -14,7 +14,8 @@ import switch, peerid, peerinfo, stream/connection, multiaddress, crypto/crypto, transports/[transport, tcptransport], muxers/[muxer, mplex/mplex], - protocols/[identify, secure/secure, secure/noise] + protocols/[identify, secure/secure, secure/noise], + connmanager, upgrademngrs/muxedupgrade export switch, peerid, peerinfo, connection, multiaddress, crypto @@ -39,6 +40,8 @@ type mplexOpts: MplexOpts tcpTransportOpts: TcpTransportOpts rng: ref BrHmacDrbgContext + inTimeout: Duration + outTimeout: Duration maxConnections: int maxIn: int maxOut: int @@ -154,12 +157,15 @@ proc build*(b: SwitchBuilder): Switch let identify = newIdentify(peerInfo) + connManager = ConnManager.init(b.maxConnsPerPeer, b.maxConnections, b.maxIn, b.maxOut) + ms = newMultistream() + muxedUpgrade = MuxedUpgrade.init(identify, muxers, secureManagerInstances, connManager, ms) let transports = block: var transports: seq[Transport] if b.tcpTransportOpts.enable: - transports.add(Transport(TcpTransport.init(b.tcpTransportOpts.flags))) + transports.add(Transport(TcpTransport.init(b.tcpTransportOpts.flags, muxedUpgrade))) transports if b.secureManagers.len == 0: @@ -174,10 +180,8 @@ proc build*(b: SwitchBuilder): Switch identity = identify, muxers = muxers, secureManagers = secureManagerInstances, - maxConnections = b.maxConnections, - maxIn = b.maxIn, - maxOut = b.maxOut, - maxConnsPerPeer = b.maxConnsPerPeer) + connManager = connManager, + ms = ms) return switch diff --git a/libp2p/standard_setup.nim b/libp2p/standard_setup.nim deleted file mode 100644 index b3a2d3c..0000000 --- a/libp2p/standard_setup.nim +++ /dev/null @@ -1,71 +0,0 @@ -import - options, tables, chronos, bearssl, - switch, peerid, peerinfo, stream/connection, multiaddress, - crypto/crypto, transports/[transport, tcptransport], - muxers/[muxer, mplex/mplex], - protocols/[identify, secure/secure, secure/noise], - upgrademngrs/[upgrade, muxedupgrade], connmanager - -export - switch, peerid, peerinfo, connection, multiaddress, crypto - -type - SecureProtocol* {.pure.} = enum - Noise, - Secio {.deprecated.} - -proc newStandardSwitch*(privKey = none(PrivateKey), - address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(), - secureManagers: openarray[SecureProtocol] = [ - SecureProtocol.Noise, - ], - transportFlags: set[ServerFlags] = {}, - rng = newRng(), - inTimeout: Duration = 5.minutes, - outTimeout: Duration = 5.minutes, - maxConnections = MaxConnections, - maxIn = -1, - maxOut = -1, - maxConnsPerPeer = MaxConnectionsPerPeer): Switch = - proc createMplex(conn: Connection): Muxer = - Mplex.init( - conn, - inTimeout = inTimeout, - outTimeout = outTimeout) - - if rng == nil: # newRng could fail - raise (ref CatchableError)(msg: "Cannot initialize RNG") - - let - seckey = privKey.get(otherwise = PrivateKey.random(rng[]).tryGet()) - peerInfo = PeerInfo.init(seckey, [address]) - - var - secureManagerInstances: seq[Secure] - - for sec in secureManagers: - case sec - of SecureProtocol.Noise: - secureManagerInstances &= newNoise(rng, seckey).Secure - of SecureProtocol.Secio: - quit("Secio is deprecated!") # use of secio is unsafe - - let - mplexProvider = newMuxerProvider(createMplex, MplexCodec) - ms = newMultistream() - identify = newIdentify(peerInfo) - muxers = {MplexCodec: mplexProvider}.toTable - connManager = ConnManager.init(maxConnsPerPeer, maxConnections, maxIn, maxOut) - muxedUpgrade = MuxedUpgrade.init(identify, muxers, secureManagerInstances, connManager, ms) - transports = @[Transport(TcpTransport.init(transportFlags, muxedUpgrade))] - - let switch = newSwitch( - peerInfo, - transports, - identify, - muxers, - secureManagers = secureManagerInstances, - connManager = connManager, - ms = ms) - - return switch