1 module hunt.io.channel.posix.EpollEventChannel;
2 
3 // dfmt off
4 version (HAVE_EPOLL) : 
5 // dfmt on
6 
7 import hunt.event.selector.Selector;
8 import hunt.io.channel.Common;
9 import hunt.io.channel.AbstractChannel;
10 import hunt.logging.ConsoleLogger;
11 
12 // import std.conv;
13 import std.socket;
14 import core.sys.posix.unistd;
15 import core.sys.linux.sys.eventfd;
16 
17 /**
18     https://stackoverflow.com/questions/5355791/linux-cant-get-eventfd-to-work-with-epoll-together
19 */
20 class EpollEventChannel : EventChannel {
21     this(Selector loop) {
22         super(loop);
23         setFlag(ChannelFlag.Read, true);
24         this.handle = cast(socket_t)eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
25         _isRegistered = true;
26     }
27 
28     ~this() {
29         // close();
30     }
31 
32     override void trigger() {
33         version (HUNT_IO_DEBUG) tracef("trigger the epoll selector.");
34         int r = eventfd_write(this.handle, 1);
35         if(r != 0) {
36             warningf("error: %d", r);
37         }        
38     }
39 
40     override void onWrite() {
41         version (HUNT_IO_DEBUG) tracef("eventLoop running: %s, [fd=%d]", eventLoop.isRuning, this.handle);
42         version (HUNT_IO_DEBUG) warning("do nothing");
43     }
44 
45     override void onRead() {
46         this.clearError();
47         uint64_t value;
48         int r = eventfd_read(this.handle, &value);
49         version (HUNT_IO_DEBUG) {
50             tracef("result=%d, value=%d, fd=%d", r, value, this.handle);
51             if(r != 0) {
52                 warningf("error: %d", r);
53             }
54         }
55     }
56 
57     override void onClose() {
58         version (HUNT_IO_DEBUG) tracef("onClose, [fd=%d]...", this.handle);
59         super.onClose();
60         core.sys.posix.unistd.close(this.handle);
61         version (HUNT_IO_DEBUG_MORE) tracef("onClose done, [fd=%d]", this.handle);
62     }
63 
64 }