Created
August 31, 2018 16:17
-
-
Save baixiangcpp/ae836f31e618d002e6611ad0006c5c74 to your computer and use it in GitHub Desktop.
A libevent helloworld using changelist
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
| #include <event2/event.h> | |
| #include <stdio.h> | |
| #include <unistd.h> | |
| #include <stdlib.h> | |
| #define BUF_SIZE 1024 | |
| void onError(const char *reason) | |
| { | |
| perror(reason); | |
| exit(EXIT_FAILURE); | |
| } | |
| void read_cb(int fd,short events,void* arg) | |
| { | |
| char buf[BUF_SIZE] = {0}; | |
| int ret = read(fd,buf,sizeof(buf)-1); | |
| if(ret < 0 ) | |
| onError("read()"); | |
| buf[ret] = 0; | |
| printf("What you type is : %s \n",buf); | |
| } | |
| int main() | |
| { | |
| struct event_config *cfg = event_config_new(); | |
| event_config_set_flag(cfg,EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST); | |
| struct event_base* base = event_base_new_with_config(cfg); | |
| if(!base) | |
| onError("event_base_new()"); | |
| struct event* ev = event_new(base,STDIN_FILENO,EV_READ | EV_PERSIST ,read_cb,NULL); | |
| if(!ev) | |
| onError("event_new()"); | |
| event_add(ev,NULL); | |
| event_base_dispatch(base); | |
| event_free(ev); | |
| event_base_free(base); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment