Skip to content

Commit 6d34f0e

Browse files
committed
Check if build is still valid when accepting a new client
1 parent 870c4b1 commit 6d34f0e

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

mod/src/main/kotlin/gay/object/mlogv32/ProcessorAccess.kt

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,20 @@ class ProcessorAccess(
116116
while (true) {
117117
Log.info("Waiting for clients...")
118118
val client = serverSocket.accept()
119-
launch {
120-
handleClient(client)
119+
120+
if (runOnMainThread { build.isValid }) {
121+
launch {
122+
handleClient(client)
123+
}
124+
} else {
125+
Log.err("ProcessorAccess build invalid, stopping server.")
126+
client.close()
127+
stopServer()
128+
break
121129
}
122130
}
123131
}
132+
} catch (_: CancellationException) {
124133
} catch (e: Exception) {
125134
Log.err("ProcessorAccess server failed", e)
126135
}
@@ -301,23 +310,23 @@ private fun linkedBuild(build: LogicBuild, name: String) =
301310

302311
private fun LVar.numu() = num().toUInt()
303312

304-
@Serializable
305-
sealed class Request {
306-
abstract suspend fun handle(processor: ProcessorAccess, rx: ByteReadChannel, tx: ByteWriteChannel): Response
307-
308-
protected suspend fun <T> runOnMainThread(block: () -> T): T {
309-
return suspendCancellableCoroutine { continuation ->
310-
Core.app.post {
311-
try {
312-
continuation.resume(block())
313-
} catch (e: Exception) {
314-
continuation.resumeWithException(e)
315-
}
313+
private suspend fun <T> runOnMainThread(block: () -> T): T {
314+
return suspendCancellableCoroutine { continuation ->
315+
Core.app.post {
316+
try {
317+
continuation.resume(block())
318+
} catch (e: Exception) {
319+
continuation.resumeWithException(e)
316320
}
317321
}
318322
}
319323
}
320324

325+
@Serializable
326+
sealed class Request {
327+
abstract suspend fun handle(processor: ProcessorAccess, rx: ByteReadChannel, tx: ByteWriteChannel): Response
328+
}
329+
321330
@Serializable
322331
@SerialName("flash")
323332
data class FlashRequest(

0 commit comments

Comments
 (0)