31
31
#include " Ethernet.h"
32
32
#include " Udp.h"
33
33
34
+ /* Constructor */
35
+ UDP::UDP () : _sock(MAX_SOCK_NUM) {}
36
+
34
37
/* Start UDP socket, listening at local port PORT */
35
- uint8_t UdpClass ::begin (uint16_t port) {
38
+ uint8_t UDP ::begin (uint16_t port) {
36
39
if (_sock != MAX_SOCK_NUM)
37
40
return 0 ;
38
41
@@ -56,13 +59,13 @@ uint8_t UdpClass::begin(uint16_t port) {
56
59
/* Send packet contained in buf of length len to peer at specified ip, and port */
57
60
/* Use this function to transmit binary data that might contain 0x00 bytes*/
58
61
/* 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){
60
63
return sendto (_sock,(const uint8_t *)buf,len,ip,port);
61
64
}
62
65
63
66
/* Send zero-terminated string str as packet to peer at specified ip, and port */
64
67
/* 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){
66
69
// compute strlen
67
70
const char *s;
68
71
for (s = str; *s; ++s);
@@ -72,7 +75,7 @@ uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){
72
75
}
73
76
/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes.
74
77
* returned value includes 8 byte UDP header!*/
75
- int UdpClass ::available () {
78
+ int UDP ::available () {
76
79
return W5100.getRXReceivedSize (_sock);
77
80
}
78
81
@@ -82,7 +85,7 @@ int UdpClass::available() {
82
85
/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
83
86
/* so it's easy to overflow buffer. so we check and truncate. */
84
87
/* 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) {
86
89
int packetLen = available ()-8 ; // skip UDP header;
87
90
if (packetLen < 0 ) return 0 ; // no real data here
88
91
if (packetLen > (int )bufLen) {
@@ -131,21 +134,21 @@ int UdpClass::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *
131
134
}
132
135
133
136
/* 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) {
135
138
uint8_t ip[4 ];
136
139
uint16_t port[1 ];
137
140
return recvfrom (_sock,buf,len,ip,port);
138
141
}
139
142
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) {
141
144
uint16_t myPort;
142
145
uint16_t ret = readPacket ( (byte*)buf, bufLen, ip, &myPort);
143
146
port = myPort;
144
147
return ret;
145
148
}
146
149
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 ()
149
152
{
150
153
if (_sock == MAX_SOCK_NUM)
151
154
return ;
0 commit comments