Skip to content

Commit dc38b84

Browse files
committed
EthernetClient fixes
1 parent ca94b4a commit dc38b84

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/EthernetClient.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ UIPClient::connect(IPAddress ip, uint16_t port)
5353
if (conn)
5454
{
5555
#if UIP_CONNECT_TIMEOUT > 0
56-
int32_t timeout = millis() + 1000 * UIP_CONNECT_TIMEOUT;
56+
uint32_t timeout = millis() + 1000 * UIP_CONNECT_TIMEOUT;
5757
#endif
5858
while((conn->tcpstateflags & UIP_TS_MASK) != UIP_CLOSED)
5959
{
@@ -108,7 +108,6 @@ UIPClient::stop()
108108
Serial.println(F("before stop(), with data"));
109109
_dumpAllData();
110110
#endif
111-
_flushBlocks(data->packets_in);
112111
if (data->state & UIP_CLIENT_REMOTECLOSED)
113112
{
114113
data->state = 0;
@@ -118,6 +117,7 @@ UIPClient::stop()
118117
flush();
119118
data->state |= UIP_CLIENT_CLOSE;
120119
}
120+
_flushBlocks(data->packets_in);
121121
#ifdef UIPETHERNET_DEBUG_CLIENT
122122
Serial.println(F("after stop()"));
123123
_dumpAllData();
@@ -170,7 +170,7 @@ UIPClient::write(const uint8_t *buf, size_t size)
170170
UIPEthernetClass::tick();
171171
if (u && u->state && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
172172
{
173-
uint8_t p = _currentBlock(u->packets_out);
173+
uint8_t p = _currentBlock(u->packets_out, u->out_pos);
174174
if (u->packets_out[p] == NOBLOCK)
175175
{
176176
newpacket:
@@ -208,7 +208,10 @@ UIPClient::write(const uint8_t *buf, size_t size)
208208
if (remain > 0)
209209
{
210210
if (p == 0) // block 0 just filled, start sending immediately
211+
{
211212
flush();
213+
goto repeat;
214+
}
212215
if (p == UIP_SOCKET_NUMPACKETS-1)
213216
{
214217
#if UIP_WRITE_TIMEOUT > 0
@@ -237,7 +240,7 @@ UIPClient::availableForWrite()
237240
UIPEthernetClass::tick();
238241
if (data->packets_out[0] == NOBLOCK)
239242
return MAX_AVAILABLE;
240-
uint8_t p = _currentBlock(data->packets_out);
243+
uint8_t p = _currentBlock(data->packets_out, data->out_pos);
241244
int used = UIP_SOCKET_DATALEN * p + data->out_pos;
242245
return MAX_AVAILABLE - used;
243246
}
@@ -401,7 +404,7 @@ uipclient_appcall(void)
401404
Serial.print(F("UIPClient uip_newdata, uip_len:"));
402405
Serial.println(uip_len);
403406
#endif
404-
if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
407+
if (uip_len && u->state && !(u->state & UIP_CLIENT_CLOSE))
405408
{
406409
for (uint8_t i=0; i < UIP_SOCKET_NUMPACKETS; i++)
407410
{
@@ -539,12 +542,18 @@ UIPClient::_allocateData()
539542
}
540543

541544
uint8_t
542-
UIPClient::_currentBlock(memhandle* block)
545+
UIPClient::_currentBlock(memhandle* block, uint16_t out_pos)
543546
{
547+
if (block[0] == NOBLOCK)
548+
return 0;
544549
for (uint8_t i = 1; i < UIP_SOCKET_NUMPACKETS; i++)
545550
{
546551
if (block[i] == NOBLOCK)
552+
{
553+
if (out_pos == Enc28J60Network::blockSize(block[i-1])) // block is full
554+
return i;
547555
return i-1;
556+
}
548557
}
549558
return UIP_SOCKET_NUMPACKETS-1;
550559
}

src/EthernetClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class EthernetClient : public Client {
8787
static uip_userdata_t all_data[UIP_CONNS];
8888
static uip_userdata_t* _allocateData();
8989

90-
static uint8_t _currentBlock(memhandle* blocks);
90+
static uint8_t _currentBlock(memhandle* blocks, uint16_t out_pos);
9191
static void _eatBlock(memhandle* blocks);
9292
static void _flushBlocks(memhandle* blocks);
9393

0 commit comments

Comments
 (0)