Fix thread bugs

This commit is contained in:
Matéo Duparc 2021-08-06 11:27:46 +02:00
parent cc31423cc4
commit 3562362916
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
1 changed files with 19 additions and 16 deletions

View File

@ -43,7 +43,6 @@ class AIRAService : Service() {
private val binder = AIRABinder() private val binder = AIRABinder()
val sessions = mutableMapOf<Int, Session>() val sessions = mutableMapOf<Int, Session>()
private var sessionCounter = 0 private var sessionCounter = 0
private var server: ServerSocketChannel? = null
private lateinit var selector: Selector private lateinit var selector: Selector
private val sessionIdByKey = mutableMapOf<SelectionKey, Int>() private val sessionIdByKey = mutableMapOf<SelectionKey, Int>()
private val notificationIdManager = NotificationIdManager() private val notificationIdManager = NotificationIdManager()
@ -124,16 +123,20 @@ class AIRAService : Service() {
} }
} }
private fun sendTo(sessionId: Int, buffer: ByteArray) {
serviceHandler.obtainMessage().apply {
what = MESSAGE_SEND_TO
data = Bundle().apply {
putInt("sessionId", sessionId)
putByteArray("buff", buffer)
}
serviceHandler.sendMessage(this)
}
}
fun sendOrAddToPending(sessionId: Int, buffer: ByteArray): Boolean { fun sendOrAddToPending(sessionId: Int, buffer: ByteArray): Boolean {
return if (isOnline(sessionId)) { return if (isOnline(sessionId)) {
serviceHandler.obtainMessage().apply { sendTo(sessionId, buffer)
what = MESSAGE_SEND_TO
data = Bundle().apply {
putInt("sessionId", sessionId)
putByteArray("buff", buffer)
}
serviceHandler.sendMessage(this)
}
true true
} else { } else {
pendingMsgs[sessionId]?.add(buffer) pendingMsgs[sessionId]?.add(buffer)
@ -536,10 +539,6 @@ class AIRAService : Service() {
quit() quit()
stopSelf() stopSelf()
uiCallbacks = null uiCallbacks = null
for (session in sessions.values) {
session.close()
}
server?.close()
} }
} }
} catch (e: IOException) { } catch (e: IOException) {
@ -624,7 +623,7 @@ class AIRAService : Service() {
} }
private fun startListening() { private fun startListening() {
server = try { val server = try {
ServerSocketChannel.open().apply { ServerSocketChannel.open().apply {
configureBlocking(false) configureBlocking(false)
socket().bind(InetSocketAddress(Constants.port)) socket().bind(InetSocketAddress(Constants.port))
@ -743,10 +742,10 @@ class AIRAService : Service() {
filesReceiver.fileTransferNotification, filesReceiver.fileTransferNotification,
filesReceiver.files[0], filesReceiver.files[0],
) )
session.encryptAndSend(Protocol.acceptLargeFiles(), usePadding) sendTo(sessionId, Protocol.acceptLargeFiles())
}, { filesReceiver -> }, { filesReceiver ->
receiveFileTransfers.remove(sessionId) receiveFileTransfers.remove(sessionId)
session.encryptAndSend(Protocol.abortFilesTransfer(), usePadding) sendTo(sessionId, Protocol.abortFilesTransfer())
filesReceiver.fileTransferNotification.cancel() filesReceiver.fileTransferNotification.cancel()
}, },
this, this,
@ -877,6 +876,10 @@ class AIRAService : Service() {
} }
} }
} }
for (session in sessions.values) {
session.close()
}
server?.close()
}.start() }.start()
} }