Skip to content

Commit

Permalink
Fix: parsePacket get's next received packet, flush discards it (or pa…
Browse files Browse the repository at this point in the history
…rsePacket, whatever's called first).
  • Loading branch information
aentinger committed Oct 18, 2024
1 parent cf06dce commit b8d129b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/Arduino_10BASE_T1S_UDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Arduino_10BASE_T1S_UDP::Arduino_10BASE_T1S_UDP()
: _udp_pcb{nullptr}
, _send_to_ip{0,0,0,0}
, _send_to_port{0}
, _rx_pkt{nullptr}
{

}
Expand Down Expand Up @@ -136,70 +137,83 @@ size_t Arduino_10BASE_T1S_UDP::write(const uint8_t * buffer, size_t size)
int Arduino_10BASE_T1S_UDP::parsePacket()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->totalSize();
{
/* Discard UdpRxPacket object previously held by _rx_pkt
* and replace it with the new one.
*/
_rx_pkt = _rx_pkt_list.front();
_rx_pkt_list.pop_front();
return _rx_pkt->totalSize();
}
else
{
/* Otherwise ensure that _rx_pkt definitely
* does not hold any UdpRxPacket object anymore.
*/
_rx_pkt.reset();
return 0;
}
}

int Arduino_10BASE_T1S_UDP::available()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->available();
if (_rx_pkt)
return _rx_pkt->available();
else
return 0;
}

int Arduino_10BASE_T1S_UDP::read()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->read();
if (_rx_pkt)
return _rx_pkt->read();
else
return -1;
}

int Arduino_10BASE_T1S_UDP::read(unsigned char* buffer, size_t len)
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->read(buffer, len);
if (_rx_pkt)
return _rx_pkt->read(buffer, len);
else
return -1;
}

int Arduino_10BASE_T1S_UDP::read(char* buffer, size_t len)
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->read(buffer, len);
if (_rx_pkt)
return _rx_pkt->read(buffer, len);
else
return -1;
}

int Arduino_10BASE_T1S_UDP::peek()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->peek();
if (_rx_pkt)
return _rx_pkt->peek();
else
return -1;
}

void Arduino_10BASE_T1S_UDP::flush()
{
/* Drop packet from receive buffer. */
if (_rx_pkt_list.size())
_rx_pkt_list.pop_front();
/* Delete UdpRxPacket object held by _rx_pkt. */
if (_rx_pkt)
_rx_pkt.reset();
}

IPAddress Arduino_10BASE_T1S_UDP::remoteIP()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->remoteIP();
if (_rx_pkt)
return _rx_pkt->remoteIP();
else
return IPAddress();
}

uint16_t Arduino_10BASE_T1S_UDP::remotePort()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->remotePort();
if (_rx_pkt)
return _rx_pkt->remotePort();
else
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/Arduino_10BASE_T1S_UDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ class Arduino_10BASE_T1S_UDP : public UDP
}
};
std::list<UdpRxPacket::SharedPtr> _rx_pkt_list;
UdpRxPacket::SharedPtr _rx_pkt;
};

0 comments on commit b8d129b

Please sign in to comment.