mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-01-12 01:34:10 +00:00
Fix websocket EOF reading exception (#689)
This commit is contained in:
parent
c18830ad33
commit
07da14a7a7
@ -49,11 +49,24 @@ proc new*(T: type WsStream,
|
|||||||
stream.initStream()
|
stream.initStream()
|
||||||
return stream
|
return stream
|
||||||
|
|
||||||
|
template mapExceptions(body: untyped) =
|
||||||
|
try:
|
||||||
|
body
|
||||||
|
except AsyncStreamIncompleteError:
|
||||||
|
raise newLPStreamEOFError()
|
||||||
|
except AsyncStreamUseClosedError:
|
||||||
|
raise newLPStreamEOFError()
|
||||||
|
except WSClosedError:
|
||||||
|
raise newLPStreamEOFError()
|
||||||
|
except AsyncStreamLimitError:
|
||||||
|
raise newLPStreamLimitError()
|
||||||
|
|
||||||
method readOnce*(
|
method readOnce*(
|
||||||
s: WsStream,
|
s: WsStream,
|
||||||
pbytes: pointer,
|
pbytes: pointer,
|
||||||
nbytes: int): Future[int] {.async.} =
|
nbytes: int): Future[int] {.async.} =
|
||||||
let res = await s.session.recv(pbytes, nbytes)
|
let res = mapExceptions(await s.session.recv(pbytes, nbytes))
|
||||||
|
|
||||||
if res == 0 and s.session.readyState == ReadyState.Closed:
|
if res == 0 and s.session.readyState == ReadyState.Closed:
|
||||||
raise newLPStreamEOFError()
|
raise newLPStreamEOFError()
|
||||||
return res
|
return res
|
||||||
@ -61,10 +74,7 @@ method readOnce*(
|
|||||||
method write*(
|
method write*(
|
||||||
s: WsStream,
|
s: WsStream,
|
||||||
msg: seq[byte]): Future[void] {.async.} =
|
msg: seq[byte]): Future[void] {.async.} =
|
||||||
try:
|
mapExceptions(await s.session.send(msg, Opcode.Binary))
|
||||||
await s.session.send(msg, Opcode.Binary)
|
|
||||||
except WSClosedError:
|
|
||||||
raise newLPStreamEOFError()
|
|
||||||
|
|
||||||
method closeImpl*(s: WsStream): Future[void] {.async.} =
|
method closeImpl*(s: WsStream): Future[void] {.async.} =
|
||||||
await s.session.close()
|
await s.session.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user