Skip to content

Commit

Permalink
UDP: do not discard bytes in rx_buffer at start of receive
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradRudin committed Nov 18, 2024
1 parent 1797318 commit 23baf4e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
4 changes: 1 addition & 3 deletions include/mav/UDPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ namespace mav {
ConnectionPartner receive(uint8_t *destination, uint32_t size) override {
// Receive as many messages as needed to have enough bytes available (none if already enough bytes)
while (_bytes_available < size && !_should_terminate.load()) {
// If there are residual bytes from last packet, but not enough for parsing new packet, clear out
_bytes_available = 0;
ssize_t ret = ::recvfrom(_socket, _rx_buffer.data(), RX_BUFFER_SIZE, 0,
ssize_t ret = ::recvfrom(_socket, _rx_buffer.data() + _bytes_available, RX_BUFFER_SIZE - _bytes_available, 0,
(struct sockaddr *) nullptr, nullptr);
if (ret < 0) {
throw NetworkError("Could not receive from socket", errno);
Expand Down
4 changes: 1 addition & 3 deletions include/mav/UDPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ namespace mav {
ConnectionPartner receive(uint8_t *destination, uint32_t size) override {
// Receive as many messages as needed to have enough bytes available (none if already enough bytes)
while (_bytes_available < size && !_should_terminate.load()) {
// If there are residual bytes from last packet, but not enough for parsing new packet, clear out
_bytes_available = 0;
struct sockaddr_in source_address{};
socklen_t source_address_length = sizeof(source_address);
ssize_t ret = ::recvfrom(_socket, _rx_buffer.data(), RX_BUFFER_SIZE, 0,
ssize_t ret = ::recvfrom(_socket, _rx_buffer.data() + _bytes_available, RX_BUFFER_SIZE - _bytes_available, 0,
(struct sockaddr*)&source_address, &source_address_length);
if (ret < 0) {
throw NetworkError("Could not receive from socket", errno);
Expand Down

0 comments on commit 23baf4e

Please sign in to comment.