Ejemplo n.º 1
0
void SelectReactorImpl::handle_events(TimeVal* timeout)
{
	int ret = 0;
	int minfd = 0;
	int maxfd = 0;
	EventHandler* handler = NULL;
	EventType event;

	demux_table_->convert(minfd, maxfd, readfds_, writefds_, exceptfds_);
	//printf("min: %d, max: %d\n", minfd, maxfd);
	ret = select(maxfd + 1, readfds_, writefds_, exceptfds_, NULL);	   
	if (ret > 0) //event
	{
		for (int fd = minfd; fd <= maxfd; ++fd) //遍历查找
		{
			if (demux_table_->get(fd, handler, event)) //被移除
			{
				continue;
			}

			//printf("fd: %d, event: %d\n", fd, event);
			if ((event & READ_EVENT) && FD_ISSET(fd, readfds_))
			{
				//printf("fd: %d, read event\n", fd);
				handler->handle_input(fd);
			}
			if ((event & WRITE_EVENT) && FD_ISSET(fd, writefds_))
			{
				//printf("fd: %d, write event\n", fd);
				handler->handle_output(fd);
			}
			if ((event & CLOSE_EVENT) && FD_ISSET(fd, exceptfds_))
			{
				printf("fd: %d, except event\n", fd);
				handler->handle_close(fd);
			}
		}
	}
	else if (ret == 0) //timeout
	{
	}
	else //error
	{
	}
}