William A. Kennington III | ce80c3f | 2018-07-22 14:59:13 -0700 | [diff] [blame] | 1 | #include <sdeventplus/source/io.hpp> |
| 2 | #include <type_traits> |
| 3 | #include <utility> |
| 4 | |
| 5 | namespace sdeventplus |
| 6 | { |
| 7 | namespace source |
| 8 | { |
| 9 | |
| 10 | IO::IO(const Event& event, int fd, uint32_t events, Callback&& callback) : |
| 11 | Base(event, create_source(event, fd, events), std::false_type()), |
| 12 | callback(std::move(callback)) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | int IO::get_fd() const |
| 17 | { |
| 18 | int r = event.getSdEvent()->sd_event_source_get_io_fd(source.get()); |
| 19 | if (r < 0) |
| 20 | { |
| 21 | throw SdEventError(-r, "sd_event_source_get_io_fd"); |
| 22 | } |
| 23 | return r; |
| 24 | } |
| 25 | |
| 26 | void IO::set_fd(int fd) const |
| 27 | { |
| 28 | int r = event.getSdEvent()->sd_event_source_set_io_fd(source.get(), fd); |
| 29 | if (r < 0) |
| 30 | { |
| 31 | throw SdEventError(-r, "sd_event_source_set_io_fd"); |
| 32 | } |
| 33 | } |
| 34 | |
William A. Kennington III | ce80c3f | 2018-07-22 14:59:13 -0700 | [diff] [blame] | 35 | uint32_t IO::get_events() const |
| 36 | { |
| 37 | uint32_t events; |
| 38 | int r = event.getSdEvent()->sd_event_source_get_io_events(source.get(), |
| 39 | &events); |
| 40 | if (r < 0) |
| 41 | { |
| 42 | throw SdEventError(-r, "sd_event_source_get_io_events"); |
| 43 | } |
| 44 | return events; |
| 45 | } |
| 46 | |
| 47 | void IO::set_events(uint32_t events) const |
| 48 | { |
| 49 | int r = |
| 50 | event.getSdEvent()->sd_event_source_set_io_events(source.get(), events); |
| 51 | if (r < 0) |
| 52 | { |
| 53 | throw SdEventError(-r, "sd_event_source_set_io_events"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | uint32_t IO::get_revents() const |
| 58 | { |
| 59 | uint32_t revents; |
| 60 | int r = event.getSdEvent()->sd_event_source_get_io_revents(source.get(), |
| 61 | &revents); |
| 62 | if (r < 0) |
| 63 | { |
| 64 | throw SdEventError(-r, "sd_event_source_get_io_revents"); |
| 65 | } |
| 66 | return revents; |
| 67 | } |
| 68 | |
| 69 | const IO::Callback& IO::get_callback() const |
| 70 | { |
| 71 | return callback; |
| 72 | } |
| 73 | |
| 74 | sd_event_source* IO::create_source(const Event& event, int fd, uint32_t events) |
| 75 | { |
| 76 | sd_event_source* source; |
| 77 | int r = event.getSdEvent()->sd_event_add_io(event.get(), &source, fd, |
| 78 | events, ioCallback, nullptr); |
| 79 | if (r < 0) |
| 80 | { |
| 81 | throw SdEventError(-r, "sd_event_add_io"); |
| 82 | } |
| 83 | return source; |
| 84 | } |
| 85 | |
| 86 | int IO::ioCallback(sd_event_source* source, int fd, uint32_t revents, |
| 87 | void* userdata) |
| 88 | { |
| 89 | return sourceCallback<Callback, IO, &IO::get_callback>( |
| 90 | "ioCallback", source, userdata, fd, revents); |
| 91 | } |
| 92 | |
| 93 | } // namespace source |
| 94 | } // namespace sdeventplus |