|
| 1 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// A multi-platform support c++11 library with focus on asynchronous socket I/O for any |
| 3 | +// client application. |
| 4 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 5 | +// |
| 6 | +// detail/poll_fd_set.hpp |
| 7 | +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | +// |
| 9 | +// Copyright (c) 2012-2022 HALX99 (halx99 at live dot com) |
| 10 | +#pragma once |
| 11 | +#include <vector> |
| 12 | +#include "yasio/detail/socket.hpp" |
| 13 | + |
| 14 | +namespace yasio |
| 15 | +{ |
| 16 | +YASIO__NS_INLINE |
| 17 | +namespace inet |
| 18 | +{ |
| 19 | +class poll_fd_set { |
| 20 | +public: |
| 21 | + poll_fd_set& operator=(poll_fd_set& rhs) |
| 22 | + { |
| 23 | + this->fd_set_ = rhs.fd_set_; |
| 24 | + return *this; |
| 25 | + } |
| 26 | + |
| 27 | + void reset() { this->fd_set_.clear(); } |
| 28 | + |
| 29 | + int poll_io(timeval& waitd_tv) { return ::poll(this->fd_set_.data(), static_cast<int>(this->fd_set_.size()), waitd_tv.tv_sec * 1000 + waitd_tv.tv_usec / 1000); } |
| 30 | + |
| 31 | + int is_set(socket_native_type fd, int events) const |
| 32 | + { |
| 33 | + int underlying_events = 0; |
| 34 | + if (events & socket_event::read) |
| 35 | + underlying_events |= POLLIN; |
| 36 | + if (events & socket_event::write) |
| 37 | + underlying_events |= POLLOUT; |
| 38 | + if (events & socket_event::error) |
| 39 | + underlying_events |= (POLLERR | POLLHUP | POLLNVAL); |
| 40 | + auto it = std::find_if(this->fd_set_.begin(), this->fd_set_.end(), [fd](const pollfd& pfd) { return pfd.fd == fd; }); |
| 41 | + return it != this->fd_set_.end() ? (it->revents & underlying_events) : 0; |
| 42 | + } |
| 43 | + |
| 44 | + void set(socket_native_type fd, int events) |
| 45 | + { |
| 46 | + int underlying_events = 0; |
| 47 | + if (yasio__testbits(events, socket_event::read)) |
| 48 | + underlying_events |= POLLIN; |
| 49 | + |
| 50 | + if (yasio__testbits(events, socket_event::write)) |
| 51 | + underlying_events |= POLLOUT; |
| 52 | + |
| 53 | + if (yasio__testbits(events, socket_event::error)) |
| 54 | + underlying_events |= POLLERR; |
| 55 | + pollfd_mod(this->fd_set_, fd, underlying_events, 0); |
| 56 | + } |
| 57 | + |
| 58 | + void unset(socket_native_type fd, int events) |
| 59 | + { |
| 60 | + int underlying_events = 0; |
| 61 | + if (yasio__testbits(events, socket_event::read)) |
| 62 | + underlying_events |= POLLIN; |
| 63 | + |
| 64 | + if (yasio__testbits(events, socket_event::write)) |
| 65 | + underlying_events |= POLLOUT; |
| 66 | + |
| 67 | + if (yasio__testbits(events, socket_event::error)) |
| 68 | + underlying_events |= POLLERR; |
| 69 | + |
| 70 | + pollfd_mod(this->fd_set_, fd, 0, underlying_events); |
| 71 | + } |
| 72 | + |
| 73 | +protected: |
| 74 | + static void pollfd_mod(std::vector<pollfd>& fdset, socket_native_type fd, int add_events, int remove_events) |
| 75 | + { |
| 76 | + auto it = std::find_if(fdset.begin(), fdset.end(), [fd](const pollfd& pfd) { return pfd.fd == fd; }); |
| 77 | + if (it != fdset.end()) |
| 78 | + { |
| 79 | + it->events |= add_events; |
| 80 | + it->events &= ~remove_events; |
| 81 | + if (it->events == 0) |
| 82 | + fdset.erase(it); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + auto events = add_events & ~remove_events; |
| 87 | + if (events) |
| 88 | + fdset.push_back(pollfd{fd, static_cast<short>(events), 0}); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +protected: |
| 93 | + std::vector<pollfd> fd_set_; |
| 94 | +}; |
| 95 | +} // namespace inet |
| 96 | +} // namespace yasio |
0 commit comments