@@ -37,182 +37,182 @@ EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {}
37
37
38
38
/* Start EthernetUDP socket, listening at local port PORT */
39
39
uint8_t EthernetUDP::begin (uint16_t port) {
40
- if (_sock != MAX_SOCK_NUM)
41
- return 0 ;
40
+ if (_sock != MAX_SOCK_NUM)
41
+ return 0 ;
42
42
43
- for (int i = 0 ; i < MAX_SOCK_NUM; i++) {
44
- uint8_t s = W5100.readSnSR (i);
45
- if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
46
- _sock = i;
47
- break ;
48
- }
49
- }
43
+ for (int i = 0 ; i < MAX_SOCK_NUM; i++) {
44
+ uint8_t s = W5100.readSnSR (i);
45
+ if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
46
+ _sock = i;
47
+ break ;
48
+ }
49
+ }
50
50
51
- if (_sock == MAX_SOCK_NUM)
52
- return 0 ;
51
+ if (_sock == MAX_SOCK_NUM)
52
+ return 0 ;
53
53
54
- _port = port;
55
- _remaining = 0 ;
56
- socket (_sock, SnMR::UDP, _port, 0 );
54
+ _port = port;
55
+ _remaining = 0 ;
56
+ socket (_sock, SnMR::UDP, _port, 0 );
57
57
58
- return 1 ;
58
+ return 1 ;
59
59
}
60
60
61
61
/* return number of bytes available in the current packet,
62
62
will return zero if parsePacket hasn't been called yet */
63
63
int EthernetUDP::available () {
64
- return _remaining;
64
+ return _remaining;
65
65
}
66
66
67
67
/* Release any resources being used by this EthernetUDP instance */
68
68
void EthernetUDP::stop ()
69
69
{
70
- if (_sock == MAX_SOCK_NUM)
71
- return ;
70
+ if (_sock == MAX_SOCK_NUM)
71
+ return ;
72
72
73
- close (_sock);
73
+ close (_sock);
74
74
75
- EthernetClass::_server_port[_sock] = 0 ;
76
- _sock = MAX_SOCK_NUM;
75
+ EthernetClass::_server_port[_sock] = 0 ;
76
+ _sock = MAX_SOCK_NUM;
77
77
}
78
78
79
79
int EthernetUDP::beginPacket (const char *host, uint16_t port)
80
80
{
81
- // Look up the host first
82
- int ret = 0 ;
83
- DNSClient dns;
84
- IPAddress remote_addr;
85
-
86
- dns.begin (Ethernet.dnsServerIP ());
87
- ret = dns.getHostByName (host, remote_addr);
88
- if (ret == 1 ) {
89
- return beginPacket (remote_addr, port);
90
- } else {
91
- return ret;
92
- }
81
+ // Look up the host first
82
+ int ret = 0 ;
83
+ DNSClient dns;
84
+ IPAddress remote_addr;
85
+
86
+ dns.begin (Ethernet.dnsServerIP ());
87
+ ret = dns.getHostByName (host, remote_addr);
88
+ if (ret == 1 ) {
89
+ return beginPacket (remote_addr, port);
90
+ } else {
91
+ return ret;
92
+ }
93
93
}
94
94
95
95
int EthernetUDP::beginPacket (IPAddress ip, uint16_t port)
96
96
{
97
- _offset = 0 ;
98
- return startUDP (_sock, rawIPAddress (ip), port);
97
+ _offset = 0 ;
98
+ return startUDP (_sock, rawIPAddress (ip), port);
99
99
}
100
100
101
101
int EthernetUDP::endPacket ()
102
102
{
103
- return sendUDP (_sock);
103
+ return sendUDP (_sock);
104
104
}
105
105
106
106
size_t EthernetUDP::write (uint8_t byte)
107
107
{
108
- return write (&byte, 1 );
108
+ return write (&byte, 1 );
109
109
}
110
110
111
111
size_t EthernetUDP::write (const uint8_t *buffer, size_t size)
112
112
{
113
- uint16_t bytes_written = bufferData (_sock, _offset, buffer, size);
114
- _offset += bytes_written;
115
- return bytes_written;
113
+ uint16_t bytes_written = bufferData (_sock, _offset, buffer, size);
114
+ _offset += bytes_written;
115
+ return bytes_written;
116
116
}
117
117
118
118
int EthernetUDP::parsePacket ()
119
119
{
120
- // discard any remaining bytes in the last packet
121
- flush ();
122
-
123
- if (W5100.getRXReceivedSize (_sock) > 0 )
124
- {
125
- // HACK - hand-parse the UDP packet using TCP recv method
126
- uint8_t tmpBuf[8 ];
127
- int ret =0 ;
128
- // read 8 header bytes and get IP and port from it
129
- ret = recv (_sock,tmpBuf,8 );
130
- if (ret > 0 )
131
- {
132
- _remoteIP = tmpBuf;
133
- _remotePort = tmpBuf[4 ];
134
- _remotePort = (_remotePort << 8 ) + tmpBuf[5 ];
135
- _remaining = tmpBuf[6 ];
136
- _remaining = (_remaining << 8 ) + tmpBuf[7 ];
137
-
138
- // When we get here, any remaining bytes are the data
139
- ret = _remaining;
140
- }
141
- return ret;
142
- }
143
- // There aren't any packets available
144
- return 0 ;
120
+ // discard any remaining bytes in the last packet
121
+ flush ();
122
+
123
+ if (W5100.getRXReceivedSize (_sock) > 0 )
124
+ {
125
+ // HACK - hand-parse the UDP packet using TCP recv method
126
+ uint8_t tmpBuf[8 ];
127
+ int ret =0 ;
128
+ // read 8 header bytes and get IP and port from it
129
+ ret = recv (_sock,tmpBuf,8 );
130
+ if (ret > 0 )
131
+ {
132
+ _remoteIP = tmpBuf;
133
+ _remotePort = tmpBuf[4 ];
134
+ _remotePort = (_remotePort << 8 ) + tmpBuf[5 ];
135
+ _remaining = tmpBuf[6 ];
136
+ _remaining = (_remaining << 8 ) + tmpBuf[7 ];
137
+
138
+ // When we get here, any remaining bytes are the data
139
+ ret = _remaining;
140
+ }
141
+ return ret;
142
+ }
143
+ // There aren't any packets available
144
+ return 0 ;
145
145
}
146
146
147
147
int EthernetUDP::read ()
148
148
{
149
- uint8_t byte;
149
+ uint8_t byte;
150
150
151
- if ((_remaining > 0 ) && (recv (_sock, &byte, 1 ) > 0 ))
152
- {
153
- // We read things without any problems
154
- _remaining--;
155
- return byte;
156
- }
151
+ if ((_remaining > 0 ) && (recv (_sock, &byte, 1 ) > 0 ))
152
+ {
153
+ // We read things without any problems
154
+ _remaining--;
155
+ return byte;
156
+ }
157
157
158
- // If we get here, there's no data available
159
- return -1 ;
158
+ // If we get here, there's no data available
159
+ return -1 ;
160
160
}
161
161
162
162
int EthernetUDP::read (unsigned char * buffer, size_t len)
163
163
{
164
164
165
- if (_remaining > 0 )
166
- {
165
+ if (_remaining > 0 )
166
+ {
167
167
168
- int got;
168
+ int got;
169
169
170
- if (_remaining <= len)
171
- {
172
- // data should fit in the buffer
173
- got = recv (_sock, buffer, _remaining);
174
- }
175
- else
176
- {
177
- // too much data for the buffer,
178
- // grab as much as will fit
179
- got = recv (_sock, buffer, len);
180
- }
170
+ if (_remaining <= len)
171
+ {
172
+ // data should fit in the buffer
173
+ got = recv (_sock, buffer, _remaining);
174
+ }
175
+ else
176
+ {
177
+ // too much data for the buffer,
178
+ // grab as much as will fit
179
+ got = recv (_sock, buffer, len);
180
+ }
181
181
182
- if (got > 0 )
183
- {
184
- _remaining -= got;
185
- return got;
186
- }
182
+ if (got > 0 )
183
+ {
184
+ _remaining -= got;
185
+ return got;
186
+ }
187
187
188
- }
188
+ }
189
189
190
- // If we get here, there's no data available or recv failed
191
- return -1 ;
190
+ // If we get here, there's no data available or recv failed
191
+ return -1 ;
192
192
193
193
}
194
194
195
195
int EthernetUDP::peek ()
196
196
{
197
- uint8_t b;
198
- // Unlike recv, peek doesn't check to see if there's any data available, so we must.
199
- // If the user hasn't called parsePacket yet then return nothing otherwise they
200
- // may get the UDP header
201
- if (!_remaining)
202
- return -1 ;
203
- ::peek (_sock, &b);
204
- return b;
197
+ uint8_t b;
198
+ // Unlike recv, peek doesn't check to see if there's any data available, so we must.
199
+ // If the user hasn't called parsePacket yet then return nothing otherwise they
200
+ // may get the UDP header
201
+ if (!_remaining)
202
+ return -1 ;
203
+ ::peek (_sock, &b);
204
+ return b;
205
205
}
206
206
207
207
void EthernetUDP::flush ()
208
208
{
209
- // could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
210
- // should only occur if recv fails after telling us the data is there, lets
211
- // hope the w5100 always behaves :)
212
-
213
- while (_remaining)
214
- {
215
- read ();
216
- }
209
+ // could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
210
+ // should only occur if recv fails after telling us the data is there, lets
211
+ // hope the w5100 always behaves :)
212
+
213
+ while (_remaining)
214
+ {
215
+ read ();
216
+ }
217
217
}
218
218
0 commit comments