Scoop API

scoop:scoop.http.asyncio:0.1.0

src/websocket/transfers.scoop

Selected production source · src/websocket/transfers.scoop

1package scoop.http.asyncio
3import scoop.platform.io.WriteZeroException
5internal fun planWsTransfers(state: WsState, actions: ArrayList<() -> Unit>) {
6 if (!state.outputBusy && present(state.wire)) {
7 state.outputBusy = true
8 if (state.output.readableBytes == 0L) actions.add { startWsFlush(state) }
9 else {
10 val count = state.output.readableBytes
11 val beforeRead = state.output.readPosition
12 val beforeWrite = state.output.writePosition
13 actions.add { startWsTransfer(state, false, count, beforeRead, beforeWrite) }
14 }
15 }
16 if (!state.inputBusy && !state.inputFailed && !state.eof && !present(state.peerClose) &&
17 !wsInputBlocked(state) && state.input.readableBytes == 0L) {
18 state.input.clear()
19 state.inputBusy = true
20 val count = state.input.writableBytes
21 actions.add { startWsTransfer(state, true, count, 0L, 0L) }
22 }
25private fun startWsTransfer(state: WsState, input: Boolean, count: Long, beforeRead: Long, beforeWrite: Long) {
26 val completion = WsTransferCompletion(state, input, count, beforeRead, beforeWrite)
27 if (state.lock.withLock { state.closing }) { completion.resume(0L); return }
28 val job = state.ownerJob!!
29 val loop = state.loop
30 context(job) { context(loop) {
31 startCoroutine<Long>(suspend {
32 if (input) state.transport.readInto(state.input, count) else state.transport.writeFrom(state.output, count)
33 }, completion)
34 } }
37private class WsTransferCompletion(val state: WsState, val input: Boolean, val requested: Long,
38 val beforeRead: Long, val beforeWrite: Long) : Continuation<Long> {
39 public override fun resume(value: Long) { finish(value, None) }
40 public override fun resumeWithException(exception: Throwable) { finish(0L, Some(exception)) }
42 private fun finish(count: Long, failure: Throwable?) {
43 state.lock.withLock {
44 if (input) state.inputBusy = false else state.outputBusy = false
45 if (!state.closing) {
46 try {
47 if (present(failure)) throw failure!!
48 httpProgress(count, requested)
49 if (input) {
50 if (state.input.readPosition != beforeRead || state.input.writePosition - beforeWrite != count)
51 throw IllegalStateException(Some("WebSocket input progress does not match its buffer"))
52 state.eof = count == 0L
53 } else {
54 if (state.output.readPosition - beforeRead != count || state.output.writePosition != beforeWrite)
55 throw IllegalStateException(Some("WebSocket output progress does not match its buffer"))
56 if (count == 0L) throw WriteZeroException()
57 }
58 } catch (error: Throwable) { abortWs(state, Some(error)) }
59 }
60 Unit
61 }
62 driveWs(state)
63 }
66private fun startWsFlush(state: WsState) {
67 val completion = WsFlushCompletion(state)
68 if (state.lock.withLock { state.closing }) { completion.resume(Unit); return }
69 val job = state.ownerJob!!
70 val loop = state.loop
71 context(job) { context(loop) { startCoroutine<Unit>(suspend { state.transport.flush() }, completion) } }
74private class WsFlushCompletion(val state: WsState) : Continuation<Unit> {
75 public override fun resume(value: Unit) { finish(None) }
76 public override fun resumeWithException(exception: Throwable) { finish(Some(exception)) }
77 private fun finish(failure: Throwable?) {
78 val actions = ArrayList<() -> Unit>()
79 state.lock.withLock {
80 state.outputBusy = false
81 if (!state.closing) {
82 if (present(failure)) abortWs(state, failure)
83 else finishWsWire(state, actions)
84 }
85 Unit
86 }
87 for (action in actions) action()
88 driveWs(state)
89 }