For bug fixes

Dependencies:   HTTPClient-SSL

Dependents:   mtsas

Fork of MTS-Socket by MultiTech

Revision:
8:a3b41ec82e63
Parent:
2:ebc6129de4e8
Child:
16:dbe80ac199f5
--- a/TCPSocketConnection.cpp	Tue Jun 03 14:49:26 2014 +0000
+++ b/TCPSocketConnection.cpp	Mon Jun 16 14:05:19 2014 +0000
@@ -2,6 +2,10 @@
 #include "TCPSocketConnection.h"
 #include <algorithm>
 
+#include "MTSLog.h"
+
+using namespace mts;
+
 TCPSocketConnection::TCPSocketConnection()
 {
 }
@@ -50,27 +54,24 @@
 int TCPSocketConnection::receive(char* data, int length)
 {
     Timer tmr;
-    int time = -1;
-
-    if (!_blocking) {
+    int bytes = 0;
+    int totalbytes = 0;
+    
+    if (_blocking) {
+        return ip->read(data, length, _timeout);
+    } else {
         tmr.start();
-        while (time < _timeout + 20) {
-            if (ip->readable()) {
-                break;
+        do {
+            bytes = ip->read(data + totalbytes, length - totalbytes, 250);
+            if (bytes < 0) {
+                return -1;
             }
-            time = tmr.read_ms();
-        }
-        if (time >= _timeout + 20) {
-            return -1;
-        }
-    } else {
-        while(!ip->readable());
+            totalbytes += bytes;
+        } while ( tmr.read() < _timeout && totalbytes < length);
+        return totalbytes;
     }
-
-    return ip->read(data, length, 0);
 }
 
-
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive_all(char* data, int length)
 {