Multi-Hackers / SocketModem

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Revision:
65:ed113472b2c1
Parent:
60:ee9c7a700330
Parent:
64:0ca9c7123ffc
Child:
67:1003b410f781
--- a/utils/MTSCircularBuffer.cpp	Fri Dec 20 23:12:53 2013 +0000
+++ b/utils/MTSCircularBuffer.cpp	Sat Dec 21 04:25:43 2013 +0000
@@ -5,7 +5,7 @@
 
 using namespace mts;
 
-MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), _threshold(-1), _op(Vars::GREATER)
+MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), bytes(0), _threshold(-1), _op(Vars::GREATER)
 {
     buffer = new char[bufferSize];
 }
@@ -23,11 +23,12 @@
 int MTSCircularBuffer::read(char* data, int length)
 {
     int i = 0;
-    while ((i < length) && (available() > 0)) {
+    while ((i < length) && (bytes > 0)) {
         if (readIndex == bufferSize) {
             readIndex = 0;
         }
         data[i++] = buffer[readIndex++];
+        bytes--;
         checkThreshold();
     }
     return i;
@@ -35,13 +36,14 @@
 
 int MTSCircularBuffer::read(char& data)
 {
-    if (available() == 0) {
+    if (bytes == 0) {
         return 0;
     }
     if (readIndex == bufferSize) {
         readIndex = 0;
     }
     data = buffer[readIndex++];
+    bytes--;
     checkThreshold();
     return 1;
 }
@@ -49,11 +51,12 @@
 int MTSCircularBuffer::write(const char* data, int length)
 {
     int i = 0;
-    while((i < length) && capacity()) {
+    while((i < length) && (bytes < bufferSize)) {
         if(writeIndex == bufferSize) {
             writeIndex = 0;
         }
         buffer[writeIndex++] = data[i++];
+        bytes++;
         checkThreshold();
     }
     return i;
@@ -61,34 +64,31 @@
 
 int MTSCircularBuffer::write(char data)
 {
-    if (available() == bufferSize) {
+    if (bytes == bufferSize) {
         return 0;
     }
     if(writeIndex == bufferSize) {
         writeIndex = 0;
     }
     buffer[writeIndex++] = data;
+    bytes++;
     checkThreshold();
     return 1;
 }
 
 int MTSCircularBuffer::capacity()
 {
-    return bufferSize - available();
+    return bufferSize - bytes;
 }
 
 int MTSCircularBuffer::available()
 {
-    if (readIndex <= writeIndex) {
-        return writeIndex - readIndex;
-    } else {
-        return bufferSize + writeIndex - readIndex;
-    }
+    return bytes;
 }
 
 bool MTSCircularBuffer::isFull()
 {
-    if (available() == bufferSize) {
+    if (bytes == bufferSize) {
         return true;
     } else {
         return false;
@@ -97,7 +97,7 @@
 
 bool MTSCircularBuffer::isEmpty()
 {
-    if (available() == 0) {
+    if (bytes == 0) {
         return true;
     } else {
         return false;
@@ -106,7 +106,7 @@
 
 void MTSCircularBuffer::clear()
 {
-    writeIndex = readIndex;
+    writeIndex = readIndex = bytes = 0;
 }
 
 void MTSCircularBuffer::checkThreshold()
@@ -116,27 +116,27 @@
     }
     switch (_op) {
         case Vars::GREATER:
-            if (available() > _threshold) {
+            if (bytes > _threshold) {
                 notify.call();
             }
             break;
         case Vars::LESS:
-            if (available() < _threshold) {
+            if (bytes < _threshold) {
                 notify.call();
             }
             break;
         case Vars::GREATER_EQUAL:
-            if (available() >= _threshold) {
+            if (bytes >= _threshold) {
                 notify.call();
             }
             break;
         case Vars::LESS_EQUAL:
-            if (available() <= _threshold) {
+            if (bytes <= _threshold) {
                 notify.call();
             }
             break;
         case Vars::EQUAL:
-            if (available() == _threshold) {
+            if (bytes == _threshold) {
                 notify.call();
             }
             break;