3131#include " Ethernet.h"
3232#include " Udp.h"
3333
34+ /* Constructor */
35+ UDP::UDP () : _sock(MAX_SOCK_NUM) {}
36+
3437/* Start UDP socket, listening at local port PORT */
35- uint8_t UdpClass ::begin (uint16_t port) {
38+ uint8_t UDP ::begin (uint16_t port) {
3639 if (_sock != MAX_SOCK_NUM)
3740 return 0 ;
3841
@@ -56,13 +59,13 @@ uint8_t UdpClass::begin(uint16_t port) {
5659/* Send packet contained in buf of length len to peer at specified ip, and port */
5760/* Use this function to transmit binary data that might contain 0x00 bytes*/
5861/* This function returns sent data size for success else -1. */
59- uint16_t UdpClass ::sendPacket (uint8_t * buf, uint16_t len, uint8_t * ip, uint16_t port){
62+ uint16_t UDP ::sendPacket (uint8_t * buf, uint16_t len, uint8_t * ip, uint16_t port){
6063 return sendto (_sock,(const uint8_t *)buf,len,ip,port);
6164}
6265
6366/* Send zero-terminated string str as packet to peer at specified ip, and port */
6467/* This function returns sent data size for success else -1. */
65- uint16_t UdpClass ::sendPacket (const char str[], uint8_t * ip, uint16_t port){
68+ uint16_t UDP ::sendPacket (const char str[], uint8_t * ip, uint16_t port){
6669 // compute strlen
6770 const char *s;
6871 for (s = str; *s; ++s);
@@ -72,7 +75,7 @@ uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){
7275}
7376/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes.
7477 * returned value includes 8 byte UDP header!*/
75- int UdpClass ::available () {
78+ int UDP ::available () {
7679 return W5100.getRXReceivedSize (_sock);
7780}
7881
@@ -82,7 +85,7 @@ int UdpClass::available() {
8285/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
8386/* so it's easy to overflow buffer. so we check and truncate. */
8487/* returns number of bytes read, or negative number of bytes we would have needed if we truncated */
85- int UdpClass ::readPacket (uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
88+ int UDP ::readPacket (uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
8689 int packetLen = available ()-8 ; // skip UDP header;
8790 if (packetLen < 0 ) return 0 ; // no real data here
8891 if (packetLen > (int )bufLen) {
@@ -131,21 +134,21 @@ int UdpClass::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *
131134}
132135
133136/* Read a received packet, throw away peer's ip and port. See note above. */
134- int UdpClass ::readPacket (uint8_t * buf, uint16_t len) {
137+ int UDP ::readPacket (uint8_t * buf, uint16_t len) {
135138 uint8_t ip[4 ];
136139 uint16_t port[1 ];
137140 return recvfrom (_sock,buf,len,ip,port);
138141}
139142
140- int UdpClass ::readPacket (char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
143+ int UDP ::readPacket (char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
141144uint16_t myPort;
142145uint16_t ret = readPacket ( (byte*)buf, bufLen, ip, &myPort);
143146port = myPort;
144147return ret;
145148}
146149
147- /* Release any resources being used by this UdpClass instance */
148- void UdpClass ::stop ()
150+ /* Release any resources being used by this UDP instance */
151+ void UDP ::stop ()
149152{
150153 if (_sock == MAX_SOCK_NUM)
151154 return ;
0 commit comments