コード例 #1
0
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;
}
コード例 #2
0
static void *worker_thread_or_server(void *userdata)
{
	int ready, i, n, bufsize = 128 * 1024;
	void *buf;
	struct epoll_event ev, *e;

	unsigned short port = (unsigned short)(int)(long)userdata;

	int s_listen, new_fd;
	struct sockaddr_in their_addr;
	struct sockaddr_in my_addr;
	socklen_t sin_size;

	ignore_pipe();

	if ((s_listen = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		kerror("c:%s, e:%s\n", "socket", strerror(errno));
		return NULL;
	}

	config_socket(s_listen);

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(port);
	my_addr.sin_addr.s_addr = INADDR_ANY;
	memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero));
	if (bind(s_listen, (struct sockaddr *) &my_addr, sizeof(my_addr)) == -1) {
		kerror("c:%s, e:%s\n", "bind", strerror(errno));
		return NULL;
	}

	if (listen(s_listen, BACKLOG) == -1) {
		kerror("c:%s, e:%s\n", "listen", strerror(errno));
		return NULL;
	}

	__g_epoll_fd = epoll_create(__g_epoll_max);
	memset(&ev, 0, sizeof(ev));
	ev.data.fd = s_listen;
	ev.events = EPOLLIN;
	epoll_ctl(__g_epoll_fd, EPOLL_CTL_ADD, s_listen, &ev);

	buf = kmem_alloc(bufsize, char);
	for (;;) {
		do
			ready = epoll_wait(__g_epoll_fd, __g_epoll_events, __g_epoll_max, -1);
		while ((ready == -1) && (errno == EINTR));

		for (i = 0; i < ready; i++) {
			e = __g_epoll_events + i;

			if (e->data.fd == s_listen) {
				sin_size = sizeof(their_addr);
				if ((new_fd = accept(s_listen, (struct sockaddr *) &their_addr, &sin_size)) == -1) {
					kerror("c:%s, e:%s\n", "accept", strerror(errno));
					continue;
				}

				/* FIXME: non-blocking will cause orbatch bang */
				/* setnonblocking(new_fd); */

				/* XXX: new_fd can be o or w */
				if (process_connect(new_fd))
					close_connect(new_fd);

				continue;
			} else if (!(e->events & EPOLLIN)) {
				kerror("!!! Not EPOLLIN: event is %08x, fd:%d\n", e->events, e->data.fd);
				continue;
			}

			if ((n = recv(e->data.fd, buf, bufsize, 0)) > 0) {
				if (do_opt_command(e->data.fd, buf, n))
					close_connect(e->data.fd);
			} else {
				klog("Remote close socket: %d\n", e->data.fd);
				close_connect(e->data.fd);
			}
		}
	}
	kmem_free(buf);

	close(__g_epoll_fd);
	__g_epoll_fd = -1;

	return NULL;
}
コード例 #3
0
ファイル: kvsp-tpub.c プロジェクト: JHUAPL/kvspool
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;
}
コード例 #4
0
ファイル: epoll_udp.c プロジェクト: songtzu/iotp
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);
				//} 
			} 
		}
	}

    close(listener);
    return 0;
}
コード例 #5
0
ファイル: sock_util.c プロジェクト: kevin4fly/network
/* 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);
                }
            }

            if (fd == connfd && (events[i].events & EPOLLIN) )
            {
                int space = buffer_hasspace(&recvbuf);
                if (space > 0)
                {
                    int nread = read(fd,&recvbuf.buffer[recvbuf.in],space);

                    /* read error */
                    if (nread < 0)
                    {
                        perror_exit("read error");
                    }

                    /* read "FIN" from server */
                    else if (nread == 0)
                    {
                        /* we have sent "FIN" already */
                        if (shutdown_flag == 0)
                        {
                            printf("server terminates unexpectedly!\n");
                            exit(EXIT_FAILURE);
                        }
                        else
                            return;
                    }

                    else
                    {
                        recvbuf.in += nread;

                        /* add STDOUT_FILENO to epoll set */
                        delete_epoll_event(epollfd,connfd,EPOLLIN);
                        add_epoll_event(epollfd,STDOUT_FILENO,EPOLLOUT);
                    }
                }
            }

            if (fd == STDOUT_FILENO && (events[i].events & EPOLLOUT) )
            {
                int ntotal = strlen(recvbuf.buffer);

                int nwrite = write(fd,&recvbuf.buffer[recvbuf.out],ntotal);

                if (nwrite < 0)
                {
                    if (errno != EAGAIN)
                    {
                        perror_exit("write error");
                    }
                }

                else
                {
                    recvbuf.out += ntotal;

                    /* all data has been sent to STANDARD OUTPUT, reset
                     * the buffer space */
                    if (recvbuf.in == recvbuf.out)
                    {
                        buffer_reset(&recvbuf);

                        delete_epoll_event(epollfd,STDOUT_FILENO,EPOLLOUT);
                    }
                }
            }
        }
    }
}
コード例 #6
0
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);
        } else if (bytes < (ssize_t)sizeof(fired)) {
            fprintf(stderr, "unexpected read from timer fd: %zd\n", bytes);
        }

        if (fired != 1) {
            fprintf(stderr, "unexpected timer fd fired count: %" PRIu64 "\n", fired);
        }

        ret = clock_gettime(CLOCK_BOOTTIME, &actual_time);
        if (ret < 0) {
            perror("failed to get time");
            exit(EXIT_FAILURE);
        }

        long long diff = timediff_ns(&actual_time, &expected_time);
        if (llabs(diff) > NSEC_PER_SEC) {
            fprintf(stderr, "alarm arrived %lld.%03lld seconds %s\n",
                    llabs(diff) / NSEC_PER_SEC,
                    (llabs(diff) / NSEC_PER_MSEC) % MSEC_PER_SEC,
                    diff > 0 ? "late" : "early");
            KLOG_ERROR("suspend_stress", "alarm arrived %lld.%03lld seconds %s\n",
                    llabs(diff) / NSEC_PER_SEC,
                    (llabs(diff) / NSEC_PER_MSEC) % MSEC_PER_SEC,
                    diff > 0 ? "late" : "early");
            if (abort_on_failure) {
                exit(EXIT_FAILURE);
            }
        }

        time_t t = time(NULL);
        i += fired;
        printf("timer fired: %d at boottime %lld.%.3ld, %s", i,
                   (long long)actual_time.tv_sec,
                   actual_time.tv_nsec / NSEC_PER_MSEC,
                   ctime(&t));

        KLOG_INFO("suspend_stress", "timer fired: %d at boottime %lld.%.3ld, %s", i,
                   (long long)actual_time.tv_sec,
                   actual_time.tv_nsec / NSEC_PER_MSEC,
                   ctime(&t));

        if (count > 0)
            count--;
    }
    return 0;
}
コード例 #7
0
ファイル: test_main.cpp プロジェクト: AlaAyesh/libvma
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);
}
コード例 #8
0
ファイル: ngx_event.c プロジェクト: ygliang2009/ngx
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);


#if (NGX_HAVE_RTSIG)

    if (!rtsig) {
        return NGX_CONF_OK;
    }

    if (ecf->accept_mutex) {
        return NGX_CONF_OK;
    }

    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

    if (ccf->worker_processes == 0) {
        return NGX_CONF_OK;
    }

    ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
                  "the \"rtsig\" method requires \"accept_mutex\" to be on");

    return NGX_CONF_ERROR;

#else

    return NGX_CONF_OK;

