Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.
Dependents: christmasLights device_server pop3demo device_server_udp ... more
core/net.cpp@1:9d93f4dc2f46, 2012-07-18 (annotated)
- Committer:
- hlipka
- Date:
- Wed Jul 18 20:28:43 2012 +0000
- Revision:
- 1:9d93f4dc2f46
- Parent:
- 0:8b387bed54c2
updated to the new mbed library (version 41) and the new LwIP stack. Code is still untested.
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 "net.h" |
hlipka | 0:8b387bed54c2 | 25 | |
hlipka | 0:8b387bed54c2 | 26 | #include "host.h" |
hlipka | 0:8b387bed54c2 | 27 | #include "ipaddr.h" |
hlipka | 0:8b387bed54c2 | 28 | #include "netservice.h" |
hlipka | 0:8b387bed54c2 | 29 | #include "if/net/netif.h" |
hlipka | 0:8b387bed54c2 | 30 | #include "if/net/nettcpsocket.h" |
hlipka | 0:8b387bed54c2 | 31 | #include "if/net/netudpsocket.h" |
hlipka | 0:8b387bed54c2 | 32 | #include "if/net/netdnsrequest.h" |
hlipka | 0:8b387bed54c2 | 33 | |
hlipka | 0:8b387bed54c2 | 34 | //#define __DEBUG |
hlipka | 0:8b387bed54c2 | 35 | #include "dbg/dbg.h" |
hlipka | 0:8b387bed54c2 | 36 | |
hlipka | 0:8b387bed54c2 | 37 | Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket() |
hlipka | 0:8b387bed54c2 | 38 | { |
hlipka | 0:8b387bed54c2 | 39 | |
hlipka | 0:8b387bed54c2 | 40 | } |
hlipka | 0:8b387bed54c2 | 41 | |
hlipka | 0:8b387bed54c2 | 42 | Net::~Net() |
hlipka | 0:8b387bed54c2 | 43 | { |
hlipka | 0:8b387bed54c2 | 44 | |
hlipka | 0:8b387bed54c2 | 45 | } |
hlipka | 0:8b387bed54c2 | 46 | |
hlipka | 0:8b387bed54c2 | 47 | |
hlipka | 0:8b387bed54c2 | 48 | void Net::poll() |
hlipka | 0:8b387bed54c2 | 49 | { |
hlipka | 0:8b387bed54c2 | 50 | //DBG("\r\nNet : Services polling\r\n"); |
hlipka | 0:8b387bed54c2 | 51 | |
hlipka | 0:8b387bed54c2 | 52 | //Poll Services |
hlipka | 0:8b387bed54c2 | 53 | NetService::servicesPoll(); |
hlipka | 0:8b387bed54c2 | 54 | |
hlipka | 0:8b387bed54c2 | 55 | //DBG("\r\nNet : Interfaces polling\r\n"); |
hlipka | 0:8b387bed54c2 | 56 | |
hlipka | 0:8b387bed54c2 | 57 | //Poll Interfaces |
hlipka | 0:8b387bed54c2 | 58 | list<NetIf*>::iterator pIfIt; |
hlipka | 0:8b387bed54c2 | 59 | |
hlipka | 0:8b387bed54c2 | 60 | for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ ) |
hlipka | 0:8b387bed54c2 | 61 | { |
hlipka | 0:8b387bed54c2 | 62 | (*pIfIt)->poll(); |
hlipka | 0:8b387bed54c2 | 63 | } |
hlipka | 0:8b387bed54c2 | 64 | |
hlipka | 0:8b387bed54c2 | 65 | //DBG("\r\nNet : Sockets polling\r\n"); |
hlipka | 0:8b387bed54c2 | 66 | |
hlipka | 0:8b387bed54c2 | 67 | //Poll Tcp Sockets |
hlipka | 0:8b387bed54c2 | 68 | list<NetTcpSocket*>::iterator pNetTcpSocketIt; |
hlipka | 0:8b387bed54c2 | 69 | |
hlipka | 0:8b387bed54c2 | 70 | for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); ) |
hlipka | 0:8b387bed54c2 | 71 | { |
hlipka | 0:8b387bed54c2 | 72 | (*pNetTcpSocketIt)->poll(); |
hlipka | 0:8b387bed54c2 | 73 | |
hlipka | 0:8b387bed54c2 | 74 | if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) ) |
hlipka | 0:8b387bed54c2 | 75 | { |
hlipka | 0:8b387bed54c2 | 76 | (*pNetTcpSocketIt)->m_removed = true; |
hlipka | 0:8b387bed54c2 | 77 | delete (*pNetTcpSocketIt); |
hlipka | 0:8b387bed54c2 | 78 | (*pNetTcpSocketIt) = NULL; |
hlipka | 0:8b387bed54c2 | 79 | pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt); |
hlipka | 0:8b387bed54c2 | 80 | } |
hlipka | 0:8b387bed54c2 | 81 | else |
hlipka | 0:8b387bed54c2 | 82 | { |
hlipka | 0:8b387bed54c2 | 83 | pNetTcpSocketIt++; |
hlipka | 0:8b387bed54c2 | 84 | } |
hlipka | 0:8b387bed54c2 | 85 | } |
hlipka | 0:8b387bed54c2 | 86 | |
hlipka | 0:8b387bed54c2 | 87 | //Poll Udp Sockets |
hlipka | 0:8b387bed54c2 | 88 | list<NetUdpSocket*>::iterator pNetUdpSocketIt; |
hlipka | 0:8b387bed54c2 | 89 | |
hlipka | 0:8b387bed54c2 | 90 | for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); ) |
hlipka | 0:8b387bed54c2 | 91 | { |
hlipka | 0:8b387bed54c2 | 92 | (*pNetUdpSocketIt)->poll(); |
hlipka | 0:8b387bed54c2 | 93 | |
hlipka | 0:8b387bed54c2 | 94 | if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) ) |
hlipka | 0:8b387bed54c2 | 95 | { |
hlipka | 0:8b387bed54c2 | 96 | (*pNetUdpSocketIt)->m_removed = true; |
hlipka | 0:8b387bed54c2 | 97 | delete (*pNetUdpSocketIt); |
hlipka | 0:8b387bed54c2 | 98 | (*pNetUdpSocketIt) = NULL; |
hlipka | 0:8b387bed54c2 | 99 | pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt); |
hlipka | 0:8b387bed54c2 | 100 | } |
hlipka | 0:8b387bed54c2 | 101 | else |
hlipka | 0:8b387bed54c2 | 102 | { |
hlipka | 0:8b387bed54c2 | 103 | pNetUdpSocketIt++; |
hlipka | 0:8b387bed54c2 | 104 | } |
hlipka | 0:8b387bed54c2 | 105 | } |
hlipka | 0:8b387bed54c2 | 106 | |
hlipka | 0:8b387bed54c2 | 107 | |
hlipka | 0:8b387bed54c2 | 108 | } |
hlipka | 0:8b387bed54c2 | 109 | |
hlipka | 0:8b387bed54c2 | 110 | NetTcpSocket* Net::tcpSocket(NetIf& netif) { |
hlipka | 0:8b387bed54c2 | 111 | NetTcpSocket* pNetTcpSocket = netif.tcpSocket(); |
hlipka | 0:8b387bed54c2 | 112 | pNetTcpSocket->m_refs++; |
hlipka | 0:8b387bed54c2 | 113 | return pNetTcpSocket; |
hlipka | 0:8b387bed54c2 | 114 | } |
hlipka | 0:8b387bed54c2 | 115 | |
hlipka | 0:8b387bed54c2 | 116 | NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if |
hlipka | 0:8b387bed54c2 | 117 | if ( net().m_defaultIf == NULL ) |
hlipka | 0:8b387bed54c2 | 118 | return NULL; |
hlipka | 0:8b387bed54c2 | 119 | NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket(); |
hlipka | 0:8b387bed54c2 | 120 | pNetTcpSocket->m_refs++; |
hlipka | 0:8b387bed54c2 | 121 | return pNetTcpSocket; |
hlipka | 0:8b387bed54c2 | 122 | } |
hlipka | 0:8b387bed54c2 | 123 | |
hlipka | 0:8b387bed54c2 | 124 | void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket) |
hlipka | 0:8b387bed54c2 | 125 | { |
hlipka | 0:8b387bed54c2 | 126 | pNetTcpSocket->m_refs--; |
hlipka | 0:8b387bed54c2 | 127 | if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs) |
hlipka | 0:8b387bed54c2 | 128 | pNetTcpSocket->close(); |
hlipka | 0:8b387bed54c2 | 129 | } |
hlipka | 0:8b387bed54c2 | 130 | |
hlipka | 0:8b387bed54c2 | 131 | NetUdpSocket* Net::udpSocket(NetIf& netif) { |
hlipka | 0:8b387bed54c2 | 132 | NetUdpSocket* pNetUdpSocket = netif.udpSocket(); |
hlipka | 0:8b387bed54c2 | 133 | pNetUdpSocket->m_refs++; |
hlipka | 0:8b387bed54c2 | 134 | return pNetUdpSocket; |
hlipka | 0:8b387bed54c2 | 135 | } |
hlipka | 0:8b387bed54c2 | 136 | |
hlipka | 0:8b387bed54c2 | 137 | NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if |
hlipka | 0:8b387bed54c2 | 138 | if ( net().m_defaultIf == NULL ) |
hlipka | 0:8b387bed54c2 | 139 | return NULL; |
hlipka | 0:8b387bed54c2 | 140 | NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket(); |
hlipka | 0:8b387bed54c2 | 141 | pNetUdpSocket->m_refs++; |
hlipka | 0:8b387bed54c2 | 142 | return pNetUdpSocket; |
hlipka | 0:8b387bed54c2 | 143 | } |
hlipka | 0:8b387bed54c2 | 144 | |
hlipka | 0:8b387bed54c2 | 145 | void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket) |
hlipka | 0:8b387bed54c2 | 146 | { |
hlipka | 0:8b387bed54c2 | 147 | pNetUdpSocket->m_refs--; |
hlipka | 0:8b387bed54c2 | 148 | if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs) |
hlipka | 0:8b387bed54c2 | 149 | pNetUdpSocket->close(); |
hlipka | 0:8b387bed54c2 | 150 | } |
hlipka | 0:8b387bed54c2 | 151 | |
hlipka | 0:8b387bed54c2 | 152 | NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) { |
hlipka | 0:8b387bed54c2 | 153 | return netif.dnsRequest(hostname); |
hlipka | 0:8b387bed54c2 | 154 | } |
hlipka | 0:8b387bed54c2 | 155 | |
hlipka | 0:8b387bed54c2 | 156 | NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if |
hlipka | 0:8b387bed54c2 | 157 | if ( net().m_defaultIf == NULL ) |
hlipka | 0:8b387bed54c2 | 158 | return NULL; |
hlipka | 0:8b387bed54c2 | 159 | return net().m_defaultIf->dnsRequest(hostname); |
hlipka | 0:8b387bed54c2 | 160 | } |
hlipka | 0:8b387bed54c2 | 161 | |
hlipka | 0:8b387bed54c2 | 162 | NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif) |
hlipka | 0:8b387bed54c2 | 163 | { |
hlipka | 0:8b387bed54c2 | 164 | return netif.dnsRequest(pHost); |
hlipka | 0:8b387bed54c2 | 165 | } |
hlipka | 0:8b387bed54c2 | 166 | |
hlipka | 0:8b387bed54c2 | 167 | NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if |
hlipka | 0:8b387bed54c2 | 168 | { |
hlipka | 0:8b387bed54c2 | 169 | if ( net().m_defaultIf == NULL ) |
hlipka | 0:8b387bed54c2 | 170 | return NULL; |
hlipka | 0:8b387bed54c2 | 171 | return net().m_defaultIf->dnsRequest(pHost); |
hlipka | 0:8b387bed54c2 | 172 | } |
hlipka | 0:8b387bed54c2 | 173 | |
hlipka | 0:8b387bed54c2 | 174 | void Net::setDefaultIf(NetIf& netif) { |
hlipka | 0:8b387bed54c2 | 175 | net().m_defaultIf = &netif; |
hlipka | 0:8b387bed54c2 | 176 | } |
hlipka | 0:8b387bed54c2 | 177 | |
hlipka | 0:8b387bed54c2 | 178 | void Net::setDefaultIf(NetIf* pIf) |
hlipka | 0:8b387bed54c2 | 179 | { |
hlipka | 0:8b387bed54c2 | 180 | net().m_defaultIf = pIf; |
hlipka | 0:8b387bed54c2 | 181 | } |
hlipka | 0:8b387bed54c2 | 182 | |
hlipka | 0:8b387bed54c2 | 183 | NetIf* Net::getDefaultIf() { |
hlipka | 0:8b387bed54c2 | 184 | return net().m_defaultIf; |
hlipka | 0:8b387bed54c2 | 185 | } |
hlipka | 0:8b387bed54c2 | 186 | |
hlipka | 0:8b387bed54c2 | 187 | void Net::registerIf(NetIf* pIf) |
hlipka | 0:8b387bed54c2 | 188 | { |
hlipka | 0:8b387bed54c2 | 189 | net().m_lpIf.push_back(pIf); |
hlipka | 0:8b387bed54c2 | 190 | } |
hlipka | 0:8b387bed54c2 | 191 | |
hlipka | 0:8b387bed54c2 | 192 | void Net::unregisterIf(NetIf* pIf) |
hlipka | 0:8b387bed54c2 | 193 | { |
hlipka | 0:8b387bed54c2 | 194 | if( net().m_defaultIf == pIf ) |
hlipka | 0:8b387bed54c2 | 195 | net().m_defaultIf = NULL; |
hlipka | 0:8b387bed54c2 | 196 | net().m_lpIf.remove(pIf); |
hlipka | 0:8b387bed54c2 | 197 | } |
hlipka | 0:8b387bed54c2 | 198 | |
hlipka | 0:8b387bed54c2 | 199 | void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket) |
hlipka | 0:8b387bed54c2 | 200 | { |
hlipka | 0:8b387bed54c2 | 201 | net().m_lpNetTcpSocket.push_back(pNetTcpSocket); |
hlipka | 0:8b387bed54c2 | 202 | DBG("\r\nNetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size()); |
hlipka | 0:8b387bed54c2 | 203 | } |
hlipka | 0:8b387bed54c2 | 204 | |
hlipka | 0:8b387bed54c2 | 205 | void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket) |
hlipka | 0:8b387bed54c2 | 206 | { |
hlipka | 0:8b387bed54c2 | 207 | DBG("\r\nNetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1); |
hlipka | 0:8b387bed54c2 | 208 | if(!pNetTcpSocket->m_removed) |
hlipka | 0:8b387bed54c2 | 209 | net().m_lpNetTcpSocket.remove(pNetTcpSocket); |
hlipka | 0:8b387bed54c2 | 210 | } |
hlipka | 0:8b387bed54c2 | 211 | |
hlipka | 0:8b387bed54c2 | 212 | void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket) |
hlipka | 0:8b387bed54c2 | 213 | { |
hlipka | 0:8b387bed54c2 | 214 | net().m_lpNetUdpSocket.push_back(pNetUdpSocket); |
hlipka | 0:8b387bed54c2 | 215 | DBG("\r\nNetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size()); |
hlipka | 0:8b387bed54c2 | 216 | } |
hlipka | 0:8b387bed54c2 | 217 | |
hlipka | 0:8b387bed54c2 | 218 | void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket) |
hlipka | 0:8b387bed54c2 | 219 | { |
hlipka | 0:8b387bed54c2 | 220 | DBG("\r\nNetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1); |
hlipka | 0:8b387bed54c2 | 221 | if(!pNetUdpSocket->m_removed) |
hlipka | 0:8b387bed54c2 | 222 | net().m_lpNetUdpSocket.remove(pNetUdpSocket); |
hlipka | 0:8b387bed54c2 | 223 | } |
hlipka | 0:8b387bed54c2 | 224 | |
hlipka | 0:8b387bed54c2 | 225 | Net& Net::net() |
hlipka | 0:8b387bed54c2 | 226 | { |
hlipka | 0:8b387bed54c2 | 227 | static Net* pInst = new Net(); //Called only once |
hlipka | 0:8b387bed54c2 | 228 | return *pInst; |
hlipka | 0:8b387bed54c2 | 229 | } |