Scoop API

scoop:scoop.http.asyncio:0.1.0

src/http2_transfers.scoop

Selected production source · src/http2_transfers.scoop

1package scoop.http.asyncio
3import scoop.asyncio.IoClosedException
4import scoop.platform.io.WriteZeroException
5import scoop.platform.time.MonotonicTimeSource
7internal fun planH2Transfers(state: H2State, actions: ArrayList<() -> Unit>): Boolean {
8 var changed = false
9 if (!state.outputBusy) {
10 if (state.output.readableBytes == 0L && selectH2Flush(state)) {
11 state.outputBusy = true
12 actions.add { startH2Flush(state) }
13 } else {
14 if (state.output.readableBytes == 0L && state.session.wantsWrite) {
15 state.output.clear()
16 val chunk = state.session.takeOutputChunk(state.output)
17 state.outputStreamId = chunk.streamId
18 state.outputKind = chunk.kind
19 changed = chunk.count != 0L
20 }
21 if (state.output.readableBytes != 0L) {
22 state.outputBusy = true
23 val count = state.output.readableBytes
24 actions.add { startH2Transfer(state, false, count) }
25 } else if (state.outputDirty) {
26 state.outputBusy = true
27 actions.add { startH2Flush(state) }
28 }
29 }
30 }
31 if (!state.inputBusy && state.session.wantsRead) {
32 state.input.clear()
33 state.inputBusy = true
34 val count = state.input.writableBytes
35 actions.add { startH2Transfer(state, true, count) }
36 }
37 return changed
40private fun selectH2Flush(state: H2State): Boolean {
41 for (stream in state.streams.values) {
42 if (present(stream.writer) && stream.writer!!.kind == H2BodyKind.Flush && !state.session.hasPendingOutput(stream.id))
43 state.flushing.add((stream, stream.writer!!))
44 }
45 if (state.goAwaySerialized && state.session.bufferedOutputBytes == 0L) {
46 for (wait in state.connectionFlush) state.flushingConnection.add(wait)
47 state.connectionFlush.clear()
48 }
49 return state.flushing.size != 0L || state.flushingConnection.size != 0L
52private fun startH2Transfer(state: H2State, input: Boolean, count: Long) {
53 val completion = H2TransferCompletion(state, input, count)
54 if (state.lock.withLock { state.closing }) { completion.resume(0L); return }
55 val job = state.ownerJob!!
56 val loop = state.loop
57 context(job) { context(loop) {
58 startCoroutine<Long>(suspend {
59 if (input) state.transport.readInto(state.input, count) else state.transport.writeFrom(state.output, count)
60 }, completion)
61 } }
64private class H2TransferCompletion(val state: H2State, val input: Boolean, val requested: Long) : Continuation<Long> {
65 public override fun resume(value: Long) { finish(value, None) }
66 public override fun resumeWithException(exception: Throwable) { finish(0L, Some(exception)) }
67 private fun finish(count: Long, failure: Throwable?) {
68 val actions = ArrayList<() -> Unit>()
69 state.lock.withLock {
70 if (input) state.inputBusy = false else state.outputBusy = false
71 if (!state.closing) {
72 if (state.goAwaySerialized && state.session.bufferedOutputBytes == 0L) state.goAwayPending = false
73 try {
74 if (present(failure)) throw failure!!
75 httpProgress(count, requested)
76 if (count == 0L && !input) throw WriteZeroException()
77 val now = MonotonicTimeSource.markNow()
78 if (count > 0L) checkH2Idle(state, actions, now)
79 if (input) {
80 if (count == 0L) { state.session.endInput(); terminateH2(state, IoClosedException()) }
81 else while (state.input.readableBytes != 0L) {
82 val consumed = if (state.idleTimers == 0L && !state.captureTimings) state.session.receive(state.input) else {
83 val result = state.session.receiveWithProgress(state.input)
84 progressH2Input(state, result.progress, now)
85 result.count
86 }
87 if (consumed == 0L)
88 throw IllegalStateException(Some("HTTP/2 input made no progress"))
89 }
90 } else {
91 state.outputDirty = true
92 progressH2Output(state, now)
93 }
94 } catch (error: Throwable) {
95 if (input) {
96 try { receiveH2Events(state, actions) }
97 catch (_: Throwable) { terminateH2(state, error) }
98 }
99 terminateH2(state, error)
100 }
101 }
102 Unit
103 }
104 for (action in actions) action()
105 driveH2(state)
106 }
109private fun startH2Flush(state: H2State) {
110 val completion = H2FlushCompletion(state)
111 if (state.lock.withLock { state.closing }) { completion.resume(Unit); return }
112 val job = state.ownerJob!!
113 val loop = state.loop
114 context(job) { context(loop) { startCoroutine<Unit>(suspend { state.transport.flush() }, completion) } }
117private class H2FlushCompletion(val state: H2State) : Continuation<Unit> {
118 public override fun resume(value: Unit) { finish(None) }
119 public override fun resumeWithException(exception: Throwable) { finish(Some(exception)) }
120 private fun finish(failure: Throwable?) {
121 val actions = ArrayList<() -> Unit>()
122 state.lock.withLock {
123 state.outputBusy = false
124 state.outputDirty = false
125 if (!state.closing && present(failure)) terminateH2(state, failure!!)
126 if (!state.closing) {
127 checkH2Idle(state, actions)
128 for (stream in state.streams.values) stream.outputDirty = false
129 for ((stream, entry) in state.flushing) {
130 if (!entry.wait.released) finishH2Body(state, stream, entry, 0L, None, actions)
131 }
132 for (wait in state.flushingConnection) succeedH2Wait(wait, Unit, actions)
133 state.flushing.clear(); state.flushingConnection.clear()
134 for (stream in state.streams.values) {
135 if (present(stream.writeIdle) && !present(stream.writer) && !h2PendingOutput(state, stream))
136 stopH2Idle(stream, false, actions)
137 }
138 }
139 Unit
140 }
141 for (action in actions) action()
142 driveH2(state)
143 }