EPoll.h
branchv_0
changeset 0 bb715a82a8f1
equal deleted inserted replaced
-1:000000000000 0:bb715a82a8f1
       
     1 /**
       
     2  * ShaderShark
       
     3  * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #pragma once
       
    19 
       
    20 #include <stdexcept>
       
    21 #include <string>
       
    22 
       
    23 #include <signal.h>
       
    24 #include <sys/signalfd.h>
       
    25 #include <sys/epoll.h>
       
    26 #include <unistd.h>
       
    27 #include <sys/fcntl.h>
       
    28 #include <sys/socket.h>
       
    29 #include <sys/un.h>
       
    30 
       
    31 #include <stdexcept>
       
    32 #include <string>
       
    33 
       
    34 class EPoll {
       
    35 private:
       
    36 	int epollEventCount;
       
    37 	epoll_event* epollEvents;
       
    38 	int epollFD;
       
    39 
       
    40 	void static log(const std::string& message) {
       
    41 		std::cout << message << std::endl;
       
    42 	}
       
    43 public:
       
    44 
       
    45 	class Exception : public std::logic_error {
       
    46 	public:
       
    47 
       
    48 		Exception(const std::string& msg) : logic_error(msg) {
       
    49 		}
       
    50 
       
    51 		Exception(const std::string& msg, int errNo) :
       
    52 				logic_error(msg + strerror(errNo)) {
       
    53 		}
       
    54 
       
    55 	};
       
    56 
       
    57 	EPoll(int eventCount = 5) : epollEventCount(eventCount) {
       
    58 		epollEvents = (epoll_event*) malloc(sizeof (*epollEvents) * eventCount);
       
    59 		epollFD = epoll_create1(0);
       
    60 		log("EPoll(): epoll_create() = " + std::to_string(epollFD));
       
    61 	}
       
    62 
       
    63 	virtual ~EPoll() {
       
    64 		::close(epollFD);
       
    65 		free(epollEvents);
       
    66 	}
       
    67 
       
    68 	EPoll(const EPoll&) = delete;
       
    69 	EPoll& operator=(const EPoll&) = delete;
       
    70 
       
    71 	static void processSignal(int signal) {
       
    72 		log("got signal: " + std::to_string(signal));
       
    73 	}
       
    74 
       
    75 	int addSignals() {
       
    76 		::signal(SIGTERM, EPoll::processSignal);
       
    77 		::signal(SIGINT, EPoll::processSignal);
       
    78 
       
    79 		sigset_t mask;
       
    80 		::sigemptyset(&mask);
       
    81 		::sigaddset(&mask, SIGTERM);
       
    82 		::sigaddset(&mask, SIGINT);
       
    83 		int signalFD = ::signalfd(-1, &mask, 0);
       
    84 		add(signalFD, EPOLLIN | EPOLLET | EPOLLHUP);
       
    85 		log("signalFD=" + std::to_string(signalFD));
       
    86 		return signalFD;
       
    87 	}
       
    88 
       
    89 	void add(int fd, uint32_t events = EPOLL_EVENTS::EPOLLIN) {
       
    90 		struct epoll_event ev;
       
    91 		ev.data.fd = fd;
       
    92 		ev.events = events;
       
    93 		int err = epoll_ctl(epollFD, EPOLL_CTL_ADD, ev.data.fd, &ev);
       
    94 		if (err) throw Exception("EPoll::add() failed (a file?):", errno);
       
    95 		// Regular files can not be monitored this way, thus:
       
    96 		//   cat Makefile | opengl-pdf      # works
       
    97 		//   opengl-pdf < Makefile          # does not work
       
    98 	}
       
    99 
       
   100 	void del(int fd) {
       
   101 		int err = epoll_ctl(epollFD, EPOLL_CTL_DEL, fd, nullptr);
       
   102 		if (err) throw Exception("EPoll::del() failed: ", errno);
       
   103 	}
       
   104 
       
   105 	int wait(int timeout = 1000) {
       
   106 		int count = epoll_wait(epollFD, epollEvents, epollEventCount, timeout);
       
   107 		if (count >= 0) return count;
       
   108 		else throw Exception("EPoll::wait() failed: ", errno);
       
   109 	}
       
   110 
       
   111 	const epoll_event& operator[](std::size_t index) const {
       
   112 		if (index < epollEventCount) return epollEvents[index];
       
   113 		else throw std::out_of_range("invalid index of epollEvents");
       
   114 	}
       
   115 
       
   116 };