Free (GPLv2) TCP/IP stack developed by TASS Belgium

Fork of PicoTCP by Daniele Lacamera

Files at this revision

API Documentation at this revision

Comitter:
tass
Date:
Thu Jun 06 13:51:27 2013 +0000
Parent:
14:fe225a6f92c0
Child:
17:5292cca5c478
Commit message:
added rtosTimer

Changed in this revision

Socket/pico_bsd_layer.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Socket/pico_bsd_layer.cpp	Thu Jun 06 09:27:28 2013 +0000
+++ b/Socket/pico_bsd_layer.cpp	Thu Jun 06 13:51:27 2013 +0000
@@ -86,13 +86,16 @@
 {
     int retValue = 0;
     
-    if(sock->blocking)
+    //if(sock->blocking)
     {
         if(EVENT_PENDING(sock->revent,event))
         {
-            //printf("Already...\n");
+            RESET_EVENT(sock->event, event);
+            
+            if(event & PICO_SOCK_EV_RD)
+                RESET_EVENT(event,PICO_SOCK_EV_RD);
+                
             RESET_EVENT(sock->revent, event);
-            RESET_EVENT(sock->event, event);
         }
         else
         {
@@ -437,7 +440,11 @@
         r = pico_socket_read(_sock->sock, (unsigned char *)buf + tot_len, len - tot_len); 
         if (r > 0)
             tot_len += r;
-        else break;
+        else
+        { 
+            RESET_EVENT(_sock->revent,PICO_SOCK_EV_RD);
+            break;
+        }
     }
     globalLock.unlock();
     return tot_len;
@@ -485,51 +492,52 @@
     return pico_socket_getoption(_sock->sock,option,value);
 }
 
+void timeoutCb(void const *s) 
+{
+    struct socket * _sock = (struct socket *)s;
+    _sock->mutex->unlock();
+}
+
+
 int picotcp_select(int sock, struct timeval *timeout, int read, int write)
 {
     uint32_t ms = 1000 * timeout->tv_sec + timeout->tv_usec / 1000;
     uint32_t now = PICO_TIME_MS();
-    int ret = 0;
-    
+    uint16_t event = 0;    
     struct socket * _sock =  findAfterId(sock);
-    VALIDATE_NULL(_sock);
     
-    // tell the dispatcher what events are you waiting for
-    if(read)
-        SET_EVENT(_sock->event,PICO_SOCK_EV_RD|PICO_SOCK_EV_CONN);
-    if(write)
-        SET_EVENT(_sock->event,PICO_SOCK_EV_WR);
-        
-    while (PICO_TIME_MS() < now + ms) {
-        
-        if (_sock->mutex->lock(1) == false) {
-            //Thread::wait(1);
-            continue;
-        }
-             
-        if (read && EVENT_PENDING(_sock->revent,PICO_SOCK_EV_RD|PICO_SOCK_EV_CONN))
-        {
-            RESET_EVENT(_sock->revent,PICO_SOCK_EV_CONN|PICO_SOCK_EV_RD);
-            ret++;
-        }
-        if (write && EVENT_PENDING(_sock->revent, PICO_SOCK_EV_WR))
-        {
-            RESET_EVENT(_sock->revent,PICO_SOCK_EV_WR);
-            ret++;
-        }
-        if (ret > 0)
-            break;
-        
-        
-    }
-    
-    if(read)
-        RESET_EVENT(_sock->event,PICO_SOCK_EV_CONN|PICO_SOCK_EV_RD);
-    if(write)
-        RESET_EVENT(_sock->event,PICO_SOCK_EV_WR);
+    if(!_sock)
+        return -1;
 
     
-    return ret;
+    while(PICO_TIME_MS() < ms + now)
+    {
+        int events = 0;
+        RtosTimer tioTmr(timeoutCb, osTimerPeriodic, (void*)_sock);
+        
+        if(read)
+            event |= PICO_SOCK_EV_RD;
+        if(write)
+            event |= PICO_SOCK_EV_WR;    
+            
+        waitForEvent(_sock,event,0);
+        
+        if(read && EVENT_PENDING(_sock->revent,PICO_SOCK_EV_RD))
+        {
+            events++;
+        }
+        
+        if(write && EVENT_PENDING(_sock->revent,PICO_SOCK_EV_WR))
+        {
+            events++;
+            RESET_EVENT(_sock->revent,PICO_SOCK_EV_WR);
+        }
+        
+        if(events>0)
+            return events;
+           
+    }
+    return 0;
 }
 
 int picotcp_setblocking(int sock, int blocking)