HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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