scoop:scoop.http.asyncio:0.1.0
src/http2_reset.scoop
Selected production source · src/http2_reset.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.IoClosedException5
internal fun resetH2Stream(state: H2State, stream: H2Stream, error: Throwable, code: UInt,6
actions: ArrayList<() -> Unit>, notifyFailure: Boolean = true) {7
if (stream.disposed) return8
stream.failure = Some(error)9
if (state.closing) return10
try { state.session.reset(stream.id, code) }11
catch (failure: Throwable) { terminateH2(state, failure); return }12
releaseH2Stream(state, stream, error, actions, notifyFailure)13
}15
internal fun releaseH2Stream(state: H2State, stream: H2Stream, error: Throwable,16
actions: ArrayList<() -> Unit>, notifyFailure: Boolean = true) {17
if (stream.disposed) return18
stopH2Idle(stream, actions)19
stream.disposed = true20
stream.failure = Some(error)21
if (notifyFailure) notifyH2(state, Http2Notice.StreamFailure(stream.id, error), actions)22
stream.data.clear(); stream.offset = 0L23
stream.responses.clear()24
if (present(stream.reader)) finishH2Body(state, stream, stream.reader!!, 0L, Some(error), actions)25
if (present(stream.writer)) finishH2Body(state, stream, stream.writer!!, 0L, Some(error), actions)26
if (present(stream.responseWaiter)) {27
failH2Wait(stream.responseWaiter!!, error, actions)28
stream.responseWaiter = None29
}30
var index = state.requests.size31
while (index > 0L) {32
index -= 1L33
if (state.requests[index].stream.id == stream.id) state.requests.removeAt(index)34
}35
state.streams.remove(stream.id)36
detachH2Stream(stream, actions)37
}39
internal fun retireH2Stream(state: H2State, stream: H2Stream, actions: ArrayList<() -> Unit>) {40
if (!stream.closed || stream.data.size != 0L || stream.responses.size != 0L ||41
present(stream.reader) || present(stream.writer) || present(stream.responseWaiter)) return42
if (!state.closed && h2PendingOutput(state, stream)) return43
stopH2Idle(stream, actions)44
state.streams.remove(stream.id)45
detachH2Stream(stream, actions)46
}48
internal suspend fun closeH2Stream(state: H2State, stream: H2Stream) {49
if (state.lock.withLock { stream.disposed && stream.idleTimers == 0L }) return50
httpJob(state.loop, false)51
suspendCoroutine<Unit> { continuation ->52
val actions = ArrayList<() -> Unit>()53
val done = state.lock.withLock {54
if (!stream.disposed) {55
if (state.closed) releaseH2Stream(state, stream, IoClosedException(), actions, false)56
else resetH2Stream(state, stream, IoClosedException(), 8U, actions, false)57
}58
if (stream.idleTimers == 0L) true else { stream.idleCloseWaiters.add(continuation); false }59
}60
for (action in actions) action()61
driveH2(state)62
if (done) continuation.resume(Unit)63
}64
}