#endif
}
コード例 #9
0
ファイル: pgmrecv.c プロジェクト: demonlife/infinidb
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;
}
コード例 #10
0
//接收器启动函数
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();
                        }
                    }
                }
            }
        }
    }
}
コード例 #11
0
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);
	set_epoll_descriptor(kdpfd, EPOLL_CTL_ADD, tunfd,
			     EPOLLIN | EPOLLET | EPOLLONESHOT);
	curfds = 4;

	trie_init();

	cpus = get_number_cpus_online();
	threads = cpus * THREADS_PER_CPU;
	if (!ispow2(threads))
		syslog_panic("Thread number not power of two!\n");

	threadpool = xzmalloc(sizeof(*threadpool) * threads);
	thread_spawn_or_panic(cpus, efd[1], refd[1], tunfd, ipv4, udp);

	init_cpusched(threads);

	register_socket(tunfd);
	register_socket(lfd);

	syslog(LOG_INFO, "curvetun up and running!\n");

	while (likely(!sigint)) {
		nfds = epoll_wait(kdpfd, events, curfds, -1);
		if (nfds < 0) {
			syslog(LOG_ERR, "epoll_wait error: %s\n",
			       strerror(errno));
			break;
		}

		for (i = 0; i < nfds; ++i) {
			if (unlikely(events[i].data.fd < 0))
				continue;

			if (events[i].data.fd == lfd && !udp) {
				int ncpu;
				char hbuff[256], sbuff[256];
				struct sockaddr_storage taddr;
				socklen_t tlen;

				tlen = sizeof(taddr);
				nfd = accept(lfd, (struct sockaddr *) &taddr,
					     &tlen);
				if (nfd < 0) {
					syslog(LOG_ERR, "accept error: %s\n",
					       strerror(errno));
					continue;
				}

				if (curfds + 1 > MAX_EPOLL_SIZE) {
					close(nfd);
					continue;
				}

				curfds++;

				ncpu = register_socket(nfd);

				memset(hbuff, 0, sizeof(hbuff));
				memset(sbuff, 0, sizeof(sbuff));
				getnameinfo((struct sockaddr *) &taddr, tlen,
					    hbuff, sizeof(hbuff),
					    sbuff, sizeof(sbuff),
					    NI_NUMERICHOST | NI_NUMERICSERV);

				syslog_maybe(auth_log, LOG_INFO, "New connection "
					     "from %s:%s with id %d on CPU%d, %d "
					     "active!\n", hbuff, sbuff, nfd, ncpu,
					     curfds);

				set_nonblocking(nfd);
				set_socket_keepalive(nfd);
				set_tcp_nodelay(nfd);
				ret = set_epoll_descriptor2(kdpfd, EPOLL_CTL_ADD,
						nfd, EPOLLIN | EPOLLET | EPOLLONESHOT);
				if (ret < 0) {
					close(nfd);
					curfds--;
					continue;
				}
			} else if (events[i].data.fd == refd[0]) {
				int fd_one;

				ret = read_exact(refd[0], &fd_one,
						 sizeof(fd_one), 1);
				if (ret != sizeof(fd_one) || fd_one <= 0)
					continue;

				ret = set_epoll_descriptor2(kdpfd, EPOLL_CTL_MOD,
						fd_one, EPOLLIN | EPOLLET | EPOLLONESHOT);
				if (ret < 0) {
					close(fd_one);
					continue;
				}
			} else if (events[i].data.fd == efd[0]) {
				int fd_del, test;

				ret = read_exact(efd[0], &fd_del,
						 sizeof(fd_del), 1);
				if (ret != sizeof(fd_del) || fd_del <= 0)
					continue;

				ret = read(fd_del, &test, sizeof(test));
				if (ret < 0 && errno == EBADF)
					continue;

				ret = set_epoll_descriptor2(kdpfd, EPOLL_CTL_DEL,
						fd_del, 0);
				if (ret < 0) {
					close(fd_del);
					continue;
				}

				close(fd_del);
				curfds--;
				unregister_socket(fd_del);

				syslog_maybe(auth_log, LOG_INFO, "Closed connection "
					     "with id %d, %d active!\n", fd_del,
					     curfds);
			} else {
				int cpu, fd_work = events[i].data.fd;

				if (!udp)
					cpu = socket_to_cpu(fd_work);
				else
					udp_cpu = (udp_cpu + 1) & (threads - 1);

				write_exact(threadpool[udp ? udp_cpu : cpu].efd[1],
					    &fd_work, sizeof(fd_work), 1);
			}
		}
	}

	syslog(LOG_INFO, "curvetun prepare shut down!\n");

	close(lfd);
	close(efd[0]);
	close(efd[1]);
	close(refd[0]);
	close(refd[1]);
	close(tunfd);

	thread_finish(cpus);

	xfree(threadpool);
	xfree(events);

	unregister_socket(lfd);
	unregister_socket(tunfd);

	destroy_cpusched();

	trie_cleanup();

	destroy_user_store();

	syslog(LOG_INFO, "curvetun shut down!\n");
	closelog();

	return 0;
}
コード例 #12
0
ファイル: network.c プロジェクト: aaronorosen/sos-agent
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; 


   	if( epoll_ctl(client->event_poll_out_agent, EPOLL_CTL_ADD, 
   		client->agent_sock[i], &client->event))
   	{
			perror(""); 
			printf("%s %d\n", __FILE__, __LINE__); 
   		exit(1); 
   	}

	} 

	return EXIT_SUCCESS; 
}
コード例 #13
0
ファイル: epoll_server1.c プロジェクト: wrx1721267632/small
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;
}
コード例 #14
0
ファイル: udpcli01.c プロジェクト: lainredsonic/unpv13e-prv
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);
}
コード例 #15
0
ファイル: usdf_fabric.c プロジェクト: luomiao/libfabric
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;
	}
	ret = bind(fp->fab_arp_sockfd, (struct sockaddr *) &sin, sizeof(sin));
	if (ret == -1) {
		ret = -errno;
		goto fail;
	}

	atomic_init(&fp->fab_refcnt, 0);
	fattrp->fabric = fab_utof(fp);
	fattrp->prov_version = USDF_PROV_VERSION;
	*fabric = fab_utof(fp);
	USDF_INFO("successfully opened %s/%s\n", fattrp->name,
			fp->fab_dev_attrs->uda_ifname);
	return 0;

