Skip to content
Snippets Groups Projects
Commit 095c76c1 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Fixed a bug where Android’s implementation of Thread::interrupt and

Channel::close was broken
parent 0feb395e
Branches
Tags
No related merge requests found
......@@ -3,6 +3,8 @@ package de.kuschku.quasseldroid_ng.service
import android.arch.lifecycle.LifecycleService
import android.content.Intent
import android.os.Binder
import android.os.Handler
import android.os.HandlerThread
import de.kuschku.libquassel.protocol.*
import de.kuschku.libquassel.session.Backend
import de.kuschku.libquassel.session.Session
......@@ -18,7 +20,7 @@ import javax.net.ssl.X509TrustManager
class QuasselService : LifecycleService() {
private lateinit var session: Session
private val backend = object : Backend {
private val backendImplementation = object : Backend {
override fun session() = session
override fun connect(address: SocketAddress, user: String, pass: String) {
......@@ -33,6 +35,30 @@ class QuasselService : LifecycleService() {
}
}
private val asyncBackend = object : Backend {
private val thread = HandlerThread("BackendHandler")
private val handler: Handler
init {
thread.start()
handler = Handler(thread.looper)
}
override fun connect(address: SocketAddress, user: String, pass: String) {
handler.post {
backendImplementation.connect(address, user, pass)
}
}
override fun disconnect() {
handler.post {
backendImplementation.disconnect()
}
}
override fun session() = backendImplementation.session()
}
private lateinit var database: QuasselDatabase
override fun onCreate() {
......@@ -63,7 +89,7 @@ class QuasselService : LifecycleService() {
override fun onBind(intent: Intent?): QuasselBinder {
super.onBind(intent)
return QuasselBinder(backend)
return QuasselBinder(asyncBackend)
}
class QuasselBinder(val backend: Backend) : Binder()
......
......@@ -112,18 +112,14 @@ class CoreConnection(
}
override fun close() {
interrupt()
handlerService.quit()
val thread = Thread {
try {
channel?.close()
interrupt()
handlerService.quit()
} catch (e: Throwable) {
log(WARN, TAG, "Error encountered while closing connection", e)
}
}
thread.start()
thread.join()
}
override fun dispatch(message: HandshakeMessage) {
handlerService.parse {
......@@ -166,9 +162,11 @@ class CoreConnection(
if (size > 64 * 1024 * 1024)
throw SocketException("Too large frame received: $size")
val dataBuffer = ByteBuffer.allocateDirect(size)
while (dataBuffer.position() < dataBuffer.limit() && channel?.read(dataBuffer) ?: -1 > 0) {
while (!isInterrupted && dataBuffer.position() < dataBuffer.limit() && channel?.read(
dataBuffer) ?: -1 > 0) {
}
dataBuffer.flip()
if (!isInterrupted)
handlerService.parse {
when (internalState.value) {
ConnectionState.HANDSHAKE -> processHandshake(dataBuffer)
......
......@@ -149,15 +149,16 @@ class Session(
backlogManager = null
bufferSyncer = null
bufferViewManager = null
certManagers.clear()
coreInfo = null
dccConfig = null
identities.clear()
ignoreListManager = null
ircListHelper = null
networks.clear()
networkConfig = null
certManagers.clear()
identities.clear()
networks.clear()
super.cleanUp()
}
}
......@@ -176,17 +176,3 @@ class WrappedChannel(
flusher?.invoke()
}
}
fun ByteArray.toHexDump(): String {
val buf = StringBuffer()
var i = 0
buf.append("HexDump ========================================================================\n")
while (i < this.size) {
buf.append(String.format("%02x ", this[i]))
if (i > 0 && (i + 1) % 32 == 0) {
buf.append("\n")
}
i++
}
return buf.toString()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment