network: Read all available messages on ev_sock_

When more than one message is available to read on the ev_sock_
socket, only the first one is read.

Make some changes to be able to read all the messages available by
setting the socket to non-blocking. This way we can detect when
there's nothing left to read and loop back to wait with epoll.
This commit is contained in:
Anthony PERARD 2021-05-15 16:38:00 +01:00
parent 9357a6cb88
commit 63fdf66ad6

View File

@ -160,6 +160,9 @@ void waybar::modules::Network::createEventSocket() {
if (nl_connect(ev_sock_, NETLINK_ROUTE) != 0) {
throw std::runtime_error("Can't connect network socket");
}
if (nl_socket_set_nonblocking(ev_sock_)) {
throw std::runtime_error("Can't set non-blocking on network socket");
}
nl_socket_add_membership(ev_sock_, RTNLGRP_LINK);
if (family_ == AF_INET) {
nl_socket_add_membership(ev_sock_, RTNLGRP_IPV4_IFADDR);
@ -235,7 +238,23 @@ void waybar::modules::Network::worker() {
int ec = epoll_wait(efd_, events.data(), EPOLL_MAX, -1);
if (ec > 0) {
for (auto i = 0; i < ec; i++) {
if (events[i].data.fd != nl_socket_get_fd(ev_sock_) || nl_recvmsgs_default(ev_sock_) < 0) {
if (events[i].data.fd == nl_socket_get_fd(ev_sock_)) {
int rc = 0;
// Read as many message as possible, until the socket blocks
while (true) {
errno = 0;
rc = nl_recvmsgs_default(ev_sock_);
if (rc == -NLE_AGAIN || errno == EAGAIN) {
rc = 0;
break;
}
}
if (rc < 0) {
spdlog::error("nl_recvmsgs_default error: {}", nl_geterror(-rc));
thread_.stop();
break;
}
} else {
thread_.stop();
break;
}