scoop:scoop.http.asyncio:0.1.0
src/websocket/close.scoop
Selected production source · src/websocket/close.scoop
1
package scoop.http.asyncio3
internal fun abortWs(state: WsState, error: Throwable?) {4
if (state.closing)5
return6
if (!present(state.failure) && present(error))7
state.failure = error8
state.closing = true9
}11
internal 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)19
}21
internal 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
else29
state.closeWaiters.add(continuation)30
}31
for (action in actions)32
action()33
driveWs(state)34
}35
}37
internal fun advanceWsClose(state: WsState, actions: ArrayList<() -> Unit>): Boolean {38
if (!state.ownerCancellationStarted) {39
state.ownerCancellationStarted = true40
val owner = state.ownerJob!!41
val source = state.sourceJob42
actions.add {43
source?.cancel();44
owner.cancel()45
}46
return true47
}48
if (state.inputBusy || state.outputBusy)49
return false50
finishWsOperations(state, actions)51
if (state.reading || state.writing)52
return false53
if (!state.transportClosing) {54
state.transportClosing = true55
actions.add { startWsClose(state) }56
return true57
}58
if (!state.transportClosed)59
return false60
state.closeFailure = releaseWsResources(state, state.closeFailure)61
if (present(state.closeFailure))62
state.failure = appendWsFailure(state.failure, state.closeFailure!!)63
state.closed = true64
val failure = state.failure ?: wsClosed(state)65
for (wait in state.shutdownWaiters) {66
if (wsClean(state))67
succeedWsWait(wait, state.peerClose!!, actions)68
else69
failWsWait(wait, failure, actions)70
}71
state.shutdownWaiters.clear()72
if (present(state.incoming)) {73
state.incoming!!.discarded = !state.incoming!!.life.complete74
state.incoming!!.data = None75
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 = None83
val listener = state.ownerListener84
val owner = state.ownerWaiter85
state.ownerListener = None;86
state.ownerWaiter = None;87
state.ownerJob = None;88
state.ownerScope = None;89
state.timer = None90
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 true101
}103
private 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
else112
waiter.resume(Unit)113
}114
}116
private fun startWsClose(state: WsState) {117
val job = state.ownerJob!!118
val loop = state.loop119
context(job) {120
context(loop) {121
startCoroutine<Unit>(122
suspend { nonCancellableScope<Unit> { state.owner.close() } },123
WsCloseCompletion(state)124
)125
}126
}127
}129
private 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 = true139
if (present(failure))140
state.closeFailure = appendWsFailure(state.closeFailure, failure!!)141
Unit142
}143
driveWs(state)144
}145
}