Created
October 6, 2016 19:54
-
-
Save yannick/6b217a3502f166ab5352570311b3ab0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.stdio; | |
| import louio.epoll; | |
| void main() | |
| { | |
| Loop l = Loop(); | |
| l.init(); | |
| l.doLoop(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module louio.epoll; | |
| //import core.time : Duration; | |
| //import core.sys.posix.sys.time : timeval; | |
| import core.sys.linux.epoll; | |
| import core.sys.posix.stdio; | |
| import core.sys.posix.stdlib; | |
| import core.sys.posix.fcntl; | |
| @nogc: | |
| alias Handler = int; | |
| struct Loop | |
| { | |
| private { | |
| int _fd; | |
| int _maxEvents = 1; //how many events should be returned by epoll | |
| int _maxWait = 1000; //how long the wait should maximally be; | |
| } | |
| void init() | |
| { | |
| _fd = () @trusted { return epoll_create1(0); } (); | |
| if (_fd == -1) { | |
| perror("Couldn't create epoll FD"); | |
| exit(1); | |
| } | |
| } | |
| void addHandler(Handler handler) | |
| { | |
| epoll_event event; | |
| //event.data.ptr = handler; //point to the callback data | |
| //event.events = event_mask; | |
| if (epoll_ctl(_fd, EPOLL_CTL_ADD, handler, &event) == -1) { | |
| perror("Couldn't register server socket with epoll"); | |
| exit(-1); | |
| } | |
| } | |
| void removeHandler(Handler handler) | |
| { | |
| epoll_ctl(_fd, EPOLL_CTL_DEL, handler, null); | |
| } | |
| void doLoop() | |
| { | |
| epoll_event current_epoll_events; | |
| int numberOfNewEvents; | |
| while (1) { | |
| //epoll_event_handler* handler; | |
| Handler handler; | |
| numberOfNewEvents = epoll_wait(_fd, ¤t_epoll_events, _maxEvents, _maxWait); //TODO: replace with pwait | |
| if (numberOfNewEvents == -1) | |
| { | |
| perror("WTF"); | |
| exit(-1); | |
| } | |
| //handler = (Handler*) current_epoll_events.data.ptr; | |
| //handler->handle(handler, current_epoll_event.events); | |
| //TODO: free data | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment