Scoop API

scoop:scoop.tls.asyncio:0.1.0

src/registration.scoop

Selected production source · src/registration.scoop

1package scoop.tls.asyncio
3import scoop.asyncio.*
4import scoop.platform.memory.*
6internal suspend fun request(state: TlsConnection, kind: RequestKind, buffer: NativeByteBuffer?, count: Long): Long {
7 val job = requireJob(state.loop, true)
8 return suspendCoroutine<Long> { continuation ->
9 val operation = state.lock.withLock { register(state, kind, job, continuation, buffer, count) }
10 when (operation) {
11 case None -> continuation.resume(0L)
12 case Some(val entry) -> {
13 val listener = job.invokeOnCancellation { error -> cancelRequest(state, entry, error) }
14 val finished = state.lock.withLock {
15 if (entry.released) true else { entry.listener = Some(listener); false }
16 }
17 if (finished) listener.dispose()
18 drive(state)
19 }
20 }
21 }
24private fun register(state: TlsConnection, kind: RequestKind, job: Job, continuation: Continuation<Long>, buffer: NativeByteBuffer?, count: Long): TlsRequest? {
25 job.ensureActive()
26 if (state.closing) throw IoClosedException()
27 val both = kind == RequestKind.Handshake || kind == RequestKind.Shutdown
28 val reading = kind == RequestKind.Read
29 if (present(state.exclusive) || (both && (present(state.reader) || present(state.writer))) ||
30 (!both && (if (reading) present(state.reader) else present(state.writer))))
31 throw IllegalStateException(Some("TLS direction is busy"))
32 var borrow: NativeByteBorrow? = None
33 if (kind == RequestKind.Read || kind == RequestKind.Write) {
34 val bytes = buffer!!
35 val available = if (reading) bytes.writableBytes else bytes.readableBytes
36 if (count < 0L) throw IllegalArgumentException(Some("negative TLS transfer count"))
37 if (count > available) throw IndexOutOfBoundsException()
38 if (count == 0L) return None
39 val prefix = if (!reading && count > 16384L) 16384L else count
40 @Unsafe { borrow = Some(if (reading) bytes.borrowWritable(prefix) else bytes.borrowReadable(prefix)) }
41 }
42 val entry = TlsRequest(kind, job, continuation, borrow)
43 if (both) state.exclusive = Some(entry)
44 else if (reading) state.reader = Some(entry)
45 else state.writer = Some(entry)
46 if (kind == RequestKind.Handshake) state.adopted = true
47 return Some(entry)
50private fun cancelRequest(state: TlsConnection, entry: TlsRequest, error: CancellationException) {
51 state.lock.withLock {
52 if (!entry.selected && !entry.released) terminate(state, Some(entry), error)
53 Unit
54 }
55 drive(state)
58internal fun terminate(state: TlsConnection, origin: TlsRequest?, error: Throwable) {
59 if (!state.closing && present(origin)) state.closeJob = origin!!.job
60 state.closing = true
61 for (entry in requests(state)) {
62 if (!entry.selected) {
63 entry.selected = true
64 entry.failure = Some(if (present(origin) && entry === origin!!) error else IoClosedException())
65 }
66 }
69internal fun releaseRequest(state: TlsConnection, entry: TlsRequest, actions: ArrayList<() -> Unit>) {
70 finishBorrow(entry)
71 entry.released = true
72 when (entry.kind) {
73 case Read -> state.reader = None
74 case Write -> state.writer = None
75 case Flush -> state.writer = None
76 case Handshake -> state.exclusive = None
77 case Shutdown -> state.exclusive = None
78 }
79 val listener = entry.listener
80 entry.listener = None
81 val failure = entry.failure
82 actions.add {
83 if (present(listener)) listener!!.dispose()
84 when (failure) {
85 case Some(val error) -> entry.continuation.resumeWithException(error)
86 case None -> entry.continuation.resume(entry.progress)
87 }
88 }
91internal fun finishBorrow(entry: TlsRequest) {
92 @Unsafe {
93 if (present(entry.borrow)) entry.borrow!!.complete(entry.progress)
94 }
95 entry.borrow = None