Scoop API

scoop:scoop.http.client:0.1.0

src/websocket/session.scoop

Selected production source · src/websocket/session.scoop

1package scoop.http.client
3import scoop.asyncio.AsyncByteSource
4import scoop.asyncio.AsyncCloseable
5import scoop.collections.emptyList
6import scoop.http.*
7import scoop.http.asyncio.*
8import scoop.url.Url
10/**
11 * An owned HTTP/1.1 WebSocket session with handshake history and streaming messages. Successful
12 * upgrade ends the handshake total deadline; session lifecycle is explicit.
13 */
14public class HttpWebSocketSession internal constructor(internal val state: ClientWebSocketState, private val call: ClientCall,
15 /**
16 * The final response or accepted handshake head.
17 */
18 public val head: HttpResponseHead, negotiation: WebSocketNegotiation) : AsyncCloseable {
19 /**
20 * The final URL after redirects.
21 */
22 public val url: Url = clientWebSocketUrl(call.request.url)
23 /**
24 * Completed redirect steps in order.
25 */
26 public val redirectHistory: Array<HttpRedirect> = Array<HttpRedirect>(call.sequence.history.size) { index ->
27 val previous = call.sequence.history[index]
28 HttpRedirect(clientWebSocketUrl(previous.url), previous.method, previous.head,
29 clientWebSocketUrl(previous.nextUrl), previous.nextMethod)
30 }
31 /**
32 * The number of retries before this result.
33 */
34 public val retryCount: Long = call.sequence.retryCount
35 /**
36 * The number of origin authentication retries before this result.
37 */
38 public val authenticationCount: Long = call.sequence.authenticationCount
39 /**
40 * The number of proxy authentication retries before this result.
41 */
42 public val proxyAuthenticationCount: Long = call.sequence.proxyAuthenticationCount
43 /**
44 * The latest optional attempt-metrics snapshot.
45 */
46 public val metrics: HttpAttemptMetrics? get() = clientMetrics(call)
47 /**
48 * The selected subprotocol, or None.
49 */
50 public val protocol: String? = negotiation.protocol
51 /**
52 * Effective permessage-deflate settings, or None.
53 */
54 public val deflate: WebSocketDeflateNegotiation? = negotiation.deflate
55 /**
56 * Whether the underlying session is closed.
57 */
58 public val isClosed: Boolean get() = state.session.isClosed
59 /**
60 * The received close status and reason, when present.
61 */
62 public val peerClose: WebSocketClose? get() = state.session.peerClose
63 /**
64 * The locally sent close status and reason, when present.
65 */
66 public val sentClose: WebSocketClose? get() = state.session.sentClose
67 /**
68 * The session's terminal failure, if any.
69 */
70 public val failure: Throwable? get() = state.session.failure
72 /**
73 * Begins one outgoing text or binary message and returns a writer that must be finished or
74 * closed. Compression is used only when negotiated.
75 */
76 public suspend fun beginMessage(type: WebSocketMessageType, compress: Boolean = true): WebSocketMessageWriter =
77 state.session.beginMessage(type, compress)
78 /**
79 * Returns the next owned incoming message, or None after normal session end; finish or close a
80 * message before reading the next.
81 */
82 public suspend fun receive(): WebSocketMessage? = state.session.receive()
83 /**
84 * Sends one complete UTF-8 text message, with compression when enabled and negotiated.
85 */
86 public suspend fun sendText(text: String, compress: Boolean = true) { state.session.sendText(text, compress) }
87 /**
88 * Sends one complete binary message, with compression when enabled and negotiated.
89 */
90 public suspend fun sendBinary(bytes: List<Byte>, compress: Boolean = true) { state.session.sendBinary(bytes, compress) }
91 /**
92 * Streams one message from the borrowed source; cancellation waits for source work and
93 * required cleanup.
94 */
95 public suspend fun send(type: WebSocketMessageType, source: AsyncByteSource, compress: Boolean = true) {
96 val active = ClientWebSocketSource(state.client)
97 try { context(active) { state.session.send(type, source, compress) } }
98 finally { state.client.lock.withLock { active.running = false } }
99 }
100 /**
101 * Sends a ping control frame with a payload of at most 125 bytes.
102 */
103 public suspend fun ping(payload: List<Byte> = emptyList<Byte>()) { state.session.ping(payload) }
104 /**
105 * Sends a pong control frame with a payload of at most 125 bytes.
106 */
107 public suspend fun pong(payload: List<Byte> = emptyList<Byte>()) { state.session.pong(payload) }
108 /**
109 * Sends Close and waits for the peer reply under the session's close timeout, returning the
110 * peer close value.
111 */
112 public suspend fun shutdownGracefully(value: WebSocketClose = WebSocketClose(Some(1000U))): WebSocketClose =
113 state.session.shutdownGracefully(value)
114 /**
115 * Waits for session termination and reports its failure according to session rules.
116 */
117 public suspend fun awaitClosed() { state.session.awaitClosed() }
118 /**
119 * Closes the local session and awaits cleanup; use shutdownGracefully for an explicit
120 * close-handshake wait.
121 */
122 public override suspend fun close() { state.session.close() }