epoll+lua単純ゲームサーバ(二)

1083 ワード


/**       */
long now()
{
	struct timeval t;
	gettimeofday(&t, NULL);
	return t.tv_sec * 1000 + t.tv_usec / 1000;
}

/**           epoll  , common:        (     sock)*/
void add_fd(int fd, boolean common)
{
	if(common)
	{
		int idx;
                //       (NULL)
		for(idx = 0; idx < MAX_CLIENT && clients[idx] != NULL; idx++);
		if(idx == MAX_CLIENT)
		{
			fprintf(stderr, "max client
"); close(fd); return; } client_data *client = malloc(sizeof(client_data)); bzero(client, sizeof(client_data)); client->fd = fd; client->last = now(); // clients[fds[fd] = idx] = client; // } struct epoll_event e; e.data.fd = fd; e.events = EPOLLIN | EPOLLET; fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &e); }