sandbox / mbed-client-classic

Fork of mbed-client-classic by Christopher Haster

Revision:
5:babbe7026a0c
Parent:
4:0c58f5786538
Child:
6:e61698377f43
--- a/source/m2mconnectionhandlerpimpl.cpp	Thu Mar 03 18:48:23 2016 +0000
+++ b/source/m2mconnectionhandlerpimpl.cpp	Thu Mar 03 19:22:34 2016 +0000
@@ -43,8 +43,9 @@
  _socket(0),
  _listening(false),
  _listen_thread(0),
- _recv_thread(0),
- _send_thread(0)
+ _socket_thread(0)
+ //_recv_thread(0),
+ //_send_thread(0)
 {
     memset(&_address_buffer, 0, sizeof _address_buffer);
     memset(&_address, 0, sizeof _address);
@@ -60,12 +61,9 @@
     }
     
     _running = true;
-    _recv_thread = rtos::create_thread<
+    _socket_thread = rtos::create_thread<
         M2MConnectionHandlerPimpl,
-        &M2MConnectionHandlerPimpl::recv_handler>(this, osPriorityAboveNormal);
-    _send_thread = rtos::create_thread<
-        M2MConnectionHandlerPimpl,
-        &M2MConnectionHandlerPimpl::send_handler>(this, osPriorityAboveNormal);
+        &M2MConnectionHandlerPimpl::socket_handler>(this, osPriorityAboveNormal);
     _listen_thread = rtos::create_thread<
         M2MConnectionHandlerPimpl,
         &M2MConnectionHandlerPimpl::listen_handler>(this, osPriorityAboveNormal);    
@@ -81,14 +79,9 @@
         _listen_thread = 0;
     }
     
-    if (_recv_thread) {
-        delete _recv_thread;
-        _recv_thread = 0;
-    }
-    
-    if (_send_thread) {
-        delete _send_thread;
-        _send_thread = 0;
+    if (_socket_thread) {
+        delete _socket_thread;
+        _socket_thread = 0;
     }
     
     if (_socket) {
@@ -152,38 +145,35 @@
 }
 
 
-void M2MConnectionHandlerPimpl::recv_handler()
+void M2MConnectionHandlerPimpl::socket_handler()
 {
     while (_running) {
-        if (!_socket) {
+        if (!_socket || !_socket->isConnected()) {
             rtos::Thread::wait(1000);
             continue;
         }
         
+        // check recvs
         int size = _socket->recv((char*)_recv_buffer, sizeof _recv_buffer, false);
-        if (size <= 0) {
-            rtos::Thread::wait(200);
+        if (size > 0) {
+            _recv_queue.put(new std::string(_recv_buffer, _recv_buffer+size));
             continue;
         }
-
-        _recv_queue.put(new std::string(_recv_buffer, _recv_buffer+size));
+        
+        // check sends
+        osEvent e = _send_queue.get(0);
+        if (e.status == osEventMessage) {
+            std::string *packet = (std::string*)e.value.p;
+            int size = _socket->send((char*)packet->data(), packet->size());
+            delete packet;
+            continue;
+        }
+        
+        // no activity
+        rtos::Thread::wait(10);
     }
 }
 
-void M2MConnectionHandlerPimpl::send_handler()
-{    
-    while (_running) {
-        osEvent e = _send_queue.get();
-        if (e.status != osEventMessage) {
-            rtos::Thread::wait(200);
-            continue;
-        }
-            
-        std::string *packet = (std::string*)e.value.p;
-        int size = _socket->send((char*)packet->data(), packet->size());
-        delete packet;
-    }
-}
 
 bool M2MConnectionHandlerPimpl::send_data(uint8_t *data,
                                           uint16_t data_len,