NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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