with with

This commit is contained in:
Giovanni Petrantoni 2021-03-25 12:50:30 +09:00
parent fb74d00daa
commit 1facb7c613
1 changed files with 87 additions and 83 deletions

View File

@ -15,112 +15,114 @@ type
Secio {.deprecated.} Secio {.deprecated.}
SwitchBuilder* {.byref.} = object SwitchBuilder* {.byref.} = object
bprivKey: Option[PrivateKey] privKey: Option[PrivateKey]
baddress: MultiAddress address: MultiAddress
bsecureManagers: seq[SecureProtocol] secureManagers: seq[SecureProtocol]
btransportFlags: set[ServerFlags] tcpTransportFlags: Option[set[ServerFlags]]
brng: ref BrHmacDrbgContext rng: ref BrHmacDrbgContext
binTimeout: Duration inTimeout: Duration
boutTimeout: Duration outTimeout: Duration
bmaxConnections: int maxConnections: int
bmaxIn: int maxIn: int
bmaxOut: int maxOut: int
bmaxConnsPerPeer: int maxConnsPerPeer: int
bprotoVersion: Option[string] protoVersion: Option[string]
bagentVersion: Option[string] agentVersion: Option[string]
proc init*(_: type[SwitchBuilder]): SwitchBuilder = proc init*(_: type[SwitchBuilder]): SwitchBuilder =
SwitchBuilder( SwitchBuilder(
bprivKey: none(PrivateKey), privKey: none(PrivateKey),
baddress: MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(), address: MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
bsecureManagers: @[], secureManagers: @[],
btransportFlags: {}, tcpTransportFlags: block:
brng: newRng(), let flags: set[ServerFlags] = {}
binTimeout: 5.minutes, some(flags),
boutTimeout: 5.minutes, rng: newRng(),
bmaxConnections: MaxConnections, inTimeout: 5.minutes,
bmaxIn: -1, outTimeout: 5.minutes,
bmaxOut: -1, maxConnections: MaxConnections,
bmaxConnsPerPeer: MaxConnectionsPerPeer, maxIn: -1,
bprotoVersion: none(string), maxOut: -1,
bagentVersion: none(string)) maxConnsPerPeer: MaxConnectionsPerPeer,
protoVersion: none(string),
agentVersion: none(string))
# so I tried using var inpit and return but did not work... # so I tried using var inpit and return but did not work...
# as in proc privateKey*(builder: var SwitchBuilder, privateKey: PrivateKey): var SwitchBuilder = # as in proc privateKey*(builder: var SwitchBuilder, privateKey: PrivateKey): var SwitchBuilder =
# so in nim we are stuck with this hardly efficient way and hopey compiler figures it out.. heh # so in nim we are stuck with this hardly efficient way and hopey compiler figures it out.. heh
# maybe {.byref.} works.... I would not bet on it but let's use it. # maybe {.byref.} works.... I would not bet on it but let's use it.
proc privateKey*(builder: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder = proc withPrivateKey*(builder: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder =
var b = builder var b = builder
b.bprivKey = some(privateKey) b.privKey = some(privateKey)
b b
proc address*(builder: SwitchBuilder, address: MultiAddress): SwitchBuilder = proc withAddress*(builder: SwitchBuilder, address: MultiAddress): SwitchBuilder =
var b = builder var b = builder
b.baddress = address b.address = address
b b
proc secureManager*(builder: SwitchBuilder, secureManager: SecureProtocol): SwitchBuilder = proc withSecureManager*(builder: SwitchBuilder, secureManager: SecureProtocol): SwitchBuilder =
var b = builder var b = builder
b.bsecureManagers &= secureManager b.secureManagers &= secureManager
b b
proc transportFlag*(builder: SwitchBuilder, flag: ServerFlags): SwitchBuilder = proc withTcpTransport*(builder: SwitchBuilder, flags: set[ServerFlags] = {}): SwitchBuilder =
var b = builder var b = builder
b.btransportFlags = b.btransportFlags + {flag} b.tcpTransportFlags = some(flags)
b b
proc rng*(builder: SwitchBuilder, rng: ref BrHmacDrbgContext): SwitchBuilder = proc withRng*(builder: SwitchBuilder, rng: ref BrHmacDrbgContext): SwitchBuilder =
var b = builder var b = builder
b.brng = rng b.rng = rng
b b
proc inTimeout*(builder: SwitchBuilder, inTimeout: Duration): SwitchBuilder = proc withInTimeout*(builder: SwitchBuilder, inTimeout: Duration): SwitchBuilder =
var b = builder var b = builder
b.binTimeout = inTimeout b.inTimeout = inTimeout
b b
proc outTimeout*(builder: SwitchBuilder, outTimeout: Duration): SwitchBuilder = proc withOutTimeout*(builder: SwitchBuilder, outTimeout: Duration): SwitchBuilder =
var b = builder var b = builder
b.boutTimeout = outTimeout b.outTimeout = outTimeout
b b
proc maxConnections*(builder: SwitchBuilder, maxConnections: int): SwitchBuilder = proc withMaxConnections*(builder: SwitchBuilder, maxConnections: int): SwitchBuilder =
var b = builder var b = builder
b.bmaxConnections = maxConnections b.maxConnections = maxConnections
b b
proc maxIn*(builder: SwitchBuilder, maxIn: int): SwitchBuilder = proc withMaxIn*(builder: SwitchBuilder, maxIn: int): SwitchBuilder =
var b = builder var b = builder
b.bmaxIn = maxIn b.maxIn = maxIn
b b
proc maxOut*(builder: SwitchBuilder, maxOut: int): SwitchBuilder = proc withMaxOut*(builder: SwitchBuilder, maxOut: int): SwitchBuilder =
var b = builder var b = builder
b.bmaxOut = maxOut b.maxOut = maxOut
b b
proc maxConnsPerPeer*(builder: SwitchBuilder, maxConnsPerPeer: int): SwitchBuilder = proc withMaxConnsPerPeer*(builder: SwitchBuilder, maxConnsPerPeer: int): SwitchBuilder =
var b = builder var b = builder
b.bmaxConnsPerPeer = maxConnsPerPeer b.maxConnsPerPeer = maxConnsPerPeer
b b
proc protoVersion*(builder: SwitchBuilder, protoVersion: string): SwitchBuilder = proc withProtoVersion*(builder: SwitchBuilder, protoVersion: string): SwitchBuilder =
var b = builder var b = builder
b.bprotoVersion = some(protoVersion) b.protoVersion = some(protoVersion)
b b
proc agentVersion*(builder: SwitchBuilder, agentVersion: string): SwitchBuilder = proc withAgentVersion*(builder: SwitchBuilder, agentVersion: string): SwitchBuilder =
var b = builder var b = builder
b.bagentVersion = some(agentVersion) b.agentVersion = some(agentVersion)
b b
proc build*(builder: SwitchBuilder): Switch = proc build*(builder: SwitchBuilder): Switch =
var b = builder var b = builder
let let
inTimeout = b.binTimeout inTimeout = b.inTimeout
outTimeout = b.boutTimeout outTimeout = b.outTimeout
proc createMplex(conn: Connection): Muxer = proc createMplex(conn: Connection): Muxer =
Mplex.init( Mplex.init(
@ -128,32 +130,36 @@ proc build*(builder: SwitchBuilder): Switch =
inTimeout = inTimeout, inTimeout = inTimeout,
outTimeout = outTimeout) outTimeout = outTimeout)
if b.brng == nil: # newRng could fail if b.rng == nil: # newRng could fail
raise (ref CatchableError)(msg: "Cannot initialize RNG") raise (ref CatchableError)(msg: "Cannot initialize RNG")
let let
seckey = b.bprivKey.get(otherwise = PrivateKey.random(b.brng[]).tryGet()) seckey = b.privKey.get(otherwise = PrivateKey.random(b.rng[]).tryGet())
peerInfo = block: peerInfo = block:
let info = PeerInfo.init(seckey, [b.baddress]) let info = PeerInfo.init(seckey, [b.address])
if b.bprotoVersion.isSome(): if b.protoVersion.isSome():
info.protoVersion = b.bprotoVersion.get() info.protoVersion = b.protoVersion.get()
if b.bagentVersion.isSome(): if b.agentVersion.isSome():
info.agentVersion = b.bagentVersion.get() info.agentVersion = b.agentVersion.get()
info info
mplexProvider = newMuxerProvider(createMplex, MplexCodec) mplexProvider = newMuxerProvider(createMplex, MplexCodec)
transports = @[Transport(TcpTransport.init(b.btransportFlags))] transports = block:
var transports: seq[Transport]
if b.tcpTransportFlags.isSome():
transports &= Transport(TcpTransport.init(b.tcpTransportFlags.get()))
transports
muxers = {MplexCodec: mplexProvider}.toTable muxers = {MplexCodec: mplexProvider}.toTable
identify = newIdentify(peerInfo) identify = newIdentify(peerInfo)
if b.bsecureManagers.len == 0: if b.secureManagers.len == 0:
b.bsecureManagers &= SecureProtocol.Noise b.secureManagers &= SecureProtocol.Noise
var var
secureManagerInstances: seq[Secure] secureManagerInstances: seq[Secure]
for sec in b.bsecureManagers: for sec in b.secureManagers:
case sec case sec
of SecureProtocol.Noise: of SecureProtocol.Noise:
secureManagerInstances &= newNoise(b.brng, seckey).Secure secureManagerInstances &= newNoise(b.rng, seckey).Secure
of SecureProtocol.Secio: of SecureProtocol.Secio:
quit("Secio is deprecated!") # use of secio is unsafe quit("Secio is deprecated!") # use of secio is unsafe
@ -163,10 +169,10 @@ proc build*(builder: SwitchBuilder): Switch =
identify, identify,
muxers, muxers,
secureManagers = secureManagerInstances, secureManagers = secureManagerInstances,
maxConnections = b.bmaxConnections, maxConnections = b.maxConnections,
maxIn = b.bmaxIn, maxIn = b.maxIn,
maxOut = b.bmaxOut, maxOut = b.maxOut,
maxConnsPerPeer = b.bmaxConnsPerPeer) maxConnsPerPeer = b.maxConnsPerPeer)
return switch return switch
@ -185,22 +191,20 @@ proc newStandardSwitch*(privKey = none(PrivateKey),
maxConnsPerPeer = MaxConnectionsPerPeer): Switch = maxConnsPerPeer = MaxConnectionsPerPeer): Switch =
var b = SwitchBuilder var b = SwitchBuilder
.init() .init()
.address(address) .withAddress(address)
.rng(rng) .withRng(rng)
.inTimeout(inTimeout) .withInTimeout(inTimeout)
.outTimeout(outTimeout) .withOutTimeout(outTimeout)
.maxConnections(maxConnections) .withMaxConnections(maxConnections)
.maxIn(maxIn) .withMaxIn(maxIn)
.maxOut(maxOut) .withMaxOut(maxOut)
.maxConnsPerPeer(maxConnsPerPeer) .withMaxConnsPerPeer(maxConnsPerPeer)
.withTcpTransport(transportFlags)
if privKey.isSome(): if privKey.isSome():
b = b.privateKey(privKey.get()) b = b.withPrivateKey(privKey.get())
for sm in secureManagers: for sm in secureManagers:
b = b.secureManager(sm) b = b.withSecureManager(sm)
for flag in transportFlags:
b = b.transportFlag(flag)
b.build() b.build()