scoop:scoop.http.asyncio:0.1.0
src/websocket/session.scoop
Selected production source · src/websocket/session.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.collections.emptyList5
import scoop.http.*7
/**8
* A structured WebSocket session over an already-upgraded transport. Background tasks handle9
* control frames and bounded message backpressure; serialize message operations in each direction.10
*/11
public class AsyncWebSocketSession internal constructor(12
private val state: WsState13
): AsyncCloseable {14
/**15
* Starts a session while transferring an established transport into a supplied coroutine16
* scope.17
*/18
public companion object {19
/**20
* Takes an already-upgraded transport with its role and actual compression negotiation.21
* Invalid initial options leave ownership with the caller; failures after takeover clean22
* up the transport.23
*/24
public suspend fun <T> takeOwnership(25
transport: T,26
scope: CoroutineScope,27
role: HttpRole,28
options: WebSocketSessionOptions = webSocketSessionOptions(),29
deflate: WebSocketDeflateNegotiation? = None30
): AsyncWebSocketSession31
where T: AsyncByteStream, T: AsyncCloseable =32
adoptWs(transport, transport, scope, role, options, deflate)33
}35
/**36
* The local client/server role controlling masking and negotiated settings.37
*/38
public val role: HttpRole get() = state.role39
/**40
* The actual permessage-deflate negotiation, if compression was negotiated.41
*/42
public val deflate: WebSocketDeflateNegotiation? get() =43
when (state.deflate) {44
case Some(val value) -> Some(value.negotiated)45
case None -> None46
}47
/**48
* Whether owned session cleanup has completed.49
*/50
public val isClosed: Boolean get() = state.lock.withLock { state.closed }51
/**52
* The received peer close frame, when available.53
*/54
public val peerClose: WebSocketClose? get() = state.lock.withLock { state.peerClose }55
/**56
* The close frame sent by this session, when available.57
*/58
public val sentClose: WebSocketClose? get() = state.lock.withLock { state.sentClose }59
/**60
* The retained terminal session failure, or None when closure has no recorded failure.61
*/62
public val failure: Throwable? get() = state.lock.withLock { state.failure }64
/**65
* Begins one streamed outgoing message. Compression can be disabled for this message even when66
* negotiated; finish the returned writer before another message.67
*/68
public suspend fun beginMessage(69
type: WebSocketMessageType,70
compress: Boolean = true71
): WebSocketMessageWriter =72
wsOperation<WsOutgoing, WebSocketMessageWriter>(73
state,74
false,75
{ beginWsMessage(state, type, compress) },76
suspend {77
message ->78
val job = httpJob(state.loop, true)79
listenWsLife(state, message.life, job)80
job.ensureActive()81
WebSocketMessageWriter(state, message)82
}83
)85
/**86
* Waits for the next message or returns None at normal session end. Complete the current87
* message before requesting another.88
*/89
public suspend fun receive(): WebSocketMessage? = receiveWsMessage(state)90
/**91
* Sends one complete UTF-8 text message with optional negotiated compression.92
*/93
public suspend fun sendText(text: String, compress: Boolean = true) {94
sendWsBytes(state, WebSocketMessageType.Text, text.toByteArray(), compress)95
}96
/**97
* Sends one complete binary message with optional negotiated compression.98
*/99
public suspend fun sendBinary(bytes: List<Byte>, compress: Boolean = true) {100
sendWsBytes(state, WebSocketMessageType.Binary, bytes.toArray(), compress)101
}102
/**103
* Streams one complete message from a borrowed source; the source is not closed by this call.104
*/105
public suspend fun send(106
type: WebSocketMessageType,107
source: AsyncByteSource,108
compress: Boolean = true109
) {110
sendWsSource(state, type, source, compress)111
}112
/**113
* Sends a Ping and waits for its matching Pong.114
*/115
public suspend fun ping(payload: List<Byte> = emptyList<Byte>()) {116
sendWsControl(state, true, payload)117
}118
/**119
* Sends an unsolicited Pong without waiting for a reply.120
*/121
public suspend fun pong(payload: List<Byte> = emptyList<Byte>()) {122
sendWsControl(state, false, payload)123
}124
/**125
* Sends Close and waits for peer Close, transport flush, and cleanup within the configured126
* deadline.127
*/128
public suspend fun shutdownGracefully(129
value: WebSocketClose = WebSocketClose(Some(1000U))130
): WebSocketClose = shutdownWs(state, value)131
/**132
* Waits for session cleanup to complete.133
*/134
public suspend fun awaitClosed() {135
observeWsClosed(state)136
}137
/**138
* Performs local closure, cancelling owned I/O and waiting for native borrows and cleanup.139
* Repeated close is supported, including cleanup from an already-cancelled caller.140
*/141
public override suspend fun close() {142
closeWs(state)143
}144
}