本文整理汇总了C++中epoll_create函数的典型用法代码示例。如果您正苦于以下问题:C++ epoll_create函数的具体用法?C++ epoll_create怎么用?C++ epoll_create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了epoll_create函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: ngx_event_core_init_conf
static char *
ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)
{
ngx_event_conf_t *ecf = conf;
#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
int fd;
#endif
ngx_int_t i;
ngx_module_t *module;
ngx_event_module_t *event_module;
module = NULL;
#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
fd = epoll_create(100);
if (fd != -1) {
(void) close(fd);
module = &ngx_epoll_module;
} else if (ngx_errno != NGX_ENOSYS) {
module = &ngx_epoll_module;
}
#endif
#if (NGX_HAVE_DEVPOLL)
module = &ngx_devpoll_module;
#endif
#if (NGX_HAVE_KQUEUE)
module = &ngx_kqueue_module;
#endif
#if (NGX_HAVE_SELECT)
if (module == NULL) {
module = &ngx_select_module;
}
#endif
//ngx_event_core_module后的第一个NGX_EVENT_MODULE也就是ngx_epoll_module默认作为第一个event模块
if (module == NULL) {
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
continue;
}
event_module = ngx_modules[i]->ctx;
if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0) //不能为ngx_event_core_module
{
continue;
}
module = ngx_modules[i];
break;
}
}
if (module == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
return NGX_CONF_ERROR;
}
ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS);
cycle->connection_n = ecf->connections;
ngx_conf_init_uint_value(ecf->use, module->ctx_index);
event_module = module->ctx;
ngx_conf_init_ptr_value(ecf->name, event_module->name->data);
ngx_conf_init_value(ecf->multi_accept, 0);
ngx_conf_init_value(ecf->accept_mutex, 1);
ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
return NGX_CONF_OK;
}
开发者ID:cokeboL,项目名称:reading-code-of-nginx-1.9.2,代码行数:87,代码来源:ngx_event.c
示例2: main
int main(int argc, char *argv[]) {
int opt, rc, n, *fd;
cfg.prog = argv[0];
utarray_new(cfg.clients,&ut_int_icd);
utarray_new(cfg.outbufs,&ut_ptr_icd);
utarray_new(cfg.outidxs, &ut_int_icd);
cfg.set = kv_set_new();
struct epoll_event ev;
UT_string **s;
utstring_new(cfg.s);
utarray_new(output_keys, &ut_str_icd);
utarray_new(output_defaults, &ut_str_icd);
utarray_new(output_types,&ut_int_icd);
while ( (opt=getopt(argc,argv,"vb:p:m:d:rS:h")) != -1) {
switch(opt) {
case 'v': cfg.verbose++; break;
case 'p': cfg.listener_port=atoi(optarg); break;
case 'm': cfg.mb_per_client=atoi(optarg); break;
case 'd': cfg.dir=strdup(optarg); break;
case 'r': cfg.mode=round_robin; break;
case 'b': cfg.config_file=strdup(optarg); break;
case 'S': cfg.stats_file=strdup(optarg); break;
case 'h': default: usage(); break;
}
}
if (cfg.listener_port==0) usage();
if (setup_client_listener()) goto done;
if (cfg.config_file==NULL) goto done;
if (parse_config(cfg.config_file) < 0) goto done;
if ( !(cfg.sp = kv_spoolreader_new(cfg.dir))) goto done;
/* block all signals. we take signals synchronously via signalfd */
sigset_t all;
sigfillset(&all);
sigprocmask(SIG_SETMASK,&all,NULL);
/* a few signals we'll accept via our signalfd */
sigset_t sw;
sigemptyset(&sw);
for(n=0; n < sizeof(sigs)/sizeof(*sigs); n++) sigaddset(&sw, sigs[n]);
/* create the signalfd for receiving signals */
cfg.signal_fd = signalfd(-1, &sw, 0);
if (cfg.signal_fd == -1) {
fprintf(stderr,"signalfd: %s\n", strerror(errno));
goto done;
}
/* set up the epoll instance */
cfg.epoll_fd = epoll_create(1);
if (cfg.epoll_fd == -1) {
fprintf(stderr,"epoll: %s\n", strerror(errno));
goto done;
}
/* add descriptors of interest */
if (new_epoll(EPOLLIN, cfg.listener_fd)) goto done; // new client connections
if (new_epoll(EPOLLIN, cfg.signal_fd)) goto done; // signal socket
alarm(1);
while (epoll_wait(cfg.epoll_fd, &ev, 1, -1) > 0) {
if (cfg.verbose > 1) fprintf(stderr,"epoll reports fd %d\n", ev.data.fd);
if (ev.data.fd == cfg.signal_fd) { if (handle_signal() < 0) goto done; }
else if (ev.data.fd == cfg.listener_fd) { if (accept_client() < 0) goto done; }
else feed_client(ev.data.fd, ev.events);
}
done:
/* free the clients: close and deep free their buffers */
fd=NULL; s=NULL;
while ( (fd=(int*)utarray_prev(cfg.clients,fd))) {
s=(UT_string**)utarray_prev(cfg.outbufs,s);
close(*fd);
utstring_free(*s);
}
utarray_free(cfg.clients);
utarray_free(cfg.outbufs);
utarray_free(cfg.outidxs);
utarray_free(output_keys);
utarray_free(output_defaults);
utarray_free(output_types);
utstring_free(cfg.s);
if (cfg.listener_fd) close(cfg.listener_fd);
if (cfg.signal_fd) close(cfg.signal_fd);
if (cfg.sp) kv_spoolreader_free(cfg.sp);
if (cfg.set) kv_set_free(cfg.set);
return 0;
}
开发者ID:JHUAPL,项目名称:kvspool,代码行数:90,代码来源:kvsp-tpub.c
示例3: main
int main(int argc, char **argv)
{
int listener, kdpfd, nfds, n, curfds;
socklen_t len;
struct sockaddr_in my_addr, their_addr;
unsigned int myport;
struct epoll_event ev;
struct epoll_event events[MAXEPOLLSIZE];
struct rlimit rt;
myport = 1234;
pthread_t thread;
pthread_attr_t attr;
/* init thread pool */
struct st_threadpool *pool = threadpool_init(10, 20);
/* 设置每个进程允许打开的最大文件数 */
rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;
if (setrlimit(RLIMIT_NOFILE, &rt) == -1)
{
perror("setrlimit");
exit(1);
}
else
{
printf("设置系统资源参数成功!\n");
}
/* 开启 socket 监听 */
if ((listener = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket 创建失败!");
exit(1);
}
else
{
printf("socket 创建成功!\n");
}
/*设置socket属性,端口可以重用*/
int opt=SO_REUSEADDR;
setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
setnonblocking(listener);
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(myport);
my_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
else
{
printf("IP 地址和端口绑定成功\n");
}
/* 创建 epoll 句柄,把监听 socket 加入到 epoll 集合里 */
kdpfd = epoll_create(MAXEPOLLSIZE);
len = sizeof(struct sockaddr_in);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listener;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0)
{
fprintf(stderr, "epoll set insertion error: fd=%d\n", listener);
return -1;
}
else
{
printf("监听 socket 加入 epoll 成功!\n");
}
while (1)
{
/* 等待有事件发生 */
nfds = epoll_wait(kdpfd, events, 10000, -1);
if (nfds == -1)
{
perror("epoll_wait");
break;
}
/* 处理所有事件 */
for (n = 0; n < nfds; ++n)
{
if (events[n].data.fd == listener)
{
threadpool_add_job(pool, threadpool_callback, (void*)&(events[n].data.fd));
/*初始化属性值,均设为默认值*/
//pthread_attr_init(&attr);
//pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* 设置线程为分离属性*/
//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
//if(pthread_create(&thread,&attr,(void*)pthread_handle_message,(void*)&(events[n].data.fd)))
//{
// perror("pthread_creat error!");
// exit(-1);
//.........这里部分代码省略.........
开发者ID:songtzu,项目名称:iotp,代码行数:101,代码来源:epoll_udp.c
示例4: client_info
/* client handle the info received from both server and standard input
* @connfd: the connected socket used for communication
*
*/
void client_info(int connfd)
{
int i;
int shutdown_flag = 0;
/* recv and send buffer */
buffer_t recvbuf, sendbuf;
memset(&recvbuf,0,sizeof(buffer_t));
memset(&sendbuf,0,sizeof(buffer_t));
//setnonblock(connfd);
/* epollfd set monitors conncted socket fd and standard input, if either one is
* readable, then we obtain the info from it*/
int epollfd, fd;
if ( (epollfd = epoll_create(EPOLL_SIZE)) < 0 )
{
perror_exit("epoll create error");
}
struct epoll_event events[4];
int nready;
add_epoll_event(epollfd,STDIN_FILENO,EPOLLIN);
while( 1 )
{
if ( (nready = epoll_wait(epollfd,events,4,INFTIM)) < 0 )
{
perror("epollfd error");
}
for (i = 0; i < nready; ++i)
{
fd = events[i].data.fd;
if (fd == STDIN_FILENO && (events[i].events & EPOLLIN) )
{
int space = buffer_hasspace(&sendbuf);
if (space > 0)
{
int nread = read(fd,&sendbuf.buffer[sendbuf.in],space);
/* read error */
if (nread < 0)
{
if (errno != EINTR)
{
perror_exit("read error");
}
}
else if (nread == 0)
{
/* read "ctrl+d" from client, close the connection and delete the event from epoll set */
shutdown_flag = 1;
shutdown(connfd,SHUT_WR);
delete_epoll_event(epollfd,fd, EPOLLIN);
}
else
{
sendbuf.in += nread;
/* add connection fd to epoll set */
add_epoll_event(epollfd,connfd,EPOLLOUT);
}
}
}
if (fd == connfd && (events[i].events & EPOLLOUT) )
{
int ntotal = strlen(sendbuf.buffer);
int nwrite = write(fd,&sendbuf.buffer[sendbuf.out],ntotal);
if (nwrite < 0)
{
perror_exit("write error");
}
else
{
sendbuf.out += ntotal;
/* all data has benn sent out, reset the buffer space */
if (sendbuf.in == sendbuf.out)
{
buffer_reset(&sendbuf);
}
/* modify the fd from epoll set to EPOLLIN since all data has been sent out */
modify_epoll_event(epollfd,fd,EPOLLIN);
}
}
//.........这里部分代码省略.........
开发者ID:kevin4fly,项目名称:network,代码行数:101,代码来源:sock_util.c
示例5: main
int main(int argc, char **argv)
{
int alarm_time = 5;
int count = -1;
bool abort_on_failure = false;
while (1) {
const static struct option long_options[] = {
{"abort", no_argument, 0, 'a'},
{"count", required_argument, 0, 'c'},
{"time", required_argument, 0, 't'},
};
int c = getopt_long(argc, argv, "ac:t:", long_options, NULL);
if (c < 0) {
break;
}
switch (c) {
case 'a':
abort_on_failure = true;
break;
case 'c':
count = strtoul(optarg, NULL, 0);
break;
case 't':
alarm_time = strtoul(optarg, NULL, 0);
break;
case '?':
usage();
exit(EXIT_FAILURE);
default:
abort();
}
}
klog_init();
klog_set_level(KLOG_INFO_LEVEL);
if (optind < argc) {
fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
usage();
exit(EXIT_FAILURE);
}
int fd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
if (fd < 0) {
perror("timerfd_create failed");
exit(EXIT_FAILURE);
}
struct itimerspec delay = itimerspec();
delay.it_value.tv_sec = alarm_time;
int i = 0;
int epoll_fd = epoll_create(1);
if (epoll_fd < 0) {
perror("epoll_create failed");
exit(EXIT_FAILURE);
}
struct epoll_event ev = epoll_event();
ev.events = EPOLLIN | EPOLLWAKEUP;
int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
if (ret < 0) {
perror("epoll_ctl failed");
exit(EXIT_FAILURE);
}
while (count != 0) {
struct timespec expected_time;
struct timespec actual_time;
uint64_t fired = 0;
ret = timerfd_settime(fd, 0, &delay, NULL);
if (ret < 0) {
perror("timerfd_settime failed");
exit(EXIT_FAILURE);
}
ret = clock_gettime(CLOCK_BOOTTIME, &expected_time);
if (ret < 0) {
perror("failed to get time");
exit(EXIT_FAILURE);
}
expected_time.tv_sec += alarm_time;
ret = 0;
while (ret != 1) {
struct epoll_event out_ev;
ret = epoll_wait(epoll_fd, &out_ev, 1, -1);
if (ret < 0 && errno != EINTR) {
perror("epoll_wait failed");
exit(EXIT_FAILURE);
}
}
ssize_t bytes = read(fd, &fired, sizeof(fired));
if (bytes < 0) {
perror("read from timer fd failed");
exit(EXIT_FAILURE);
//.........这里部分代码省略.........
开发者ID:Blakez,项目名称:jollipop_system_extras,代码行数:101,代码来源:suspend_stress.cpp
示例6: netlink_test
void netlink_test()
{
g_vlogger_level=3;
netlink_wrapper* nl = new netlink_wrapper();
g_p_netlink_handler=nl;
neigh_observer neigh_obs;
route_observer route_obs;
link_observer link_obs;
nl->register_event(nlgrpNEIGH, &neigh_obs);
//nl->register_event(nlgrpROUTE, &route_obs);
//nl->register_event(nlgrpLINK, &link_obs);
int nevents;
struct epoll_event events[32];
if (nl->open_channel()) {
printf("fail to open nl channel\n");
exit(-1);
}
int fd = nl->get_channel();
if (fd < 0) {
printf("netlink channel is illegal\n");
exit(-1);
}
int epfd = epoll_create(10);
struct epoll_event* e = new struct epoll_event();
e->data.fd=fd;
e->data.ptr=NULL;
e->events=EPOLLIN | EPOLLET;
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, e);
// netlink_neigh_info* neigh_info = new netlink_neigh_info();
// printf("GOING TO NIEGH QUERY\n");
// int rc = nl->get_neigh("172.30.20.111", 1, neigh_info);
// if (rc == 1) {
// printf("NIEGH QUERY is:\n");
// printf("NEIGH: ip=%s, lladdr=%s, state=%s\n", neigh_info->dst_addr_str.c_str(), neigh_info->lladdr_str.c_str(), neigh_info->get_state2str().c_str());
// printf("NIEGH QUERY done\n");
// }
// else {
// printf("NO NIEGH QUERY, rc=%d\n", rc);
// }
//
while (1) {
/* Poll events from both main threads and the event channel */
nevents = epoll_wait(epfd, events,
sizeof(events) / sizeof(events[0]), 2000);
if (nevents < 0) {
if (errno != EINTR) {
printf("epoll_wait errno=%m\n");
}
} else if (nevents) {
printf("*** --> going to handle events (n=%d)\n", nevents);
nl->handle_events();
printf("*** <-- handle events\n");
}
}
printf("-------->>>>> event_processor thread stopped <<<<<--------");
exit(1);
}
开发者ID:AlaAyesh,项目名称:libvma,代码行数:66,代码来源:test_main.cpp
示例7: ngx_event_core_init_conf
static char *
ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)
{
ngx_event_conf_t *ecf = conf;
#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
int fd;
#endif
#if (NGX_HAVE_RTSIG)
ngx_uint_t rtsig;
ngx_core_conf_t *ccf;
#endif
ngx_int_t i;
ngx_module_t *module;
ngx_event_module_t *event_module;
module = NULL;
#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
fd = epoll_create(100);
if (fd != -1) {
(void) close(fd);
module = &ngx_epoll_module;
} else if (ngx_errno != NGX_ENOSYS) {
module = &ngx_epoll_module;
}
#endif
#if (NGX_HAVE_RTSIG)
if (module == NULL) {
module = &ngx_rtsig_module;
rtsig = 1;
} else {
rtsig = 0;
}
#endif
#if (NGX_HAVE_DEVPOLL)
module = &ngx_devpoll_module;
#endif
#if (NGX_HAVE_KQUEUE)
module = &ngx_kqueue_module;
#endif
#if (NGX_HAVE_SELECT)
if (module == NULL) {
module = &ngx_select_module;
}
#endif
if (module == NULL) {
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
continue;
}
event_module = ngx_modules[i]->ctx;
if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0)
{
continue;
}
module = ngx_modules[i];
break;
}
}
if (module == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
return NGX_CONF_ERROR;
}
ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS);
cycle->connection_n = ecf->connections;
ngx_conf_init_uint_value(ecf->use, module->ctx_index);
event_module = module->ctx;
ngx_conf_init_ptr_value(ecf->name, event_module->name->data);
ngx_conf_init_value(ecf->multi_accept, 0);
ngx_conf_init_value(ecf->accept_mutex, 1);
ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
//.........这里部分代码省略.........
开发者ID:ygliang2009,项目名称:ngx,代码行数:101,代码来源:ngx_event.c
示例8: receiver_thread
static gpointer
receiver_thread (
gpointer data
)
{
pgm_transport_t* transport = (pgm_transport_t*)data;
long iov_max = sysconf( SC_IOV_MAX );
pgm_msgv_t msgv[iov_max];
#ifdef CONFIG_HAVE_EPOLL
int efd = epoll_create (IP_MAX_MEMBERSHIPS);
if (efd < 0) {
g_error ("epoll_create failed errno %i: \"%s\"", errno, strerror(errno));
g_main_loop_quit(g_loop);
return NULL;
}
int retval = pgm_transport_epoll_ctl (g_transport, efd, EPOLL_CTL_ADD, EPOLLIN);
if (retval < 0) {
g_error ("pgm_epoll_ctl failed errno %i: \"%s\"", errno, strerror(errno));
g_main_loop_quit(g_loop);
return NULL;
}
#else
int n_fds = 2;
struct pollfd fds[ n_fds ];
#endif /* !CONFIG_HAVE_EPOLL */
do {
gssize len = pgm_transport_recvmsgv (transport, msgv, iov_max, MSG_DONTWAIT /* non-blocking */);
if (len >= 0)
{
on_msgv (msgv, len, NULL);
}
else if (errno == EAGAIN) /* len == -1, an error occured */
{
#ifdef CONFIG_HAVE_EPOLL
struct epoll_event events[1]; /* wait for maximum 1 event */
epoll_wait (efd, events, G_N_ELEMENTS(events), 1000 /* ms */);
#else
memset (fds, 0, sizeof(fds));
pgm_transport_poll_info (g_transport, fds, &n_fds, POLLIN);
poll (fds, n_fds, 1000 /* ms */);
#endif /* !CONFIG_HAVE_EPOLL */
}
else if (errno == ECONNRESET)
{
pgm_sock_err_t* pgm_sock_err = (pgm_sock_err_t*)msgv[0].msgv_iov->iov_base;
g_warning ("pgm socket lost %" G_GUINT32_FORMAT " packets detected from %s",
pgm_sock_err->lost_count,
pgm_print_tsi(&pgm_sock_err->tsi));
continue;
}
else if (errno == ENOTCONN) /* socket(s) closed */
{
g_error ("pgm socket closed.");
g_main_loop_quit(g_loop);
break;
}
else
{
g_error ("pgm socket failed errno %i: \"%s\"", errno, strerror(errno));
g_main_loop_quit(g_loop);
break;
}
} while (!g_quit);
#ifdef CONFIG_HAVE_EPOLL
close (efd);
#endif
return NULL;
}
开发者ID:demonlife,项目名称:infinidb,代码行数:73,代码来源:pgmrecv.c
示例9: socket
//接收器启动函数
int RServer_requestAcceptor::start() {
//cout << "begin start"<<endl;
listenFd = socket(AF_INET,SOCK_STREAM,0);
setnonblocking(listenFd); //设置listenFd为非阻塞式的
bind(listenFd,(struct sockaddr*)&listenAddr,sizeof(listenAddr)); //绑定socket
//cout << "start creat epoll"<<endl;
epfd = epoll_create(MAX_LEN);
struct epoll_event tempEvent;
tempEvent.data.fd = listenFd; //设置tempEvent的描述符为listenfd
//tempEvent.events = EPOLLIN|EPOLLET; //设置tempEvent要监听的事件为可读且为边沿触发
tempEvent.events = EPOLLIN; //设置tempEvent要监听的事件为可读且为水平触发
epoll_ctl(epfd,EPOLL_CTL_ADD,listenFd,&tempEvent);//将tempEvent注册进epoll中
listen(listenFd,5); //监听,并设置监听队列长度为5
cout << "***welcome RServer!***"<<endl;
cout << "*********Rain*********"<<endl;
cout << "*****start listen*****" <<endl;
int eventsNum = 0;
int readLength = 0;
char buff[MAX_BUFFLEN];
unsigned int connFd;
epoll_event events[MAX_LEN];
struct sockaddr_in clientAddr;
unsigned int clientLen=0;
while(1) {
memset(buff,0,sizeof(buff));
eventsNum = epoll_wait(epfd,events,MAX_LEN,-1); //IO复用开始等待事件
for(int i=0; i<eventsNum; ++i) {
if(events[i].data.fd == listenFd) { //表示有新的连接请求
memset(&clientAddr,0,sizeof(clientAddr));
clientLen = sizeof(clientAddr);
connFd = accept(listenFd,(struct sockaddr*)&clientAddr,&clientLen); //接收请求
if(connFd < 0) {
cout << "Accept error! IP:"<<inet_ntoa(clientAddr.sin_addr);
cout <<" port:"<<ntohs(clientAddr.sin_port);
cout <<" Socket:"<<connFd<<endl;
continue;
}
//setnonblocking(connFd);
tempEvent.data.fd = connFd;
//tempEvent.events = EPOLLIN|EPOLLET; //设置新的连接要监听的事件为可读且为边沿触发
tempEvent.events = EPOLLIN;
epoll_ctl(epfd,EPOLL_CTL_ADD,connFd,&tempEvent);
} else if(events[i].events&EPOLLIN) { //如果是链接可读事件
readLength = recv(events[i].data.fd,buff,MAX_BUFFLEN,0);
if(readLength<0) {
memset(&clientAddr,0,sizeof(clientAddr));
clientLen = sizeof(clientAddr);
getsockname(events[i].data.fd,(struct sockaddr*)&clientAddr,&clientLen);
cout << "Read error! IP:"<<inet_ntoa(clientAddr.sin_addr);
cout <<" port:"<<ntohs(clientAddr.sin_port);
cout <<" Socket:"<<events[i].data.fd<<endl;
continue;
}
else if(readLength>0) {
buff[readLength] = '\0';
//cout<<endl<<events[i].data.fd <<": "<< buff; //打印请求
//RServer_requestMessage为请求信息类,创建一个请求信息对象并初始化,详见RServer_requestMessageQueue.h文件
RServer_threadTask* newReqeust = new RServer_threadTask(events[i].data.fd,buff,reqeustProcessFun,NULL);
//这里需要加锁
if(taskQueue->lockQueue()!=0) { //对请求队列taskQueue进行加锁,直至成功加锁为止
cout <<"error: get queue lock failed when insert task in queue"<<endl;
delete(newReqeust); //若失败则delete newrequest
close(events[i].data.fd); //并且一定要关闭socket
}
else {
if(!taskQueue->insertTask(newReqeust)) { //向请求队列末尾插入最新请求
close(newReqeust->getConnfd());
delete(newReqeust); //若失败加入队列失败,要记得释放newRequest的空间
}
taskQueue->unLockQueue(); //对taskQueue进行解锁
if(threadsPoll->lock()!=0) //发送信号量之前一定要用锁
cout << "error: get cond lock failed"<<endl;
else {
threadsPoll->postSignal(); //发送信号量
threadsPoll->unLock();
}
}
}
}
}
}
}
开发者ID:userain,项目名称:RServer,代码行数:87,代码来源:RServer_requestAcceptor.cpp
示例10: server_main
int server_main(char *home, char *dev, char *port, int udp, int ipv4, int log)
{
int lfd = -1, kdpfd, nfds, nfd, curfds, efd[2], refd[2], tunfd, i;
unsigned int cpus = 0, threads, udp_cpu = 0;
ssize_t ret;
struct epoll_event *events;
struct addrinfo hints, *ahead, *ai;
auth_log = !!log;
openlog("curvetun", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_DAEMON);
syslog(LOG_INFO, "curvetun server booting!\n");
syslog_maybe(!auth_log, LOG_INFO, "curvetun user logging disabled!\n");
parse_userfile_and_generate_user_store_or_die(home);
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
ret = getaddrinfo(NULL, port, &hints, &ahead);
if (ret < 0)
syslog_panic("Cannot get address info!\n");
for (ai = ahead; ai != NULL && lfd < 0; ai = ai->ai_next) {
lfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (lfd < 0)
continue;
if (ai->ai_family == AF_INET6) {
#ifdef IPV6_V6ONLY
ret = set_ipv6_only(lfd);
if (ret < 0) {
close(lfd);
lfd = -1;
continue;
}
#else
close(lfd);
lfd = -1;
continue;
#endif /* IPV6_V6ONLY */
}
set_reuseaddr(lfd);
set_mtu_disc_dont(lfd);
ret = bind(lfd, ai->ai_addr, ai->ai_addrlen);
if (ret < 0) {
close(lfd);
lfd = -1;
continue;
}
if (!udp) {
ret = listen(lfd, 5);
if (ret < 0) {
close(lfd);
lfd = -1;
continue;
}
}
if (ipv4 == -1) {
ipv4 = (ai->ai_family == AF_INET6 ? 0 :
(ai->ai_family == AF_INET ? 1 : -1));
}
syslog_maybe(auth_log, LOG_INFO, "curvetun on IPv%d via %s "
"on port %s!\n", ai->ai_family == AF_INET ? 4 : 6,
udp ? "UDP" : "TCP", port);
syslog_maybe(auth_log, LOG_INFO, "Allowed overlay proto is "
"IPv%d!\n", ipv4 ? 4 : 6);
}
freeaddrinfo(ahead);
if (lfd < 0 || ipv4 < 0)
syslog_panic("Cannot create socket!\n");
tunfd = tun_open_or_die(dev ? dev : DEVNAME_SERVER, IFF_TUN | IFF_NO_PI);
pipe_or_die(efd, O_NONBLOCK);
pipe_or_die(refd, O_NONBLOCK);
set_nonblocking(lfd);
events = xzmalloc(MAX_EPOLL_SIZE * sizeof(*events));
for (i = 0; i < MAX_EPOLL_SIZE; ++i)
events[i].data.fd = -1;
kdpfd = epoll_create(MAX_EPOLL_SIZE);
if (kdpfd < 0)
syslog_panic("Cannot create socket!\n");
set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, lfd,
udp ? EPOLLIN | EPOLLET | EPOLLONESHOT : EPOLLIN);
set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, efd[0], EPOLLIN);
set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, refd[0], EPOLLIN);
//.........这里部分代码省略.........
开发者ID:markusa,项目名称:netsniff-ng_filter,代码行数:101,代码来源:ct_server.c
示例11: configure_poll
int configure_poll(client_t * client)
{
int i, j;
client->header_size = malloc(sizeof(uint32_t) * client->num_parallel_connections);
client->buffered_packet = malloc(sizeof(packet_hash_t *) * client->num_parallel_connections);
client->agent_packet_index_in = malloc(sizeof(int) *client->num_parallel_connections);
client->agent_packet_queue_count = malloc(sizeof(int)*client->num_parallel_connections);
client->agent_needed_header_size = malloc(sizeof(int)*client->num_parallel_connections);
client->packet = malloc(sizeof(serialized_data_t) * client->num_parallel_connections);
for(i = 0; i < client->num_parallel_connections; i++) {
client->packet[i].serialized_data = malloc(sizeof(uint8_t) * client->transfer_request->buffer_size);
if(client->packet[i].serialized_data == NULL) {
printf("Malloc failed\n");
exit(1);
}
client->buffered_packet[i] = malloc(sizeof(packet_hash_t)*client->transfer_request->queue_size);
if(client->buffered_packet[i] == NULL) {
printf("Malloc failed\n");
exit(1);
}
client->agent_packet_queue_count[i] = 0;
client->packet[i].host_packet_size = 0;
client->agent_packet_index_in[i] = EMPTY;
client->agent_needed_header_size[i] = 0;
for(j = 0; j < client->transfer_request->queue_size; j++)
{
client->buffered_packet[i][j].size = EMPTY ;
client->buffered_packet[i][j].in_use = 0 ;
client->buffered_packet[i][j].serialized_data = malloc(sizeof(uint8_t)*client->transfer_request->buffer_size);
if(client->buffered_packet[i][j].serialized_data == NULL) {
printf("malloc failed\n");
exit(1);
}
}
}
client->event_poll_out_host = epoll_create(1);
client->event_poll_out_agent = epoll_create(1);
client->client_event_pool = epoll_create(1);
if(client->client_event_pool < 0 || client->event_poll_out_agent < 0)
{
perror("client_event_pool");
return EXIT_FAILURE;
}
client->event.data.ptr = &client->host_side_event_info;
client->host_side_event_info.type = HOST_SIDE_DATA;
client->host_side_event_info.fd = client->host_sock;
client->host_side_event_info.client = client;
client->event.events = EPOLLIN;
client->host_fd_poll = IN;
if( epoll_ctl(client->client_event_pool, EPOLL_CTL_ADD,
client->host_sock, &client->event))
{
perror("");
printf("%s %d\n", __FILE__, __LINE__);
exit(1);
}
client->event.events = EPOLLOUT;
if( epoll_ctl(client->event_poll_out_host, EPOLL_CTL_ADD,
client->host_sock, &client->event))
{
perror("");
printf("%s %d\n", __FILE__, __LINE__);
exit(1);
}
for(i = 0; i < client->num_parallel_connections; i++)
{
client->event.events = EPOLLIN;
client->event.data.ptr = &client->agent_side_event_info[i];
client->agent_side_event_info[i].fd = client->agent_sock[i];
client->agent_side_event_info[i].agent_id = i;
client->agent_side_event_info[i].type = AGENT_SIDE_DATA;
client->agent_side_event_info[i].client = client;
client->agent_fd_poll[i] = IN;
if( epoll_ctl(client->client_event_pool, EPOLL_CTL_ADD,
client->agent_sock[i], &client->event))
{
perror("");
printf("%s %d\n", __FILE__, __LINE__);
exit(1);
}
client->event.events = EPOLLOUT;
//.........这里部分代码省略.........
开发者ID:aaronorosen,项目名称:sos-agent,代码行数:101,代码来源:network.c
示例12: main
int main(int argc, char **argv)
{
if (argc <= 2)
{
printf("usage: %s ip_address port_number\n", argv[0]);
return -1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int ret = 0;
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
inet_pton(AF_INET, ip, &address.sin_addr);
address.sin_port = htons(port);
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
assert( listenfd >= 0 );
ret = bind(listenfd, (struct sockaddr *) &address, sizeof(address));
assert( ret != -1 );
ret = listen(listenfd, 5);
assert( ret != -1 );
struct epoll_event events[MAX_EVENT_NUMBER];
int epollfd = epoll_create(5);
assert( epollfd != -1 );
addfd( epollfd, listenfd, 0 );
while (1)
{
int ret = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
if (ret < 0)
{
printf("epoll failure\n");
break;
}
for(int i = 0; i < ret; i++)
{
int sockfd = events[i].data.fd;
if (sockfd == listenfd)
{
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(listenfd, (struct sockaddr *) &client_address, &client_addrlength);
addfd(epollfd, connfd, 1);
}
else if (events[i].events & EPOLLIN)
{
pthread_t thread;
struct fds fds_for_new_worker;
fds_for_new_worker.epollfd = epollfd;
fds_for_new_worker.sockfd = sockfd;
pthread_create(&thread, NULL, worker, (void *) &fds_for_new_worker);
}
else
{
printf("something else happened \n");
}
}
}
close(listenfd);
return 0;
}
开发者ID:wrx1721267632,项目名称:small,代码行数:68,代码来源:epoll_server1.c
示例13: main
int
main(int argc, char **argv)
{
int sockfd;
struct server_node sn[SERV_NR], *sn_tmp;
struct timeval at;
struct itimerval delay;
unsigned long udelay;
int timer_ret;
struct sigaction tm_action;
struct sockaddr_in servaddr;
int i=0;
/* dns query: www.google.com */
unsigned char msg[32] = {0x05,0xf7,0x01,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x03, \
0x77,0x77,0x77,0x06,0x67,0x6f,0x6f,0x67,0x6c,0x65,0x03,0x63,0x6f,0x6d,0x00,0x00,\
0x01,0x00,0x01};
int event_nr;
char buf[BUFFSIZE];
int n;
int epfd;
struct epoll_event sock_event[SERV_NR];
struct epoll_event callback_events[SERV_NR];
int serv_nr;
if (argc < 2)
err_quit("usage: udpcli <DNS_SERVER1> <DNS_SERVER2> ...");
serv_nr = argc-1;
delay.it_value.tv_sec = 5;
delay.it_value.tv_usec = 0;
delay.it_interval.tv_sec = 0;
delay.it_interval.tv_usec = 0;
tm_action.sa_handler = &epoll_tm_handler;
tm_action.sa_flags = SA_SIGINFO|SA_RESTART|SA_RESETHAND;
epfd = epoll_create(SERV_NR);
if (epfd < 0)
err_quit("epoll_create");
for (;i<serv_nr;i++){
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(53);
Inet_pton(AF_INET, argv[i+1], &servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
sn[i].fd = sockfd;
sn[i].server_ip = argv[i+1];
sock_event[i].data.ptr = &sn[i];
sock_event[i].events = EPOLLIN;
if(epoll_ctl (epfd, EPOLL_CTL_ADD, sockfd, &sock_event[i]))
err_quit("epoll_ctl");
gettimeofday(&sn[i].t,NULL);
Sendto(sockfd, msg, 32, 0, (SA *)&servaddr, sizeof(struct sockaddr_in));
}
sigaction(SIGALRM, &tm_action, NULL);
timer_ret = setitimer(ITIMER_REAL, &delay, NULL);
if(timer_ret){
err_quit("setitimer");
}
while (serv_nr>0) {
event_nr = epoll_wait(epfd, (struct epoll_event *)&callback_events, SERV_NR, 1000);
if (event_nr < 0){
err_quit("timeout");
}else if(event_nr == 0){
printf(".");
fflush(stdout);
}
for (i=0; i<event_nr; i++){
sn_tmp = (struct server_node *)(callback_events[i].data.ptr);
gettimeofday(&at, NULL);
udelay = (at.tv_sec-(sn_tmp->t).tv_sec)*1000000+(at.tv_usec-(sn_tmp->t).tv_usec);
n = Read(sn_tmp->fd, buf, BUFFSIZE);
if (n == 0) {
err_quit("Read nothing");
}
if(epoll_ctl(epfd, EPOLL_CTL_DEL, sn_tmp->fd, NULL)){
err_quit("epoll_ctl del");
}
memset(buf, 0, BUFFSIZE);
close(sn_tmp->fd);
printf("response from server: %s\t%lu\n",sn_tmp->server_ip,udelay/1000);
}
serv_nr -= event_nr;
}
close(epfd);
printf("\n");
exit(0);
}
开发者ID:lainredsonic,项目名称:unpv13e-prv,代码行数:95,代码来源:udpcli01.c
示例14: usdf_fabric_open
static int
usdf_fabric_open(struct fi_fabric_attr *fattrp, struct fid_fabric **fabric,
void *context)
{
struct usdf_fabric *fp;
struct usdf_usnic_info *dp;
struct usdf_dev_entry *dep;
struct epoll_event ev;
struct sockaddr_in sin;
int ret;
int d;
/* Make sure this fabric exists */
dp = __usdf_devinfo;
for (d = 0; d < dp->uu_num_devs; ++d) {
dep = &dp->uu_info[d];
if (dep->ue_dev != NULL &&
strcmp(fattrp->name, dep->ue_dattr.uda_devname) == 0) {
break;
}
}
if (d >= dp->uu_num_devs) {
USDF_INFO("device \"%s\" does not exit, returning -FI_ENODEV\n",
fattrp->name);
return -FI_ENODEV;
}
fp = calloc(1, sizeof(*fp));
if (fp == NULL) {
USDF_INFO("unable to allocate memory for fabric\n");
return -FI_ENOMEM;
}
fp->fab_epollfd = -1;
fp->fab_arp_sockfd = -1;
LIST_INIT(&fp->fab_domain_list);
fp->fab_attr.fabric = fab_utof(fp);
fp->fab_attr.name = strdup(fattrp->name);
fp->fab_attr.prov_name = strdup(USDF_PROV_NAME);
fp->fab_attr.prov_version = USDF_PROV_VERSION;
if (fp->fab_attr.name == NULL ||
fp->fab_attr.prov_name == NULL) {
ret = -FI_ENOMEM;
goto fail;
}
fp->fab_fid.fid.fclass = FI_CLASS_FABRIC;
fp->fab_fid.fid.context = context;
fp->fab_fid.fid.ops = &usdf_fi_ops;
fp->fab_fid.ops = &usdf_ops_fabric;
fp->fab_dev_attrs = &dep->ue_dattr;
fp->fab_epollfd = epoll_create(1024);
if (fp->fab_epollfd == -1) {
ret = -errno;
USDF_INFO("unable to allocate epoll fd\n");
goto fail;
}
fp->fab_eventfd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE);
if (fp->fab_eventfd == -1) {
ret = -errno;
USDF_INFO("unable to allocate event fd\n");
goto fail;
}
fp->fab_poll_item.pi_rtn = usdf_fabric_progression_cb;
fp->fab_poll_item.pi_context = fp;
ev.events = EPOLLIN;
ev.data.ptr = &fp->fab_poll_item;
ret = epoll_ctl(fp->fab_epollfd, EPOLL_CTL_ADD, fp->fab_eventfd, &ev);
if (ret == -1) {
ret = -errno;
USDF_INFO("unable to EPOLL_CTL_ADD\n");
goto fail;
}
ret = pthread_create(&fp->fab_thread, NULL,
usdf_fabric_progression_thread, fp);
if (ret != 0) {
ret = -ret;
USDF_INFO("unable to create progress thread\n");
goto fail;
}
/* initialize timer subsystem */
ret = usdf_timer_init(fp);
if (ret != 0) {
USDF_INFO("unable to initialize timer\n");
goto fail;
}
/* create and bind socket for ARP resolution */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = fp->fab_dev_attrs->uda_ipaddr_be;
fp->fab_arp_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (fp->fab_arp_sockfd == -1) {
USDF_INFO("unable to create socket\n");
goto fail;
//.........这里部分代码省略.........
开发者ID:luomiao,项目名称:libfabric,代码行数:101,代码来源:usdf_fabric.c
示例15: main
int main (int argc, char **argv)
{
struct epoll_event ev, events[MAX_EVENTS];
struct sockaddr_in server_addr, client_addr;
int sock, conn_sock, epollfd, nfds;
int n;
if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
perror ("socket");
return EXIT_FAILURE;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons (5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero (&(server_addr.sin_zero), 8);
if (bind (sock, (struct sockaddr*) &server_addr, sizeof (struct sockaddr))) {
perror ("bind");
return EXIT_FAILURE;
}
if (listen (sock, 5) == -1) {
perror ("listen");
return EXIT_FAILURE;
}
epollfd = epoll_create (MAX_EVENTS);
if (epollfd == -1) {
perror ("epoll_create");
return EXIT_FAILURE;
}
ev.events = EPOLLIN;
ev.data.fd = sock;
if (epoll_ctl (epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) {
perror ("epoll_ctl: sock");
return EXIT_FAILURE;
}
for (;;) {
nfds = epoll_wait (epollfd, events, MAX_EVENTS, -1);
if (nfds == -1) {
perror ("epoll_wait");
return EXIT_FAILURE;
}
for (n = 0; n < nfds; ++n) {
if (events[n].data.fd == sock) {
conn_sock = accept (sock,
(struct sockaddr *) &client_addr, sizeof (struct sockaddr_in));
if (conn_sock == -1) {
perror ("accept");
exit (EXIT_FAILURE);
}
//setnonblocking(conn_sock);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = conn_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) {
perror("epoll_ctl: conn_sock");
exit(EXIT_FAILURE);
}
} else {
//do_use_fd(events[n].data.fd);
}
}
}
close (epollfd);
return 0;
}
开发者ID:wfelipe,项目名称:misc,代码行数:69,代码来源:listen.c
示例16: epoll_create
void my_epoll::create_epoll(){
epollfd = epoll_create(100);
}
开发者ID:yikiinvisible,项目名称:ChatRoom,代码行数:3,代码来源:my_epoll.cpp
|
请发表评论