Scoop API

scoop:scoop.tls.asyncio:0.1.0

src/stream.scoop

Selected production source · src/stream.scoop

1package scoop.tls.asyncio
3import scoop.asyncio.*
4import scoop.platform.memory.NativeByteBuffer
5import scoop.tls.*
6import scoop.tls.native.TlsEngine
8/**
9 * A TLS stream integrated with the current event loop and structured cancellation. The stream owns
10 * its transport after adoption and exposes plaintext through [AsyncByteStream].
11 *
12 * [close] waits for local cleanup. Use [shutdownGracefully] to exchange TLS close notifications.
13 */
14public class AsyncTlsStream internal constructor(private val state: TlsConnection) : AsyncByteStream, AsyncCloseable {
15 /**
16 * Adopts a duplex asynchronous transport and completes its TLS handshake.
17 */
18 public companion object {
19 // Completes the handshake; ownership transfers when its request is registered.
20 /**
21 * Completes a client handshake for the expected peer on the current event loop and job.
22 * Transport ownership transfers when the handshake request is registered; subsequent
23 * failure or cancellation cleans it up.
24 */
25 public suspend fun <T> takeOwnershipAsClient(transport: T, context: TlsClientContext, peer: TlsPeerName): AsyncTlsStream
26 where T : AsyncByteStream, T : AsyncCloseable {
27 val loop = currentEventLoop()
28 val job = requireJob(loop, true)
29 return adoptAsync(TlsEngine.client(context, peer), transport, loop, job)
30 }
32 // Completes the handshake; ownership transfers when its request is registered.
33 /**
34 * Completes a server handshake on the current event loop and job. Transport ownership
35 * transfers when the handshake request is registered; subsequent failure or cancellation
36 * cleans it up.
37 */
38 public suspend fun <T> takeOwnershipAsServer(transport: T, context: TlsServerContext): AsyncTlsStream
39 where T : AsyncByteStream, T : AsyncCloseable {
40 val loop = currentEventLoop()
41 val job = requireJob(loop, true)
42 return adoptAsync(TlsEngine.server(context), transport, loop, job)
43 }
44 }
46 /**
47 * Whether the connection has finished local cleanup.
48 */
49 public val isClosed: Boolean get() = state.lock.withLock { state.closed }
50 /**
51 * The negotiated TLS version and ALPN; access fails with [IoClosedException] once closing
52 * begins.
53 */
54 public val connectionInfo: TlsConnectionInfo get() = state.lock.withLock {
55 if (state.closing) throw IoClosedException()
56 state.engine.connectionInfo
57 }
59 /**
60 * Suspends until plaintext can be read into the requested writable buffer region. The returned
61 * count records actual progress.
62 */
63 public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long = request(state, RequestKind.Read, Some(buffer), count)
64 /**
65 * Suspends while writing a prefix of readable plaintext and advances the buffer by actual
66 * progress. Repeat to send the entire input.
67 */
68 public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long = request(state, RequestKind.Write, Some(buffer), count)
69 /**
70 * Suspends until pending ciphertext has been drained and the underlying stream flushed.
71 */
72 public override suspend fun flush() { request(state, RequestKind.Flush, None, 0L) }
73 /**
74 * Exchanges TLS close notifications and then closes the owned connection, suspending while
75 * protocol I/O is required.
76 */
77 public suspend fun shutdownGracefully() { request(state, RequestKind.Shutdown, None, 0L) }
78 // Waits for local cleanup; use shutdownGracefully for the TLS close handshake.
79 /**
80 * Waits for local transport and engine cleanup without performing a TLS close handshake.
81 */
82 public override suspend fun close() { closeConnection(state) }