示例#1
0
int32_t kn_epoll_loop(kn_proactor_t p,int32_t ms)
{
	uint64_t    sleep_ms;
	uint64_t    timeout = kn_systemms64() + (uint64_t)ms;
	uint64_t    current_tick;
	int32_t     nfds;
	int32_t     i;
	uint64_t    l_now;
	kn_dlist*   actived;
	kn_fd_t     s;
	kn_epoll*   ep = (kn_epoll*)p;
	if(ms < 0) ms = 0;
	do{
		if(!kn_dlist_empty(&p->connecting)){
			l_now = kn_systemms64();
            kn_dlist_check_remove(&p->connecting,check_connect_timeout,(void*)&l_now);
		}
		actived = kn_proactor_activelist(p);	
        if(!kn_dlist_empty(actived)){
            p->actived_index = (p->actived_index+1)%2;
            while((s = (kn_fd_t)kn_dlist_pop(actived)) != NULL){
                if(s->process(s))
                    kn_procator_putin_active(p,s);
            }
        }
       	current_tick = kn_systemms64();
       	actived = kn_proactor_activelist(p);
        if(kn_dlist_empty(actived))
			sleep_ms = timeout > current_tick ? timeout - current_tick:0;
		else
			sleep_ms = 0;
        nfds = _epoll_wait(ep->epfd,ep->events,ep->eventsize,(uint32_t)sleep_ms);
		if(nfds < 0)
			return -1;
		
		for(i=0; i < nfds ; ++i)
		{
			s = (kn_fd_t)ep->events[i].data.ptr;
			s->on_active(s,ep->events[i].events);
		}
		current_tick = kn_systemms64();
	}while(timeout > current_tick);
	return 0;
}
示例#2
0
文件: epoll.c 项目: Stan1990/KendyNet
int32_t epoll_loop(poller_t n,int32_t ms)
{
	assert(n);
	if(ms < 0)ms = 0;
	uint64_t sleep_ms;
	uint64_t timeout = GetSystemMs64() + (uint64_t)ms;
	uint64_t current_tick;
	uint32_t read_event = EV_IN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
	int32_t notify = 0;
	do{

        if(!dlist_empty(&n->connecting))
	    {
	        //check timeout connecting
	        uint64_t l_now = GetSystemMs64();
            dlist_check_remove(&n->connecting,check_connect_timeout,(void*)&l_now);
	    }
        if(!is_active_empty(n))
        {
            struct dlist *actived = get_active_list(n);
            n->actived_index = (n->actived_index+1)%2;
            socket_t s;
            while((s = (socket_t)dlist_pop(actived)) != NULL)
            {
                if(Process(s))
                    putin_active(n,(struct dnode*)s);
            }
        }
		current_tick = GetSystemMs64();
        if(is_active_empty(n))
			sleep_ms = timeout > current_tick ? timeout - current_tick:0;
		else
			sleep_ms = 0;
		notify = 0;
        int32_t nfds = _epoll_wait(n->poller_fd,n->events,MAX_SOCKET,(uint32_t)sleep_ms);
		if(nfds < 0)
			return -1;
		int32_t i;
		for(i = 0 ; i < nfds ; ++i)
		{
			if(n->events[i].data.fd == n->pipe_reader)
			{
				char buf[1];
				read(n->pipe_reader,buf,1);
				notify = 1;
			}else{
				socket_t sock = (socket_t)n->events[i].data.ptr;
				if(sock)
				{
					if(sock->socket_type == CONNECT){
						process_connect(sock);
					}
					else if(sock->socket_type == LISTEN){
						process_accept(sock);
					}
					else{
						if(n->events[i].events & read_event)
							on_read_active(sock);
						if(n->events[i].events & EPOLLOUT)
							on_write_active(sock);
					}
				}
			}
		}
		current_tick = GetSystemMs64();
	}while(notify == 0 && timeout > current_tick);
	return 0;
}