コード例 #1
0
ファイル: onion.c プロジェクト: arunsirrpi/onion
/**
 * @short Internal accept of just one request. 
 * 
 * It might be called straight from listen, or from the epoller.
 * 
 * @returns 0 if ok, <0 if error.
 */
static int onion_accept_request(onion *o){
  struct sockaddr_storage cli_addr;
  socklen_t cli_len;
  int clientfd=onion_accept(o, &cli_addr, &cli_len);
  
  if (clientfd<0)
    return -1;
  
	if (o->flags&O_POLL){
		onion_request *req=onion_connection_start(o, clientfd, &cli_addr, cli_len);
		if (!req){
      shutdown(clientfd,SHUT_RDWR); // Socket must be destroyed.
      close(clientfd);
			return 0;
		}
		onion_poller_slot *slot=onion_poller_slot_new(clientfd, (void*)onion_connection_read, req);
		onion_poller_slot_set_shutdown(slot, (void*)onion_connection_shutdown, req);
		onion_poller_slot_set_timeout(slot, o->timeout);
		
		onion_poller_add(o->poller, slot);
	}
	else{
		onion_process_request(o, clientfd, &cli_addr, cli_len);
	}
	return 0;
}
コード例 #2
0
ファイル: listen_point.c プロジェクト: Andrepuel/onion
/**
 * @short Called when a new connection appears on the listenfd
 * @memberof onion_listen_point_t
 * 
 * When the new connection appears, creates the request and adds it to the pollers.
 * 
 * It returns always 1 as any <0 would detach from the poller and close the listen point, 
 * and not accepting a request does not mean the connection point is corrupted. If a 
 * connection point may become corrupted should be the connection point itself who detaches 
 * from the poller.
 * 
 * @param op The listen point from where the request must be built
 * @returns 1 always. 
 */
int onion_listen_point_accept(onion_listen_point *op){
	onion_request *req=onion_request_new(op);
	if (req){
		if (req->connection.fd>0){
			onion_poller_slot *slot=onion_poller_slot_new(req->connection.fd, (void*)onion_listen_point_read_ready, req);
			if (!slot)
				return 1;
			onion_poller_slot_set_timeout(slot, req->connection.listen_point->server->timeout);
			onion_poller_slot_set_shutdown(slot, (void*)onion_request_free, req);
			onion_poller_add(req->connection.listen_point->server->poller, slot);
			return 1;
		}
		if (req->connection.fd<0)
			ONION_ERROR("Error creating connection");
		// else, no need for fd... no use case yet.
	}

	return 1;
}
コード例 #3
0
ファイル: listen_point.c プロジェクト: MariadeAnton/onion
/**
 * @short Called when a new connection appears on the listenfd
 * @memberof onion_listen_point_t
 * 
 * When the new connection appears, creates the request and adds it to the pollers.
 * 
 * It returns always 1 as any <0 would detach from the poller and close the listen point, 
 * and not accepting a request does not mean the connection point is corrupted. If a 
 * connection point may become corrupted should be the connection point itself who detaches 
 * from the poller.
 * 
 * @param op The listen point from where the request must be built
 * @returns 1 always. The poller needs one to keep listening for connections.
 */
int onion_listen_point_accept(onion_listen_point *op){
	onion_request *req=onion_request_new(op);
	if (req){
		if (req->connection.fd>0){
			onion_poller_slot *slot=onion_poller_slot_new(req->connection.fd, (void*)onion_listen_point_read_ready, req);
			if (!slot)
				return 1;
			onion_poller_slot_set_timeout(slot, req->connection.listen_point->server->timeout);
			onion_poller_slot_set_shutdown(slot, (void*)onion_request_free, req);
			onion_poller_add(req->connection.listen_point->server->poller, slot);
			return 1;
		}
		// No fd. This could mean error, or not fd based. Normally error would not return a req.
		onion_request_free(req);
		ONION_ERROR("Error creating connection");
		return 1;
	}

	return 1;
}