Scoop API

scoop:scoop.tls.asyncio:0.1.0

src/verification.scoop

Selected production source · src/verification.scoop

1package scoop.tls.asyncio
3import scoop.tls.native.*
5internal fun planVerification(state: TlsConnection, actions: ArrayList<() -> Unit>): Boolean {
6 if (state.verificationBusy)
7 return false
8 for (entry in requests(state)) {
9 if (entry.waiting != RetryKind.Verification)
10 continue
11 state.verificationBusy = true
12 actions.add { startVerification(state, entry) }
13 return true
14 }
15 return false
18private fun startVerification(state: TlsConnection, entry: TlsRequest) {
19 val job = entry.job
20 val loop = state.loop
21 context(job) {
22 context(loop) {
23 startCoroutine<Unit>(
24 suspend {
25 @Unsafe
26 {
27 val work: VerificationWork? =
28 state.lock.withLock {
29 if (state.closing)
30 return@withLock None
31 @Unsafe
32 {
33 val pointer = state.engine.takeVerification()
34 return@withLock Some(VerificationWork(pointer))
35 }
36 }
37 if (present(work))
38 work!!.use suspend {
39 native ->
40 loop.executor.blocking { native.run() }
41 job.ensureActive()
42 state.lock.withLock {
43 if (!state.closing)
44 @Unsafe
45 {
46 state.engine.completeVerification(native.pointer)
47 for (waiting in requests(state)) {
48 if (waiting.waiting == RetryKind.Verification)
49 waiting.waiting = RetryKind.Ready
50 }
51 }
52 Unit
53 }
54 }
55 }
56 },
57 VerificationCompletion(state, entry)
58 )
59 }
60 }
63private class VerificationCompletion(val state: TlsConnection, val entry: TlsRequest): Continuation<
64 Unit
65> {
66 public override fun resume(value: Unit) {
67 finish(None)
68 }
69 public override fun resumeWithException(exception: Throwable) {
70 finish(Some(exception))
71 }
72 private fun finish(failure: Throwable?) {
73 state.lock.withLock {
74 state.verificationBusy = false
75 if (!state.closing && present(failure))
76 terminate(state, Some(entry), failure!!)
77 Unit
78 }
79 drive(state)
80 }