summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJavier S. Pedro <maemo@javispedro.com>2013-03-28 13:59:54 +0100
committerJavier S. Pedro <maemo@javispedro.com>2013-03-28 13:59:54 +0100
commit016b75cd6318c37aaf33504e3771d4f360f5e044 (patch)
tree1f22f9eb6395a30460e74986d0625b93a81430a8
parentcb04065d6a84bb54b485d0ad8715f09a76191412 (diff)
downloadsowatch-016b75cd6318c37aaf33504e3771d4f360f5e044.tar.gz
sowatch-016b75cd6318c37aaf33504e3771d4f360f5e044.zip
add some ability to resynchronize if frame miss
-rw-r--r--metawatch/metawatch.cpp23
1 files changed, 19 insertions, 4 deletions
diff --git a/metawatch/metawatch.cpp b/metawatch/metawatch.cpp
index c287ccb..66fa171 100644
--- a/metawatch/metawatch.cpp
+++ b/metawatch/metawatch.cpp
@@ -923,12 +923,27 @@ void MetaWatch::realReceive(bool block)
/* Still not enough data available. */
return; /* Wait for more, if non blocking. */
}
- char header[4];
+ static const int HEADER_SIZE = 4;
+ char header[HEADER_SIZE];
- dataRead = _socket->read(header, 4);
- if (dataRead < 4 || header[0] != 0x01) {
- qWarning() << "TODO: Resync to start of frame";
+ dataRead = _socket->read(header, HEADER_SIZE);
+ if (dataRead < 4) {
+ qWarning() << "Short read";
return;
+ } else if (header[0] != 0x01) {
+ qWarning() << "Header not found, trying to recover";
+ // Let's try to find the header in one of the four bits we read
+ for (int i = 1; i < HEADER_SIZE; i++) {
+ if (header[i] == 0x01) {
+ // Header possibly found, try to recover by pushing
+ // the partial header back into the buffer and retrying
+ for (int j = HEADER_SIZE - 1; j >= i; j--) {
+ _socket->ungetChar(header[j]);
+ }
+ }
+ }
+ // In any case, try to repeat.
+ continue;
}
_partialReceived.type = static_cast<MessageType>(header[2]);