Scoop API

scoop:scoop.tls.asyncio:0.1.0

src/transfers.scoop

Selected production source · src/transfers.scoop

1package scoop.tls.asyncio
3import scoop.platform.io.WriteZeroException
4import scoop.tls.*
6internal 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 true
11 }
14private fun planAvailable(state: TlsConnection, actions: ArrayList<() -> Unit>): Boolean {
15 var changed = false
16 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 += 1L
21 if (present(state.writer)) state.writer!!.flushed = false
22 changed = true
23 }
24 if (state.output.readableBytes > 0L) {
25 val entry = outputRequest(state)
26 val count = state.output.readableBytes
27 state.outputBusy = true
28 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 = true
32 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.inputCapacity
39 if (count == 0L) throw IllegalStateException(Some("TLS input retry has no BIO space"))
40 state.input.clear()
41 state.inputBusy = true
42 state.inputOwner = reader
43 val entry = reader!!
44 actions.add { startTransfer(state, entry, true, count) }
45 }
46 return changed
49private 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.job
54 val loop = state.loop
55 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 } }
63private 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 = false
71 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 += 1L
79 } else if (count == 0L) throw WriteZeroException()
80 } catch (error: Throwable) { terminate(state, Some(entry), error) }
81 }
82 Unit
83 }
84 drive(state)
85 }
88private fun startFlush(state: TlsConnection, entry: TlsRequest) {
89 val job = entry.job
90 val loop = state.loop
91 context(job) { context(loop) {
92 startCoroutine<Unit>(suspend { state.transport.flush() }, FlushCompletion(state, entry))
93 } }
96private 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 = false
102 entry.flushed = true
103 if (!state.closing && present(failure)) terminate(state, Some(entry), failure!!)
104 Unit
105 }
106 drive(state)
107 }