scoop:scoop.tls.asyncio:0.1.0
src/transfers.scoop
Selected production source · src/transfers.scoop
1
package scoop.tls.asyncio3
import scoop.platform.io.WriteZeroException4
import scoop.tls.*6
internal fun planTransfers(state: TlsConnection, actions: ArrayList<() -> Unit>): Boolean {7
try { return planAvailable(state, actions) }8
catch (error: Throwable) {9
terminate(state, Some(outputRequest(state)), error)10
return true11
}12
}14
private fun planAvailable(state: TlsConnection, actions: ArrayList<() -> Unit>): Boolean {15
var changed = false16
if (!state.outputBusy) {17
if (state.output.readableBytes == 0L && state.engine.pendingOutput > 0L) {18
state.output.clear()19
state.engine.takeCiphertext(state.output)20
state.outputEpoch += 1L21
if (present(state.writer)) state.writer!!.flushed = false22
changed = true23
}24
if (state.output.readableBytes > 0L) {25
val entry = outputRequest(state)26
val count = state.output.readableBytes27
state.outputBusy = true28
actions.add { startTransfer(state, entry, false, count) }29
} else if (present(state.writer) && state.writer!!.kind == RequestKind.Flush && !state.writer!!.flushed) {30
val entry = state.writer!!31
state.outputBusy = true32
actions.add { startFlush(state, entry) }33
}34
}35
val reader = inputRequest(state)36
if (!state.inputBusy && present(reader)) {37
if (state.inputEnded) throw TlsException("TLS read", TlsFailureKind.Truncated, 0UL, 0L, 0, "TLS requested data after transport EOF")38
val count = state.engine.inputCapacity39
if (count == 0L) throw IllegalStateException(Some("TLS input retry has no BIO space"))40
state.input.clear()41
state.inputBusy = true42
state.inputOwner = reader43
val entry = reader!!44
actions.add { startTransfer(state, entry, true, count) }45
}46
return changed47
}49
private fun startTransfer(state: TlsConnection, entry: TlsRequest, input: Boolean, count: Long) {50
val completion = TransferCompletion(state, entry, input, count)51
val closing = state.lock.withLock { state.closing }52
if (closing) { completion.resume(0L); return }53
val job = entry.job54
val loop = state.loop55
context(job) { context(loop) {56
startCoroutine<Long>(suspend {57
if (input) state.transport.readInto(state.input, count)58
else state.transport.writeFrom(state.output, count)59
}, completion)60
} }61
}63
private class TransferCompletion(val state: TlsConnection, val entry: TlsRequest, val input: Boolean, val requested: Long) : Continuation<Long> {64
public override fun resume(value: Long) { finish(value, None) }65
public override fun resumeWithException(exception: Throwable) { finish(0L, Some(exception)) }67
private fun finish(count: Long, failure: Throwable?) {68
state.lock.withLock {69
if (input) { state.inputBusy = false; state.inputOwner = None }70
else state.outputBusy = false71
if (!state.closing) {72
try {73
if (present(failure)) throw failure!!74
if (count < 0L || count > requested) throw IllegalStateException(Some("invalid TLS transport progress"))75
if (input) {76
if (count == 0L) { state.inputEnded = true; state.engine.endInput() }77
else state.engine.receiveCiphertext(state.input)78
state.inputEpoch += 1L79
} else if (count == 0L) throw WriteZeroException()80
} catch (error: Throwable) { terminate(state, Some(entry), error) }81
}82
Unit83
}84
drive(state)85
}86
}88
private fun startFlush(state: TlsConnection, entry: TlsRequest) {89
val job = entry.job90
val loop = state.loop91
context(job) { context(loop) {92
startCoroutine<Unit>(suspend { state.transport.flush() }, FlushCompletion(state, entry))93
} }94
}96
private class FlushCompletion(val state: TlsConnection, val entry: TlsRequest) : Continuation<Unit> {97
public override fun resume(value: Unit) { finish(None) }98
public override fun resumeWithException(exception: Throwable) { finish(Some(exception)) }99
private fun finish(failure: Throwable?) {100
state.lock.withLock {101
state.outputBusy = false102
entry.flushed = true103
if (!state.closing && present(failure)) terminate(state, Some(entry), failure!!)104
Unit105
}106
drive(state)107
}108
}