Scoop API

scoop:scoop.http.asyncio:0.1.0

src/transport.scoop

Selected production source · src/transport.scoop

1package scoop.http.asyncio
3import scoop.asyncio.*
4import scoop.platform.memory.NativeByteBuffer
6/**
7 * Owns a transport plus a preserved input prefix, allowing protocol handoff without losing bytes
8 * read past the previous protocol boundary.
9 */
10public class HttpTransport internal constructor(
11 private val stream: AsyncByteStream, private val owner: AsyncCloseable,
12 private val loop: EventLoop, private var prefix: Array<Byte>
13) : AsyncByteStream, AsyncCloseable {
14 /**
15 * Ownership-transfer construction with already-read prefix bytes.
16 */
17 public companion object {
18 /**
19 * Takes transport ownership and copies prefix bytes to be delivered before new transport
20 * reads.
21 */
22 public fun <T> takeOwnershipWithPrefix(transport: T, prefix: List<Byte>): HttpTransport
23 where T : AsyncByteStream, T : AsyncCloseable {
24 val loop = currentEventLoop()
25 val job = httpJob(loop, true)
26 val bytes = prefix.toArray()
27 job.ensureActive()
28 return HttpTransport(transport, transport, loop, bytes)
29 }
30 }
32 private val lock: HttpLock = HttpLock()
33 private var offset: Long = 0L
34 private var closing: Boolean = false
35 private var closed: Boolean = false
36 private val closeWaiters: ArrayList<Continuation<Unit>> = ArrayList<Continuation<Unit>>()
38 /**
39 * Reads preserved prefix bytes first, then reads from the underlying transport.
40 */
41 public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long {
42 httpJob(loop, true)
43 val copied = lock.withLock {
44 if (closing) throw IoClosedException()
45 httpCount(count, buffer.writableBytes)
46 if (count == 0L) return@withLock 0L
47 val available = prefix.size - offset
48 if (available == 0L) return@withLock -1L
49 val amount = if (count < available) count else available
50 buffer.writeBytes(prefix, offset, amount)
51 offset += amount
52 if (offset == prefix.size) { prefix = Array<Byte>(0L) { _ -> 0.toByte() }; offset = 0L }
53 amount
54 }
55 return if (copied >= 0L) copied else stream.readInto(buffer, count)
56 }
58 /**
59 * Writes readable bytes to the owned transport.
60 */
61 public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long {
62 httpJob(loop, true)
63 lock.withLock { if (closing) throw IoClosedException(); httpCount(count, buffer.readableBytes) }
64 return if (count == 0L) 0L else stream.writeFrom(buffer, count)
65 }
67 /**
68 * Flushes the underlying transport.
69 */
70 public override suspend fun flush() {
71 httpJob(loop, true)
72 lock.withLock { if (closing) throw IoClosedException(); Unit }
73 stream.flush()
74 }
76 /**
77 * Closes the owned transport after active I/O cleanup.
78 */
79 public override suspend fun close() {
80 if (lock.withLock { closed }) return
81 val job = httpJob(loop, false)
82 suspendCoroutine<Unit> { continuation ->
83 val begin = lock.withLock {
84 if (closed) -1 else {
85 closeWaiters.add(continuation)
86 if (closing) 0 else { closing = true; 1 }
87 }
88 }
89 if (begin == -1) continuation.resume(Unit)
90 else if (begin == 1) context(job) { context(loop) {
91 startCoroutine<Unit>(suspend { owner.close() }, HttpTransportClose(this))
92 } }
93 }
94 }
96 internal fun finishClose(failure: Throwable?) {
97 val waiters = lock.withLock {
98 closed = true
99 prefix = Array<Byte>(0L) { _ -> 0.toByte() }
100 offset = 0L
101 val result = closeWaiters.toArray()
102 closeWaiters.clear()
103 result
104 }
105 for (waiter in waiters) when (failure) {
106 case Some(val error) -> waiter.resumeWithException(error)
107 case None -> waiter.resume(Unit)
108 }
109 }
112private class HttpTransportClose(val transport: HttpTransport) : Continuation<Unit> {
113 public override fun resume(value: Unit) { transport.finishClose(None) }
114 public override fun resumeWithException(exception: Throwable) { transport.finishClose(Some(exception)) }