/*** int UdpServer::availableClients(void) ** ** Synopsis: ** Checks to see how many pending clients are availabe to be accepted. ** ** Parameters: ** None ** ** Return Values: ** The number of waiting UdpClients to be accepted. ** ** Errors: ** None ** ** Notes: ** ** This is the workhorse of the UdpServer Class ** It will update pending clients if a datagram is detected ** It will attempt to start listening if a socket comes avalialbe for listening ** It will clean up disconnected clients */ int UdpServer::availableClients(void) { int i = 0; EthernetPeriodicTasks(); if(isListening() && UdpClientAvailable(_rghUDP[_cPending]) > 0) { // remember, we listen on the last hUDP in the buffer. _cPending++; } // The AcceptClient could have removed someone from the list // do this first, so if we resume we have a socket to listen while(i <_cPending) { // see if we need to clear this one if(_rghUDP[i] >= INVALID_UDP_SOCKET) { int j = i; byte iBuff = _iBuff[i]; // this buffer came available // shift everyone from this location down. // include everything possible (_cPendingMax), even things beyond _cPending // because I want to shift the listening one at _rghUDP[_cPending] as well for(; j < _cPendingMax-1; j++) { _rghUDP[j] = _rghUDP[j+1]; _iBuff[j] = _iBuff[j+1]; } // we know the very last slot is open, because we just removed one and shifted down // make sure we have an invalid socket number in there. // also, since we shifted, the last 2 slots will have the same iBuff index // but we just opened up a new index that we just removed, move it to the last socket slot // we know this last socket slot is open, if we are not listening, then this will be picked // as our listening slot when we do the resume below _rghUDP[_cPendingMax-1] = INVALID_UDP_SOCKET; _iBuff[_cPendingMax-1] = iBuff; // reduce the pending count as we just removed one _cPending--; } // if nothing was shifted, look at the next value; if we shifted // don't increment as we want to look at he shifted one. else { i++; } } // if we are supposed to be listening, then try to listening if(_fListening) { resumeListening(); } return(_cPending); }
/*** int UdpClient::available(void) ** ** Synopsis: ** Returns the number of bytes in the next available datagram ** ** Parameters: ** None ** ** Return Values: ** The actual number of bytes available in the datagram ready for reading, 0 if no datagrams are in the cache ** ** Errors: ** None ** ** Notes: ** ** If you read a partial datagram on a previous ReadDatagram, this call will return the ** number of unread bytes in the remaining datagram. If other datagrams are in the cache, they ** will not be visible until the previous datagram is read and removed from the cache. ** */ size_t UdpClient::available(void) { // isDNETcK::EndPointResolved will call periodic tasks if(isEndPointResolved(DNETcK::msImmediate)) { return((unsigned int) UdpClientAvailable(_hUDP)); } else { return(0); } }