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 Jul 11 10:17:25 2013 +0000
Parent:
38:b71e5dd1806a
Child:
40:c8ab0d2bba0b
Commit message:
Fixed read/write routines failing too soon.; Fixed some minor things, removed redundant code.

Changed in this revision

Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Socket/Socket.cpp	Fri Jul 05 12:38:29 2013 +0000
+++ b/Socket/Socket.cpp	Thu Jul 11 10:17:25 2013 +0000
@@ -81,6 +81,14 @@
     return (select(&timeout._time, false, true) == 0 ? -1 : 0);
 }
 
+int Socket::is_readable(void) {
+    return (this->_ep->revents & PICO_SOCK_EV_RD);
+}
+
+int Socket::is_writable(void) {
+    return (this->_ep->revents & PICO_SOCK_EV_WR);
+}
+
 int Socket::close() {
     if (_ep == NULL)
         return -1;
--- a/Socket/Socket.h	Fri Jul 05 12:38:29 2013 +0000
+++ b/Socket/Socket.h	Thu Jul 11 10:17:25 2013 +0000
@@ -88,6 +88,9 @@
     
     int wait_readable(TimeInterval& timeout);
     int wait_writable(TimeInterval& timeout);
+
+    int is_readable(void);
+    int is_writable(void);
     
     bool _blocking;
     unsigned int _timeout;
--- a/Socket/TCPSocketConnection.cpp	Fri Jul 05 12:38:29 2013 +0000
+++ b/Socket/TCPSocketConnection.cpp	Thu Jul 11 10:17:25 2013 +0000
@@ -64,13 +64,21 @@
 
 int TCPSocketConnection::send(char* data, int length) {
     int ret;
-    if ((_ep < 0) || !_is_connected)
+    if ((_ep == NULL) || !_is_connected)
         return -1;
     
+    if(is_writable())
+    {
+        ret = picotcp_write(_ep, data, length);
+        if(ret < length)
+            _ep->revents &= (~PICO_SOCK_EV_WR);
+        if(ret) // data was read or error was reported
+            return ret;
+    }
+    
     TimeInterval timeout(!_blocking ? _timeout : osWaitForever);
     if (wait_writable(timeout) != 0)
     {
-        printf("Failed\n");
         return -1;
     }
     
@@ -84,34 +92,26 @@
 
 // -1 if unsuccessful, else number of bytes written
 int TCPSocketConnection::send_all(char* data, int length) {
-    int ret;
-    if ((_ep < 0) || !_is_connected)
-        return -1;
-        
-    
-    TimeInterval timeout(!_blocking? _timeout : osWaitForever);
-    // Wait for socket to be writeable
-    if (wait_writable(timeout) != 0) {
-       return 0;
-    }
-    ret = picotcp_write(_ep, data, length);
-    if (ret < length) {
-        _ep->revents &= (~PICO_SOCK_EV_WR);
-        //printf("Short write\n");
-    }
-    return ret;
+    return send(data,length);
 }
 
 int TCPSocketConnection::receive(char* data, int length) {
     int ret;
-    if ((_ep < 0) || !_is_connected)
+    if ((_ep == NULL) || !_is_connected)
         return -1;
     
+    if(is_readable())
+    {
+        ret = picotcp_read(_ep, data, length);
+        if(ret<length)
+            _ep->revents &= (~PICO_SOCK_EV_RD);
+        if(ret) // data was read or error was reported
+            return ret;
+    }   
     
     TimeInterval timeout(!_blocking ? _timeout : osWaitForever);
     if (wait_readable(timeout) != 0)
     {
-        printf("Failed receiving\n");
         return -1;
     }
 
--- a/Socket/UDPSocket.cpp	Fri Jul 05 12:38:29 2013 +0000
+++ b/Socket/UDPSocket.cpp	Thu Jul 11 10:17:25 2013 +0000
@@ -86,15 +86,29 @@
 
 // -1 if unsuccessful, else number of bytes received
 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
+    socklen_t remoteHostLen = sizeof(remote._remoteHost);  
+    int ret;
     
     if (!_ep)
         return -1;
     
+    if(is_readable())
+    {
+        ret = picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
+        if(ret < length)
+            _ep->revents &= (~PICO_SOCK_EV_RD);
+        if(ret) // data was read or error was reported
+            return ret;
+    }   
+        
     TimeInterval timeout(!_blocking ? _timeout : osWaitForever);
     if (wait_readable(timeout) != 0)
         return 0;
-    
+
 
-    socklen_t remoteHostLen = sizeof(remote._remoteHost);
-    return picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
+    ret = picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
+    if(ret < length)
+        _ep->revents &= (~PICO_SOCK_EV_RD);
+    
+    return ret;
 }