Created
November 6, 2012 17:07
-
-
Save thabongshot/4026066 to your computer and use it in GitHub Desktop.
craftIk epoll test
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 "event.h" | |
void craftIk_epoll_init( struct craftIk_epoll* epoll, int listenfd, int max_clients) | |
{ | |
struct epoll_event ev; | |
epoll->listenfd = listenfd; | |
epoll->max_clients = max_clients; | |
epoll->events = (struct epoll_event*)malloc( sizeof(struct epoll_event) * max_clients ); | |
epoll->epfd = epoll_create(max_clients); | |
if(!epoll->epfd){ | |
perror("epoll_create"); | |
exit(1); | |
} | |
ev.events = EPOLLIN | EPOLLHUP | EPOLLET; | |
ev.data.fd = listenfd; | |
if( epoll_ctl( epoll->epfd, EPOLL_CTL_ADD, listenfd, &ev) < 0 ){ | |
perror("epoll_ctl, adding listenfd"); | |
exit(1); | |
} | |
} | |
int craftIk_epoll_add( struct craftIk_epoll* epoll, int clifd ) | |
{ | |
struct epoll_event ev; | |
nonblock(clifd); | |
ev.events = EPOLLIN | EPOLLET; | |
ev.data.fd = clifd; | |
if( epoll_ctl( epoll->epfd, EPOLL_CTL_ADD, clifd, &ev) < 0 ){ | |
perror("epoll_ctl ADD"); | |
exit(1); | |
} | |
} | |
int craftIk_epoll_del( struct craftIk_epoll* epoll, int clifd ) | |
{ | |
if( epoll_ctl( epoll->epfd, EPOLL_CTL_ADD, clifd, NULL) < 0 ){ | |
perror("epoll_ctl DEL"); | |
exit(1); | |
} | |
} | |
int craftIk_epoll_getEvents( struct craftIk_epoll* epoll ) | |
{ | |
return epoll_wait( epoll->epfd, epoll->events, epoll->max_clients, 0.1); | |
} | |
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 <errno.h> | |
#include <unistd.h> | |
#include "event.h" | |
void nonblock(int sockfd) | |
{ | |
int opts; | |
opts = fcntl(sockfd, F_GETFL); | |
if(opts < 0) | |
{ | |
perror("fcntl(F_GETFL)\n"); | |
exit(1); | |
} | |
opts = (opts | O_NONBLOCK); | |
if(fcntl(sockfd, F_SETFL, opts) < 0) | |
{ | |
perror("fcntl(F_SETFL)\n"); | |
exit(1); | |
} | |
} |
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
#ifndef __EVENT_H__ | |
#define __EVENT_H__ | |
#include <stdio.h> | |
#include <sys/timeb.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <sys/epoll.h> // epoll | |
#include <netinet/in.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <signal.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
// create nonblock socket | |
void nonblock(int sockfd); | |
// epoll | |
typedef struct craftIk_epoll | |
{ | |
int max_clients; | |
int listenfd; | |
int epfd; | |
struct epoll_event* events; | |
} craftIk_epoll; | |
void craftIk_epoll_init( struct craftIk_epoll* epoll, int listenfd, int max_clients ); | |
int craftIk_epoll_add( struct craftIk_epoll* epoll, int clifd ); | |
int craftIk_epoll_del( struct craftIk_epoll* epoll, int clifd ); | |
int craftIk_epoll_getEvents( struct craftIk_epoll* epoll); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment