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 "lwipNetTcpSocket.h"
tax 0:66300c77c6e9 25 #include "lwip/tcp.h"
tax 0:66300c77c6e9 26
tax 0:66300c77c6e9 27 //#define __DEBUG
tax 0:66300c77c6e9 28 #include "dbg/dbg.h"
tax 0:66300c77c6e9 29
tax 0:66300c77c6e9 30 #include "netCfg.h"
tax 0:66300c77c6e9 31 #if NET_LWIP_STACK
tax 0:66300c77c6e9 32
tax 0:66300c77c6e9 33 LwipNetTcpSocket::LwipNetTcpSocket(tcp_pcb* pPcb /*= NULL*/) : NetTcpSocket(), m_pPcb(pPcb), m_lpInNetTcpSocket(), //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
tax 0:66300c77c6e9 34 m_pReadPbuf(NULL)
tax 0:66300c77c6e9 35 {
tax 0:66300c77c6e9 36 DBG("New NetTcpSocket %p\n", (void*)this);
tax 0:66300c77c6e9 37 if(!m_pPcb)
tax 0:66300c77c6e9 38 {
tax 0:66300c77c6e9 39 m_pPcb = tcp_new();
tax 0:66300c77c6e9 40 DBG("Creating new PCB %p\n", m_pPcb);
tax 0:66300c77c6e9 41 }
tax 0:66300c77c6e9 42 if(m_pPcb)
tax 0:66300c77c6e9 43 {
tax 0:66300c77c6e9 44 //Setup callbacks
tax 0:66300c77c6e9 45 tcp_arg( (tcp_pcb*) m_pPcb, (void*) this ); //this will be passed to each static callback
tax 0:66300c77c6e9 46
tax 0:66300c77c6e9 47 tcp_recv( (tcp_pcb*) m_pPcb, LwipNetTcpSocket::sRecvCb );
tax 0:66300c77c6e9 48 tcp_sent((tcp_pcb*) m_pPcb, LwipNetTcpSocket::sSentCb );
tax 0:66300c77c6e9 49 tcp_err( (tcp_pcb*) m_pPcb, LwipNetTcpSocket::sErrCb );
tax 0:66300c77c6e9 50 //Connected callback is defined in connect()
tax 0:66300c77c6e9 51 //Accept callback is defined in listen()
tax 0:66300c77c6e9 52 DBG("NetTcpSocket created.\n");
tax 0:66300c77c6e9 53 }
tax 0:66300c77c6e9 54 }
tax 0:66300c77c6e9 55
tax 0:66300c77c6e9 56 LwipNetTcpSocket::~LwipNetTcpSocket()
tax 0:66300c77c6e9 57 {
tax 0:66300c77c6e9 58 /* if(m_pPcb)
tax 0:66300c77c6e9 59 tcp_close( (tcp_pcb*) m_pPcb); //Disconnect & free pcb*/
tax 0:66300c77c6e9 60 close();
tax 0:66300c77c6e9 61 }
tax 0:66300c77c6e9 62
tax 0:66300c77c6e9 63 NetTcpSocketErr LwipNetTcpSocket::bind(const Host& me)
tax 0:66300c77c6e9 64 {
tax 0:66300c77c6e9 65 if(!m_pPcb)
tax 0:66300c77c6e9 66 return NETTCPSOCKET_MEM; //NetTcpSocket was not properly initialised, should destroy it & retry
tax 0:66300c77c6e9 67
tax 0:66300c77c6e9 68 err_t err = tcp_bind( (tcp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
tax 0:66300c77c6e9 69 if(err)
tax 0:66300c77c6e9 70 return NETTCPSOCKET_INUSE;
tax 0:66300c77c6e9 71
tax 0:66300c77c6e9 72 return NETTCPSOCKET_OK;
tax 0:66300c77c6e9 73 }
tax 0:66300c77c6e9 74
tax 0:66300c77c6e9 75 NetTcpSocketErr LwipNetTcpSocket::listen()
tax 0:66300c77c6e9 76 {
tax 0:66300c77c6e9 77 if(!m_pPcb)
tax 0:66300c77c6e9 78 return NETTCPSOCKET_MEM; //NetTcpSocket was not properly initialised, should destroy it & retry
tax 0:66300c77c6e9 79 /*
tax 0:66300c77c6e9 80 From doc/rawapi.txt :
tax 0:66300c77c6e9 81
tax 0:66300c77c6e9 82 The tcp_listen() function returns a new connection identifier, and
tax 0:66300c77c6e9 83 the one passed as an argument to the function will be
tax 0:66300c77c6e9 84 deallocated. The reason for this behavior is that less memory is
tax 0:66300c77c6e9 85 needed for a connection that is listening, so tcp_listen() will
tax 0:66300c77c6e9 86 reclaim the memory needed for the original connection and allocate a
tax 0:66300c77c6e9 87 new smaller memory block for the listening connection.
tax 0:66300c77c6e9 88 */
tax 0:66300c77c6e9 89
tax 0:66300c77c6e9 90 // tcp_pcb* pNewPcb = tcp_listen(m_pPcb);
tax 0:66300c77c6e9 91 tcp_pcb* pNewPcb = tcp_listen_with_backlog((tcp_pcb*)m_pPcb, 5);
tax 0:66300c77c6e9 92 if( !pNewPcb ) //Not enough memory to create the listening pcb
tax 0:66300c77c6e9 93 return NETTCPSOCKET_MEM;
tax 0:66300c77c6e9 94
tax 0:66300c77c6e9 95 m_pPcb = pNewPcb;
tax 0:66300c77c6e9 96
tax 0:66300c77c6e9 97 tcp_accept( (tcp_pcb*) m_pPcb, LwipNetTcpSocket::sAcceptCb );
tax 0:66300c77c6e9 98
tax 0:66300c77c6e9 99 return NETTCPSOCKET_OK;
tax 0:66300c77c6e9 100 }
tax 0:66300c77c6e9 101
tax 0:66300c77c6e9 102 NetTcpSocketErr LwipNetTcpSocket::connect(const Host& host)
tax 0:66300c77c6e9 103 {
tax 0:66300c77c6e9 104 if(!m_pPcb)
tax 0:66300c77c6e9 105 return NETTCPSOCKET_MEM; //NetTcpSocket was not properly initialised, should destroy it & retry
tax 0:66300c77c6e9 106
tax 0:66300c77c6e9 107 ip_addr_t ip = host.getIp().getStruct();
tax 0:66300c77c6e9 108 err_t err = tcp_connect( (tcp_pcb*) m_pPcb, &ip, host.getPort(), LwipNetTcpSocket::sConnectedCb );
tax 0:66300c77c6e9 109
tax 0:66300c77c6e9 110 if(err)
tax 0:66300c77c6e9 111 return NETTCPSOCKET_MEM;
tax 0:66300c77c6e9 112
tax 0:66300c77c6e9 113 return NETTCPSOCKET_OK;
tax 0:66300c77c6e9 114 }
tax 0:66300c77c6e9 115
tax 0:66300c77c6e9 116 NetTcpSocketErr LwipNetTcpSocket::accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket)
tax 0:66300c77c6e9 117 {
tax 0:66300c77c6e9 118 if( !m_pPcb ) //Pcb doesn't exist (anymore)
tax 0:66300c77c6e9 119 return NETTCPSOCKET_MEM;
tax 0:66300c77c6e9 120 //Dequeue a connection
tax 0:66300c77c6e9 121 //if( m_lpInPcb.empty() )
tax 0:66300c77c6e9 122 if( m_lpInNetTcpSocket.empty() )
tax 0:66300c77c6e9 123 return NETTCPSOCKET_EMPTY;
tax 0:66300c77c6e9 124
tax 0:66300c77c6e9 125 tcp_accepted( ((tcp_pcb*) m_pPcb) ); //Should fire proper events //WARN: m_pPcb is the GOOD param here (and not pInPcb)
tax 0:66300c77c6e9 126
tax 0:66300c77c6e9 127 /* tcp_pcb* pInPcb = m_lpInPcb.front();
tax 0:66300c77c6e9 128 m_lpInPcb.pop();*/
tax 0:66300c77c6e9 129
tax 0:66300c77c6e9 130 if( (m_lpInNetTcpSocket.front()) == NULL )
tax 0:66300c77c6e9 131 {
tax 0:66300c77c6e9 132 m_lpInNetTcpSocket.pop();
tax 0:66300c77c6e9 133 return NETTCPSOCKET_RST;
tax 0:66300c77c6e9 134 }
tax 0:66300c77c6e9 135
tax 0:66300c77c6e9 136 if( (m_lpInNetTcpSocket.front())->m_closed )
tax 0:66300c77c6e9 137 {
tax 0:66300c77c6e9 138 Net::releaseTcpSocket(m_lpInNetTcpSocket.front());
tax 0:66300c77c6e9 139 m_lpInNetTcpSocket.pop();
tax 0:66300c77c6e9 140 return NETTCPSOCKET_RST;
tax 0:66300c77c6e9 141 }
tax 0:66300c77c6e9 142
tax 0:66300c77c6e9 143 ip_addr_t* ip = (ip_addr_t*) &( (m_lpInNetTcpSocket.front()->m_pPcb)->remote_ip);
tax 0:66300c77c6e9 144
tax 0:66300c77c6e9 145 *ppNewNetTcpSocket = m_lpInNetTcpSocket.front();
tax 0:66300c77c6e9 146 *pClient = Host(
tax 0:66300c77c6e9 147 IpAddr(
tax 0:66300c77c6e9 148 ip
tax 0:66300c77c6e9 149 ),
tax 0:66300c77c6e9 150 m_lpInNetTcpSocket.front()->m_pPcb->remote_port
tax 0:66300c77c6e9 151 );
tax 0:66300c77c6e9 152 m_lpInNetTcpSocket.pop();
tax 0:66300c77c6e9 153 // *pClient = Host( IpAddr(pInPcb->remote_ip), pInPcb->remote_port );
tax 0:66300c77c6e9 154
tax 0:66300c77c6e9 155 //Return a new socket
tax 0:66300c77c6e9 156 // *ppNewNetTcpSocket = (NetTcpSocket*) new LwipNetTcpSocket(pInPcb);
tax 0:66300c77c6e9 157
tax 0:66300c77c6e9 158 //tcp_accepted( ((tcp_pcb*) m_pPcb) ); //Should fire proper events //WARN: m_pPcb is the GOOD param here (and not pInPcb)
tax 0:66300c77c6e9 159
tax 0:66300c77c6e9 160 /* if(*ppNewNetTcpSocket == NULL)
tax 0:66300c77c6e9 161 {
tax 0:66300c77c6e9 162 DBG("Not enough mem, socket dropped in LwipNetTcpSocket::accept.\n");
tax 0:66300c77c6e9 163 tcp_abort(pInPcb);
tax 0:66300c77c6e9 164 }*/
tax 0:66300c77c6e9 165
tax 0:66300c77c6e9 166 return NETTCPSOCKET_OK;
tax 0:66300c77c6e9 167 }
tax 0:66300c77c6e9 168
tax 0:66300c77c6e9 169 #define MAX(a,b) (((a)>(b))?(a):(b))
tax 0:66300c77c6e9 170 #define MIN(a,b) (((a)<(b))?(a):(b))
tax 0:66300c77c6e9 171
tax 0:66300c77c6e9 172 int /*if < 0 : NetTcpSocketErr*/ LwipNetTcpSocket::send(const char* buf, int len)
tax 0:66300c77c6e9 173 {
tax 0:66300c77c6e9 174 if( !m_pPcb ) //Pcb doesn't exist (anymore)
tax 0:66300c77c6e9 175 return NETTCPSOCKET_MEM;
tax 0:66300c77c6e9 176 int outLen = MIN( len, tcp_sndbuf( (tcp_pcb*) m_pPcb) );
tax 0:66300c77c6e9 177 //tcp_sndbuf() returns the number of bytes available in the output queue, so never go above it
tax 0:66300c77c6e9 178 err_t err = tcp_write( (tcp_pcb*) m_pPcb, (void*) buf, outLen, TCP_WRITE_FLAG_COPY );
tax 0:66300c77c6e9 179 //Flags are TCP_WRITE_FLAG_COPY & TCP_WRITE_FLAG_MORE (see tcp_out.c) :
tax 0:66300c77c6e9 180 //If TCP_WRITE_FLAG_MORE is not set ask client to push buffered data to app
tax 0:66300c77c6e9 181 if(err)
tax 0:66300c77c6e9 182 {
tax 0:66300c77c6e9 183 switch( err )
tax 0:66300c77c6e9 184 {
tax 0:66300c77c6e9 185 case ERR_CONN:
tax 0:66300c77c6e9 186 return (int) NETTCPSOCKET_SETUP; //Not connected properly
tax 0:66300c77c6e9 187 case ERR_ARG:
tax 0:66300c77c6e9 188 return (int) NETTCPSOCKET_SETUP; //Wrong args ! (like buf pointing to NULL)
tax 0:66300c77c6e9 189 case ERR_MEM:
tax 0:66300c77c6e9 190 default:
tax 0:66300c77c6e9 191 return (int) NETTCPSOCKET_MEM; //Not enough memory
tax 0:66300c77c6e9 192 }
tax 0:66300c77c6e9 193 }
tax 0:66300c77c6e9 194 return outLen;
tax 0:66300c77c6e9 195 }
tax 0:66300c77c6e9 196
tax 0:66300c77c6e9 197 int /*if < 0 : NetTcpSocketErr*/ LwipNetTcpSocket::recv(char* buf, int len)
tax 0:66300c77c6e9 198 {
tax 0:66300c77c6e9 199 if( !m_pPcb ) //Pcb doesn't exist (anymore)
tax 0:66300c77c6e9 200 return NETTCPSOCKET_MEM;
tax 0:66300c77c6e9 201 int inLen = 0;
tax 0:66300c77c6e9 202 int cpyLen = 0;
tax 0:66300c77c6e9 203
tax 0:66300c77c6e9 204 static int rmgLen = 0;
tax 0:66300c77c6e9 205 //Contains the remaining len in this pbuf
tax 0:66300c77c6e9 206
tax 0:66300c77c6e9 207 if( !m_pReadPbuf )
tax 0:66300c77c6e9 208 {
tax 0:66300c77c6e9 209 rmgLen = 0;
tax 0:66300c77c6e9 210 return 0;
tax 0:66300c77c6e9 211 }
tax 0:66300c77c6e9 212
tax 0:66300c77c6e9 213 if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn
tax 0:66300c77c6e9 214 {
tax 0:66300c77c6e9 215 rmgLen = m_pReadPbuf->len;
tax 0:66300c77c6e9 216 }
tax 0:66300c77c6e9 217
tax 0:66300c77c6e9 218 while ( inLen < len )
tax 0:66300c77c6e9 219 {
tax 0:66300c77c6e9 220 cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf
tax 0:66300c77c6e9 221 memcpy((void*)buf, (void*)((char*)(m_pReadPbuf->payload) + (m_pReadPbuf->len - rmgLen)), cpyLen);
tax 0:66300c77c6e9 222 inLen += cpyLen;
tax 0:66300c77c6e9 223 buf += cpyLen;
tax 0:66300c77c6e9 224
tax 0:66300c77c6e9 225 rmgLen = rmgLen - cpyLen; //Update rmgLen
tax 0:66300c77c6e9 226
tax 0:66300c77c6e9 227 if( rmgLen > 0 )
tax 0:66300c77c6e9 228 {
tax 0:66300c77c6e9 229 //We did not read this pbuf completely, so let's save it's pos & return
tax 0:66300c77c6e9 230 break;
tax 0:66300c77c6e9 231 }
tax 0:66300c77c6e9 232
tax 0:66300c77c6e9 233 if(m_pReadPbuf->next)
tax 0:66300c77c6e9 234 {
tax 0:66300c77c6e9 235 pbuf* pNextPBuf = m_pReadPbuf->next;
tax 0:66300c77c6e9 236 m_pReadPbuf->next = NULL; //So that it is not freed as well
tax 0:66300c77c6e9 237 //We get the reference to pNextPBuf from m_pReadPbuf
tax 0:66300c77c6e9 238 pbuf_free((pbuf*)m_pReadPbuf);
tax 0:66300c77c6e9 239 m_pReadPbuf = pNextPBuf;
tax 0:66300c77c6e9 240 rmgLen = m_pReadPbuf->len;
tax 0:66300c77c6e9 241 }
tax 0:66300c77c6e9 242 else
tax 0:66300c77c6e9 243 {
tax 0:66300c77c6e9 244 pbuf_free((pbuf*)m_pReadPbuf);
tax 0:66300c77c6e9 245 m_pReadPbuf = NULL;
tax 0:66300c77c6e9 246 rmgLen = 0;
tax 0:66300c77c6e9 247 break; //No more data to read
tax 0:66300c77c6e9 248 }
tax 0:66300c77c6e9 249
tax 0:66300c77c6e9 250 }
tax 0:66300c77c6e9 251
tax 0:66300c77c6e9 252 //tcp_recved(m_pPcb, inLen); //Acknowledge the reception
tax 0:66300c77c6e9 253
tax 0:66300c77c6e9 254 return inLen;
tax 0:66300c77c6e9 255 }
tax 0:66300c77c6e9 256
tax 0:66300c77c6e9 257 NetTcpSocketErr LwipNetTcpSocket::close()
tax 0:66300c77c6e9 258 {
tax 0:66300c77c6e9 259 //DBG("LwipNetTcpSocket::close() : Closing...\n");
tax 0:66300c77c6e9 260
tax 0:66300c77c6e9 261 if(m_closed)
tax 0:66300c77c6e9 262 return NETTCPSOCKET_OK; //Already being closed
tax 0:66300c77c6e9 263 m_closed = true;
tax 0:66300c77c6e9 264
tax 0:66300c77c6e9 265 if( !m_pPcb ) //Pcb doesn't exist (anymore)
tax 0:66300c77c6e9 266 return NETTCPSOCKET_MEM;
tax 0:66300c77c6e9 267
tax 0:66300c77c6e9 268 //Cleanup incoming data
tax 0:66300c77c6e9 269 cleanUp();
tax 0:66300c77c6e9 270
tax 0:66300c77c6e9 271 if( !!tcp_close( (tcp_pcb*) m_pPcb) )
tax 0:66300c77c6e9 272 {
tax 0:66300c77c6e9 273 DBG("LwipNetTcpSocket::close() could not close properly, abort.\n");
tax 0:66300c77c6e9 274 tcp_abort( (tcp_pcb*) m_pPcb);
tax 0:66300c77c6e9 275 m_pPcb = NULL;
tax 0:66300c77c6e9 276 return NETTCPSOCKET_MEM;
tax 0:66300c77c6e9 277 }
tax 0:66300c77c6e9 278
tax 0:66300c77c6e9 279 DBG("LwipNetTcpSocket::close() : connection closed successfully.\n");
tax 0:66300c77c6e9 280
tax 0:66300c77c6e9 281 m_pPcb = NULL;
tax 0:66300c77c6e9 282 return NETTCPSOCKET_OK;
tax 0:66300c77c6e9 283 }
tax 0:66300c77c6e9 284
tax 0:66300c77c6e9 285 NetTcpSocketErr LwipNetTcpSocket::poll()
tax 0:66300c77c6e9 286 {
tax 0:66300c77c6e9 287 NetTcpSocket::flushEvents();
tax 0:66300c77c6e9 288 return NETTCPSOCKET_OK;
tax 0:66300c77c6e9 289 }
tax 0:66300c77c6e9 290
tax 0:66300c77c6e9 291 // Callbacks events
tax 0:66300c77c6e9 292
tax 0:66300c77c6e9 293 err_t LwipNetTcpSocket::acceptCb(struct tcp_pcb *newpcb, err_t err)
tax 0:66300c77c6e9 294 {
tax 0:66300c77c6e9 295 if(err)
tax 0:66300c77c6e9 296 {
tax 0:66300c77c6e9 297 DBG("Error %d in LwipNetTcpSocket::acceptCb.\n", err);
tax 0:66300c77c6e9 298 return err;
tax 0:66300c77c6e9 299 }
tax 0:66300c77c6e9 300 //FIXME: MEM Errs
tax 0:66300c77c6e9 301 //m_lpInPcb.push(newpcb); //Add connection to the queue
tax 0:66300c77c6e9 302 LwipNetTcpSocket* pNewNetTcpSocket = new LwipNetTcpSocket(newpcb);
tax 0:66300c77c6e9 303
tax 0:66300c77c6e9 304 if(pNewNetTcpSocket == NULL)
tax 0:66300c77c6e9 305 {
tax 0:66300c77c6e9 306 DBG("Not enough mem, socket dropped in LwipNetTcpSocket::acceptCb.\n");
tax 0:66300c77c6e9 307 tcp_abort(newpcb);
tax 0:66300c77c6e9 308 return ERR_ABRT;
tax 0:66300c77c6e9 309 }
tax 0:66300c77c6e9 310
tax 0:66300c77c6e9 311 pNewNetTcpSocket->m_refs++;
tax 0:66300c77c6e9 312 m_lpInNetTcpSocket.push( pNewNetTcpSocket );
tax 0:66300c77c6e9 313
tax 0:66300c77c6e9 314 // tcp_accepted(newpcb);
tax 0:66300c77c6e9 315 // tcp_accepted( m_pPcb ); //Should fire proper events //WARN: m_pPcb is the GOOD param here (and not pInPcb)
tax 0:66300c77c6e9 316 queueEvent(NETTCPSOCKET_ACCEPT);
tax 0:66300c77c6e9 317 return ERR_OK;
tax 0:66300c77c6e9 318 }
tax 0:66300c77c6e9 319
tax 0:66300c77c6e9 320 err_t LwipNetTcpSocket::connectedCb(struct tcp_pcb *tpcb, err_t err)
tax 0:66300c77c6e9 321 {
tax 0:66300c77c6e9 322 queueEvent(NETTCPSOCKET_CONNECTED);
tax 0:66300c77c6e9 323 return ERR_OK;
tax 0:66300c77c6e9 324 }
tax 0:66300c77c6e9 325
tax 0:66300c77c6e9 326 void LwipNetTcpSocket::errCb(err_t err)
tax 0:66300c77c6e9 327 {
tax 0:66300c77c6e9 328 DBG("NetTcpSocket %p - Error %d in LwipNetTcpSocket::errCb.\n", (void*)this, err);
tax 0:66300c77c6e9 329 //WARN: At this point, m_pPcb has been freed by lwIP
tax 0:66300c77c6e9 330 m_pPcb = NULL;
tax 0:66300c77c6e9 331 //These errors are fatal, discard all events queued before so that the errors are handled first
tax 0:66300c77c6e9 332 discardEvents();
tax 0:66300c77c6e9 333 m_closed = true;
tax 0:66300c77c6e9 334 cleanUp();
tax 0:66300c77c6e9 335 if( err == ERR_ABRT)
tax 0:66300c77c6e9 336 queueEvent(NETTCPSOCKET_CONABRT);
tax 0:66300c77c6e9 337 else //if( err == ERR_RST)
tax 0:66300c77c6e9 338 queueEvent(NETTCPSOCKET_CONRST);
tax 0:66300c77c6e9 339 }
tax 0:66300c77c6e9 340
tax 0:66300c77c6e9 341 err_t LwipNetTcpSocket::sentCb(tcp_pcb* tpcb, u16_t len)
tax 0:66300c77c6e9 342 {
tax 0:66300c77c6e9 343 // DBG("%d bytes ACKed by host.\n", len);
tax 0:66300c77c6e9 344 queueEvent(NETTCPSOCKET_WRITEABLE);
tax 0:66300c77c6e9 345 return ERR_OK;
tax 0:66300c77c6e9 346 }
tax 0:66300c77c6e9 347
tax 0:66300c77c6e9 348 err_t LwipNetTcpSocket::recvCb(tcp_pcb* tpcb, pbuf *p, err_t err)
tax 0:66300c77c6e9 349 {
tax 0:66300c77c6e9 350 //Store pbuf ptr
tax 0:66300c77c6e9 351 // DBG("Receive CB with err = %d & len = %d.\n", err, p->tot_len);
tax 0:66300c77c6e9 352 // tcp_recved( (tcp_pcb*) m_pPcb, p->tot_len); //Acknowledge the reception
tax 0:66300c77c6e9 353
tax 0:66300c77c6e9 354 if(err)
tax 0:66300c77c6e9 355 {
tax 0:66300c77c6e9 356 queueEvent(NETTCPSOCKET_ERROR);
tax 0:66300c77c6e9 357 return ERR_OK; //FIXME: More robust error handling there
tax 0:66300c77c6e9 358 }
tax 0:66300c77c6e9 359 else if(!p)
tax 0:66300c77c6e9 360 {
tax 0:66300c77c6e9 361 DBG("NetTcpSocket %p - Connection closed by remote host (LwipNetTcpSocket::recvCb).\n", (void*)this);
tax 0:66300c77c6e9 362 //Buf is NULL, that means that the connection has been closed by remote host
tax 0:66300c77c6e9 363
tax 0:66300c77c6e9 364 //FIX: 27/05/2010: We do not want to deallocate the socket while some data might still be readable
tax 0:66300c77c6e9 365 //REMOVED: close();
tax 0:66300c77c6e9 366
tax 0:66300c77c6e9 367 //However we do not want to close the socket yet
tax 0:66300c77c6e9 368
tax 0:66300c77c6e9 369 queueEvent(NETTCPSOCKET_DISCONNECTED);
tax 0:66300c77c6e9 370 return ERR_OK;
tax 0:66300c77c6e9 371 }
tax 0:66300c77c6e9 372
tax 0:66300c77c6e9 373 //We asserted that p is a valid pointer
tax 0:66300c77c6e9 374
tax 0:66300c77c6e9 375 //New data processing
tax 0:66300c77c6e9 376 tcp_recved( tpcb, p->tot_len); //Acknowledge the reception
tax 0:66300c77c6e9 377 if(!m_pReadPbuf)
tax 0:66300c77c6e9 378 {
tax 0:66300c77c6e9 379 m_pReadPbuf = p;
tax 0:66300c77c6e9 380 queueEvent(NETTCPSOCKET_READABLE);
tax 0:66300c77c6e9 381 }
tax 0:66300c77c6e9 382 else
tax 0:66300c77c6e9 383 {
tax 0:66300c77c6e9 384 pbuf_cat((pbuf*)m_pReadPbuf, p); //m_pReadPbuf is not empty, tail p to it and drop our ref
tax 0:66300c77c6e9 385 //No need to queue an event in that case since the read buf has not been processed yet
tax 0:66300c77c6e9 386 }
tax 0:66300c77c6e9 387 return ERR_OK;
tax 0:66300c77c6e9 388 }
tax 0:66300c77c6e9 389
tax 0:66300c77c6e9 390
tax 0:66300c77c6e9 391 void LwipNetTcpSocket::cleanUp() //Flush input buffer
tax 0:66300c77c6e9 392 {
tax 0:66300c77c6e9 393 //Ensure that further error won't be followed to this inst (which can be destroyed)
tax 0:66300c77c6e9 394 if( m_pPcb )
tax 0:66300c77c6e9 395 {
tax 0:66300c77c6e9 396 tcp_arg( (tcp_pcb*) m_pPcb, (void*) NULL );
tax 0:66300c77c6e9 397 tcp_recv( (tcp_pcb*) m_pPcb, NULL );
tax 0:66300c77c6e9 398 tcp_sent((tcp_pcb*) m_pPcb, NULL );
tax 0:66300c77c6e9 399 tcp_err( (tcp_pcb*) m_pPcb, NULL );
tax 0:66300c77c6e9 400 }
tax 0:66300c77c6e9 401
tax 0:66300c77c6e9 402 if( m_pReadPbuf )
tax 0:66300c77c6e9 403 {
tax 0:66300c77c6e9 404 DBG("Deallocating unread data.\n");
tax 0:66300c77c6e9 405 pbuf_free((pbuf*)m_pReadPbuf); //Free all unread data
tax 0:66300c77c6e9 406 m_pReadPbuf = NULL;
tax 0:66300c77c6e9 407 recv(NULL,0); //Update recv ptr position
tax 0:66300c77c6e9 408 }
tax 0:66300c77c6e9 409 }
tax 0:66300c77c6e9 410
tax 0:66300c77c6e9 411 // Static callbacks from LwIp
tax 0:66300c77c6e9 412
tax 0:66300c77c6e9 413 err_t LwipNetTcpSocket::sAcceptCb(void *arg, struct tcp_pcb *newpcb, err_t err)
tax 0:66300c77c6e9 414 {
tax 0:66300c77c6e9 415 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
tax 0:66300c77c6e9 416 return pMe->acceptCb( newpcb, err );
tax 0:66300c77c6e9 417 }
tax 0:66300c77c6e9 418
tax 0:66300c77c6e9 419 err_t LwipNetTcpSocket::sConnectedCb(void *arg, struct tcp_pcb *tpcb, err_t err)
tax 0:66300c77c6e9 420 {
tax 0:66300c77c6e9 421 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
tax 0:66300c77c6e9 422 return pMe->connectedCb( tpcb, err );
tax 0:66300c77c6e9 423 }
tax 0:66300c77c6e9 424
tax 0:66300c77c6e9 425 void LwipNetTcpSocket::sErrCb(void *arg, err_t err)
tax 0:66300c77c6e9 426 {
tax 0:66300c77c6e9 427 if( !arg )
tax 0:66300c77c6e9 428 {
tax 0:66300c77c6e9 429 DBG("NetTcpSocket - Error %d in LwipNetTcpSocket::sErrCb.\n", err);
tax 0:66300c77c6e9 430 return; //The socket has been destroyed, discard error
tax 0:66300c77c6e9 431 }
tax 0:66300c77c6e9 432 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
tax 0:66300c77c6e9 433 return pMe->errCb( err );
tax 0:66300c77c6e9 434 }
tax 0:66300c77c6e9 435
tax 0:66300c77c6e9 436 err_t LwipNetTcpSocket::sSentCb(void *arg, struct tcp_pcb *tpcb, u16_t len)
tax 0:66300c77c6e9 437 {
tax 0:66300c77c6e9 438 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
tax 0:66300c77c6e9 439 return pMe->sentCb( tpcb, len );
tax 0:66300c77c6e9 440 }
tax 0:66300c77c6e9 441
tax 0:66300c77c6e9 442 err_t LwipNetTcpSocket::sRecvCb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
tax 0:66300c77c6e9 443 {
tax 0:66300c77c6e9 444 if( tpcb->flags & TF_RXCLOSED )
tax 0:66300c77c6e9 445 {
tax 0:66300c77c6e9 446 //The Pcb is in a closing state
tax 0:66300c77c6e9 447 //Discard that data here since we might have destroyed the corresponding socket object
tax 0:66300c77c6e9 448 tcp_recved( tpcb, p->tot_len);
tax 0:66300c77c6e9 449 pbuf_free( p );
tax 0:66300c77c6e9 450 return ERR_OK;
tax 0:66300c77c6e9 451 }
tax 0:66300c77c6e9 452 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
tax 0:66300c77c6e9 453 return pMe->recvCb( tpcb, p, err );
tax 0:66300c77c6e9 454 }
tax 0:66300c77c6e9 455
tax 0:66300c77c6e9 456 #endif