stm32duino / STM32Ethernet

Arduino library to support Ethernet for STM32 based board

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EthernetClient::write cannot send large packages

ramboerik opened this issue Β· comments

Hi again πŸ˜ƒ

I'm trying to send a bit larger packages and discovered that it isn't possible with the current implementation. The EthernetClient implementation passes all calls through to LwIP but LwIP's tcp_write refuses larger packages than TCP_SND_BUF = 5840. The doc for tcp_write states:

 * The tcp_write() function will fail and return ERR_MEM if the length
 * of the data exceeds the current send buffer size or if the length of
 * the queue of outgoing segment is larger than the upper limit defined
 * in lwipopts.h. The number of bytes available in the output queue can
 * be retrieved with the tcp_sndbuf() function.
 *
 * The proper way to use this function is to call the function with at
 * most tcp_sndbuf() bytes of data. If the function returns ERR_MEM,
 * the application should wait until some of the currently enqueued
 * data has been successfully received by the other host and try again.
 *

It should be up to the caller (in this case EthernetClient::write) to make multiple calls to tcp_write with the data length less than tcp_sndbuf().

Current tcp_write call in EthernetClient::write

99  if(ERR_OK != tcp_write(_tcp_client->pcb, buf, size, TCP_WRITE_FLAG_COPY)) {
100    return 0;
101  }

I guess a simple fix for this is to loop over tcp_write and split the buffer in smaller sizes if data length is larger than tcp_sndbuf()

Hi @ramboerik
Do not hesitate to provide a PR πŸ˜‰
This is a community project so any help are welcome.

Fix available in #38. tested with larger packet sizes and it works. However, there is no timeout condition in the write loop and it expects that all data can sooner or later be sent by the stack below. if tcp_sndbuf(_tcp_client->pcb) would be set to zero for eternity the write loop would hang. I don't know if that can happen but just pointing it out here.