11import chronos
2+ import chronicles
23import ./ lsquic_ffi
34
45type StreamError * = object of IOError
@@ -23,30 +24,42 @@ proc new*(T: typedesc[Stream], quicStream: ptr lsquic_stream_t = nil): T =
2324 closed: newAsyncEvent (),
2425 )
2526
26- proc close * (stream: Stream ): bool =
27+ proc abortPendingWrites * (stream: Stream , reason: string = " " ) =
28+ for pendingWrite in stream.toWrite.mitems:
29+ if not pendingWrite.doneFut.finished:
30+ pendingWrite.doneFut.fail (newException (StreamError , reason))
31+ stream.toWrite.setLen (0 )
32+
33+ proc abort * (stream: Stream ) =
34+ if stream.closeWrite and stream.isEof:
35+ if not stream.closed.isSet ():
36+ stream.closed.fire ()
37+ stream.abortPendingWrites (" stream aborted" )
38+ return
39+
40+ let ret = lsquic_stream_close (stream.quicStream)
41+ if ret != 0 :
42+ error " could not abort stream" , streamId = lsquic_stream_id (stream.quicStream)
43+
44+ stream.closeWrite = true
45+ stream.isEof = true
46+ stream.abortPendingWrites (" stream aborted" )
47+ stream.closed.fire ()
48+
49+ proc close * (stream: Stream ) =
2750 if stream.closeWrite:
28- return true
51+ return
2952
3053 # Closing only the write side
3154 let ret = lsquic_stream_shutdown (stream.quicStream, 1 )
3255 if ret == 0 :
3356 if stream.isEof:
3457 if lsquic_stream_close (stream.quicStream) != 0 :
58+ stream.abort ()
3559 raise newException (StreamError , " could not close the stream" )
3660
61+ stream.abortPendingWrites (" steam closed" )
3762 stream.closeWrite = true
38- return true
39- false
40- # TODO : clear all pending writes
41-
42- proc abort * (stream: Stream ): bool =
43- let ret = lsquic_stream_close (stream.quicStream) == 0
44- if ret:
45- stream.closeWrite = true
46- stream.isEof = true
47- return true
48- false
49- # TODO : clear all pending writes and cancel reads
5063
5164proc read * (
5265 stream: Stream
@@ -55,7 +68,7 @@ proc read*(
5568 return @ []
5669
5770 if lsquic_stream_wantread (stream.quicStream, 1 ) == - 1 :
58- discard stream.abort ()
71+ stream.abort ()
5972 raise newException (StreamError , " could not set wantread" )
6073
6174 let incomingFut = stream.incoming.get ()
@@ -65,14 +78,14 @@ proc read*(
6578 await incomingFut.cancelAndWait ()
6679 stream.isEof = true
6780 stream.closeWrite = true
68- raise newException (StreamError , " connection closed" )
81+ raise newException (StreamError , " stream closed" )
6982
7083 let incoming = await incomingFut
7184 if incoming.len == 0 :
7285 if stream.closeWrite:
7386 # We were already closed for write. Close the stream completely
7487 if lsquic_stream_close (stream.quicStream) != 0 :
75- discard stream.abort ()
88+ stream.abort ()
7689 raise newException (StreamError , " could not close the stream" )
7790 stream.isEof = true
7891
@@ -90,7 +103,7 @@ proc write*(
90103 discard lsquic_stream_wantwrite (stream.quicStream, 1 )
91104 let raceFut = await race (closedFut, doneFut)
92105 if raceFut == closedFut:
93- doneFut.fail (newException (StreamError , " connection closed" ))
106+ doneFut.fail (newException (StreamError , " stream closed" ))
94107 stream.closeWrite = true
95108
96109 await doneFut
0 commit comments