Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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