NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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