123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /** @file pollfd.c
- */
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <poll.h>
- #include "faux/faux.h"
- #include "faux/net.h"
- #include "faux/vec.h"
- #include "private.h"
- /** @brief Callback function to search specified fd within pollfd structures.
- */
- static int cmp_by_fd(const void *key, const void *item)
- {
- int k = *(int *)key;
- struct pollfd *i = (struct pollfd *)item;
- if (k == i->fd)
- return 0;
- return -1;
- }
- /** @brief Allocates memory for faux_pollfd_t object.
- *
- * @return Allocated faux_pollfd_t object or NULL on error.
- */
- faux_pollfd_t *faux_pollfd_new(void)
- {
- faux_pollfd_t *faux_pollfd = NULL;
- faux_pollfd = faux_zmalloc(sizeof(*faux_pollfd));
- assert(faux_pollfd);
- if (!faux_pollfd)
- return NULL;
- faux_pollfd->vec = faux_vec_new(sizeof(struct pollfd), cmp_by_fd);
- return faux_pollfd;
- }
- /** @brief Frees previously allocated faux_pollfd_t object.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- */
- void faux_pollfd_free(faux_pollfd_t *faux_pollfd)
- {
- if (!faux_pollfd)
- return;
- faux_vec_free(faux_pollfd->vec);
- faux_free(faux_pollfd);
- }
- /** @brief Returns whole "struct pollfd" vector.
- *
- * It can be used while poll() call.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @return Pointer to "struct pollfd" vector or NULL on error.
- */
- struct pollfd *faux_pollfd_vector(faux_pollfd_t *faux_pollfd)
- {
- assert(faux_pollfd);
- if (!faux_pollfd)
- return NULL;
- return faux_vec_data(faux_pollfd->vec);
- }
- /** @brief Returns number of "struct pollfd" items within object.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @return Number of items.
- */
- size_t faux_pollfd_len(faux_pollfd_t *faux_pollfd)
- {
- assert(faux_pollfd);
- if (!faux_pollfd)
- return 0;
- return faux_vec_len(faux_pollfd->vec);
- }
- /** @brief Returns "struct pollfd" item by specified index.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [in] index Index of item to get.
- * @return Pointer to item or NULL on error.
- */
- struct pollfd *faux_pollfd_item(faux_pollfd_t *faux_pollfd, unsigned int index)
- {
- assert(faux_pollfd);
- if (!faux_pollfd)
- return NULL;
- return (struct pollfd *)faux_vec_item(faux_pollfd->vec, index);
- }
- /** @brief Finds item with specified fd value.
- *
- * File descriptor is a key for array. Object can contain the only one item
- * with the same fd value.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [in] fd File descriptor to search for.
- * @return Pointer to found item or NULL on error or in "not found" case.
- */
- struct pollfd *faux_pollfd_find(faux_pollfd_t *faux_pollfd, int fd)
- {
- int index = 0;
- assert(faux_pollfd);
- if (!faux_pollfd)
- return NULL;
- assert(fd >= 0);
- if (fd < 0)
- return NULL;
- index = faux_vec_find(faux_pollfd->vec, &fd, 0);
- if (index < 0)
- return NULL;
- return (struct pollfd *)faux_vec_item(faux_pollfd->vec, index);
- }
- /** @brief Adds new item to object.
- *
- * The file descriptors are unique within array. So function try to find item
- * with the specified fd. If it's found the correspondent item will be returned
- * but new item will not be created.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [in] fd File descriptor to set to newly created item.
- * @param [in] events The events interested in.
- * @return Pointer to new item or NULL on error.
- */
- struct pollfd *faux_pollfd_add(faux_pollfd_t *faux_pollfd, int fd, short events)
- {
- struct pollfd *pollfd = NULL;
- assert(faux_pollfd);
- if (!faux_pollfd)
- return NULL;
- assert(fd >= 0);
- if (fd < 0)
- return NULL;
- // Don't add duplicated fd
- pollfd = faux_pollfd_find(faux_pollfd, fd);
- if (!pollfd) {
- // Create new item
- pollfd = faux_vec_add(faux_pollfd->vec);
- assert(pollfd);
- if (!pollfd)
- return NULL;
- pollfd->fd = fd;
- }
- pollfd->events = events;
- pollfd->revents = 0;
- return pollfd;
- }
- /** @brief Removes item specified by fd.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [in] fd File descriptor to remove.
- * @return BOOL_TRUE - success, BOOL_FALSE on error.
- */
- bool_t faux_pollfd_del_by_fd(faux_pollfd_t *faux_pollfd, int fd)
- {
- int index = 0;
- assert(faux_pollfd);
- if (!faux_pollfd)
- return BOOL_FALSE;
- assert(fd >= 0);
- if (fd < 0)
- return BOOL_FALSE;
- index = faux_vec_find(faux_pollfd->vec, &fd, 0);
- if (index < 0) // Not found
- return BOOL_FALSE;
- if (faux_vec_del(faux_pollfd->vec, index) < 0)
- return BOOL_FALSE;
- return BOOL_TRUE;
- }
- /** @brief Removes item specified by index.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [in] index Index of item to remove.
- * @return BOOL_TRUE - success, BOOL_FALSE on error.
- */
- bool_t faux_pollfd_del_by_index(faux_pollfd_t *faux_pollfd, unsigned int index)
- {
- assert(faux_pollfd);
- if (!faux_pollfd)
- return BOOL_FALSE;
- if (faux_vec_del(faux_pollfd->vec, index) < 0)
- return BOOL_FALSE;
- return BOOL_TRUE;
- }
- /** @brief Initilizes iterator to iterate through all the vector.
- *
- * @sa faux_pollfd_each()
- * @sa faux_pollfd_each_active()
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [out] iterator Iterator to initialize.
- */
- void faux_pollfd_init_iterator(faux_pollfd_t *faux_pollfd, faux_pollfd_iterator_t *iterator)
- {
- assert(faux_pollfd);
- if (!faux_pollfd)
- return;
- if (!iterator)
- return;
- *iterator = 0;
- }
- /** @brief Iterate through all the vector.
- *
- * The iterator must be initialized first by faux_pollfd_init_iterator().
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [out] iterator Initialized iterator.
- * @return Pointer to item or NULL on error or on end of vector.
- */
- struct pollfd *faux_pollfd_each(faux_pollfd_t *faux_pollfd, faux_pollfd_iterator_t *iterator)
- {
- unsigned int old_iterator = 0;
- assert(faux_pollfd);
- if (!faux_pollfd)
- return NULL;
- if (!iterator)
- return NULL;
- old_iterator = *iterator;
- (*iterator)++;
- return faux_pollfd_item(faux_pollfd, old_iterator);
- }
- /** @brief Iterate through all active items of vector.
- *
- * The iterator must be initialized first by faux_pollfd_init_iterator().
- * Function returns items that has non-null value in "revent" field i.e.
- * active items.
- *
- * @param [in] faux_pollfd Allocated faux_pollfd_t object.
- * @param [out] iterator Initialized iterator.
- * @return Pointer to active item or NULL on error or on end of vector.
- */
- struct pollfd *faux_pollfd_each_active(faux_pollfd_t *faux_pollfd, faux_pollfd_iterator_t *iterator)
- {
- struct pollfd *pollfd = NULL;
- assert(faux_pollfd);
- if (!faux_pollfd)
- return NULL;
- if (!iterator)
- return NULL;
- while ((pollfd = faux_pollfd_each(faux_pollfd, iterator))) {
- if (pollfd->revents != 0)
- return pollfd;
- }
- return NULL;
- }
|