mirror of https://github.com/vacp2p/nim-libp2p.git
emulate `defered`
This commit is contained in:
parent
abf659a01a
commit
1b4876d26d
|
@ -66,6 +66,7 @@ proc newStreamInternal*(m: Mplex,
|
|||
|
||||
method handle*(m: Mplex) {.async, gcsafe.} =
|
||||
trace "starting mplex main loop", oid = m.oid
|
||||
try:
|
||||
try:
|
||||
while not m.connection.closed:
|
||||
trace "waiting for data", oid = m.oid
|
||||
|
@ -157,11 +158,11 @@ method handle*(m: Mplex) {.async, gcsafe.} =
|
|||
channoid = channel.oid,
|
||||
oid = m.oid
|
||||
break
|
||||
except CatchableError as exc:
|
||||
trace "Exception occurred", exception = exc.msg, oid = m.oid
|
||||
finally:
|
||||
trace "stopping mplex main loop", oid = m.oid
|
||||
await m.close()
|
||||
except CatchableError as exc:
|
||||
trace "Exception occurred", exception = exc.msg, oid = m.oid
|
||||
|
||||
proc newMplex*(conn: Connection,
|
||||
maxChanns: uint = MaxChannels): Mplex =
|
||||
|
@ -199,26 +200,16 @@ method close*(m: Mplex) {.async, gcsafe.} =
|
|||
|
||||
try:
|
||||
trace "closing mplex muxer", oid = m.oid
|
||||
let channs = toSeq(m.remote.values) &
|
||||
toSeq(m.local.values)
|
||||
await all(
|
||||
toSeq(m.remote.values).mapIt(it.reset()) &
|
||||
toSeq(m.local.values).mapIt(it.reset()))
|
||||
|
||||
for chann in channs:
|
||||
try:
|
||||
await chann.reset()
|
||||
await all(m.conns.mapIt(it.close())) # dispose of channel's connections
|
||||
await all(m.handlerFuts)
|
||||
except CatchableError as exc:
|
||||
warn "error resetting channel", exc = exc.msg
|
||||
|
||||
for conn in m.conns:
|
||||
try:
|
||||
await conn.close()
|
||||
except CatchableError as exc:
|
||||
warn "error closing channel's connection"
|
||||
|
||||
checkFutures(
|
||||
await allFinished(m.handlerFuts))
|
||||
|
||||
await m.connection.close()
|
||||
trace "exception in mplex close", exc = exc.msg
|
||||
finally:
|
||||
await m.connection.close()
|
||||
m.remote.clear()
|
||||
m.local.clear()
|
||||
m.conns = @[]
|
||||
|
|
|
@ -60,7 +60,7 @@ method init(c: MuxerProvider) =
|
|||
if not isNil(c.muxerHandler):
|
||||
futs &= c.muxerHandler(muxer)
|
||||
|
||||
checkFutures(await allFinished(futs))
|
||||
await all(futs)
|
||||
except CatchableError as exc:
|
||||
trace "exception in muxer handler", exc = exc.msg
|
||||
|
||||
|
|
|
@ -65,6 +65,7 @@ proc sendObservers(p: PubSubPeer, msg: var RPCMsg) =
|
|||
|
||||
proc handle*(p: PubSubPeer, conn: Connection) {.async.} =
|
||||
trace "handling pubsub rpc", peer = p.id, closed = conn.closed
|
||||
try:
|
||||
try:
|
||||
while not conn.closed:
|
||||
trace "waiting for data", peer = p.id, closed = conn.closed
|
||||
|
@ -77,18 +78,18 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} =
|
|||
|
||||
var msg = decodeRpcMsg(data)
|
||||
trace "decoded msg from peer", peer = p.id, msg = msg.shortLog
|
||||
|
||||
p.recvObservers(msg) # hooks can modify the message
|
||||
|
||||
# trigger hooks
|
||||
for obs in p.observers[]:
|
||||
obs.onRecv(p, msg)
|
||||
await p.handler(p, @[msg])
|
||||
p.recvdRpcCache.put(digest)
|
||||
except CatchableError as exc:
|
||||
trace "Exception occurred in PubSubPeer.handle", exc = exc.msg
|
||||
finally:
|
||||
trace "exiting pubsub peer read loop", peer = p.id
|
||||
if not conn.closed():
|
||||
await conn.close()
|
||||
|
||||
except CatchableError as exc:
|
||||
trace "Exception occurred in PubSubPeer.handle", exc = exc.msg
|
||||
|
||||
proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} =
|
||||
for m in msgs.items:
|
||||
trace "sending msgs to peer", toPeer = p.id, msgs = $msgs
|
||||
|
|
Loading…
Reference in New Issue