From ca18615e72cb161184c743c87f43bb37ed7273bc Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Tue, 8 Nov 2022 17:32:58 +0300 Subject: [PATCH] android: Fix of StackoverflowError --- .../java/chat/simplex/app/model/SimpleXAPI.kt | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt index a77cc0dfee..5271a65267 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt @@ -264,8 +264,17 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a private fun startReceiver() { Log.d(TAG, "ChatController startReceiver") if (receiverStarted) return - thread(name="receiver") { - GlobalScope.launch { withContext(Dispatchers.IO) { recvMspLoop() } } + receiverStarted = true + CoroutineScope(Dispatchers.IO).launch { + while (true) { + val ctrl = ctrl + if (ctrl == null) { + receiverStarted = false + return@launch + } + val msg = recvMsg(ctrl) + if (msg != null) processReceivedMsg(msg) + } } } @@ -291,26 +300,18 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } } - private suspend fun recvMsg(ctrl: ChatCtrl): CR? { - return withContext(Dispatchers.IO) { - val json = chatRecvMsgWait(ctrl, MESSAGE_TIMEOUT) - if (json == "") { - null - } else { - val r = APIResponse.decodeStr(json).resp - Log.d(TAG, "chatRecvMsg: ${r.responseType}") - if (r is CR.Response || r is CR.Invalid) Log.d(TAG, "chatRecvMsg json: $json") - r - } + private fun recvMsg(ctrl: ChatCtrl): CR? { + val json = chatRecvMsgWait(ctrl, MESSAGE_TIMEOUT) + return if (json == "") { + null + } else { + val r = APIResponse.decodeStr(json).resp + Log.d(TAG, "chatRecvMsg: ${r.responseType}") + if (r is CR.Response || r is CR.Invalid) Log.d(TAG, "chatRecvMsg json: $json") + r } } - private suspend fun recvMspLoop() { - val msg = recvMsg(ctrl ?: return) - if (msg != null) processReceivedMsg(msg) - recvMspLoop() - } - suspend fun apiGetActiveUser(): User? { val r = sendCmd(CC.ShowActiveUser()) if (r is CR.ActiveUser) return r.user