use set[ServerFlags] params instead of hardcoded flags

This commit is contained in:
Ștefan Talpalaru 2020-05-05 15:51:20 +02:00 committed by Dmitriy Ryajov
parent 125843af7d
commit c480e65055
3 changed files with 13 additions and 5 deletions

View File

@ -22,6 +22,8 @@ import connection,
errors, errors,
peer peer
export ServerFlags
logScope: logScope:
topic = "Switch" topic = "Switch"
@ -282,7 +284,7 @@ proc mount*[T: LPProtocol](s: Switch, proto: T) {.gcsafe.} =
s.ms.addHandler(proto.codec, proto) s.ms.addHandler(proto.codec, proto)
proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} = proc start*(s: Switch, serverFlags: set[ServerFlags] = {}): Future[seq[Future[void]]] {.async, gcsafe.} =
trace "starting switch" trace "starting switch"
proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} = proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} =
@ -298,7 +300,7 @@ proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
for t in s.transports: # for each transport for t in s.transports: # for each transport
for i, a in s.peerInfo.addrs: for i, a in s.peerInfo.addrs:
if t.handles(a): # check if it handles the multiaddr if t.handles(a): # check if it handles the multiaddr
var server = await t.listen(a, handle) var server = await t.listen(a, handle, serverFlags)
s.peerInfo.addrs[i] = t.ma # update peer's address s.peerInfo.addrs[i] = t.ma # update peer's address
startFuts.add(server) startFuts.add(server)

View File

@ -16,6 +16,8 @@ import transport,
../multicodec, ../multicodec,
../stream/chronosstream ../stream/chronosstream
export ServerFlags
logScope: logScope:
topic = "TcpTransport" topic = "TcpTransport"
@ -122,12 +124,13 @@ method close*(t: TcpTransport) {.async, gcsafe.} =
method listen*(t: TcpTransport, method listen*(t: TcpTransport,
ma: MultiAddress, ma: MultiAddress,
handler: ConnHandler): handler: ConnHandler,
serverFlags: set[ServerFlags] = {}):
Future[Future[void]] {.async, gcsafe.} = Future[Future[void]] {.async, gcsafe.} =
discard await procCall Transport(t).listen(ma, handler) # call base discard await procCall Transport(t).listen(ma, handler) # call base
## listen on the transport ## listen on the transport
t.server = createStreamServer(t.ma, connCb, {ReuseAddr}, t) t.server = createStreamServer(t.ma, connCb, serverFlags, t)
t.server.start() t.server.start()
# always get the resolved address in case we're bound to 0.0.0.0:0 # always get the resolved address in case we're bound to 0.0.0.0:0

View File

@ -14,6 +14,8 @@ import ../connection,
../multicodec, ../multicodec,
../errors ../errors
export ServerFlags
type type
ConnHandler* = proc (conn: Connection): Future[void] {.gcsafe.} ConnHandler* = proc (conn: Connection): Future[void] {.gcsafe.}
@ -39,7 +41,8 @@ method close*(t: Transport) {.base, async, gcsafe.} =
method listen*(t: Transport, method listen*(t: Transport,
ma: MultiAddress, ma: MultiAddress,
handler: ConnHandler): handler: ConnHandler,
serverFlags: set[ServerFlags] = {}):
Future[Future[void]] {.base, async, gcsafe.} = Future[Future[void]] {.base, async, gcsafe.} =
## listen for incoming connections ## listen for incoming connections
t.ma = ma t.ma = ma