예제 #1
0
파일: im-srv.c 프로젝트: Tetralet/hime
static gboolean cb_read_hime_client_data(GIOChannel *source, GIOCondition condition, gpointer data)
{
    int fd=GPOINTER_TO_INT(data);

    process_client_req(fd);
    return TRUE;
}
예제 #2
0
/**
 * read_from_client
 *
 * Handles the activities that take place on the sockets monitored by
 * select. The activity includes new connections or data transfer from
 * existing connections.
 *
 * @param s Server struct, contains information about the server.
 */
void read_from_socket(server *s) {
	//int ret, maxi;
	client *c = NULL;
	node *n = NULL;

	//maxi = 0;

	/* Check if a client is trying to connect */
	pthread_mutex_trylock(&s->dataLock);
	if (FD_ISSET(s->listen_sd, &s->allset)) {

		/* get the client data ready */
		c = client_new();

		/* blocking call waiting for connections */
		c->fd = accept(s->listen_sd, (struct sockaddr *) &c->sa, &c->sa_len);
		/* make the client Socket non-blocking */
		if (fcntl(c->fd, F_SETFL, O_NONBLOCK | fcntl(c->fd, F_GETFL, 0)) == -1)
			SystemFatal("fcntl(): Client Non-Block Failed\n");

		fprintf(stdout, "Received connection from (%s, %d)\n",
			inet_ntoa(c->sa.sin_addr),
			ntohs(c->sa.sin_port));
		s->n_clients++;
		s->n_max_connected++;
		/*s->n_max_connected = (s->n_clients > s->n_max_connected) ?
				s->n_clients : s->n_max_connected;*/
		s->client_list = llist_append(s->client_list, (void *)c);
		fprintf(stdout, "Added client to list, new size: %d\n",
			llist_length(s->client_list));
		/* add the client to the list */
		/*for (i = 0; i < FD_SETSIZE; i++) {
			if (s->clientConn[i] == NULL) {
			s->clientConn[i] = c;
			//break;
			}
			if (s->clients[i] < 0) {
			s->clients[i] = c->fd;
			break;
			}
			if (i == FD_SETSIZE) {
			SystemFatal("To Many Clients\n");
			}
			}*/
		/* add the socket connections to the fd_set */
		/*FD_SET(c->fd, &s->allset);
		s->maxfd = (c->fd > s->maxfd) ?
		c->fd : s->maxfd;
		maxi = (i > maxi) ? i : maxi;*/
		c = NULL;
	}
	pthread_mutex_unlock(&s->dataLock);

	for (n = s->client_list->link; n != NULL; n = n->next) {

		/* check if the client cause an event */
		pthread_mutex_trylock(&s->dataLock);
		c = (client *)n->data;
		if (FD_ISSET(c->fd, &s->allset)) {
			process_client_req(c, s);
			//process_client_data(c, s);
		}
		pthread_mutex_unlock(&s->dataLock);

		//pthread_mutex_trylock(&s->dataLock);  TRY THIS LATER
		if (c->quit) {
			pthread_mutex_trylock(&s->dataLock);
			s->n_clients--;
			s->client_list = llist_remove(s->client_list, (void *)c, client_compare);
			fprintf(stderr, "[%5d]Removed client from list, new size: %d\n",
				c->fd, llist_length(s->client_list));
			close(c->fd);
			free(c);
			c = NULL;
			pthread_mutex_unlock(&s->dataLock);
		}
		//pthread_mutex_unlock(&s->dataLock);  TRY THIS LATER
	}

	/* go through the available connections */
	/*for(n = 0; n <= maxi; n++) {
		pthread_mutex_trylock(&s->dataLock);
		if(FD_ISSET(s->clients[n], &s->allset)){
		process_client_data();
		}
		pthread_mutex_unlock(&s->dataLock);
		}*/
	//free(c);
}