Skip to content

Commit 35c738c

Browse files
committed
EthernetClient - UIP_WRITE_TIMEOUT instead of UIP_ATTEMPTS_ON_WRITE
and static _write merged to not static version to use setWriteError() on timeout
1 parent 4fe83a5 commit 35c738c

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

src/EthernetClient.cpp

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,17 @@ UIPClient::operator bool()
153153
size_t
154154
UIPClient::write(uint8_t c)
155155
{
156-
return _write(data, &c, 1);
156+
return write(&c, 1);
157157
}
158158

159159
size_t
160160
UIPClient::write(const uint8_t *buf, size_t size)
161161
{
162-
return _write(data, buf, size);
163-
}
164-
165-
size_t
166-
UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size)
167-
{
162+
uip_userdata_t* u = data;
168163
int remain = size;
169164
uint16_t written;
170-
#if UIP_ATTEMPTS_ON_WRITE > 0
171-
uint16_t attempts = UIP_ATTEMPTS_ON_WRITE;
165+
#if UIP_WRITE_TIMEOUT > 0
166+
uint32_t timeout_start = millis();
172167
#endif
173168
repeat:
174169
UIPEthernetClass::tick();
@@ -181,13 +176,14 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size)
181176
u->packets_out[p] = Enc28J60Network::allocBlock(UIP_SOCKET_DATALEN);
182177
if (u->packets_out[p] == NOBLOCK)
183178
{
184-
#if UIP_ATTEMPTS_ON_WRITE > 0
185-
if ((--attempts)>0)
186-
#endif
187-
#if UIP_ATTEMPTS_ON_WRITE != 0
188-
goto repeat;
179+
#if UIP_WRITE_TIMEOUT > 0
180+
if (millis() - timeout_start > UIP_WRITE_TIMEOUT)
181+
{
182+
setWriteError();
183+
goto ready;
184+
}
189185
#endif
190-
goto ready;
186+
goto repeat;
191187
}
192188
u->out_pos = 0;
193189
}
@@ -211,13 +207,14 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size)
211207
{
212208
if (p == UIP_SOCKET_NUMPACKETS-1)
213209
{
214-
#if UIP_ATTEMPTS_ON_WRITE > 0
215-
if ((--attempts)>0)
216-
#endif
217-
#if UIP_ATTEMPTS_ON_WRITE != 0
218-
goto repeat;
210+
#if UIP_WRITE_TIMEOUT > 0
211+
if (millis() - timeout_start > UIP_WRITE_TIMEOUT)
212+
{
213+
setWriteError();
214+
goto ready;
215+
}
219216
#endif
220-
goto ready;
217+
goto repeat;
221218
}
222219
p++;
223220
goto newpacket;
@@ -253,22 +250,15 @@ UIPClient::available()
253250
if (!(*this))
254251
return 0;
255252

256-
int len = _available(data);
257-
258-
// if sketch checks for incoming data and there are unsent data, flush the transmit buffer
259-
if (!len)
260-
flush();
261-
return len;
262-
}
263-
264-
int
265-
UIPClient::_available(uip_userdata_t *u)
266-
{
267253
int len = 0;
268254
for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++)
269255
{
270-
len += Enc28J60Network::blockSize(u->packets_in[i]);
256+
len += Enc28J60Network::blockSize(data->packets_in[i]);
271257
}
258+
259+
// if sketch checks for incoming data and there are unsent data, flush the transmit buffer
260+
if (!len)
261+
flush();
272262
return len;
273263
}
274264

src/EthernetClient.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ class EthernetClient : public Client {
8888
static uip_userdata_t all_data[UIP_CONNS];
8989
static uip_userdata_t* _allocateData();
9090

91-
static size_t _write(uip_userdata_t *,const uint8_t *buf, size_t size);
92-
static int _available(uip_userdata_t *);
93-
9491
static uint8_t _currentBlock(memhandle* blocks);
9592
static void _eatBlock(memhandle* blocks);
9693
static void _flushBlocks(memhandle* blocks);

src/utility/uipethernet-conf.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#define UIP_CONF_UDP_CONNS 4
1919
#endif
2020

21-
/* number of attempts on write before returning number of bytes sent so far
22-
* set to -1 to block until connection is closed by timeout */
23-
#ifndef UIP_ATTEMPTS_ON_WRITE
24-
#define UIP_ATTEMPTS_ON_WRITE -1
21+
/* timeout in ms for attempts to get a free memory block to write
22+
* before returning number of bytes sent so far
23+
* set to 0 to block until connection is closed by timeout */
24+
#ifndef UIP_WRITE_TIMEOUT
25+
#define UIP_WRITE_TIMEOUT 2000
2526
#endif
2627

2728
/* timeout after which UIPClient::connect gives up. The timeout is specified in seconds.

0 commit comments

Comments
 (0)