scoop:scoop.http.asyncio:0.1.0
src/http2_transfers.scoop
Selected production source · src/http2_transfers.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.IoClosedException4
import scoop.platform.io.WriteZeroException5
import scoop.platform.time.MonotonicTimeSource7
internal fun planH2Transfers(state: H2State, actions: ArrayList<() -> Unit>): Boolean {8
var changed = false9
if (!state.outputBusy) {10
if (state.output.readableBytes == 0L && selectH2Flush(state)) {11
state.outputBusy = true12
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.streamId18
state.outputKind = chunk.kind19
changed = chunk.count != 0L20
}21
if (state.output.readableBytes != 0L) {22
state.outputBusy = true23
val count = state.output.readableBytes24
actions.add { startH2Transfer(state, false, count) }25
} else if (state.outputDirty) {26
state.outputBusy = true27
actions.add { startH2Flush(state) }28
}29
}30
}31
if (!state.inputBusy && state.session.wantsRead) {32
state.input.clear()33
state.inputBusy = true34
val count = state.input.writableBytes35
actions.add { startH2Transfer(state, true, count) }36
}37
return changed38
}40
private 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 != 0L50
}52
private 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.loop57
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
} }62
}64
private 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 = false71
if (!state.closing) {72
if (state.goAwaySerialized && state.session.bufferedOutputBytes == 0L) state.goAwayPending = false73
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.count86
}87
if (consumed == 0L)88
throw IllegalStateException(Some("HTTP/2 input made no progress"))89
}90
} else {91
state.outputDirty = true92
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
Unit103
}104
for (action in actions) action()105
driveH2(state)106
}107
}109
private 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.loop114
context(job) { context(loop) { startCoroutine<Unit>(suspend { state.transport.flush() }, completion) } }115
}117
private 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 = false124
state.outputDirty = false125
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 = false129
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
Unit140
}141
for (action in actions) action()142
driveH2(state)143
}144
}