コード例 #1
0
int
owlist_free_all
(
	OWList * list,
	void (* free_element) (void *)
)
{
	int return_code = 0 ;
	OWListIterator * iterator ;
	void * element ;

	iterator = owlist_iterator_new (list, OWLIST_WRITE) ;
	if (iterator != NULL)
	{
		while (owlist_iterator_next (iterator) == 0)
		{
			element = owlist_iterator_get (iterator) ;
			return_code |= owlist_iterator_remove (iterator) ;
			free_element (element) ;
			/* if free_element has a return code, it cannot be tested */
		}
		return_code |= owlist_iterator_free (iterator) ;
	}

	return_code |= owlist_free (list) ;

	return return_code ;
}
コード例 #2
0
int
owlist_contains
(
	OWList * list,
	const void * element,
	OWListCompare compare
)
{
	int return_code = -1 ;
	OWListIterator * iterator ;

	iterator = owlist_iterator_new (list, OWLIST_READ) ;
	if (iterator == NULL)
	{
		return -1 ;
	}

	while (owlist_iterator_next (iterator) == 0)
	{
		if (owlist_compare (iterator->current->element, element, compare) == 0)
		{
			return_code = 0 ;
			break ;
		}
	}

	if (owlist_iterator_free (iterator))
	{
		return -1 ;
	}

	return return_code ;
}
コード例 #3
0
int
owlist_remove_all
(
	OWList * list
)
{
	int return_code = 0 ;
	OWListIterator * iterator ;

	iterator = owlist_iterator_new (list, OWLIST_WRITE) ;
	if (iterator == NULL)
	{
		return -1 ;
	}

	while (owlist_iterator_next (iterator) == 0)
	{
		free (iterator->current) ;
	}

	if (owlist_iterator_free (iterator))
	{
		return_code = -1 ;
	}

	list->first = NULL ;
	list->last = NULL ;
	list->count = 0 ;

	return return_code ;
}
コード例 #4
0
void *
owlist_remove
(
	OWList * list,
	const void * element,
	OWListCompare compare
)
{
	void * found = NULL ;
	OWListIterator * iterator ;

	iterator = owlist_iterator_new (list, OWLIST_WRITE) ;
	if (iterator == NULL)
	{
		return NULL ;
	}

	while (owlist_iterator_next (iterator) == 0)
	{
		if (owlist_compare (iterator->current->element, element, compare) == 0)
		{
			found = iterator->current->element ;
			owlist_iterator_remove (iterator) ;
			break ;
		}
	}

	if (owlist_iterator_free (iterator))
	{
		return NULL ;
	}

	return found ;
}
コード例 #5
0
int
owlist_add
(
	OWList * list,
	const void * element,
	OWListCompare compare
)
{
	OWListIterator * iterator ;
	OWListNode * node ;

	/* write lock */
	iterator = owlist_iterator_new (list, OWLIST_WRITE) ;
	if (iterator == NULL)
	{
		return -1 ;
	}

	/* check if the element is already in the list */
	while (owlist_iterator_next (iterator) == 0)
	{
		if (owlist_compare (iterator->current->element, element, compare) == 0)
		{
			owlist_iterator_free (iterator) ;
			return -1 ;
		}
	}

	/* create a new node */
	node = malloc (sizeof (OWListNode)) ;
	if (node == NULL)
	{
		owlist_iterator_free (iterator) ;
		return -1 ;
	}
	node->element = (void *) element ;
	node->next = NULL ;

	/* add the node at the end of the list */
	if (list->first == NULL)
	{
		list->first = node ;
	}
	else
	{
		list->last->next = node ;
	}
	list->last = node ;
	list->count ++ ;

	/* write unlock */
	if (owlist_iterator_free (iterator))
	{
		return -1 ;
	}

	return 0 ;
}
コード例 #6
0
struct sockaddr *
transport_listening_address_get
(
	TransportProtocol protocol,
	OWSLAddressFamily address_family,
	OWListIterator ** iterator
)
{
	OWList * list ;
	struct sockaddr * address = NULL ;
	OWSLSocketType implementation ;

	if (iterator == NULL)
	{
		return NULL ;
	}

	implementation = transport_implementation_get (protocol, address_family) ;
	if (implementation != OWSL_TYPE_UNKNOWN)
	{
		if (* iterator == NULL)
		{
			list = transport_socket_list_get (TRANSPORT_LISTENING, owsl_socket_type_mode_get (implementation)) ;
			* iterator = owlist_iterator_new (list, OWLIST_READ) ;
		}
		if (* iterator != NULL)
		{
			OWSLSocket socket = transport_socket_get_next
			(
				implementation,
				* iterator
			) ;
			if (socket > 0)
			{
				address = owsl_bound_address_get (socket) ;
			}
		}
	}

	return address ;
}
コード例 #7
0
static OWSLSocket
transport_socket_get
(
	TransportType type,
	OWSLSocketType implementation,
	const struct sockaddr * address,
	int strict_address
)
{
	OWSLSocketMode mode ;
	OWList * list ;
	OWListIterator * socket_list_iterator ;
	OWSLSocket socket, best_socket = 0 ;
	struct sockaddr * (* address_get) (OWSLSocket) = NULL ;
	struct sockaddr * socket_address ;
	int socket_found = 0 ;

	mode = owsl_socket_type_mode_get (implementation) ;

	list = transport_socket_list_get (type, mode) ;
	if (list == NULL)
	{
		return -1 ;
	}

	if (address != NULL)
	{
		switch (type)
		{
			case TRANSPORT_DATA :
			{
				if (mode == OWSL_MODE_STREAM)
				{
					address_get = owsl_remote_address_get ;
				}
				break ;
			}
			case TRANSPORT_LISTENING :
			{
				address_get = owsl_bound_address_get ;
				break ;
			}
		}
	}

	socket_list_iterator = owlist_iterator_new (list, OWLIST_READ) ;
	if (socket_list_iterator == NULL)
	{
		return -1 ;
	}

	do
	{
		socket = transport_socket_get_next (implementation, socket_list_iterator) ;
		if (socket > 0)
		{
			if (address_get != NULL)
			{
				socket_address = address_get (socket) ;
				if (owsl_address_compare ((const struct sockaddr *) address, socket_address) == 0)
				{
					socket_found = 1 ;
				}
				else if (! strict_address)
				{
					best_socket = socket ;
				}
			}
			else
			{
				socket_found = 1 ;
			}
		}
	} while (! socket_found && socket > 0) ;

	if (owlist_iterator_free (socket_list_iterator))
	{
		return -1 ;
	}

	return socket_found ? socket : (strict_address ? 0 : best_socket) ;
}