NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Revision:
2:a4f97773c90f
Parent:
1:abb442332fa8
Child:
5:dd63a1e02b1b
--- a/if/lwip/lwipNetUdpSocket.cpp	Mon Jun 14 10:33:54 2010 +0000
+++ b/if/lwip/lwipNetUdpSocket.cpp	Fri Jun 18 09:22:54 2010 +0000
@@ -30,16 +30,16 @@
 #include "netCfg.h"
 #if NET_LWIP_STACK
 
-LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : m_pPcb(pPcb), m_lInPkt() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
+LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
 {
-  DBG("\r\nNew NetUdpSocket %p\r\n", (void*)this);
+  DBG("\r\nNew LwipNetUdpSocket %p (pPCb=%p)\r\n", (void*)this, (void*) pPcb);
   if(!m_pPcb)
     m_pPcb = udp_new();
- /* if(m_pPcb)
+  if(m_pPcb)
   {
     //Setup callback
-   // udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
-  }*/
+    udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
+  }
 }
 
 LwipNetUdpSocket::~LwipNetUdpSocket()
@@ -51,11 +51,11 @@
 {
   if(!m_pPcb)
     return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
-    
+
   err_t err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
   if(err)
     return NETUDPSOCKET_INUSE;
-    
+
   //Setup callback
   udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
     
@@ -69,12 +69,18 @@
 {
   if( !m_pPcb ) //Pcb doesn't exist (anymore)
     return NETUDPSOCKET_MEM;
-  pbuf* p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
+  pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL);
   if( !p )
     return NETUDPSOCKET_MEM;
-  memcpy (p->payload, (void*)buf, len);  
-  //udp_connect( (udp_pcb*) m_pPcb, &(pHost->getIp().getStruct()), pHost->getPort() );
-  //err_t err = udp_send( (udp_pcb*) m_pPcb, p);
+  char* pBuf = (char*) buf;
+  pbuf* q = p; 
+  do
+  {
+    memcpy (q->payload, (void*)pBuf, q->len);  
+    pBuf += q->len;
+    q = q->next;
+  } while(q != NULL);
+
   err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() );
   pbuf_free( p );
   if(err)
@@ -151,7 +157,7 @@
 
 NetUdpSocketErr LwipNetUdpSocket::close()
 {
-  //DBG("\r\nLwipNetUdpSocket::close() : Closing...\r\n");
+  DBG("\r\nLwipNetUdpSocket::close() : Closing...\r\n");
 
   if(m_closed)
     return NETUDPSOCKET_OK; //Already being closed
@@ -160,11 +166,14 @@
   if( !m_pPcb ) //Pcb doesn't exist (anymore)
     return NETUDPSOCKET_MEM;
     
+  DBG("\r\nLwipNetUdpSocket::close() : Cleanup...\r\n");
+    
   //Cleanup incoming data
   cleanUp();
  
+  DBG("\r\nLwipNetUdpSocket::close() : removing m_pPcb...\r\n");
   udp_remove( (udp_pcb*) m_pPcb);
-  
+    
   m_pPcb = NULL;
   return NETUDPSOCKET_OK;
 }
@@ -186,7 +195,7 @@
     if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) )
     {
       //Let's tail this packet to the previous one
-      pbuf_cat((pbuf*)(*it).pBuf, p);
+      pbuf_cat((pbuf*)((*it).pBuf), p);
       //No need to queue an event in that case since the read buf has not been processed yet
       return;
     }
@@ -204,13 +213,18 @@
 
 void LwipNetUdpSocket::cleanUp() //Flush input buffer
 {
+  //Ensure that further error won't be followed to this inst (which can be destroyed)
+  if( m_pPcb )
+  {
+    udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL );
+  }
+  
   list<InPacket>::iterator it;
   for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
   {
     //Free buf
-    pbuf_free((pbuf*)(*it).pBuf);
+    pbuf_free((pbuf*)((*it).pBuf));
   } 
-  recvfrom(NULL, 0, NULL);
   m_lInPkt.clear();
 }