fail:
	if (fp != NULL) {
		if (fp->fab_epollfd != -1) {
			close(fp->fab_epollfd);
		}
		if (fp->fab_eventfd != -1) {
			close(fp->fab_eventfd);
		}
		if (fp->fab_arp_sockfd != -1) {
			close(fp->fab_arp_sockfd);
		}
		usdf_timer_deinit(fp);
		free(fp);
	}
	USDF_DEBUG("returning %d (%s)\n", ret, fi_strerror(-ret));
	return ret;
}
コード例 #16
0
ファイル: listen.c プロジェクト: wfelipe/misc
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;
}
コード例 #17
0
ファイル: my_epoll.cpp プロジェクト: yikiinvisible/ChatRoom
void my_epoll::create_epoll(){
	epollfd = epoll_create(100);
}
コード例 #18
0
/* create a new node using this plug-in */
void filetransfer_new(int argc, char* argv[]) {
	ft->shadowlib->log(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__, "filetransfer_new called");

	ft->client = NULL;
	ft->server = NULL;

	const gchar* USAGE = "\nFiletransfer usage:\n"
			"\t'server serverListenPort pathToDocRoot'\n"
			"\t'client single fileServerHostname fileServerPort socksServerHostname(or 'none') socksServerPort nDownloads pathToFile'\n"
			"\t'client multi pathToDownloadSpec socksServerHostname(or 'none') socksServerPort pathToThinktimeCDF(or 'none') secondsRunTime(or '-1') [nDownloads(or '-1')]'\n";
	if(argc < 2) goto printUsage;

	/* parse command line args, first is program name */
	gchar* mode = argv[1];

	/* create an epoll so we can wait for IO events */
	gint epolld = epoll_create(1);
	if(epolld == -1) {
		ft->shadowlib->log(SHADOW_LOG_LEVEL_WARNING, __FUNCTION__, "Error in server epoll_create");
		close(epolld);
		epolld = 0;
	}

	if(g_ascii_strncasecmp(mode, "client", 6) == 0) {
		/* check client args */
		if(argc < 3) goto printUsage;

		ft->client = g_new0(service_filegetter_t, 1);

		gchar* clientMode = argv[2];
		gint sockd = -1;

		if(g_ascii_strncasecmp(clientMode, "single", 6) == 0) {
			service_filegetter_single_args_t args;

			args.http_server.host = argv[3];
			args.http_server.port = argv[4];
			args.socks_proxy.host = argv[5];
			args.socks_proxy.port = argv[6];
			args.num_downloads = argv[7];
			args.filepath = _filetransfer_getHomePath(argv[8]);

			args.log_cb = &_filetransfer_logCallback;
			args.hostbyname_cb = &_filetransfer_HostnameCallback;
			args.sleep_cb = &_filetransfer_sleepCallback;

			enum filegetter_code result = service_filegetter_start_single(ft->client, &args, epolld, &sockd);

			if(result != FG_SUCCESS) {
				ft->shadowlib->log(SHADOW_LOG_LEVEL_CRITICAL, __FUNCTION__, "fileclient error, not started!");
				g_free(ft->client);
				ft->client = NULL;
			}

			g_free(args.filepath);
		} else if(g_ascii_strncasecmp(clientMode, "multi", 5) == 0) {
			service_filegetter_multi_args_t args;
			memset(&args, 0, sizeof(service_filegetter_multi_args_t));

			args.server_specification_filepath = _filetransfer_getHomePath(argv[3]);
			args.socks_proxy.host = argv[4];
			args.socks_proxy.port = argv[5];
			args.thinktimes_cdf_filepath = _filetransfer_getHomePath(argv[6]);
			args.runtime_seconds = argv[7];

			if(argc > 8) {
				args.num_downloads = argv[8];
			}

			if(g_ascii_strncasecmp(args.thinktimes_cdf_filepath, "none", 4) == 0) {
				args.thinktimes_cdf_filepath = NULL;
			}

			args.log_cb = &_filetransfer_logCallback;
			args.hostbyname_cb = &_filetransfer_HostnameCallback;
			args.sleep_cb = &_filetransfer_sleepCallback;

			enum filegetter_code result = service_filegetter_start_multi(ft->client, &args, epolld, &sockd);

			if(result != FG_SUCCESS) {
				ft->shadowlib->log(SHADOW_LOG_LEVEL_CRITICAL, __FUNCTION__, "fileclient error, not started!");
				g_free(ft->client);
				ft->client = NULL;
			}

			if(args.thinktimes_cdf_filepath)
				g_free(args.thinktimes_cdf_filepath);
			g_free(args.server_specification_filepath);
		} else {
			/* unknown client mode */
			g_free(ft->client);
			ft->client = NULL;
			goto printUsage;
		}

		/* successfull arguments */
		if(sockd >= 0) {
			service_filegetter_activate(ft->client, sockd);
		}
	} else if(g_ascii_strncasecmp(mode, "server", 6) == 0) {
		/* check server args */
		if(argc < 4) goto printUsage;

		/* we are running a server */
		in_addr_t listenIP = INADDR_ANY;
		in_port_t listenPort = (in_port_t) atoi(argv[2]);
		gchar* docroot = _filetransfer_getHomePath(argv[3]);

		ft->server = g_new0(fileserver_t, 1);

		ft->shadowlib->log(SHADOW_LOG_LEVEL_INFO, __FUNCTION__, "serving '%s' on port %u", docroot, listenPort);
		enum fileserver_code res = fileserver_start(ft->server, epolld, htonl(listenIP), htons(listenPort), docroot, 1000);

		if(res == FS_SUCCESS) {
			gchar ipStringBuffer[INET_ADDRSTRLEN+1];
			memset(ipStringBuffer, 0, INET_ADDRSTRLEN+1);
			inet_ntop(AF_INET, &listenIP, ipStringBuffer, INET_ADDRSTRLEN);
			ft->shadowlib->log(SHADOW_LOG_LEVEL_MESSAGE, __FUNCTION__, "fileserver running on at %s:%u", ipStringBuffer, listenPort);
		} else {
			ft->shadowlib->log(SHADOW_LOG_LEVEL_CRITICAL, __FUNCTION__, "fileserver error, not started!");
			g_free(ft->server);
			ft->server = NULL;
		}

		g_free(docroot);
	} else {
		/* not client or server... */
		goto printUsage;
	}

	return;
printUsage:
	ft->shadowlib->log(SHADOW_LOG_LEVEL_CRITICAL, __FUNCTION__, (gchar*)USAGE);
	return;
}
コード例 #19
0
ファイル: pgmrecv.c プロジェクト: banburybill/openpgm
static
gpointer
receiver_thread (
	gpointer	data
	)
{
	pgm_sock_t* rx_sock = (pgm_sock_t*)data;
	const long iov_len = 20;
	const long ev_len  = 1;
	struct pgm_msgv_t msgv[iov_len];

#ifdef CONFIG_HAVE_EPOLL
	struct epoll_event events[ev_len];	/* wait for maximum 1 event */
	const 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;
	}

	if (pgm_epoll_ctl (rx_sock, efd, EPOLL_CTL_ADD, EPOLLIN) < 0)
	{
		g_error ("pgm_epoll_ctl failed errno %i: \"%s\"", errno, strerror(errno));
		g_main_loop_quit (g_loop);
		return NULL;
	}
	struct epoll_event event;
	event.events = EPOLLIN;
	event.data.fd = g_quit_pipe[0];
	if (epoll_ctl (efd, EPOLL_CTL_ADD, g_quit_pipe[0], &event) < 0)
	{
		g_error ("epoll_ctl failed errno %i: \"%s\"", errno, strerror(errno));
		g_main_loop_quit (g_loop);
		return NULL;
	}
#elif defined(CONFIG_HAVE_POLL)
	int n_fds = 2;
	struct pollfd fds[ 1 + n_fds ];
#elif defined(G_OS_UNIX) /* HAVE_SELECT */
	int n_fds;
	fd_set readfds;
#else /* G_OS_WIN32 */
	SOCKET recv_sock, pending_sock;
	DWORD cEvents = PGM_RECV_SOCKET_READ_COUNT + 1;
	WSAEVENT waitEvents[ PGM_RECV_SOCKET_READ_COUNT + 1 ];
	socklen_t socklen = sizeof (SOCKET);

	waitEvents[0] = g_quit_event;
	waitEvents[1] = WSACreateEvent ();
	pgm_getsockopt (rx_sock, IPPROTO_PGM, PGM_RECV_SOCK, &recv_sock, &socklen);
	WSAEventSelect (recv_sock, waitEvents[1], FD_READ);
	waitEvents[2] = WSACreateEvent ();
	pgm_getsockopt (rx_sock, IPPROTO_PGM, PGM_PENDING_SOCK, &pending_sock, &socklen);
	WSAEventSelect (pending_sock, waitEvents[2], FD_READ);
