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