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