Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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