Skip to content

Commit fe46c28

Browse files
committed
comments + tweaks
1 parent db63d1a commit fe46c28

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/client/sse/EventStream.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public void run() {
4343
client.errorsCount.set(0);
4444
readStream(is);
4545
} catch (InterruptedException ie){
46-
Log.i("EventStream.run(): Caught InterruptedException"); //thrown by interruptBackgroundThread()
46+
//Caused by stopBackgroundThread(), exit without restarting as the new bgThread does it
47+
Log.i("EventStream.run(): Caught InterruptedException");
4748
return;
4849
} catch (Exception e) {
4950
Log.e("Error reading from event-stream, continuous errors: " + client.errorsCount.incrementAndGet(), e);
@@ -60,9 +61,10 @@ public void run() {
6061
protected int readFromStream(InputStream inputStream, byte[] buffer) throws IOException, InterruptedException {
6162
int len;
6263
while (true) {
64+
//Checking if bytes are available to read to avoid performing a blocking read
6365
int available = inputStream.available();
6466
if (available > 0) break;
65-
Thread.sleep(1);
67+
Thread.sleep(1); //If bgThread was interrupted this throws an InterruptedException
6668
}
6769

6870
len = inputStream.read(buffer);

src/AndroidClient/android/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ public String getConnectionDisplayName() {
272272
: "(not connected)";
273273
}
274274

275-
protected synchronized void interruptBackgroundThread() {
275+
protected synchronized void stopBackgroundThread() {
276276
if (bgThread != null){
277277
bgEventStream.close();
278-
bgThread.interrupt();
278+
bgThread.interrupt(); //notify the bgThread to exit
279279
try {
280280
bgThread.join();
281281
} catch (InterruptedException ignore) {}
@@ -288,7 +288,7 @@ protected EventStream createEventStream(){
288288
}
289289

290290
public synchronized ServerEventsClient start(){
291-
interruptBackgroundThread();
291+
stopBackgroundThread();
292292

293293
stopped.set(false);
294294
bgEventStream = createEventStream();
@@ -370,7 +370,7 @@ private synchronized void internalStop() {
370370
}
371371

372372
connectionInfo = null;
373-
interruptBackgroundThread();
373+
stopBackgroundThread();
374374
}
375375

376376
private void onCommandReceived(ServerEventMessage e) {

src/AndroidClient/client/src/main/java/net/servicestack/client/sse/EventStream.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public void run() {
4343
client.errorsCount.set(0);
4444
readStream(is);
4545
} catch (InterruptedException ie){
46-
Log.i("EventStream.run(): Caught InterruptedException"); //thrown by interruptBackgroundThread()
46+
//Caused by stopBackgroundThread(), exit without restarting as the new bgThread does it
47+
Log.i("EventStream.run(): Caught InterruptedException");
4748
return;
4849
} catch (Exception e) {
4950
Log.e("Error reading from event-stream, continuous errors: " + client.errorsCount.incrementAndGet(), e);
@@ -60,9 +61,10 @@ public void run() {
6061
protected int readFromStream(InputStream inputStream, byte[] buffer) throws IOException, InterruptedException {
6162
int len;
6263
while (true) {
64+
//Checking if bytes are available to read to avoid performing a blocking read
6365
int available = inputStream.available();
6466
if (available > 0) break;
65-
Thread.sleep(1);
67+
Thread.sleep(1); //If bgThread was interrupted this throws an InterruptedException
6668
}
6769

6870
len = inputStream.read(buffer);

src/AndroidClient/client/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ public String getConnectionDisplayName() {
272272
: "(not connected)";
273273
}
274274

275-
protected synchronized void interruptBackgroundThread() {
275+
protected synchronized void stopBackgroundThread() {
276276
if (bgThread != null){
277277
bgEventStream.close();
278-
bgThread.interrupt();
278+
bgThread.interrupt(); //notify the bgThread to exit
279279
try {
280280
bgThread.join();
281281
} catch (InterruptedException ignore) {}
@@ -288,7 +288,7 @@ protected EventStream createEventStream(){
288288
}
289289

290290
public synchronized ServerEventsClient start(){
291-
interruptBackgroundThread();
291+
stopBackgroundThread();
292292

293293
stopped.set(false);
294294
bgEventStream = createEventStream();
@@ -370,7 +370,7 @@ private synchronized void internalStop() {
370370
}
371371

372372
connectionInfo = null;
373-
interruptBackgroundThread();
373+
stopBackgroundThread();
374374
}
375375

376376
private void onCommandReceived(ServerEventMessage e) {

0 commit comments

Comments
 (0)