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