Skip to content

Commit

Permalink
fix UDP service losing incomplete messages
Browse files Browse the repository at this point in the history
  • Loading branch information
voroshkov committed Jan 8, 2018
1 parent da85fd0 commit d5adef9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Android/ChorusRFLaptimer/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "app.andrey_voroshkov.chorus_laptimer"
minSdkVersion 16
targetSdkVersion 25
versionCode 16
versionName "0.7.4"
versionCode 17
versionName "0.7.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
archivesBaseName = "ChorusRFLaptimer"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,28 @@ public void run() {

private class ListenerThread extends Thread {

String mLastIncompleteChunk = "";

private void parseAndCallback(String str) {
if (mConnectionListener == null) return;
if (mConnectionListener == null || str.length() == 0) return;

char lastChar = str.charAt(str.length()-1);
boolean isLastChunkIncomplete = lastChar != '\n';

String[] chunks = TextUtils.split(str, "\n");
int lastChunkIndex = chunks.length - 1;

if (!mLastIncompleteChunk.isEmpty()) {
chunks[0] = mLastIncompleteChunk + chunks[0];
}

if (isLastChunkIncomplete) {
mLastIncompleteChunk = chunks[lastChunkIndex];
chunks[lastChunkIndex] = "";
} else {
mLastIncompleteChunk = "";
}

for (String chunk : chunks) {
if (chunk.isEmpty()) continue;
mActivityHandler.sendMessage(composeMessage(MSG_ON_RECEIVE, chunk));
Expand Down

0 comments on commit d5adef9

Please sign in to comment.