Fix thread bugs
This commit is contained in:
parent
cc31423cc4
commit
3562362916
|
@ -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,8 +123,7 @@ class AIRAService : Service() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendOrAddToPending(sessionId: Int, buffer: ByteArray): Boolean {
|
private fun sendTo(sessionId: Int, buffer: ByteArray) {
|
||||||
return if (isOnline(sessionId)) {
|
|
||||||
serviceHandler.obtainMessage().apply {
|
serviceHandler.obtainMessage().apply {
|
||||||
what = MESSAGE_SEND_TO
|
what = MESSAGE_SEND_TO
|
||||||
data = Bundle().apply {
|
data = Bundle().apply {
|
||||||
|
@ -134,6 +132,11 @@ class AIRAService : Service() {
|
||||||
}
|
}
|
||||||
serviceHandler.sendMessage(this)
|
serviceHandler.sendMessage(this)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun sendOrAddToPending(sessionId: Int, buffer: ByteArray): Boolean {
|
||||||
|
return if (isOnline(sessionId)) {
|
||||||
|
sendTo(sessionId, buffer)
|
||||||
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue