modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
0:355018f44c9f
minor change

Who changed what in which revision?

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