Skip to content
Snippets Groups Projects
Commit 1b1b54e9 authored by Recolic's avatar Recolic :house_with_garden:
Browse files

quick_poll

parent 298b1717
No related branches found
No related tags found
No related merge requests found
Pipeline #907 passed with stage
in 3 minutes and 3 seconds
...@@ -17,6 +17,7 @@ namespace rlib { ...@@ -17,6 +17,7 @@ namespace rlib {
#include <fcntl.h> #include <fcntl.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <poll.h>
#endif #endif
// Include winsock2.h before windows.h // Include winsock2.h before windows.h
...@@ -24,6 +25,7 @@ namespace rlib { ...@@ -24,6 +25,7 @@ namespace rlib {
#include <cstdlib> #include <cstdlib>
#include <unistd.h> #include <unistd.h>
#include <string> #include <string>
#include <list>
#include <stdexcept> #include <stdexcept>
#include <rlib/sys/fd.hpp> #include <rlib/sys/fd.hpp>
...@@ -31,6 +33,9 @@ namespace rlib { ...@@ -31,6 +33,9 @@ namespace rlib {
#include <rlib/string.hpp> #include <rlib/string.hpp>
#include <rlib/scope_guard.hpp> #include <rlib/scope_guard.hpp>
#if RLIB_CXX_STD >= 2020
#include <ranges>
#endif
namespace rlib { namespace rlib {
// Both POSIX and Win32. Note what MinGW32 does not have POSIX support, so network operations will not compile on them. // Both POSIX and Win32. Note what MinGW32 does not have POSIX support, so network operations will not compile on them.
...@@ -68,8 +73,6 @@ namespace rlib { ...@@ -68,8 +73,6 @@ namespace rlib {
ipstr = std::string() + '[' + str + ']'; ipstr = std::string() + '[' + str + ']';
} }
return {ipstr, port}; return {ipstr, port};
} }
#if RLIB_OS_ID != OS_WINDOWS #if RLIB_OS_ID != OS_WINDOWS
...@@ -93,6 +96,36 @@ namespace rlib { ...@@ -93,6 +96,36 @@ namespace rlib {
} }
#endif #endif
#if RLIB_OS_ID != OS_WINDOWS
/*
* poll() multiple sockets, and return when one of them has event.
*/
template <typename Iterable>
#if RLIB_CXX_STD >= 2020
requires std::ranges::range<Iterable> && std::is_convertible_v<std::ranges::range_value_t<Iterable>, fd_t>
#endif
static inline auto quick_poll(const Iterable &fds, short events, int timeout = -1) {
std::vector<pollfd> pollfds;
for (const auto &fd : fds) {
pollfds.push_back(pollfd {
.fd = fd,
.events = events,
.revents = 0
});
}
int nfds = poll(pollfds.data(), pollfds.size(), timeout);
if (nfds == -1) {
throw std::runtime_error("poll failed. errno = {}"_format(strerror(errno)));
}
std::list<pollfd> results;
for (auto && pfd : pollfds) {
if (pfd.revents != 0)
results.emplace_back(pfd);
}
return results;
}
#endif
#if RLIB_OS_ID == OS_WINDOWS #if RLIB_OS_ID == OS_WINDOWS
template <bool doNotWSAStartup = false> template <bool doNotWSAStartup = false>
static inline sockfd_t quick_listen(const std::string &addr, uint16_t port, bool use_udp = false) { static inline sockfd_t quick_listen(const std::string &addr, uint16_t port, bool use_udp = false) {
......
This diff is collapsed.
...@@ -8,7 +8,7 @@ fi ...@@ -8,7 +8,7 @@ fi
for cxx in $compilers for cxx in $compilers
do do
for std in 14 17 for std in 14 17 20 23
do do
echo "Testing $cxx c++$std..." echo "Testing $cxx c++$std..."
make CXX="$cxx" STD="$std" make CXX="$cxx" STD="$std"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment