scoop:scoop.http.asyncio:0.1.0
src/websocket/writer.scoop
Selected production source · src/websocket/writer.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.memory.NativeByteBuffer7
/**8
* The active outgoing message. Short writes respect configured frame size; finish emits FIN after9
* any compression boundary completes.10
*/11
public class WebSocketMessageWriter internal constructor(private val state: WsState, private val message: WsOutgoing)12
: AsyncByteSink, AsyncCloseable {13
/**14
* The Text or Binary message category.15
*/16
public val type: WebSocketMessageType get() = message.type17
/**18
* Whether this particular message uses negotiated compression.19
*/20
public val isCompressed: Boolean get() = message.isCompressed21
/**22
* Whether this body or message reached its successful boundary.23
*/24
public val isComplete: Boolean get() = state.lock.withLock { message.life.complete }25
/**26
* Whether this body or message has been closed.27
*/28
public val isClosed: Boolean get() = state.lock.withLock { message.life.closed }30
/**31
* Accepts up to count readable bytes with bounded frame-sized progress.32
*/33
public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long =34
wsOperation<Unit, Long>(state, false, {35
checkWsWriter(state, message, false)36
httpCount(count, buffer.readableBytes)37
}, suspend { _ ->38
if (count == 0L) 0L else writeWsMessage(state, message, buffer, count)39
})41
/**42
* Flushes accepted bytes, including a compression synchronization point when active, without43
* emitting FIN.44
*/45
public override suspend fun flush() {46
wsOperation<Unit, Unit>(state, false, { checkWsWriter(state, message, true) }, suspend { _ ->47
if (!state.lock.withLock { message.life.complete }) {48
if (message.isCompressed) flushWsDeflate(state, false)49
submitWsWrite(state, WsWriteAction.Flush)50
}51
Unit52
})53
}55
/**56
* Completes compression if selected and ends the message with FIN.57
*/58
public suspend fun finish() {59
wsOperation<Unit, Unit>(state, false, { checkWsWriter(state, message, true) }, suspend { _ ->60
if (!state.lock.withLock { message.life.complete }) finishWsMessage(state, message)61
Unit62
})63
}65
/**66
* Closes the writer. Abandoning an unfinished message aborts its session.67
*/68
public override suspend fun close() { closeWsLife(state, message.life) }69
}71
internal fun beginWsMessage(state: WsState, type: WebSocketMessageType, compress: Boolean): WsOutgoing {72
wsOpen(state)73
if (present(state.outgoing)) throw IllegalStateException(Some("previous WebSocket message is unfinished"))74
val message = WsOutgoing(type, compress && present(state.deflate))75
state.outgoing = Some(message)76
return message77
}79
private fun checkWsWriter(state: WsState, message: WsOutgoing, allowComplete: Boolean) {80
if (message.life.closed) throw wsClosed(state)81
if (message.life.complete) {82
if (allowComplete) return83
throw IllegalStateException(Some("WebSocket message is complete"))84
}85
wsOpen(state)86
if (!present(state.outgoing) || state.outgoing!! !== message)87
throw IllegalStateException(Some("this is not the active WebSocket writer"))88
}90
internal suspend fun writeWsMessage(state: WsState, message: WsOutgoing, buffer: NativeByteBuffer, count: Long): Long {91
val accepted = if (count < state.options.outgoingFrameBytes) count else state.options.outgoingFrameBytes92
return if (message.isCompressed) writeWsDeflate(state, message, buffer, accepted)93
else submitWsWrite(state, WsWriteAction.Data(message, buffer, accepted))94
}96
internal suspend fun finishWsMessage(state: WsState, message: WsOutgoing) {97
if (message.isCompressed) flushWsDeflate(state, true)98
submitWsWrite(state, WsWriteAction.Finish(message))99
}101
internal suspend fun submitWsWrite(state: WsState, action: WsWriteAction): Long = suspendCoroutine<Long> { continuation ->102
val wait = WsWait<Long>(continuation)103
val actions = ArrayList<() -> Unit>()104
state.lock.withLock {105
if (state.closed) failWsWait(wait, state.failure ?: wsClosed(state), actions)106
else {107
if (present(state.writer)) throw IllegalStateException(Some("WebSocket write is already pending"))108
state.writer = Some(WsWriteRequest(action, wait))109
}110
}111
for (next in actions) next()112
driveWs(state)113
}115
internal suspend fun sendWsControl(state: WsState, ping: Boolean, payload: List<Byte>) {116
val bytes = payload.toArray()117
wsOperation<Unit, Unit>(state, false, {118
wsOpen(state)119
if (bytes.size > 125L) throw IllegalArgumentException(Some("WebSocket control payload exceeds 125 bytes"))120
}, suspend { _ ->121
submitWsWrite(state, if (ping) WsWriteAction.Ping(bytes) else WsWriteAction.Pong(bytes))122
Unit123
})124
}