I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1
tax 0:66300c77c6e9 2 /*
tax 0:66300c77c6e9 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
tax 0:66300c77c6e9 4
tax 0:66300c77c6e9 5 Permission is hereby granted, free of charge, to any person obtaining a copy
tax 0:66300c77c6e9 6 of this software and associated documentation files (the "Software"), to deal
tax 0:66300c77c6e9 7 in the Software without restriction, including without limitation the rights
tax 0:66300c77c6e9 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
tax 0:66300c77c6e9 9 copies of the Software, and to permit persons to whom the Software is
tax 0:66300c77c6e9 10 furnished to do so, subject to the following conditions:
tax 0:66300c77c6e9 11
tax 0:66300c77c6e9 12 The above copyright notice and this permission notice shall be included in
tax 0:66300c77c6e9 13 all copies or substantial portions of the Software.
tax 0:66300c77c6e9 14
tax 0:66300c77c6e9 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tax 0:66300c77c6e9 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tax 0:66300c77c6e9 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
tax 0:66300c77c6e9 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tax 0:66300c77c6e9 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tax 0:66300c77c6e9 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
tax 0:66300c77c6e9 21 THE SOFTWARE.
tax 0:66300c77c6e9 22 */
tax 0:66300c77c6e9 23
tax 0:66300c77c6e9 24 #include "lwip/ip_addr.h"
tax 0:66300c77c6e9 25 #include "lwipNetUdpSocket.h"
tax 0:66300c77c6e9 26 #include "lwip/udp.h"
tax 0:66300c77c6e9 27 #include "lwip/igmp.h"
tax 0:66300c77c6e9 28
tax 0:66300c77c6e9 29
tax 0:66300c77c6e9 30 //#define __DEBUG
tax 0:66300c77c6e9 31 #include "dbg/dbg.h"
tax 0:66300c77c6e9 32
tax 0:66300c77c6e9 33 #include "netCfg.h"
tax 0:66300c77c6e9 34 #if NET_LWIP_STACK
tax 0:66300c77c6e9 35
tax 0:66300c77c6e9 36 LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt(), m_multicastGroup() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
tax 0:66300c77c6e9 37 {
tax 0:66300c77c6e9 38 DBG("New LwipNetUdpSocket %p (pPCb=%p)\n", (void*)this, (void*) pPcb);
tax 0:66300c77c6e9 39 if(!m_pPcb)
tax 0:66300c77c6e9 40 m_pPcb = udp_new();
tax 0:66300c77c6e9 41 if(m_pPcb)
tax 0:66300c77c6e9 42 {
tax 0:66300c77c6e9 43 //Setup callback
tax 0:66300c77c6e9 44 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
tax 0:66300c77c6e9 45 }
tax 0:66300c77c6e9 46 }
tax 0:66300c77c6e9 47
tax 0:66300c77c6e9 48 LwipNetUdpSocket::~LwipNetUdpSocket()
tax 0:66300c77c6e9 49 {
tax 0:66300c77c6e9 50 close();
tax 0:66300c77c6e9 51 }
tax 0:66300c77c6e9 52
tax 0:66300c77c6e9 53 NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me)
tax 0:66300c77c6e9 54 {
tax 0:66300c77c6e9 55 err_t err;
tax 0:66300c77c6e9 56
tax 0:66300c77c6e9 57 if(!m_pPcb)
tax 0:66300c77c6e9 58 return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
tax 0:66300c77c6e9 59
tax 0:66300c77c6e9 60 #if LWIP_IGMP //Multicast support enabled
tax 0:66300c77c6e9 61 if(me.getIp().isMulticast())
tax 0:66300c77c6e9 62 {
tax 0:66300c77c6e9 63 DBG("This is a multicast addr, joining multicast group\n");
tax 0:66300c77c6e9 64 m_multicastGroup = me.getIp();
tax 0:66300c77c6e9 65 err = igmp_joingroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
tax 0:66300c77c6e9 66 if(err)
tax 0:66300c77c6e9 67 return NETUDPSOCKET_IF; //Could not find or create group
tax 0:66300c77c6e9 68 }
tax 0:66300c77c6e9 69 #endif
tax 0:66300c77c6e9 70
tax 0:66300c77c6e9 71 err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
tax 0:66300c77c6e9 72 if(err)
tax 0:66300c77c6e9 73 return NETUDPSOCKET_INUSE;
tax 0:66300c77c6e9 74
tax 0:66300c77c6e9 75 //Setup callback
tax 0:66300c77c6e9 76 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
tax 0:66300c77c6e9 77
tax 0:66300c77c6e9 78 return NETUDPSOCKET_OK;
tax 0:66300c77c6e9 79 }
tax 0:66300c77c6e9 80
tax 0:66300c77c6e9 81 #define MAX(a,b) ((a>b)?a:b)
tax 0:66300c77c6e9 82 #define MIN(a,b) ((a<b)?a:b)
tax 0:66300c77c6e9 83
tax 0:66300c77c6e9 84 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::sendto(const char* buf, int len, Host* pHost)
tax 0:66300c77c6e9 85 {
tax 0:66300c77c6e9 86 if( !m_pPcb ) //Pcb doesn't exist (anymore)
tax 0:66300c77c6e9 87 return NETUDPSOCKET_MEM;
tax 0:66300c77c6e9 88 pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL);
tax 0:66300c77c6e9 89 if( !p )
tax 0:66300c77c6e9 90 return NETUDPSOCKET_MEM;
tax 0:66300c77c6e9 91 char* pBuf = (char*) buf;
tax 0:66300c77c6e9 92 pbuf* q = p;
tax 0:66300c77c6e9 93 do
tax 0:66300c77c6e9 94 {
tax 0:66300c77c6e9 95 memcpy (q->payload, (void*)pBuf, q->len);
tax 0:66300c77c6e9 96 pBuf += q->len;
tax 0:66300c77c6e9 97 q = q->next;
tax 0:66300c77c6e9 98 } while(q != NULL);
tax 0:66300c77c6e9 99
tax 0:66300c77c6e9 100 err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() );
tax 0:66300c77c6e9 101 pbuf_free( p );
tax 0:66300c77c6e9 102 if(err)
tax 0:66300c77c6e9 103 return NETUDPSOCKET_SETUP; //Connection problem
tax 0:66300c77c6e9 104 DBG("%d bytes sent in UDP Socket.\n", len);
tax 0:66300c77c6e9 105 return len;
tax 0:66300c77c6e9 106 }
tax 0:66300c77c6e9 107
tax 0:66300c77c6e9 108 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::recvfrom(char* buf, int len, Host* pHost)
tax 0:66300c77c6e9 109 {
tax 0:66300c77c6e9 110 if( !m_pPcb ) //Pcb doesn't exist (anymore)
tax 0:66300c77c6e9 111 return NETUDPSOCKET_MEM;
tax 0:66300c77c6e9 112 int inLen = 0;
tax 0:66300c77c6e9 113 int cpyLen = 0;
tax 0:66300c77c6e9 114
tax 0:66300c77c6e9 115 static int rmgLen = 0;
tax 0:66300c77c6e9 116 //Contains the remaining len in this pbuf
tax 0:66300c77c6e9 117
tax 0:66300c77c6e9 118 if( m_lInPkt.empty() )
tax 0:66300c77c6e9 119 return 0;
tax 0:66300c77c6e9 120
tax 0:66300c77c6e9 121 pbuf* pBuf = (pbuf*) m_lInPkt.front().pBuf;
tax 0:66300c77c6e9 122
tax 0:66300c77c6e9 123 if(pHost)
tax 0:66300c77c6e9 124 *pHost = Host( IpAddr(&m_lInPkt.front().addr), m_lInPkt.front().port );
tax 0:66300c77c6e9 125
tax 0:66300c77c6e9 126 if( !pBuf )
tax 0:66300c77c6e9 127 {
tax 0:66300c77c6e9 128 rmgLen = 0;
tax 0:66300c77c6e9 129 return 0;
tax 0:66300c77c6e9 130 }
tax 0:66300c77c6e9 131
tax 0:66300c77c6e9 132 if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn
tax 0:66300c77c6e9 133 {
tax 0:66300c77c6e9 134 rmgLen = pBuf->len;
tax 0:66300c77c6e9 135 }
tax 0:66300c77c6e9 136
tax 0:66300c77c6e9 137 while ( inLen < len )
tax 0:66300c77c6e9 138 {
tax 0:66300c77c6e9 139 cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf
tax 0:66300c77c6e9 140 memcpy((void*)buf, (void*)((char*)(pBuf->payload) + (pBuf->len - rmgLen)), cpyLen);
tax 0:66300c77c6e9 141 inLen += cpyLen;
tax 0:66300c77c6e9 142 buf += cpyLen;
tax 0:66300c77c6e9 143
tax 0:66300c77c6e9 144 rmgLen = rmgLen - cpyLen; //Update rmgLen
tax 0:66300c77c6e9 145
tax 0:66300c77c6e9 146 if( rmgLen > 0 )
tax 0:66300c77c6e9 147 {
tax 0:66300c77c6e9 148 //We did not read this pbuf completely, so let's save it's pos & return
tax 0:66300c77c6e9 149 break;
tax 0:66300c77c6e9 150 }
tax 0:66300c77c6e9 151
tax 0:66300c77c6e9 152 if(pBuf->next)
tax 0:66300c77c6e9 153 {
tax 0:66300c77c6e9 154 pbuf* pNextPBuf = pBuf->next;
tax 0:66300c77c6e9 155 pBuf->next = NULL; //So that it is not freed as well
tax 0:66300c77c6e9 156 //We get the reference to pNextPBuf from m_pReadPbuf
tax 0:66300c77c6e9 157 pbuf_free((pbuf*)pBuf);
tax 0:66300c77c6e9 158 pBuf = pNextPBuf;
tax 0:66300c77c6e9 159 rmgLen = pBuf->len;
tax 0:66300c77c6e9 160 }
tax 0:66300c77c6e9 161 else
tax 0:66300c77c6e9 162 {
tax 0:66300c77c6e9 163 pbuf_free((pbuf*)pBuf);
tax 0:66300c77c6e9 164 pBuf = NULL;
tax 0:66300c77c6e9 165 rmgLen = 0;
tax 0:66300c77c6e9 166 m_lInPkt.pop_front();
tax 0:66300c77c6e9 167 break; //No more data to read
tax 0:66300c77c6e9 168 }
tax 0:66300c77c6e9 169 }
tax 0:66300c77c6e9 170
tax 0:66300c77c6e9 171 return inLen;
tax 0:66300c77c6e9 172 }
tax 0:66300c77c6e9 173
tax 0:66300c77c6e9 174 NetUdpSocketErr LwipNetUdpSocket::close()
tax 0:66300c77c6e9 175 {
tax 0:66300c77c6e9 176 DBG("LwipNetUdpSocket::close() : Closing...\n");
tax 0:66300c77c6e9 177
tax 0:66300c77c6e9 178 if(m_closed)
tax 0:66300c77c6e9 179 return NETUDPSOCKET_OK; //Already being closed
tax 0:66300c77c6e9 180 m_closed = true;
tax 0:66300c77c6e9 181
tax 0:66300c77c6e9 182 if( !m_pPcb ) //Pcb doesn't exist (anymore)
tax 0:66300c77c6e9 183 return NETUDPSOCKET_MEM;
tax 0:66300c77c6e9 184
tax 0:66300c77c6e9 185 DBG("LwipNetUdpSocket::close() : Cleanup...\n");
tax 0:66300c77c6e9 186
tax 0:66300c77c6e9 187 //Cleanup incoming data
tax 0:66300c77c6e9 188 cleanUp();
tax 0:66300c77c6e9 189
tax 0:66300c77c6e9 190
tax 0:66300c77c6e9 191 DBG("LwipNetUdpSocket::close() : removing m_pPcb...\n");
tax 0:66300c77c6e9 192 udp_remove( (udp_pcb*) m_pPcb);
tax 0:66300c77c6e9 193
tax 0:66300c77c6e9 194 m_pPcb = NULL;
tax 0:66300c77c6e9 195 return NETUDPSOCKET_OK;
tax 0:66300c77c6e9 196 }
tax 0:66300c77c6e9 197
tax 0:66300c77c6e9 198 NetUdpSocketErr LwipNetUdpSocket::poll()
tax 0:66300c77c6e9 199 {
tax 0:66300c77c6e9 200 NetUdpSocket::flushEvents();
tax 0:66300c77c6e9 201 return NETUDPSOCKET_OK;
tax 0:66300c77c6e9 202 }
tax 0:66300c77c6e9 203
tax 0:66300c77c6e9 204 // Callbacks events
tax 0:66300c77c6e9 205
tax 0:66300c77c6e9 206 void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port)
tax 0:66300c77c6e9 207 {
tax 0:66300c77c6e9 208 DBG(" Packet of length %d arrived in UDP Socket.\n", p->tot_len);
tax 0:66300c77c6e9 209 list<InPacket>::iterator it;
tax 0:66300c77c6e9 210 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
tax 0:66300c77c6e9 211 {
tax 0:66300c77c6e9 212 if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) )
tax 0:66300c77c6e9 213 {
tax 0:66300c77c6e9 214 //Let's tail this packet to the previous one
tax 0:66300c77c6e9 215 pbuf_cat((pbuf*)((*it).pBuf), p);
tax 0:66300c77c6e9 216 //No need to queue an event in that case since the read buf has not been processed yet
tax 0:66300c77c6e9 217 return;
tax 0:66300c77c6e9 218 }
tax 0:66300c77c6e9 219 }
tax 0:66300c77c6e9 220
tax 0:66300c77c6e9 221 //New host, add a packet to the queue
tax 0:66300c77c6e9 222 InPacket pkt;
tax 0:66300c77c6e9 223 pkt.pBuf = p;
tax 0:66300c77c6e9 224 pkt.addr = *addr;
tax 0:66300c77c6e9 225 pkt.port = port;
tax 0:66300c77c6e9 226 m_lInPkt.push_back(pkt);
tax 0:66300c77c6e9 227
tax 0:66300c77c6e9 228 queueEvent(NETUDPSOCKET_READABLE);
tax 0:66300c77c6e9 229 }
tax 0:66300c77c6e9 230
tax 0:66300c77c6e9 231 void LwipNetUdpSocket::cleanUp() //Flush input buffer
tax 0:66300c77c6e9 232 {
tax 0:66300c77c6e9 233 //Ensure that further error won't be followed to this inst (which can be destroyed)
tax 0:66300c77c6e9 234 if( m_pPcb )
tax 0:66300c77c6e9 235 {
tax 0:66300c77c6e9 236 udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL );
tax 0:66300c77c6e9 237 }
tax 0:66300c77c6e9 238
tax 0:66300c77c6e9 239 //Leaving multicast group(Ok because LwIP has a refscount for multicast group)
tax 0:66300c77c6e9 240 #if LWIP_IGMP //Multicast support enabled
tax 0:66300c77c6e9 241 if(m_multicastGroup.isMulticast())
tax 0:66300c77c6e9 242 {
tax 0:66300c77c6e9 243 igmp_leavegroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
tax 0:66300c77c6e9 244 m_multicastGroup = IpAddr();
tax 0:66300c77c6e9 245 }
tax 0:66300c77c6e9 246 #endif
tax 0:66300c77c6e9 247
tax 0:66300c77c6e9 248 list<InPacket>::iterator it;
tax 0:66300c77c6e9 249 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
tax 0:66300c77c6e9 250 {
tax 0:66300c77c6e9 251 //Free buf
tax 0:66300c77c6e9 252 pbuf_free((pbuf*)((*it).pBuf));
tax 0:66300c77c6e9 253 }
tax 0:66300c77c6e9 254 m_lInPkt.clear();
tax 0:66300c77c6e9 255 }
tax 0:66300c77c6e9 256
tax 0:66300c77c6e9 257 // Static callback from LwIp
tax 0:66300c77c6e9 258
tax 0:66300c77c6e9 259 void LwipNetUdpSocket::sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
tax 0:66300c77c6e9 260 {
tax 0:66300c77c6e9 261 LwipNetUdpSocket* pMe = (LwipNetUdpSocket*) arg;
tax 0:66300c77c6e9 262 return pMe->recvCb( pcb, p, addr, port );
tax 0:66300c77c6e9 263 }
tax 0:66300c77c6e9 264
tax 0:66300c77c6e9 265 #endif