Skip to content

Commit dc01131

Browse files
ramboerikfpistm
authored andcommitted
Fix issue with EthernetClient::write and large packages
Fixes #37
1 parent 87a5de5 commit dc01131

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/EthernetClient.cpp

+27-8
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,35 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
9696
return 0;
9797
}
9898

99-
if(ERR_OK != tcp_write(_tcp_client->pcb, buf, size, TCP_WRITE_FLAG_COPY)) {
100-
return 0;
101-
}
99+
size_t max_send_size, bytes_to_send;
100+
size_t bytes_sent = 0;
101+
size_t bytes_left = size;
102+
err_t res;
103+
104+
do{
105+
max_send_size = tcp_sndbuf(_tcp_client->pcb);
106+
bytes_to_send = bytes_left > max_send_size ? max_send_size : bytes_left;
107+
108+
if(bytes_to_send > 0){
109+
res = tcp_write(_tcp_client->pcb, &buf[bytes_sent], bytes_to_send, TCP_WRITE_FLAG_COPY);
110+
111+
if(res == ERR_OK){
112+
bytes_sent += bytes_to_send;
113+
bytes_left = size - bytes_sent;
114+
}
115+
else if(res != ERR_MEM){
116+
// other error, cannot continue
117+
return 0;
118+
}
119+
}
102120

103-
//Force to send data right now!
104-
if(ERR_OK != tcp_output(_tcp_client->pcb)) {
105-
return 0;
106-
}
121+
//Force to send data right now!
122+
if(ERR_OK != tcp_output(_tcp_client->pcb)) {
123+
return 0;
124+
}
125+
stm32_eth_scheduler();
107126

108-
stm32_eth_scheduler();
127+
} while(bytes_sent != size);
109128

110129
return size;
111130
}

0 commit comments

Comments
 (0)