My fork

Dependencies:   WncControllerK64F

Fork of WNCInterface by Jan Korycan

Revision:
7:fded23f50479
Parent:
1:e511ea8d39d5
Child:
9:9f0578ff157a
--- a/Socket/TCPSocketConnection.cpp	Wed Sep 28 00:17:57 2016 +0000
+++ b/Socket/TCPSocketConnection.cpp	Thu Oct 06 21:17:18 2016 +0000
@@ -38,9 +38,10 @@
 
     if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
         FATAL_WNC_ERROR(void);
- 
+    M_LOCK;
     WNCInterface::_pwnc->setReadRetryWait(0, 0);
     WNCInterface::_pwnc->setReadRetries(0, 0);
+    M_ULOCK;
 }
 
 
@@ -55,18 +56,20 @@
 }
 
 int TCPSocketConnection::send(char* data, int length) {
+    int ret = -1;
+    
     WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
 
     if( s == FATAL_FLAG )
         FATAL_WNC_ERROR(fail);
  
-    if( s != WncController_fk::WncController::WNC_ON )
-        return -1;
-
-    if( WNCInterface::_pwnc->write(0, data, length) )
-        return length;
-    else
-        return -1;
+    if( s == WncController_fk::WncController::WNC_ON ) {
+      M_LOCK;
+      if( WNCInterface::_pwnc->write(0, data, length) )
+        ret = length;
+      M_ULOCK;
+      }
+    return ret;
 }
 
 int TCPSocketConnection::receive(char *readBuf, int length) {
@@ -83,6 +86,7 @@
         return ret;
 
     t.start();
+    M_LOCK;
     do {
         cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
         if( _is_blocking )
@@ -92,7 +96,8 @@
         }
     while( !done );
     t.stop();
-
+    M_ULOCK;
+    
     if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) {
         readBuf[cnt] = '\0';
         ret = (int)cnt;
@@ -113,6 +118,9 @@
 
 int TCPSocketConnection::close(void) {
   Socket::disconnect();
-  return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
+  M_LOCK;
+  int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
+  M_ULOCK;
+  return ret;
 }