diff --git a/example/main_epoll.c b/example/main_epoll.c new file mode 100644 index 000000000..cf526919b --- /dev/null +++ b/example/main_epoll.c @@ -0,0 +1,145 @@ +#include + #include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ff_config.h" +#include "ff_api.h" +#include "ff_epoll.h" + + +#define MAX_EVENTS 512 + +struct epoll_event ev; + +struct epoll_event events[MAX_EVENTS]; +struct kevent kqevents[MAX_EVENTS]; + +int epfd; +int sockfd; + +char html[] = +"HTTP/1.1 200 OK\r\n" +"Server: F-Stack\r\n" +"Date: Sat, 25 Feb 2017 09:26:33 GMT\r\n" +"Content-Type: text/html\r\n" +"Content-Length: 439\r\n" +"Last-Modified: Tue, 21 Feb 2017 09:44:03 GMT\r\n" +"Connection: keep-alive\r\n" +"Accept-Ranges: bytes\r\n" +"\r\n" +"\r\n" +"\r\n" +"\r\n" +"Welcome to F-Stack!\r\n" +"\r\n" +"\r\n" +"\r\n" +"

Welcome to F-Stack!

\r\n" +"\r\n" +"

For online documentation and support please refer to\r\n" +"F-Stack.org.
\r\n" +"\r\n" +"

Thank you for using F-Stack.

\r\n" +"\r\n" +""; + +int loop(void *arg) +{ + /* Wait for events to happen */ + + int nevents = ff_epoll_wait(epfd, events, MAX_EVENTS, 0); + int i; + + for (i = 0; i < nevents; ++i) { + /* Handle new connect */ + if (events[i].data.fd == sockfd) { + int nclientfd = ff_accept(sockfd, NULL, NULL); + assert(nclientfd > 0); + /* Add to event list */ + ev.data.fd = nclientfd; + ev.events = EPOLLIN; + assert(ff_epoll_ctl(epfd, EPOLL_CTL_ADD, nclientfd, &ev) == 0); + fprintf(stderr, "A new client connected to the server..., fd:%d\n", nclientfd); + } else { + if (events[i].events & EPOLLERR ) { + /* Simply close socket */ + ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL); + ff_close(events[i].data.fd); + fprintf(stderr, "A client has left the server...,fd:%d\n", events[i].data.fd); + } else if (events[i].events & EPOLLIN) { + char buf[256]; + size_t readlen = ff_read( events[i].data.fd, buf, sizeof(buf)); + fprintf(stderr, "bytes are available to read..., readlen:%d, fd:%d\n", readlen, events[i].data.fd); + if(readlen > 0){ + ff_write( events[i].data.fd, html, sizeof(html)); + } + else{ + ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL); + ff_close( events[i].data.fd); + fprintf(stderr, "A client has left the server...,fd:%d\n", events[i].data.fd); + } + } else { + fprintf(stderr, "unknown event: %8.8X\n", events[i].events); + } + } + } +} + +int main(int argc, char * argv[]) +{ + char *conf; + if (argc < 2) { + conf = "./config.ini"; + } else { + conf = argv[1]; + } + + ff_init(conf, argc, argv); + + sockfd = ff_socket(AF_INET, SOCK_STREAM, 0); + printf("sockfd:%d\n", sockfd); + if (sockfd < 0) + printf("ff_socket failed\n"); + + int on = 1; + ff_ioctl(sockfd, FIONBIO, &on); + + struct sockaddr_in my_addr; + bzero(&my_addr, sizeof(my_addr)); + my_addr.sin_family = AF_INET; + my_addr.sin_port = htons(80); + my_addr.sin_addr.s_addr = htonl(INADDR_ANY); + + int ret = ff_bind(sockfd, (struct linux_sockaddr *)&my_addr, sizeof(my_addr)); + if (ret < 0) { + printf("ff_bind failed\n"); + } + + ret = ff_listen(sockfd, MAX_EVENTS); + if (ret < 0) { + printf("ff_listen failed\n"); + } + + assert((epfd = ff_epoll_create(0)) > 0); + ev.data.fd = sockfd; + ev.events = EPOLLIN; + ff_epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev); + ff_run(loop, NULL); + return 0; +} + +