arduino-libraries / Ethernet

Ethernet Library for Arduino
http://arduino.cc/
252 stars 253 forks source link

`EthernetClass::socketRecvAvailable()` is not updated when called multiple times #259

Open Bascy opened 2 months ago

Bascy commented 2 months ago

When EthernetClass::socketRecvAvailable() is called multiple times, i.e. to wait for a minimum number of chars to reac, the returned number of recived bytes is not updated in between.

SSLClient (https://github.com/govorox/SSLClient.git) needs this in ssl__client.cpp, where in client_net_recv_timeout the process will wait until the expected number of bytes is received or a timeout is triggers.

uint16_t EthernetClass::socketRecvAvailable(uint8_t s)
{
  uint16_t ret = state[s].RX_RSR;
  if (ret == 0) {
    SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
    uint16_t rsr = getSnRX_RSR(s);
    SPI.endTransaction();
    ret = rsr - state[s].RX_inc;
    state[s].RX_RSR = ret;
  }
  return ret;
}

This solves the problem I have with SSLClient

uint16_t EthernetClass::socketRecvAvailable(uint8_t s)
{
  SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
  uint16_t rsr = getSnRX_RSR(s);
  SPI.endTransaction();
  state[s].RX_RSR = rsr - state[s].RX_inc;
  return state[s].RX_RSR;
}