#endif /* !CONFIG_HAVE_EPOLL */

	do {
		struct timeval tv;
#ifndef _WIN32
		int timeout;
#else
		DWORD dwTimeout, dwEvents;
#endif
		size_t len;
		pgm_error_t* pgm_err = NULL;
		const int status = pgm_recvmsgv (rx_sock,
					         msgv,
					         G_N_ELEMENTS(msgv),
					         0,
					         &len,
					         &pgm_err);
		switch (status) {
		case PGM_IO_STATUS_NORMAL:
			on_msgv (msgv, len);
			break;
		case PGM_IO_STATUS_TIMER_PENDING:
			{
				socklen_t optlen = sizeof (tv);
				pgm_getsockopt (rx_sock, IPPROTO_PGM, PGM_TIME_REMAIN, &tv, &optlen);
			}
			goto block;
		case PGM_IO_STATUS_RATE_LIMITED:
			{
				socklen_t optlen = sizeof (tv);
				pgm_getsockopt (rx_sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen);
			}
/* fall through */
		case PGM_IO_STATUS_WOULD_BLOCK:
block:
#ifdef CONFIG_HAVE_EPOLL
			timeout = PGM_IO_STATUS_WOULD_BLOCK == status ? -1 : ((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
			epoll_wait (efd, events, G_N_ELEMENTS(events), timeout /* ms */);
#elif defined(CONFIG_HAVE_POLL)
			timeout = PGM_IO_STATUS_WOULD_BLOCK == status ? -1 : ((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
			memset (fds, 0, sizeof(fds));
			fds[0].fd = g_quit_pipe[0];
			fds[0].events = POLLIN;
			pgm_poll_info (rx_sock, &fds[1], &n_fds, POLLIN);
			poll (fds, 1 + n_fds, timeout /* ms */);
#elif defined(G_OS_UNIX) /* HAVE_SELECT */
			FD_ZERO(&readfds);
			FD_SET(g_quit_pipe[0], &readfds);
			n_fds = g_quit_pipe[0] + 1;
			pgm_select_info (rx_sock, &readfds, NULL, &n_fds);
			select (n_fds, &readfds, NULL, NULL, PGM_IO_STATUS_RATE_LIMITED == status ? &tv : NULL);
#else /* G_OS_WIN32 */
			dwTimeout = PGM_IO_STATUS_WOULD_BLOCK == status ? WSA_INFINITE : ((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
			dwEvents = WSAWaitForMultipleEvents (cEvents, waitEvents, FALSE, dwTimeout, FALSE);
			switch (dwEvents) {
			case WSA_WAIT_EVENT_0+1: WSAResetEvent (waitEvents[1]); break;
			case WSA_WAIT_EVENT_0+2: WSAResetEvent (waitEvents[2]); break;
			default: break;
			}
#endif /* !CONFIG_HAVE_EPOLL */
			break;

		default:
			if (pgm_err) {
				g_warning ("%s", pgm_err->message);
				pgm_error_free (pgm_err);
				pgm_err = NULL;
			}
			if (PGM_IO_STATUS_ERROR == status)
				break;
		}
	} while (!g_quit);

#ifdef CONFIG_HAVE_EPOLL
	close (efd);
#elif defined(G_OS_WIN32)
	WSACloseEvent (waitEvents[1]);
	WSACloseEvent (waitEvents[2]);
#  if (__STDC_VERSION__ < 199901L)
	g_free (waitHandles);
#  endif
#endif
	return NULL;
}
コード例 #20
0
ファイル: linux_udp.c プロジェクト: opendp/dpdk-ans
int main(int argc, char **argv)
{
    int i;
    int flags, ret;
    int epfd;
    int loop = 0;
    struct epoll_event event;
    struct sockaddr_in servaddr;
    pthread_t id;


    /* create epoll socket */
    epfd = epoll_create(10);
    if(epfd < 0)
    {
        printf("create epoll socket failed \n");
        return -1;
    }

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        printf("create udp socket failed \n");
        return -1;
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(9999);

    bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    flags = fcntl (sockfd, F_GETFL, 0);
    if (flags == -1)
    {
        printf("fcntl get udp socket flag failed \n");
        return -1;
    }

    flags |= O_NONBLOCK;
    ret = fcntl (sockfd, F_SETFL, flags);
    if (ret == -1)
    {
        printf("fcntl set udp socket flag failed \n");
        return -1;
    }

    event.data.fd = sockfd;
    event.events = EPOLLIN | EPOLLET;

    ret = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
    if(ret != 0)
    {
        printf("epoll ctl failed \n");
        return -1;
    }

    printf("start linux udp application \n");


    ret=pthread_create(&id, NULL, (void *) udp_send_thread, NULL);
    if(ret!=0)
    {
        printf ("Create pthread error!\n");
        return 0;
    }

    char recvline[RECV_MAX_SIZE];

    int event_num = 0;
    int recv_len = 0;

    while(1)
    {

        event_num = epoll_wait (epfd, events, 20, -1);
        for(i = 0; i < event_num; i++)
        {
            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
            {
                printf("socket(%d) error\n", events[i].data.fd);
                close (events[i].data.fd);
                continue;
            }

            if (events[i].events & EPOLLIN)
            {
                while(1)
                {
                    recv_len = recvfrom(events[i].data.fd, recvline, RECV_MAX_SIZE, 0, NULL, NULL);
                    if((recv_len <= 0) && (EAGAIN != errno))
                    {
                        printf("socke error, recv_len:%d, errno:%d \n", recv_len, errno);
                        break;
                    }
                    else if((recv_len <= 0) && (EAGAIN == errno))
                    {
                 //       printf("no data in socket, recv_len:%d, errno:%d \n", recv_len, errno);
                        break;
                    }

                    printf("Data len: %d Recv: %s \n", recv_len, recvline);
                }

            }
            else
            {
                printf("unknow event %x, fd:%d \n", events[i].events, events[i].data.fd);
            }

        }

    }


   close(sockfd);
   close(epfd);

}
コード例 #21
0
ファイル: gps_s3c.c プロジェクト: adeelmunir/x210_ics_rtm_v13
/* this is the main thread, it waits for commands from gps_state_start/stop and,
 * when started, messages from the QEMU GPS daemon. these are simple NMEA sentences
 * that must be parsed to be converted into GPS fixes sent to the framework
 */
static void
gps_state_thread( void*  arg )
{
    GpsState*   state = (GpsState*) arg;
    NmeaReader  reader[1];
    int         epoll_fd   = epoll_create(2);
    int         started    = 0;
    int         gps_fd     = state->fd;
    int         control_fd = state->control[1];

    nmea_reader_init( reader );

    // register control file descriptors for polling
    epoll_register( epoll_fd, control_fd );
    epoll_register( epoll_fd, gps_fd );

    D("gps thread running");

    // now loop
    for (;;) {
        struct epoll_event   events[2];
        int                  ne, nevents;

        nevents = epoll_wait( epoll_fd, events, 2, -1 );
        if (nevents < 0) {
            if (errno != EINTR)
                LOGE("epoll_wait() unexpected error: %s", strerror(errno));
            continue;
        }
        D("gps thread received %d events", nevents);
        for (ne = 0; ne < nevents; ne++) {
            if ((events[ne].events & (EPOLLERR|EPOLLHUP)) != 0) {
                LOGE("EPOLLERR or EPOLLHUP after epoll_wait() !?");
                return;
            }
            if ((events[ne].events & EPOLLIN) != 0) {
                int  fd = events[ne].data.fd;

                if (fd == control_fd)
                {
                    char  cmd = 255;
                    int   ret;
                    D("gps control fd event");
                    do {
                        ret = read( fd, &cmd, 1 );
                    } while (ret < 0 && errno == EINTR);

                    if (cmd == CMD_QUIT) {
                        D("gps thread quitting on demand");
                        return;
                    }
                    else if (cmd == CMD_START) {
                        if (!started) {
                            D("gps thread starting  location_cb=%p", state->callbacks.location_cb);
                            started = 1;
                            nmea_reader_set_callback( reader, state->callbacks.location_cb );
#if GPS_SV_INCLUDE
                            nmea_reader_set_sv_callback( reader, state->callbacks.sv_status_cb );
#endif
                        }
                    }
                    else if (cmd == CMD_STOP) {
                        if (started) {
                            D("gps thread stopping");
                            started = 0;
                            nmea_reader_set_callback( reader, NULL );
#if GPS_SV_INCLUDE
                            nmea_reader_set_sv_callback( reader, NULL );
#endif
                        }
                    }
                }
                else if (fd == gps_fd)
                {
                    char  buff[32];
                    D("gps fd event");
                    for (;;) {
                        int  nn, ret;

                        ret = read( fd, buff, sizeof(buff) );
                        if (ret < 0) {
                            if (errno == EINTR)
                                continue;
                            if (errno != EWOULDBLOCK)
                                LOGE("error while reading from gps daemon socket: %s:", strerror(errno));
                            break;
                        }
                        D("received %d bytes: %.*s", ret, ret, buff);
                        for (nn = 0; nn < ret; nn++)
                            nmea_reader_addc( reader, buff[nn] );
                    }
                    D("gps fd event end");
                }
                else
                {
                    LOGE("epoll_wait() returned unkown fd %d ?", fd);
                }
            }
        }
    }
}
コード例 #22
0
ファイル: HttpClient.c プロジェクト: asureshraja/C-codes
void *throw_requests(struct req_struct *req){
  char *host = req->host;
  int port = req->port;
  int connections=req->connections;
  int num_of_requests=req->num_of_requests;
  char *get; //get request to send;
  int sock[connections];
  int i=0;int flags=0;
  //epoll declarations
  int epfd;
  epfd=epoll_create(connections+1);
  struct epoll_event *events;
  events = malloc (sizeof (struct epoll_event)*connections);
  int ret;int nr_events;
  //end of epoll declarations
  //remote address block
  struct sockaddr_in *remote; //remote address structure
  remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *)); //allocating remote address
  remote->sin_family = AF_INET;
  inet_pton(AF_INET, get_ip(host), (void *)(&(remote->sin_addr.s_addr))); //setting ip
  remote->sin_port = htons(port); //setting port


  for (i = 0; i < connections; i++) {
    if((sock[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ //creating tcp socket
      perror("Can't create TCP socket");
      exit(1);
    }
    // flags = fcntl(sock[i], F_GETFL, 0);
    // fcntl(sock[i], F_SETFL, flags | O_NONBLOCK);
    int optval=1;
    socklen_t optlen = sizeof(optval);

    if(setsockopt(sock[i], SOL_SOCKET, SO_REUSEADDR|SO_REUSEPORT, &optval, optlen) < 0) {
       perror("setsockopt()");
       exit(EXIT_FAILURE);
    }
     if(setsockopt(sock[i], SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
        perror("setsockopt()");
        exit(EXIT_FAILURE);
     }
     events[i].data.fd = sock[i];
     events[i].events = EPOLLIN;
     ret = epoll_ctl (epfd, EPOLL_CTL_ADD , sock[i], &events[i]);
  }

  get = build_get_query(host, user_given_filename); //get request to send to remote
  char *tempget;int sent = 0; ret=0;
  tempget=get;int len=strlen(get);
  int progress=num_of_requests/10;



  gettimeofday(&rt_threads[req->threadID].tv1, NULL);
    rt_threads[req->threadID].thread_start =
         (rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000 ;



  for (i = 0; i < connections; i++) {
    if(connect(sock[i], (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){ //connecting to remote address and ataching to socket
      printf("%d\n",i );
       for (i = 0; i < connections; i++) {
          close(sock[i]);
        }
      exit(1);
    }

    //following block sends get string headers to remote address
    sent = 0;ret=0;
    while(sent < len)
    {
      ret = send(sock[i], get+sent, len-sent, 0);
      if(ret == -1){
        perror("Can't send query");
        exit(1);
      }
      sent += ret;
    }
    get=tempget;
    // end of send block
  }

  gettimeofday(&rt_threads[req->threadID].tv1, NULL);
  double tm=((rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000);
  rt_threads[req->threadID].thread_time=rt_threads[req->threadID].thread_time+(tm-rt_threads[req->threadID].thread_start);

  get=tempget;int tmpsock;
  char *buf;
  buf = (char *)malloc(sizeof(char)*1024);
  int j=0;  gettimeofday(&rt_threads[req->threadID].tv1, NULL);
    rt_threads[req->threadID].thread_start =
         (rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000 ; // convert tv_sec & tv_usec to mill
  for (; i < num_of_requests;i=i) {
    nr_events = epoll_wait (epfd, events,connections, -1);
      for (j=0;j < nr_events && i < num_of_requests; i++,j++) {
        // if(i%progress==0 && i!=0)printf("%d completed\n",i );

        recv(events[j].data.fd, buf, 1024*1024, 0);
          if(keep_alive==0)
         {
           ret = epoll_ctl (epfd, EPOLL_CTL_DEL,events[j].data.fd, &events[j]);
         }


          gettimeofday(&rt_threads[req->threadID].tv1, NULL);
          double tm=((rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000);
          rt_threads[req->threadID].thread_time=rt_threads[req->threadID].thread_time+(tm-rt_threads[req->threadID].thread_start);

        success++;
        if(success>temp){
          temp=temp+(user_given_requests/10);
          printf("%d finished\n",temp);
        }
         //printf("%d %s\n",success ,buf);
         if(keep_alive==0)
        {
            close(events[j].data.fd);


          ///////////////////////////
          if((tmpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ //creating tcp socket
            perror("Can't create TCP socket");
            exit(1);
          }
          // flags = fcntl(sock[i], F_GETFL, 0);
          // fcntl(sock[i], F_SETFL, flags | O_NONBLOCK);
          int optval=1;
          socklen_t optlen = sizeof(optval);

          if(setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR|SO_REUSEPORT|SO_KEEPALIVE, (char*)&optval, sizeof(optval)) < 0) {
             perror("setsockopt()");
             exit(EXIT_FAILURE);
          }

           events[j].data.fd = tmpsock;
           events[j].events = EPOLLIN;
           ret = epoll_ctl (epfd, EPOLL_CTL_ADD, tmpsock, &events[j]);
       }
        ///////////////////////////
        gettimeofday(&rt_threads[req->threadID].tv1, NULL);
        rt_threads[req->threadID].thread_start =
             (rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000 ; // convert tv_sec & tv_usec to mill
             if(keep_alive==0)
            {
              if(connect(events[j].data.fd, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){ //connecting to remote address and ataching to socket
                perror("Could not connect");
                exit(1);
              }
            }
      //  following block sends get string headers to remote address
        sent = 0;ret=0;
        while(sent < len)
        {
          ret = send(events[j].data.fd, get+sent, len-sent, 0);
          if(ret == -1){
            perror("Can't send query");
            exit(1);
          }
          sent += ret;
        }
        get=tempget;
        // end of send block
    }
  }
  // printf("finished %d\n", success);
  // printf("%d thread finished.\n",(int)pthread_self() );
  close(epfd);
  free(buf);
  free(events);
  free(host);
  free(remote);
}
コード例 #23
0
ファイル: epoll.c プロジェクト: Teraqz/one-nio
JNIEXPORT jint JNICALL
Java_one_nio_net_NativeSelector_epollCreate(JNIEnv* env, jclass cls) {
    return epoll_create(1024);
}
コード例 #24
0
ファイル: epoll.c プロジェクト: wudikua/learning
int epoll_init(int size){
    return epoll_create(size);
}
コード例 #25
0
ファイル: aioImpl.cpp プロジェクト: longxiaozhang/Algorithms
int main(int argc, char *argv[])
{
    int efd, fd, epfd;
    io_context_t ctx;
    struct timespec tms;
    struct io_event events[NUM_EVENTS];
    struct custom_iocb iocbs[NUM_EVENTS];
    struct iocb *iocbps[NUM_EVENTS];
    struct custom_iocb *iocbp;
    int i, j, r;
    void *buf;
    struct epoll_event epevent;

    efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
    if (efd == -1) {
        perror("eventfd");
        return 2;
    }

    fd = open(TEST_FILE, O_RDWR | O_CREAT | O_DIRECT, 0644);
    if (fd == -1) {
        perror("open");
        return 3;
    }
    ftruncate(fd, TEST_FILE_SIZE);
    
    ctx = 0;
    if (io_setup(8192, &ctx)) {
        perror("io_setup");
        return 4;
    }

    if (posix_memalign(&buf, ALIGN_SIZE, RD_WR_SIZE)) {
        perror("posix_memalign");
        return 5;
    }
    printf("buf: %p\n", buf);

    for (i = 0, iocbp = iocbs; i < NUM_EVENTS; ++i, ++iocbp) {
        iocbps[i] = &iocbp->iocb;
        io_prep_pread(&iocbp->iocb, fd, buf, RD_WR_SIZE, i * RD_WR_SIZE);
        io_set_eventfd(&iocbp->iocb, efd);
        io_set_callback(&iocbp->iocb, aio_callback);
        iocbp->nth_request = i + 1;
    }

    if (io_submit(ctx, NUM_EVENTS, iocbps) != NUM_EVENTS) {
        perror("io_submit");
        return 6;
    }

    epfd = epoll_create(1);
    if (epfd == -1) {
        perror("epoll_create");
        return 7;
    }

    epevent.events = EPOLLIN | EPOLLET;
    epevent.data.ptr = NULL;
    if (epoll_ctl(epfd, EPOLL_CTL_ADD, efd, &epevent)) {
        perror("epoll_ctl");
        return 8;
    }

    i = 0;
    while (i < NUM_EVENTS) {
        uint64_t finished_aio;

        if (epoll_wait(epfd, &epevent, 1, -1) != 1) {
            perror("epoll_wait");
            return 9;
        }

        if (read(efd, &finished_aio, sizeof(finished_aio)) != sizeof(finished_aio)) {
            perror("read");
            return 10;
        }

        printf("finished io number: %"PRIu64"\n", finished_aio);
    
        while (finished_aio > 0) {
            tms.tv_sec = 0;
            tms.tv_nsec = 0;
            r = io_getevents(ctx, 1, NUM_EVENTS, events, &tms);
            if (r > 0) {
                for (j = 0; j < r; ++j) {
                    ((io_callback_t)(events[j].data))(ctx, events[j].obj, events[j].res, events[j].res2);
                }
                i += r;
                finished_aio -= r;
            }
        }
    }
    
    close(epfd);
    free(buf);
    io_destroy(ctx);
    close(fd);
    close(efd);
    remove(TEST_FILE);

    return 0;
}
コード例 #26
0
ファイル: epoll.c プロジェクト: ATCP/mtcp
static apr_status_t impl_pollset_create(apr_pollset_t *pollset,
                                        apr_uint32_t size,
                                        apr_pool_t *p,
                                        apr_uint32_t flags)
{
    apr_status_t rv;
    int fd;
#ifdef HAVE_MTCP
	int cpu = sched_getcpu();
#endif

#ifdef HAVE_EPOLL_CREATE1
#ifdef HAVE_MTCP
	fd = mtcp_epoll_create(g_mctx[cpu], size);
#else
	fd = epoll_create1(EPOLL_CLOEXEC);
#endif
#else
#ifdef HAVE_MTCP
	fd = mtcp_epoll_create(g_mctx[cpu], size);
#else
	fd = epoll_create(size);
#endif
#endif
    if (fd < 0) {
        pollset->p = NULL;
        return apr_get_netos_error();
    }

#ifndef HAVE_EPOLL_CREATE1
    {
        int flags;

        if ((flags = fcntl(fd, F_GETFD)) == -1)
            return errno;

        flags |= FD_CLOEXEC;
        if (fcntl(fd, F_SETFD, flags) == -1)
            return errno;
    }
#endif

    pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t));
#if APR_HAS_THREADS
    if ((flags & APR_POLLSET_THREADSAFE) &&
        !(flags & APR_POLLSET_NOCOPY) &&
        ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
                                       APR_THREAD_MUTEX_DEFAULT,
                                       p)) != APR_SUCCESS)) {
        pollset->p = NULL;
        return rv;
    }
#else
    if (flags & APR_POLLSET_THREADSAFE) {
        pollset->p = NULL;
        return APR_ENOTIMPL;
    }
#endif
    pollset->p->epoll_fd = fd;

#ifdef HAVE_MTCP
	pollset->p->pollset = apr_palloc(p, size * sizeof(struct mtcp_epoll_event));
#else
    pollset->p->pollset = apr_palloc(p, size * sizeof(struct epoll_event));
#endif

	pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));

    if (!(flags & APR_POLLSET_NOCOPY)) {
        APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
        APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
        APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
    }
    return APR_SUCCESS;
}
コード例 #27
0
ファイル: ep_server.c プロジェクト: ThomasLau/nginxsrp
/* 主程序 */
int main()
{
    int srvfd, cnnfd;
    int epfd, nfds;
    char buf[ECHO_MAX_IN];
    
    struct sockaddr_in srvaddr;
    struct sockaddr_in cnnaddr;
    struct epoll_event ev,events[ECHO_LITSTEN_QUEUE];
    socklen_t clilen = sizeof cnnaddr;
    
    srvfd = socket(AF_INET, SOCK_STREAM, 0);

    if ( -1 == srvfd) {
        perror("socket error\n");
        exit(1);
    }
    
    if (nonblocking(srvfd) == - 1) {
        perror("nonblocking srvfd error\n");
        exit(1);
    }

    memset(&srvaddr, 0, sizeof(struct sockaddr_in));
    srvaddr.sin_family  = AF_INET;
    srvaddr.sin_port    = htons(ECHO_PORT);
    srvaddr.sin_addr.s_addr = INADDR_ANY;

    if (bind(srvfd, (struct sockaddr*) &srvaddr, sizeof (struct sockaddr)) == -1){
       perror("bind error\n"); 
       exit(1);
    }

    if (listen(srvfd, ECHO_LITSTEN_QUEUE) == -1){
        perror("listen error\n");
        exit(1);
    }

    memset(&ev, 0, sizeof(struct epoll_event));
    epfd = epoll_create ( ECHO_LITSTEN_QUEUE );
    ev.data.fd = srvfd;
    ev.events  = EPOLLIN|EPOLLET;
    epoll_ctl(epfd, EPOLL_CTL_ADD, srvfd, &ev);

    // 主循环 
    for(;;) {
        int i;
        int n = 0; 
        nfds = epoll_wait(epfd, events, ECHO_LITSTEN_QUEUE, 500);
        for(i = 0; i < nfds; i++){
            if (events[i].data.fd == srvfd) {  /* 监听到有新的客户端连接进入 */ 
                if ((cnnfd = accept(srvfd, (struct sockaddr*) &cnnaddr, &clilen))<0) {
                    perror("cnnfd accept error\n");
                    exit(1);
                }
                nonblocking(cnnfd);
                // printf ("get connection from %s; cnnfd is %d\n", inet_ntoa(cnnaddr.sin_addr), cnnfd);
                ev.data.fd = cnnfd;
                ev.events = EPOLLIN|EPOLLET;
                epoll_ctl(epfd, EPOLL_CTL_ADD, cnnfd, &ev);
            } else if (events[i].events & EPOLLIN) { /* 粗略的认为不是主监听请求的EPOLLIN请求就是用户数据输入*/
                if ((cnnfd = events[i].data.fd) < 0 ) continue;
                memset(buf, 0, sizeof(buf));
                if (n = read(cnnfd, buf, ECHO_MAX_IN)) {
                    if ( errno == ECONNRESET ) {
                        close(cnnfd);
                        events[i].data.fd = -1;
                    } else if ( n == 0)  {
                        close(cnnfd);
                        events[i].data.fd = -1;
                    }
                    else {
                        //printf ("buf is [%s], strlen buf is [%d]\n", buf, strlen(buf));
                    }
                    write(cnnfd, buf, n);
                    // 按Q退出程序  q ENTER
                    if ( buf[0] == 'q' && (buf[1] == 10 || buf[1] == 13) ) {
                        epoll_ctl( epfd, EPOLL_CTL_DEL, cnnfd, &ev);
                        close(cnnfd);
                        goto quit;
                    }
                    /*
                    // 将客户端连接的fd设置为写操作的fd 
                    ev.data.fd = cnnfd;
                    ev.events  = EPOLLOUT|EPOLLET;
                    epoll_ctl(epfd, EPOLL_CTL_MOD, cnnfd, &ev);
                } else if (events[i].events & EPOLLOUT) {
                    cnnfd = events[i].data.fd;
                    write (cnnfd, buf, n);
                    //ev.data.fd =cnnfd;
                    // ev.events = EPOLLIN|EPOLLET;
                    //epoll_ctl(epfd, EPOLL_CTL_MOD, cnnfd, &ev);
                    */
                }

            }
            
        }
    }

    quit: 
        close( srvfd );
    return 0;
}
コード例 #28
0
ファイル: GPI2_SN.c プロジェクト: tvandera/GPI-2
void *gaspi_sn_backend(void *arg)
{
  int esock, lsock, n, i;
  struct epoll_event ev;
  struct epoll_event *ret_ev;
  gaspi_mgmt_header *ev_mgmt, *mgmt;

  signal(SIGSTKFLT, gaspi_sn_cleanup);
  signal(SIGPIPE, SIG_IGN);

  while(gaspi_master_topo_data == 0)
    gaspi_delay();

  lsock = socket(AF_INET, SOCK_STREAM, 0);
  if(lsock < 0)
    {
      gaspi_print_error("Failed to create socket");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      return NULL;
    }

  if( 0 != gaspi_sn_set_default_opts(lsock) )
    {
      gaspi_print_error("Failed to modify socket");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      close(lsock);
      return NULL;
    }

  signal(SIGPIPE, SIG_IGN);

  struct sockaddr_in listeningAddress;
  listeningAddress.sin_family = AF_INET;
  listeningAddress.sin_port = htons((glb_gaspi_cfg.sn_port + glb_gaspi_ctx.localSocket));
  listeningAddress.sin_addr.s_addr = htonl(INADDR_ANY);

  if(bind(lsock, (struct sockaddr*)(&listeningAddress), sizeof(listeningAddress)) < 0)
    {
      gaspi_print_error("Failed to bind socket (port %d)",
			glb_gaspi_cfg.sn_port + glb_gaspi_ctx.localSocket);

      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERR_SN_PORT;
      close(lsock);
      return NULL;
    }

  if ( 0 != gaspi_sn_set_non_blocking(lsock) )
    {
      gaspi_print_error("Failed to set socket");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      close(lsock);
      return NULL;
    }

  if(listen(lsock, SOMAXCONN) < 0)
    {
      gaspi_print_error("Failed to listen on socket");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      close(lsock);
      return NULL;
    }

  esock = epoll_create(GASPI_EPOLL_CREATE);
  if(esock < 0)
    {
      gaspi_print_error("Failed to create IO event facility");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      close(lsock);
      return NULL;
    }

  /* add lsock to epoll instance */
  ev.data.ptr = malloc( sizeof(gaspi_mgmt_header) );
  if(ev.data.ptr == NULL)
    {
      gaspi_print_error("Failed to allocate memory");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      close(lsock);
      return NULL;
    }

  ev_mgmt = ev.data.ptr;
  ev_mgmt->fd = lsock;
  ev.events = EPOLLIN;

  if(epoll_ctl(esock, EPOLL_CTL_ADD, lsock, &ev) < 0)
    {
      gaspi_print_error("Failed to modify IO event facility");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      close(lsock);
      return NULL;
    }

  ret_ev = calloc(GASPI_EPOLL_MAX_EVENTS, sizeof(ev));
  if(ret_ev == NULL)
    {
      gaspi_print_error("Failed to allocate memory");
      gaspi_sn_status = GASPI_SN_STATE_ERROR;
      gaspi_sn_err = GASPI_ERROR;
      close(lsock);
      return NULL;
    }

  /* main events loop */
  while(1)
    {
      n = epoll_wait(esock,ret_ev, GASPI_EPOLL_MAX_EVENTS, -1);

      /* loop over all triggered events */
      for( i = 0; i < n; i++ )
	{
	  mgmt = ret_ev[i].data.ptr;

	  if( (ret_ev[i].events & EPOLLERR)  || (ret_ev[i].events & EPOLLHUP)  ||
	      !((ret_ev[i].events & EPOLLIN) || (ret_ev[i].events & EPOLLOUT )) )
	    {
	      /* an error has occured on this fd. close it => removed from event list. */
	      gaspi_print_error( "Erroneous event." );
	      shutdown(mgmt->fd, SHUT_RDWR);
	      close(mgmt->fd);
	      free(mgmt);
	      continue;
	    }
	  else if(mgmt->fd == lsock)
	    {
	      /* process all new connections */
	      struct sockaddr in_addr;
	      socklen_t in_len = sizeof(in_addr);
	      int nsock = accept( lsock, &in_addr, &in_len );

	      if(nsock < 0)
		{
		  if( (errno == EAGAIN) || (errno == EWOULDBLOCK) )
		    {
		      /* we have processed incoming connection */
		      break;
		    }
		  else
		    {
		      /* at least check/fix open files limit */
		      int errsv = errno;
		      if(errsv == EMFILE)
			{
			  if( 0 == _gaspi_check_ofile_limit() )
			    {
			      nsock = accept( lsock, &in_addr, &in_len );
			    }
			}

		      /* still erroneous? => makes no sense to continue */
		      if(nsock < 0)
			{
			  gaspi_print_error( "Failed to accept connection." );
			  gaspi_sn_status = GASPI_SN_STATE_ERROR;
			  gaspi_sn_err = GASPI_ERROR;
			  close(lsock);
			  return NULL;
			}
		    }
		}

	      /* new socket */
	      if( 0 != gaspi_sn_set_non_blocking( nsock ) )
		{
		  gaspi_print_error( "Failed to set socket options." );
		  gaspi_sn_status = GASPI_SN_STATE_ERROR;
		  gaspi_sn_err = GASPI_ERROR;
		  close(nsock);
		  return NULL;
		}

	      /* add nsock */
	      ev.data.ptr = malloc( sizeof(gaspi_mgmt_header) );
	      if(ev.data.ptr == NULL)
		{
		  gaspi_print_error("Failed to allocate memory.");
		  gaspi_sn_status = GASPI_SN_STATE_ERROR;
		  gaspi_sn_err = GASPI_ERROR;
		  close(nsock);
		  return NULL;
		}

	      ev_mgmt = ev.data.ptr;
	      ev_mgmt->fd = nsock;
	      ev_mgmt->blen = sizeof(gaspi_cd_header);
	      ev_mgmt->bdone = 0;
	      ev_mgmt->op = GASPI_SN_HEADER;
	      ev.events = EPOLLIN ; /* read only */

	      if(epoll_ctl( esock, EPOLL_CTL_ADD, nsock, &ev ) < 0)
		{
		  gaspi_print_error("Failed to modify IO event facility");
		  gaspi_sn_status = GASPI_SN_STATE_ERROR;
		  gaspi_sn_err = GASPI_ERROR;
		  close(nsock);
		  return NULL;
		}

	      continue;
	    }/* if new connection(s) */
	  else
	    {
	      /* read or write ops */
	      int io_err = 0;

	      if( ret_ev[i].events & EPOLLIN )
		{
		  while( 1 )
		    {
		      int rcount = 0;
		      int rsize = mgmt->blen - mgmt->bdone;
		      char *ptr = NULL;

		      if( mgmt->op == GASPI_SN_HEADER )
			{
			  /* TODO: is it valid? */
			  ptr = (char *) &mgmt->cdh;
			  rcount = read( mgmt->fd, ptr + mgmt->bdone, rsize );
			}
		      else if( mgmt->op == GASPI_SN_CONNECT )
			{
			  while( !glb_gaspi_dev_init )
			    gaspi_delay();

			  ptr = pgaspi_dev_get_rrcd(mgmt->cdh.rank);
			  rcount = read( mgmt->fd, ptr + mgmt->bdone, rsize );
			}

		      /* errno==EAGAIN => we have read all data */
		      int errsv = errno;
		      if(rcount < 0)
			{
			  if (errsv == ECONNRESET || errsv == ENOTCONN)
			    {
			      gaspi_print_error(" Failed to read (op %d)", mgmt->op);
			    }

			  if(errsv != EAGAIN || errsv != EWOULDBLOCK)
			    {
			      gaspi_print_error(" Failed to read (op %d).", mgmt->op);
			      io_err = 1;
			    }
			  break;
			}
		      else if(rcount == 0) /* the remote side has closed the connection */
			{
			  io_err = 1;
			  break;
			}
		      else
			{
			  mgmt->bdone += rcount;

			  /* read all data? */
			  if(mgmt->bdone == mgmt->blen)
			    {
			      /* we got header, what do we have to do ? */
			      if(mgmt->op == GASPI_SN_HEADER)
				{
				  if(mgmt->cdh.op == GASPI_SN_PROC_KILL)
				    {
				      _exit(-1);
				    }
				  else if(mgmt->cdh.op == GASPI_SN_CONNECT)
				    {
				      GASPI_SN_RESET_EVENT( mgmt, mgmt->cdh.op_len, mgmt->cdh.op );
				    }
				  else if(mgmt->cdh.op == GASPI_SN_PROC_PING)
				    {
				      GASPI_SN_RESET_EVENT( mgmt, sizeof(gaspi_cd_header), GASPI_SN_HEADER );
				    }
				  else if(mgmt->cdh.op == GASPI_SN_GRP_CHECK)
				    {
				      struct{gaspi_group_t group;int tnc, cs, ret;} gb;
				      memset(&gb, 0, sizeof(gb));

				      gb.ret = -1;
				      gb.cs = 0;

				      const int group = mgmt->cdh.rank;
				      const int tnc = mgmt->cdh.tnc;

				      lock_gaspi_tout (&glb_gaspi_group_ctx[group].del, GASPI_BLOCK);
				      if(glb_gaspi_group_ctx[group].id >= 0)
					{
					  if(glb_gaspi_group_ctx[group].tnc == tnc)
					    {
					      int i;
					      gb.ret = 0;
					      gb.tnc = tnc;

					      for(i = 0; i < tnc; i++)
						{
						  if( NULL != glb_gaspi_group_ctx[group].rank_grp )
						    gb.cs ^= glb_gaspi_group_ctx[group].rank_grp[i];
						}
					    }
					}
				      unlock_gaspi (&glb_gaspi_group_ctx[group].del);

				      if(gaspi_sn_writen( mgmt->fd, &gb, sizeof(gb) ) < sizeof(gb) )
					{
					  gaspi_print_error("Failed response to group check.");
					  io_err = 1;
					  break;
					}

				      GASPI_SN_RESET_EVENT(mgmt, sizeof(gaspi_cd_header), GASPI_SN_HEADER );
				    }
				  else if(mgmt->cdh.op == GASPI_SN_GRP_CONNECT)
				    {
				      while( !glb_gaspi_dev_init ||
					     ( glb_gaspi_group_ctx[mgmt->cdh.ret].id == -1) )
					gaspi_delay();

				      /* TODO: check the pointer */
				      if(gaspi_sn_writen( mgmt->fd,
							  &glb_gaspi_group_ctx[mgmt->cdh.ret].rrcd[glb_gaspi_ctx.rank],
							  sizeof(gaspi_rc_mseg) ) < sizeof(gaspi_rc_mseg) )
					{
					  gaspi_print_error("Failed to connect group.");
					  io_err = 1;
					  break;
					}

				      GASPI_SN_RESET_EVENT( mgmt, sizeof(gaspi_cd_header), GASPI_SN_HEADER );
				    }
				  else if(mgmt->cdh.op == GASPI_SN_SEG_REGISTER)
				    {
				      int rret = gaspi_sn_segment_register(mgmt->cdh);

				      /* write back result of registration */
				      if(gaspi_sn_writen( mgmt->fd, &rret, sizeof(int) ) < sizeof(int) )
					{
					  gaspi_print_error("Failed response to segment register.");
					  io_err = 1;
					  break;
					}

				      GASPI_SN_RESET_EVENT(mgmt, sizeof(gaspi_cd_header), GASPI_SN_HEADER );
				    }
				}/* !header */
			      else if(mgmt->op == GASPI_SN_CONNECT)
				{
				  /* TODO: to remove */
				  while( !glb_gaspi_dev_init )
				    gaspi_delay();

				  const size_t len = pgaspi_dev_get_sizeof_rc();
				  char *ptr = NULL;

				  gaspi_return_t eret = pgaspi_create_endpoint_to(mgmt->cdh.rank, GASPI_BLOCK);
				  if( eret == GASPI_SUCCESS )
				    {
				      eret = pgaspi_connect_endpoint_to(mgmt->cdh.rank, GASPI_BLOCK);
				      if( eret == GASPI_SUCCESS)
					{
					  ptr = pgaspi_dev_get_lrcd(mgmt->cdh.rank);
					}
				    }

				  if( eret != GASPI_SUCCESS )
				    {
				      /* We set io_err, connection is closed and remote peer reads EOF */
				      io_err = 1;
				    }
				  else
				    {
				      if( NULL != ptr )
					{
					  if( gaspi_sn_writen( mgmt->fd, ptr, len ) < sizeof(len) )
					    {
					      gaspi_print_error("Failed response to connection request from %u.", mgmt->cdh.rank);
					      io_err = 1;
					    }
					}
				    }

				  GASPI_SN_RESET_EVENT( mgmt, sizeof(gaspi_cd_header), GASPI_SN_HEADER );
				}
			      else
				{
				  gaspi_print_error("Received unknown SN operation");
				  GASPI_SN_RESET_EVENT( mgmt, sizeof(gaspi_cd_header), GASPI_SN_HEADER );
				}

			      break;
			    } /* if all data */
			}/* else */
		    }/* while(1) read */
		}/* read in */

	      if( io_err )
		{
		  shutdown(mgmt->fd, SHUT_RDWR);
		  close(mgmt->fd);
		  free(mgmt);
		}
	    }
	} /* for each event */
    }/* event loop while(1) */

  return NULL;
}
コード例 #29
0
ファイル: cli.c プロジェクト: upa/lix
void wait_telnet() {
    int listener, epfd;
    struct epoll_event ev;
    struct epoll_event events[MAX_EVENTS];

    listener = create_listener();

    if((epfd = epoll_create(MAX_EVENTS)) < 0) {
        err(EXIT_FAILURE, "epoll_create");
    }

    memset(&ev, 0, sizeof ev);
    ev.events = EPOLLIN;
    ev.data.fd = listener;
    epoll_ctl(epfd, EPOLL_CTL_ADD, listener, &ev);

    while (1) {
        int i;
        int nfd = epoll_wait(epfd, events, MAX_EVENTS, -1);

        for (i = 0; i < nfd; i++) {

            if (events[i].data.fd == listener) {
                struct sockaddr_in client_addr;
                socklen_t client_addr_len = sizeof(client_addr);

                int client = accept(listener, (struct sockaddr *)&client_addr, &client_addr_len);
                if (client < 0) {
                    err(EXIT_FAILURE, "accept");
                }

                setnonblocking(client);

                memset(&ev, 0, sizeof ev);
                ev.events = EPOLLIN | EPOLLET;
                ev.data.fd = client;
                epoll_ctl(epfd, EPOLL_CTL_ADD, client, &ev);

                write(client, "> ", 2);
            } else {
                char buffer[1024];

                int client = events[i].data.fd;
                memset(buffer, 0, sizeof(buffer));
                int n = read(client, buffer, sizeof(buffer));
                if (n < 0) {
                    epoll_ctl(epfd, EPOLL_CTL_DEL, client, &ev);
                    close(client);
                    warn("read");
                } else if (n == 0) {
                    epoll_ctl(epfd, EPOLL_CTL_DEL, client, &ev);
                    close(client);
                } else {
                    if(switch_reaction(client, buffer) < 0) {
                        shutdown(client, SHUT_RDWR);
                        epoll_ctl(epfd, EPOLL_CTL_DEL, client, &ev);
                        close(client);
                    }
                }
            }
        }
    }
}
コード例 #30
0
ファイル: EventLoop.cpp プロジェクト: nyankosama/courses
EventLoopMgr::EventLoopMgr() {
    events_ = new epoll_event[MAX_EVENTS];
    efd_ = epoll_create(1);
}