Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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