1 module geario.net.channel.posix.EpollEventChannel; 2 3 // dfmt off 4 version (HAVE_EPOLL) : 5 // dfmt on 6 7 import geario.event.selector.Selector; 8 import geario.net.channel.Types; 9 import geario.net.channel.AbstractChannel; 10 import geario.logging; 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 (GEAR_IO_DEBUG) log.trace("trigger the epoll selector."); 34 int r = eventfd_write(this.handle, 1); 35 //do_sock_write(r); 36 if(r != 0) { 37 log.warn("Error: %d", r); 38 } 39 } 40 41 override void OnWrite() { 42 version (GEAR_IO_DEBUG) log.trace("eventLoop running: %s, [fd=%d]", eventLoop.IsRuning, this.handle); 43 version (GEAR_IO_DEBUG) log.warn("do nothing"); 44 } 45 46 override void OnRead() { 47 this.ClearError(); 48 uint64_t value; 49 int r = eventfd_read(this.handle, &value); 50 //do_sock_read(r); 51 version (GEAR_IO_DEBUG) { 52 log.trace("result=%d, value=%d, fd=%d", r, value, this.handle); 53 if(r != 0) { 54 log.warn("Error: %d", r); 55 } 56 } 57 } 58 59 override void OnClose() { 60 version (GEAR_IO_DEBUG) log.trace("onClose, [fd=%d]...", this.handle); 61 super.OnClose(); 62 core.sys.posix.unistd.close(this.handle); 63 version (GEAR_IO_DEBUG_MORE) log.trace("onClose done, [fd=%d]", this.handle); 64 } 65 66 }