I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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