Пример #1
0
int main(int argc, char *argv[])
{
	assert(argc == 4);
	int epoll_fd = epoll_create(100);
	start_conn(epoll_fd, atoi(argv[3]), argv[1], atoi(argv[2]));
	epoll_event events[10000];
	char buffer[2048];
	while(1) {
		int fds = epoll_wait(epoll_fd, events, 10000, 2000);
		for(int i = 0; i < fds; i ++) {
			int sockfd = events[i].data.fd;
			if(events[i].events & EPOLLIN) {
				if(!read_once(sockfd, buffer, 2048)) {
					close_conn(epoll_fd, sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLOUT | EPOLLET | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);
			} else if(events[i].events & EPOLLOUT) {
				if(!write_nbytes(sockfd, request, strlen(request))) {
					close_conn(epoll_fd, sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLIN |EPOLLET | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);
			} else if(events[i].events & EPOLLERR) {
				close_conn(epoll_fd, sockfd);
			}
		}
	}
}
Пример #2
0
int main(int argc, char* argv[])
{
	signal(SIGPIPE,SIG_IGN);
	signal_exit_handler();

	int background = 0;
	if(background)
	{
		daemonize();
	}
	
	assert(argc == 5);
	int maxclients = atoi(argv[3])  + CONFIG_FDSET_INCR;
	adjustOpenFilesLimit(maxclients);

	int epoll_fd = epoll_create(1024);
	start_conn(epoll_fd,atoi(argv[3]), argv[1],atoi(argv[2]),atoi(argv[4]));
	epoll_event *events = (epoll_event*)malloc(sizeof(struct epoll_event) * (maxclients) );

	char buffer[2048];
	while(!stop)
	{
		int fds = epoll_wait(epoll_fd,events,maxclients,2000);
		for(int i = 0; i < fds; i++)
		{
			int sockfd = events[i].data.fd;
			if(events[i].events & EPOLLIN)
			{
				if(!read_once(sockfd, buffer,2048))
				{
					close_conn(epoll_fd,sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLOUT | EPOLLET | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event);
			}
			else if(events[i].events & EPOLLOUT)
			{
				if(!write_nbytes(sockfd,request ,strlen(request)))
				{
					close_conn(epoll_fd,sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLIN | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event);
			}
			else if(events[i].events & EPOLLERR)
			{
				close_conn(epoll_fd,sockfd);
			}
		}
	}

	close(epoll_fd);
	if(events)
	{
		free(events);
	}
	printf("exit!\n");
}