scoop:scoop.tls.asyncio:0.1.0
src/registration.scoop
Selected production source · src/registration.scoop
1
package scoop.tls.asyncio3
import scoop.asyncio.*4
import scoop.platform.memory.*6
internal 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
}22
}24
private 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.Shutdown28
val reading = kind == RequestKind.Read29
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? = None33
if (kind == RequestKind.Read || kind == RequestKind.Write) {34
val bytes = buffer!!35
val available = if (reading) bytes.writableBytes else bytes.readableBytes36
if (count < 0L) throw IllegalArgumentException(Some("negative TLS transfer count"))37
if (count > available) throw IndexOutOfBoundsException()38
if (count == 0L) return None39
val prefix = if (!reading && count > 16384L) 16384L else count40
@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 = true47
return Some(entry)48
}50
private fun cancelRequest(state: TlsConnection, entry: TlsRequest, error: CancellationException) {51
state.lock.withLock {52
if (!entry.selected && !entry.released) terminate(state, Some(entry), error)53
Unit54
}55
drive(state)56
}58
internal fun terminate(state: TlsConnection, origin: TlsRequest?, error: Throwable) {59
if (!state.closing && present(origin)) state.closeJob = origin!!.job60
state.closing = true61
for (entry in requests(state)) {62
if (!entry.selected) {63
entry.selected = true64
entry.failure = Some(if (present(origin) && entry === origin!!) error else IoClosedException())65
}66
}67
}69
internal fun releaseRequest(state: TlsConnection, entry: TlsRequest, actions: ArrayList<() -> Unit>) {70
finishBorrow(entry)71
entry.released = true72
when (entry.kind) {73
case Read -> state.reader = None74
case Write -> state.writer = None75
case Flush -> state.writer = None76
case Handshake -> state.exclusive = None77
case Shutdown -> state.exclusive = None78
}79
val listener = entry.listener80
entry.listener = None81
val failure = entry.failure82
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
}89
}91
internal fun finishBorrow(entry: TlsRequest) {92
@Unsafe {93
if (present(entry.borrow)) entry.borrow!!.complete(entry.progress)94
}95
entry.borrow = None96
}