Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only
core/net.cpp@0:8f5825f330b0, 2011-09-08 (annotated)
- Committer:
- RodColeman
- Date:
- Thu Sep 08 10:41:36 2011 +0000
- Revision:
- 0:8f5825f330b0
setDataLen hacked to 180bytes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RodColeman | 0:8f5825f330b0 | 1 | |
RodColeman | 0:8f5825f330b0 | 2 | /* |
RodColeman | 0:8f5825f330b0 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
RodColeman | 0:8f5825f330b0 | 4 | |
RodColeman | 0:8f5825f330b0 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
RodColeman | 0:8f5825f330b0 | 6 | of this software and associated documentation files (the "Software"), to deal |
RodColeman | 0:8f5825f330b0 | 7 | in the Software without restriction, including without limitation the rights |
RodColeman | 0:8f5825f330b0 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
RodColeman | 0:8f5825f330b0 | 9 | copies of the Software, and to permit persons to whom the Software is |
RodColeman | 0:8f5825f330b0 | 10 | furnished to do so, subject to the following conditions: |
RodColeman | 0:8f5825f330b0 | 11 | |
RodColeman | 0:8f5825f330b0 | 12 | The above copyright notice and this permission notice shall be included in |
RodColeman | 0:8f5825f330b0 | 13 | all copies or substantial portions of the Software. |
RodColeman | 0:8f5825f330b0 | 14 | |
RodColeman | 0:8f5825f330b0 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
RodColeman | 0:8f5825f330b0 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
RodColeman | 0:8f5825f330b0 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
RodColeman | 0:8f5825f330b0 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
RodColeman | 0:8f5825f330b0 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
RodColeman | 0:8f5825f330b0 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
RodColeman | 0:8f5825f330b0 | 21 | THE SOFTWARE. |
RodColeman | 0:8f5825f330b0 | 22 | */ |
RodColeman | 0:8f5825f330b0 | 23 | |
RodColeman | 0:8f5825f330b0 | 24 | #include "net.h" |
RodColeman | 0:8f5825f330b0 | 25 | |
RodColeman | 0:8f5825f330b0 | 26 | #include "host.h" |
RodColeman | 0:8f5825f330b0 | 27 | #include "ipaddr.h" |
RodColeman | 0:8f5825f330b0 | 28 | #include "netservice.h" |
RodColeman | 0:8f5825f330b0 | 29 | #include "if/net/netif.h" |
RodColeman | 0:8f5825f330b0 | 30 | #include "if/net/nettcpsocket.h" |
RodColeman | 0:8f5825f330b0 | 31 | #include "if/net/netudpsocket.h" |
RodColeman | 0:8f5825f330b0 | 32 | #include "if/net/netdnsrequest.h" |
RodColeman | 0:8f5825f330b0 | 33 | |
RodColeman | 0:8f5825f330b0 | 34 | //#define __DEBUG |
RodColeman | 0:8f5825f330b0 | 35 | #include "dbg/dbg.h" |
RodColeman | 0:8f5825f330b0 | 36 | |
RodColeman | 0:8f5825f330b0 | 37 | Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket() |
RodColeman | 0:8f5825f330b0 | 38 | { |
RodColeman | 0:8f5825f330b0 | 39 | |
RodColeman | 0:8f5825f330b0 | 40 | } |
RodColeman | 0:8f5825f330b0 | 41 | |
RodColeman | 0:8f5825f330b0 | 42 | Net::~Net() |
RodColeman | 0:8f5825f330b0 | 43 | { |
RodColeman | 0:8f5825f330b0 | 44 | |
RodColeman | 0:8f5825f330b0 | 45 | } |
RodColeman | 0:8f5825f330b0 | 46 | |
RodColeman | 0:8f5825f330b0 | 47 | |
RodColeman | 0:8f5825f330b0 | 48 | void Net::poll() |
RodColeman | 0:8f5825f330b0 | 49 | { |
RodColeman | 0:8f5825f330b0 | 50 | //DBG("\r\nNet : Services polling\r\n"); |
RodColeman | 0:8f5825f330b0 | 51 | |
RodColeman | 0:8f5825f330b0 | 52 | //Poll Services |
RodColeman | 0:8f5825f330b0 | 53 | NetService::servicesPoll(); |
RodColeman | 0:8f5825f330b0 | 54 | |
RodColeman | 0:8f5825f330b0 | 55 | //DBG("\r\nNet : Interfaces polling\r\n"); |
RodColeman | 0:8f5825f330b0 | 56 | |
RodColeman | 0:8f5825f330b0 | 57 | //Poll Interfaces |
RodColeman | 0:8f5825f330b0 | 58 | list<NetIf*>::iterator pIfIt; |
RodColeman | 0:8f5825f330b0 | 59 | |
RodColeman | 0:8f5825f330b0 | 60 | for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ ) |
RodColeman | 0:8f5825f330b0 | 61 | { |
RodColeman | 0:8f5825f330b0 | 62 | (*pIfIt)->poll(); |
RodColeman | 0:8f5825f330b0 | 63 | } |
RodColeman | 0:8f5825f330b0 | 64 | |
RodColeman | 0:8f5825f330b0 | 65 | //DBG("\r\nNet : Sockets polling\r\n"); |
RodColeman | 0:8f5825f330b0 | 66 | |
RodColeman | 0:8f5825f330b0 | 67 | //Poll Tcp Sockets |
RodColeman | 0:8f5825f330b0 | 68 | list<NetTcpSocket*>::iterator pNetTcpSocketIt; |
RodColeman | 0:8f5825f330b0 | 69 | |
RodColeman | 0:8f5825f330b0 | 70 | for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); ) |
RodColeman | 0:8f5825f330b0 | 71 | { |
RodColeman | 0:8f5825f330b0 | 72 | (*pNetTcpSocketIt)->poll(); |
RodColeman | 0:8f5825f330b0 | 73 | |
RodColeman | 0:8f5825f330b0 | 74 | if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) ) |
RodColeman | 0:8f5825f330b0 | 75 | { |
RodColeman | 0:8f5825f330b0 | 76 | (*pNetTcpSocketIt)->m_removed = true; |
RodColeman | 0:8f5825f330b0 | 77 | delete (*pNetTcpSocketIt); |
RodColeman | 0:8f5825f330b0 | 78 | (*pNetTcpSocketIt) = NULL; |
RodColeman | 0:8f5825f330b0 | 79 | pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt); |
RodColeman | 0:8f5825f330b0 | 80 | } |
RodColeman | 0:8f5825f330b0 | 81 | else |
RodColeman | 0:8f5825f330b0 | 82 | { |
RodColeman | 0:8f5825f330b0 | 83 | pNetTcpSocketIt++; |
RodColeman | 0:8f5825f330b0 | 84 | } |
RodColeman | 0:8f5825f330b0 | 85 | } |
RodColeman | 0:8f5825f330b0 | 86 | |
RodColeman | 0:8f5825f330b0 | 87 | //Poll Udp Sockets |
RodColeman | 0:8f5825f330b0 | 88 | list<NetUdpSocket*>::iterator pNetUdpSocketIt; |
RodColeman | 0:8f5825f330b0 | 89 | |
RodColeman | 0:8f5825f330b0 | 90 | for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); ) |
RodColeman | 0:8f5825f330b0 | 91 | { |
RodColeman | 0:8f5825f330b0 | 92 | (*pNetUdpSocketIt)->poll(); |
RodColeman | 0:8f5825f330b0 | 93 | |
RodColeman | 0:8f5825f330b0 | 94 | if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) ) |
RodColeman | 0:8f5825f330b0 | 95 | { |
RodColeman | 0:8f5825f330b0 | 96 | (*pNetUdpSocketIt)->m_removed = true; |
RodColeman | 0:8f5825f330b0 | 97 | delete (*pNetUdpSocketIt); |
RodColeman | 0:8f5825f330b0 | 98 | (*pNetUdpSocketIt) = NULL; |
RodColeman | 0:8f5825f330b0 | 99 | pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt); |
RodColeman | 0:8f5825f330b0 | 100 | } |
RodColeman | 0:8f5825f330b0 | 101 | else |
RodColeman | 0:8f5825f330b0 | 102 | { |
RodColeman | 0:8f5825f330b0 | 103 | pNetUdpSocketIt++; |
RodColeman | 0:8f5825f330b0 | 104 | } |
RodColeman | 0:8f5825f330b0 | 105 | } |
RodColeman | 0:8f5825f330b0 | 106 | |
RodColeman | 0:8f5825f330b0 | 107 | |
RodColeman | 0:8f5825f330b0 | 108 | } |
RodColeman | 0:8f5825f330b0 | 109 | |
RodColeman | 0:8f5825f330b0 | 110 | NetTcpSocket* Net::tcpSocket(NetIf& netif) { |
RodColeman | 0:8f5825f330b0 | 111 | NetTcpSocket* pNetTcpSocket = netif.tcpSocket(); |
RodColeman | 0:8f5825f330b0 | 112 | pNetTcpSocket->m_refs++; |
RodColeman | 0:8f5825f330b0 | 113 | return pNetTcpSocket; |
RodColeman | 0:8f5825f330b0 | 114 | } |
RodColeman | 0:8f5825f330b0 | 115 | |
RodColeman | 0:8f5825f330b0 | 116 | NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if |
RodColeman | 0:8f5825f330b0 | 117 | if ( net().m_defaultIf == NULL ) |
RodColeman | 0:8f5825f330b0 | 118 | return NULL; |
RodColeman | 0:8f5825f330b0 | 119 | NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket(); |
RodColeman | 0:8f5825f330b0 | 120 | pNetTcpSocket->m_refs++; |
RodColeman | 0:8f5825f330b0 | 121 | return pNetTcpSocket; |
RodColeman | 0:8f5825f330b0 | 122 | } |
RodColeman | 0:8f5825f330b0 | 123 | |
RodColeman | 0:8f5825f330b0 | 124 | void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket) |
RodColeman | 0:8f5825f330b0 | 125 | { |
RodColeman | 0:8f5825f330b0 | 126 | pNetTcpSocket->m_refs--; |
RodColeman | 0:8f5825f330b0 | 127 | if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs) |
RodColeman | 0:8f5825f330b0 | 128 | pNetTcpSocket->close(); |
RodColeman | 0:8f5825f330b0 | 129 | } |
RodColeman | 0:8f5825f330b0 | 130 | |
RodColeman | 0:8f5825f330b0 | 131 | NetUdpSocket* Net::udpSocket(NetIf& netif) { |
RodColeman | 0:8f5825f330b0 | 132 | NetUdpSocket* pNetUdpSocket = netif.udpSocket(); |
RodColeman | 0:8f5825f330b0 | 133 | pNetUdpSocket->m_refs++; |
RodColeman | 0:8f5825f330b0 | 134 | return pNetUdpSocket; |
RodColeman | 0:8f5825f330b0 | 135 | } |
RodColeman | 0:8f5825f330b0 | 136 | |
RodColeman | 0:8f5825f330b0 | 137 | NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if |
RodColeman | 0:8f5825f330b0 | 138 | if ( net().m_defaultIf == NULL ) |
RodColeman | 0:8f5825f330b0 | 139 | return NULL; |
RodColeman | 0:8f5825f330b0 | 140 | NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket(); |
RodColeman | 0:8f5825f330b0 | 141 | pNetUdpSocket->m_refs++; |
RodColeman | 0:8f5825f330b0 | 142 | return pNetUdpSocket; |
RodColeman | 0:8f5825f330b0 | 143 | } |
RodColeman | 0:8f5825f330b0 | 144 | |
RodColeman | 0:8f5825f330b0 | 145 | void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket) |
RodColeman | 0:8f5825f330b0 | 146 | { |
RodColeman | 0:8f5825f330b0 | 147 | pNetUdpSocket->m_refs--; |
RodColeman | 0:8f5825f330b0 | 148 | if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs) |
RodColeman | 0:8f5825f330b0 | 149 | pNetUdpSocket->close(); |
RodColeman | 0:8f5825f330b0 | 150 | } |
RodColeman | 0:8f5825f330b0 | 151 | |
RodColeman | 0:8f5825f330b0 | 152 | NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) { |
RodColeman | 0:8f5825f330b0 | 153 | return netif.dnsRequest(hostname); |
RodColeman | 0:8f5825f330b0 | 154 | } |
RodColeman | 0:8f5825f330b0 | 155 | |
RodColeman | 0:8f5825f330b0 | 156 | NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if |
RodColeman | 0:8f5825f330b0 | 157 | if ( net().m_defaultIf == NULL ) |
RodColeman | 0:8f5825f330b0 | 158 | return NULL; |
RodColeman | 0:8f5825f330b0 | 159 | return net().m_defaultIf->dnsRequest(hostname); |
RodColeman | 0:8f5825f330b0 | 160 | } |
RodColeman | 0:8f5825f330b0 | 161 | |
RodColeman | 0:8f5825f330b0 | 162 | NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif) |
RodColeman | 0:8f5825f330b0 | 163 | { |
RodColeman | 0:8f5825f330b0 | 164 | return netif.dnsRequest(pHost); |
RodColeman | 0:8f5825f330b0 | 165 | } |
RodColeman | 0:8f5825f330b0 | 166 | |
RodColeman | 0:8f5825f330b0 | 167 | NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if |
RodColeman | 0:8f5825f330b0 | 168 | { |
RodColeman | 0:8f5825f330b0 | 169 | if ( net().m_defaultIf == NULL ) |
RodColeman | 0:8f5825f330b0 | 170 | return NULL; |
RodColeman | 0:8f5825f330b0 | 171 | return net().m_defaultIf->dnsRequest(pHost); |
RodColeman | 0:8f5825f330b0 | 172 | } |
RodColeman | 0:8f5825f330b0 | 173 | |
RodColeman | 0:8f5825f330b0 | 174 | void Net::setDefaultIf(NetIf& netif) { |
RodColeman | 0:8f5825f330b0 | 175 | net().m_defaultIf = &netif; |
RodColeman | 0:8f5825f330b0 | 176 | } |
RodColeman | 0:8f5825f330b0 | 177 | |
RodColeman | 0:8f5825f330b0 | 178 | void Net::setDefaultIf(NetIf* pIf) |
RodColeman | 0:8f5825f330b0 | 179 | { |
RodColeman | 0:8f5825f330b0 | 180 | net().m_defaultIf = pIf; |
RodColeman | 0:8f5825f330b0 | 181 | } |
RodColeman | 0:8f5825f330b0 | 182 | |
RodColeman | 0:8f5825f330b0 | 183 | NetIf* Net::getDefaultIf() { |
RodColeman | 0:8f5825f330b0 | 184 | return net().m_defaultIf; |
RodColeman | 0:8f5825f330b0 | 185 | } |
RodColeman | 0:8f5825f330b0 | 186 | |
RodColeman | 0:8f5825f330b0 | 187 | void Net::registerIf(NetIf* pIf) |
RodColeman | 0:8f5825f330b0 | 188 | { |
RodColeman | 0:8f5825f330b0 | 189 | net().m_lpIf.push_back(pIf); |
RodColeman | 0:8f5825f330b0 | 190 | } |
RodColeman | 0:8f5825f330b0 | 191 | |
RodColeman | 0:8f5825f330b0 | 192 | void Net::unregisterIf(NetIf* pIf) |
RodColeman | 0:8f5825f330b0 | 193 | { |
RodColeman | 0:8f5825f330b0 | 194 | if( net().m_defaultIf == pIf ) |
RodColeman | 0:8f5825f330b0 | 195 | net().m_defaultIf = NULL; |
RodColeman | 0:8f5825f330b0 | 196 | net().m_lpIf.remove(pIf); |
RodColeman | 0:8f5825f330b0 | 197 | } |
RodColeman | 0:8f5825f330b0 | 198 | |
RodColeman | 0:8f5825f330b0 | 199 | void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket) |
RodColeman | 0:8f5825f330b0 | 200 | { |
RodColeman | 0:8f5825f330b0 | 201 | net().m_lpNetTcpSocket.push_back(pNetTcpSocket); |
RodColeman | 0:8f5825f330b0 | 202 | DBG("\r\nNetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size()); |
RodColeman | 0:8f5825f330b0 | 203 | } |
RodColeman | 0:8f5825f330b0 | 204 | |
RodColeman | 0:8f5825f330b0 | 205 | void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket) |
RodColeman | 0:8f5825f330b0 | 206 | { |
RodColeman | 0:8f5825f330b0 | 207 | DBG("\r\nNetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1); |
RodColeman | 0:8f5825f330b0 | 208 | if(!pNetTcpSocket->m_removed) |
RodColeman | 0:8f5825f330b0 | 209 | net().m_lpNetTcpSocket.remove(pNetTcpSocket); |
RodColeman | 0:8f5825f330b0 | 210 | } |
RodColeman | 0:8f5825f330b0 | 211 | |
RodColeman | 0:8f5825f330b0 | 212 | void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket) |
RodColeman | 0:8f5825f330b0 | 213 | { |
RodColeman | 0:8f5825f330b0 | 214 | net().m_lpNetUdpSocket.push_back(pNetUdpSocket); |
RodColeman | 0:8f5825f330b0 | 215 | DBG("\r\nNetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size()); |
RodColeman | 0:8f5825f330b0 | 216 | } |
RodColeman | 0:8f5825f330b0 | 217 | |
RodColeman | 0:8f5825f330b0 | 218 | void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket) |
RodColeman | 0:8f5825f330b0 | 219 | { |
RodColeman | 0:8f5825f330b0 | 220 | DBG("\r\nNetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1); |
RodColeman | 0:8f5825f330b0 | 221 | if(!pNetUdpSocket->m_removed) |
RodColeman | 0:8f5825f330b0 | 222 | net().m_lpNetUdpSocket.remove(pNetUdpSocket); |
RodColeman | 0:8f5825f330b0 | 223 | } |
RodColeman | 0:8f5825f330b0 | 224 | |
RodColeman | 0:8f5825f330b0 | 225 | Net& Net::net() |
RodColeman | 0:8f5825f330b0 | 226 | { |
RodColeman | 0:8f5825f330b0 | 227 | static Net* pInst = new Net(); //Called only once |
RodColeman | 0:8f5825f330b0 | 228 | return *pInst; |
RodColeman | 0:8f5825f330b0 | 229 | } |