@@ -4,7 +4,7 @@ import ../[lsquic_ffi, stream]
44import ../ helpers/ sequninit
55
66proc onClose * (stream: ptr lsquic_stream_t, ctx: ptr lsquic_stream_ctx_t) {.cdecl .} =
7- trace " Stream closed"
7+ debug " Stream closed"
88 if ctx.isNil:
99 debug " stream_ctx is nil onClose"
1010 return
@@ -13,7 +13,8 @@ proc onClose*(stream: ptr lsquic_stream_t, ctx: ptr lsquic_stream_ctx_t) {.cdecl
1313 if not streamCtx.closeWrite:
1414 streamCtx.isEof = true
1515 streamCtx.closed.fire ()
16- streamCtx.abortPendingWrites (" stream closed" )
16+ streamCtx.abortPendingWrites (" stream closed 4" )
17+ GC_unref (streamCtx)
1718
1819type StreamReadContext = object
1920 stream: ptr lsquic_stream_t
@@ -40,12 +41,11 @@ proc onRead*(stream: ptr lsquic_stream_t, ctx: ptr lsquic_stream_ctx_t) {.cdecl.
4041 if nread < 0 :
4142 error " could not read from stream" , nread, streamId = lsquic_stream_id (stream)
4243 streamCtx.abort ()
43-
44+
4445 if lsquic_stream_wantread (stream, 0 ) == - 1 :
4546 error " could not set stream wantread" , streamId = lsquic_stream_id (stream)
4647 streamCtx.abort ()
4748
48-
4949proc onWrite * (stream: ptr lsquic_stream_t, ctx: ptr lsquic_stream_ctx_t) {.cdecl .} =
5050 trace " onWrite"
5151
@@ -55,6 +55,8 @@ proc onWrite*(stream: ptr lsquic_stream_t, ctx: ptr lsquic_stream_ctx_t) {.cdecl
5555
5656 let streamCtx = cast [Stream ](ctx)
5757 if streamCtx.toWrite.len == 0 :
58+ if not streamCtx.shouldClose.isNil and not streamCtx.shouldClose.finished:
59+ streamCtx.shouldClose.complete ()
5860 if lsquic_stream_wantwrite (stream, 0 ) == - 1 :
5961 error " could not set stream wantwrite" , streamId = lsquic_stream_id (stream)
6062 streamCtx.abort ()
@@ -71,7 +73,6 @@ proc onWrite*(stream: ptr lsquic_stream_t, ctx: ptr lsquic_stream_ctx_t) {.cdecl
7173 let p = w.data[w.offset].addr
7274 let nAvail = (w.data.len - w.offset).csize_t
7375 let n: ssize_t = lsquic_stream_write (stream, p, nAvail)
74-
7576 if n > 0 :
7677 w.offset += n.int
7778 if w.offset >= w.data.len:
@@ -82,12 +83,15 @@ proc onWrite*(stream: ptr lsquic_stream_t, ctx: ptr lsquic_stream_ctx_t) {.cdecl
8283 return
8384 streamCtx.toWrite.delete (0 )
8485 elif n == 0 :
86+ # Nothing to write
8587 break
8688 else :
8789 streamCtx.abortPendingWrites (" write failed" )
8890 return
8991
9092 if streamCtx.toWrite.len == 0 :
93+ if not streamCtx.shouldClose.isNil and not streamCtx.shouldClose.finished:
94+ streamCtx.shouldClose.complete ()
9195 if lsquic_stream_wantwrite (stream, 0 ) == - 1 :
9296 error " could not set stream wantwrite" , streamId = lsquic_stream_id (stream)
9397 streamCtx.abort ()
0 commit comments