Skip to content

Commit

Permalink
feat(udp): make polling interval configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Max SCHMELLER <[email protected]>
  • Loading branch information
mojomex committed Dec 13, 2024
1 parent 237c2b0 commit 11ed243
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class UdpSocket

enum class State { UNINITIALIZED, INITIALIZED, BOUND, ACTIVE };

static const int g_poll_timeout_ms = 10;
static constexpr std::string_view broadcast_ip{"255.255.255.255"};

public:
Expand Down Expand Up @@ -196,6 +195,23 @@ class UdpSocket
return *this;
}

/**
* @brief Set the interval at which the socket polls for new data. THis should be longer than the
* expected interval of packets arriving in order to not poll unnecessarily often, and should be
* shorter than the acceptable time delay for `unsubscribe()`. The `unsubscribe()` function blocks
* up to one full poll interval before returning.
*
* @param interval_ms The desired polling interval. See `man poll` for the meanings of 0 and
* negative values.
*/
UdpSocket & set_polling_interval(int32_t interval_ms)
{
if (state_ >= State::ACTIVE) throw UsageError("Poll timeout has to be set before subscribing");

polling_interval_ms_ = interval_ms;
return *this;
}

UdpSocket & set_socket_buffer_size(size_t bytes)
{
if (state_ > State::INITIALIZED) throw UsageError("Buffer size has to be set before binding");
Expand Down Expand Up @@ -349,7 +365,7 @@ class UdpSocket

util::expected<bool, int> is_data_available()
{
int status = poll(&poll_fd_, 1, g_poll_timeout_ms);
int status = poll(&poll_fd_, 1, polling_interval_ms_);
if (status == -1) return errno;
return (poll_fd_.revents & POLLIN) && (status > 0);
}
Expand Down Expand Up @@ -389,6 +405,7 @@ class UdpSocket

int sock_fd_;
pollfd poll_fd_;
int32_t polling_interval_ms_{10};

size_t buffer_size_{1500};
Endpoint host_;
Expand Down

0 comments on commit 11ed243

Please sign in to comment.