Revert "refactor(network): fix skipped messages"
This reverts commit 1ccf372f8e
.
This commit is contained in:
parent
7fac483530
commit
1f6f443c48
|
@ -23,6 +23,7 @@ class Network : public ALabel {
|
||||||
static const uint8_t MAX_RETRY = 5;
|
static const uint8_t MAX_RETRY = 5;
|
||||||
static const uint8_t EPOLL_MAX = 255;
|
static const uint8_t EPOLL_MAX = 255;
|
||||||
|
|
||||||
|
static int handleEvents(struct nl_msg*, void*);
|
||||||
static int handleScan(struct nl_msg*, void*);
|
static int handleScan(struct nl_msg*, void*);
|
||||||
|
|
||||||
void worker();
|
void worker();
|
||||||
|
@ -31,7 +32,6 @@ class Network : public ALabel {
|
||||||
void createEventSocket();
|
void createEventSocket();
|
||||||
int getExternalInterface();
|
int getExternalInterface();
|
||||||
void getInterfaceAddress();
|
void getInterfaceAddress();
|
||||||
void handleEvents();
|
|
||||||
int netlinkRequest(void*, uint32_t, uint32_t groups = 0);
|
int netlinkRequest(void*, uint32_t, uint32_t groups = 0);
|
||||||
int netlinkResponse(void*, uint32_t, uint32_t groups = 0);
|
int netlinkResponse(void*, uint32_t, uint32_t groups = 0);
|
||||||
void parseEssid(struct nlattr**);
|
void parseEssid(struct nlattr**);
|
||||||
|
@ -45,7 +45,7 @@ class Network : public ALabel {
|
||||||
sa_family_t family_;
|
sa_family_t family_;
|
||||||
struct sockaddr_nl nladdr_ = {0};
|
struct sockaddr_nl nladdr_ = {0};
|
||||||
struct nl_sock* sk_ = nullptr;
|
struct nl_sock* sk_ = nullptr;
|
||||||
int info_sock_;
|
struct nl_sock* info_sock_ = nullptr;
|
||||||
int efd_;
|
int efd_;
|
||||||
int ev_fd_;
|
int ev_fd_;
|
||||||
int nl80211_id_;
|
int nl80211_id_;
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
#include "modules/network.hpp"
|
#include "modules/network.hpp"
|
||||||
|
|
||||||
waybar::modules::Network::Network(const std::string& id, const Json::Value& config)
|
waybar::modules::Network::Network(const std::string& id, const Json::Value& config)
|
||||||
: ALabel(config, "{ifname}", 60), family_(AF_INET), info_sock_(-1), efd_(-1),
|
: ALabel(config, "{ifname}", 60), family_(AF_INET), efd_(-1), ev_fd_(-1),
|
||||||
ev_fd_(-1), cidr_(-1), signal_strength_dbm_(0), signal_strength_(0)
|
cidr_(-1), signal_strength_dbm_(0), signal_strength_(0)
|
||||||
{
|
{
|
||||||
label_.set_name("network");
|
label_.set_name("network");
|
||||||
if (!id.empty()) {
|
if (!id.empty()) {
|
||||||
|
@ -39,8 +39,11 @@ waybar::modules::Network::~Network()
|
||||||
if (efd_ > -1) {
|
if (efd_ > -1) {
|
||||||
close(efd_);
|
close(efd_);
|
||||||
}
|
}
|
||||||
if (info_sock_ != -1) {
|
if (info_sock_ != nullptr) {
|
||||||
close(info_sock_);
|
nl_socket_drop_membership(info_sock_, RTMGRP_LINK);
|
||||||
|
nl_socket_drop_membership(info_sock_, RTMGRP_IPV4_IFADDR);
|
||||||
|
nl_close(info_sock_);
|
||||||
|
nl_socket_free(info_sock_);
|
||||||
}
|
}
|
||||||
if (sk_ != nullptr) {
|
if (sk_ != nullptr) {
|
||||||
nl_close(sk_);
|
nl_close(sk_);
|
||||||
|
@ -50,17 +53,19 @@ waybar::modules::Network::~Network()
|
||||||
|
|
||||||
void waybar::modules::Network::createInfoSocket()
|
void waybar::modules::Network::createInfoSocket()
|
||||||
{
|
{
|
||||||
struct sockaddr_nl sa;
|
info_sock_ = nl_socket_alloc();
|
||||||
info_sock_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
if (nl_connect(info_sock_, NETLINK_ROUTE) != 0) {
|
||||||
if (info_sock_ < 0) {
|
|
||||||
throw std::runtime_error("Can't connect network socket");
|
throw std::runtime_error("Can't connect network socket");
|
||||||
}
|
}
|
||||||
sa.nl_family = AF_NETLINK;
|
if (nl_socket_add_membership(info_sock_, RTMGRP_LINK) != 0) {
|
||||||
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
|
|
||||||
auto ret = bind(info_sock_, (struct sockaddr *)&sa, sizeof(sa));
|
|
||||||
if (ret < 0) {
|
|
||||||
throw std::runtime_error("Can't add membership");
|
throw std::runtime_error("Can't add membership");
|
||||||
}
|
}
|
||||||
|
if (nl_socket_add_membership(info_sock_, RTMGRP_IPV4_IFADDR) != 0) {
|
||||||
|
throw std::runtime_error("Can't add membership");
|
||||||
|
}
|
||||||
|
nl_socket_disable_seq_check(info_sock_);
|
||||||
|
nl_socket_set_nonblocking(info_sock_);
|
||||||
|
nl_socket_modify_cb(info_sock_, NL_CB_VALID, NL_CB_CUSTOM, handleEvents, this);
|
||||||
efd_ = epoll_create1(0);
|
efd_ = epoll_create1(0);
|
||||||
if (efd_ < 0) {
|
if (efd_ < 0) {
|
||||||
throw std::runtime_error("Can't create epoll");
|
throw std::runtime_error("Can't create epoll");
|
||||||
|
@ -75,10 +80,11 @@ void waybar::modules::Network::createInfoSocket()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
auto fd = nl_socket_get_fd(info_sock_);
|
||||||
struct epoll_event event;
|
struct epoll_event event;
|
||||||
event.events = EPOLLIN | EPOLLET;
|
event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
|
||||||
event.data.fd = info_sock_;
|
event.data.fd = fd;
|
||||||
if (epoll_ctl(efd_, EPOLL_CTL_ADD, info_sock_, &event) == -1) {
|
if (epoll_ctl(efd_, EPOLL_CTL_ADD, fd, &event) == -1) {
|
||||||
throw std::runtime_error("Can't add epoll event");
|
throw std::runtime_error("Can't add epoll event");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,15 +114,16 @@ void waybar::modules::Network::worker()
|
||||||
}
|
}
|
||||||
thread_timer_.sleep_for(interval_);
|
thread_timer_.sleep_for(interval_);
|
||||||
};
|
};
|
||||||
struct epoll_event events[EPOLL_MAX] = {0};
|
struct epoll_event events[EPOLL_MAX];
|
||||||
thread_ = [this, &events] {
|
thread_ = [this, &events] {
|
||||||
int ec = epoll_wait(efd_, events, EPOLL_MAX, -1);
|
int ec = epoll_wait(efd_, events, EPOLL_MAX, -1);
|
||||||
if (ec > 0) {
|
if (ec > 0) {
|
||||||
for (auto i = 0; i < ec; i++) {
|
for (auto i = 0; i < ec; i++) {
|
||||||
if (events[i].data.fd == ev_fd_) {
|
if (events[i].data.fd == nl_socket_get_fd(info_sock_)) {
|
||||||
|
nl_recvmsgs_default(info_sock_);
|
||||||
|
} else {
|
||||||
thread_.stop();
|
thread_.stop();
|
||||||
} else if (events[i].events & EPOLLIN) {
|
break;
|
||||||
handleEvents();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (ec == -1) {
|
} else if (ec == -1) {
|
||||||
|
@ -370,7 +377,7 @@ int waybar::modules::Network::netlinkRequest(void *req,
|
||||||
sa.nl_groups = groups;
|
sa.nl_groups = groups;
|
||||||
struct iovec iov = { req, reqlen };
|
struct iovec iov = { req, reqlen };
|
||||||
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
|
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
|
||||||
return sendmsg(info_sock_, &msg, 0);
|
return sendmsg(nl_socket_get_fd(info_sock_), &msg, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int waybar::modules::Network::netlinkResponse(void *resp,
|
int waybar::modules::Network::netlinkResponse(void *resp,
|
||||||
|
@ -381,64 +388,53 @@ int waybar::modules::Network::netlinkResponse(void *resp,
|
||||||
sa.nl_groups = groups;
|
sa.nl_groups = groups;
|
||||||
struct iovec iov = { resp, resplen };
|
struct iovec iov = { resp, resplen };
|
||||||
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
|
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
|
||||||
auto ret = recvmsg(info_sock_, &msg, 0);
|
auto ret = recvmsg(nl_socket_get_fd(info_sock_), &msg, 0);
|
||||||
if (msg.msg_flags & MSG_TRUNC) {
|
if (msg.msg_flags & MSG_TRUNC) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::Network::handleEvents() {
|
int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
|
||||||
struct sockaddr_nl addr;
|
auto net = static_cast<waybar::modules::Network *>(data);
|
||||||
char buff[2048] = {0};
|
|
||||||
socklen_t len = 0;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
len = sizeof(addr);
|
|
||||||
auto ret = recvfrom(info_sock_, (void *)buff, sizeof(buff), 0,
|
|
||||||
(struct sockaddr *)&addr, &len);
|
|
||||||
auto nh = (struct nlmsghdr *)buff;
|
|
||||||
for(; NLMSG_OK(nh, ret); nh = NLMSG_NEXT(nh, ret)) {
|
|
||||||
bool need_update = false;
|
bool need_update = false;
|
||||||
|
nlmsghdr *nh = nlmsg_hdr(msg);
|
||||||
if (nh->nlmsg_type == RTM_NEWADDR) {
|
if (nh->nlmsg_type == RTM_NEWADDR) {
|
||||||
need_update = true;
|
need_update = true;
|
||||||
}
|
}
|
||||||
if (nh->nlmsg_type < RTM_NEWADDR) {
|
if (nh->nlmsg_type < RTM_NEWADDR) {
|
||||||
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
|
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
|
||||||
if (rtif->ifi_index == static_cast<int>(ifid_)) {
|
if (rtif->ifi_index == static_cast<int>(net->ifid_)) {
|
||||||
need_update = true;
|
need_update = true;
|
||||||
if (!(rtif->ifi_flags & IFF_RUNNING)) {
|
if (!(rtif->ifi_flags & IFF_RUNNING)) {
|
||||||
disconnected();
|
net->disconnected();
|
||||||
dp.emit();
|
net->dp.emit();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ifid_ <= 0 && !config_["interface"].isString()) {
|
if (net->ifid_ <= 0 && !net->config_["interface"].isString()) {
|
||||||
for (uint8_t i = 0; i < MAX_RETRY; i += 1) {
|
for (uint8_t i = 0; i < MAX_RETRY; i += 1) {
|
||||||
ifid_ = getExternalInterface();
|
net->ifid_ = net->getExternalInterface();
|
||||||
if (ifid_ > 0) {
|
if (net->ifid_ > 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Need to wait before get external interface
|
// Need to wait before get external interface
|
||||||
thread_.sleep_for(std::chrono::seconds(1));
|
net->thread_.sleep_for(std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
if (ifid_ > 0) {
|
if (net->ifid_ > 0) {
|
||||||
char ifname[IF_NAMESIZE];
|
char ifname[IF_NAMESIZE];
|
||||||
if_indextoname(ifid_, ifname);
|
if_indextoname(net->ifid_, ifname);
|
||||||
ifname_ = ifname;
|
net->ifname_ = ifname;
|
||||||
need_update = true;
|
need_update = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (need_update) {
|
if (need_update) {
|
||||||
if (ifid_ > 0) {
|
if (net->ifid_ > 0) {
|
||||||
getInfo();
|
net->getInfo();
|
||||||
}
|
|
||||||
dp.emit();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
net->dp.emit();
|
||||||
}
|
}
|
||||||
|
return NL_SKIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
int waybar::modules::Network::handleScan(struct nl_msg *msg, void *data) {
|
int waybar::modules::Network::handleScan(struct nl_msg *msg, void *data) {
|
||||||
|
|
Loading…
Reference in New Issue