Skip to content

Commit

Permalink
* InterruptedException resets interrupted flag, move to boolean to si…
Browse files Browse the repository at this point in the history
…mplify handling (we can get SocketException first)
  • Loading branch information
Ink committed Apr 12, 2024
1 parent e91cec4 commit c9fa892
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class WiFiClient : IRPCClient {

private val mQueue: BlockingQueue<RPCMessage> = LinkedBlockingDeque()

@Volatile
private var mActive = true

override fun run() {
mHandler.onWaiting()
try {
Expand All @@ -82,7 +85,7 @@ class WiFiClient : IRPCClient {
val iss = socket.getInputStream()
val oos = ObjectOutputStream(socket.getOutputStream())
val ois = ObjectInputStream(iss)
while (!isInterrupted) {
while (mActive) {
while (null != mQueue.peek()) {
val message = mQueue.take()
oos.writeObject(message)
Expand All @@ -105,6 +108,7 @@ class WiFiClient : IRPCClient {

fun shutdown() {
// we do not keep the socket reference, so just wait till next iteration
mActive = false
interrupt()
}

Expand Down
7 changes: 5 additions & 2 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,7 +103,7 @@ 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)
Expand All @@ -119,6 +121,7 @@ class WiFiHost(listener: RPCMessageListener) : IRPCHost {
}

fun shutdown() {
mActive = false
interrupt()
Closeables.close(mSocket)
Closeables.close(mServerSocket)
Expand Down

0 comments on commit c9fa892

Please sign in to comment.