Skip to content

Commit

Permalink
* actually call stop on client
Browse files Browse the repository at this point in the history
* InterruptedException resets interrupted flag, move to boolean and exit command to simplify handling (we can get SocketException first)
  • Loading branch information
Ink committed Apr 12, 2024
1 parent e91cec4 commit 31374fb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class HostService(private val context: Context) {
fun stop() {
Log.i(TAG, "HostService stopped")
gps.remove()
client.stop()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ class WiFiClient : IRPCClient {
val iss = socket.getInputStream()
val oos = ObjectOutputStream(socket.getOutputStream())
val ois = ObjectInputStream(iss)
while (!isInterrupted) {
while (true) {
while (null != mQueue.peek()) {
val message = mQueue.take()
oos.writeObject(message)
oos.flush()
if (message.service == null) {
return // disconnect requested
}
}
while (iss.available() > 0) {
val message = ois.readObject() as RPCMessage
Expand All @@ -104,8 +107,9 @@ class WiFiClient : IRPCClient {
}

fun shutdown() {
// send empty message to notify host we are shutting down (we do not guarantee it will be sent though)
// we do not keep the socket reference, so just wait till next iteration
interrupt()
mQueue.add(RPCMessage(null, null))
}

}
Expand Down
15 changes: 11 additions & 4 deletions mobile/src/main/java/com/damn/anotherglass/core/WiFiHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ class WiFiHost(listener: RPCMessageListener) : IRPCHost {
private var mServerSocket: ServerSocket? = null
@Volatile
private var mSocket: Socket? = null
@Volatile
private var mActive = true

override fun run() {
while (!isInterrupted) {
while (mActive) {
try {
mHandler.onWaiting()
ServerSocket(Constants.defaultPort).use { serverSocket ->
Expand Down Expand Up @@ -101,13 +103,16 @@ class WiFiHost(listener: RPCMessageListener) : IRPCHost {
val iss = socket.getInputStream()
val oos = ObjectOutputStream(socket.getOutputStream())
val ois = ObjectInputStream(iss)
while (!isInterrupted) {
while (mActive) {
while (mQueue.peek() != null) {
val message = mQueue.take()
oos.writeObject(message)
oos.flush()
if (message.service == null) {
return // disconnect requested
}
}
while (iss.available() > 0) {
while (mActive && iss.available() > 0) {
val message = ois.readObject() as RPCMessage
if (message.service == null) {
return // client disconnected
Expand All @@ -119,7 +124,9 @@ class WiFiHost(listener: RPCMessageListener) : IRPCHost {
}

fun shutdown() {
interrupt()
// send empty message to notify host we are shutting down (we do not guarantee it will be sent though)
mQueue.add(RPCMessage(null, null))
mActive = false
Closeables.close(mSocket)
Closeables.close(mServerSocket)
}
Expand Down

0 comments on commit 31374fb

Please sign in to comment.