Scoop API

scoop:scoop.http.asyncio:0.1.0

src/websocket/close.scoop

Selected production source · src/websocket/close.scoop

1package scoop.http.asyncio
3internal fun abortWs(state: WsState, error: Throwable?) {
4 if (state.closing)
5 return
6 if (!present(state.failure) && present(error))
7 state.failure = error
8 state.closing = true
9}
11internal suspend fun closeWs(state: WsState) {
12 if (!state.lock.withLock { state.closed }) {
13 checkWsCloseContext(state)
14 httpJob(state.loop, false)
15 state.lock.withLock { abortWs(state, None) }
16 driveWs(state)
17 }
18 awaitWsClosed(state)
21internal suspend fun awaitWsClosed(state: WsState) {
22 suspendCoroutine<Unit> {
23 continuation ->
24 val actions = ArrayList<() -> Unit>()
25 state.lock.withLock {
26 if (state.closed)
27 finishWsCloser(continuation, state.closeFailure, actions)
28 else
29 state.closeWaiters.add(continuation)
30 }
31 for (action in actions)
32 action()
33 driveWs(state)
34 }
37internal fun advanceWsClose(state: WsState, actions: ArrayList<() -> Unit>): Boolean {
38 if (!state.ownerCancellationStarted) {
39 state.ownerCancellationStarted = true
40 val owner = state.ownerJob!!
41 val source = state.sourceJob
42 actions.add {
43 source?.cancel();
44 owner.cancel()
45 }
46 return true
47 }
48 if (state.inputBusy || state.outputBusy)
49 return false
50 finishWsOperations(state, actions)
51 if (state.reading || state.writing)
52 return false
53 if (!state.transportClosing) {
54 state.transportClosing = true
55 actions.add { startWsClose(state) }
56 return true
57 }
58 if (!state.transportClosed)
59 return false
60 state.closeFailure = releaseWsResources(state, state.closeFailure)
61 if (present(state.closeFailure))
62 state.failure = appendWsFailure(state.failure, state.closeFailure!!)
63 state.closed = true
64 val failure = state.failure ?: wsClosed(state)
65 for (wait in state.shutdownWaiters) {
66 if (wsClean(state))
67 succeedWsWait(wait, state.peerClose!!, actions)
68 else
69 failWsWait(wait, failure, actions)
70 }
71 state.shutdownWaiters.clear()
72 if (present(state.incoming)) {
73 state.incoming!!.discarded = !state.incoming!!.life.complete
74 state.incoming!!.data = None
75 detachWsLife(state.incoming!!.life, actions)
76 }
77 if (present(state.outgoing))
78 detachWsLife(state.outgoing!!.life, actions)
79 state.incoming = None;
80 state.outgoing = None;
81 state.automaticPong = None;
82 state.wire = None
83 val listener = state.ownerListener
84 val owner = state.ownerWaiter
85 state.ownerListener = None;
86 state.ownerWaiter = None;
87 state.ownerJob = None;
88 state.ownerScope = None;
89 state.timer = None
90 actions.add {
91 if (present(listener))
92 listener!!.dispose();
93 if (present(owner))
94 owner!!.resume(Unit)
95 }
96 for (waiter in state.closeWaiters)
97 finishWsCloser(waiter, state.closeFailure, actions)
98 state.closeWaiters.clear()
99 finishWsObservers(state, actions)
100 return true
103private fun finishWsCloser(
104 waiter: Continuation<Unit>,
105 failure: Throwable?,
106 actions: ArrayList<() -> Unit>
107) {
108 actions.add {
109 if (present(failure))
110 waiter.resumeWithException(failure!!)
111 else
112 waiter.resume(Unit)
113 }
116private fun startWsClose(state: WsState) {
117 val job = state.ownerJob!!
118 val loop = state.loop
119 context(job) {
120 context(loop) {
121 startCoroutine<Unit>(
122 suspend { nonCancellableScope<Unit> { state.owner.close() } },
123 WsCloseCompletion(state)
124 )
125 }
126 }
129private class WsCloseCompletion(val state: WsState): Continuation<Unit> {
130 public override fun resume(value: Unit) {
131 finish(None)
132 }
133 public override fun resumeWithException(exception: Throwable) {
134 finish(Some(exception))
135 }
136 private fun finish(failure: Throwable?) {
137 state.lock.withLock {
138 state.transportClosed = true
139 if (present(failure))
140 state.closeFailure = appendWsFailure(state.closeFailure, failure!!)
141 Unit
142 }
143 driveWs(state)
144 }