Michael Fischler / Mbed 2 deprecated Contest_Winner

Dependencies:   mbed

Committer:
mafischl
Date:
Thu Oct 13 18:01:31 2011 +0000
Revision:
1:5a7cce9994a3
Parent:
0:d9266031f832

        

Who changed what in which revision?

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