From 758be2a8ab76a227b64f32c75ce59dcd66aa9761 Mon Sep 17 00:00:00 2001 From: yrutschle <git1@rutschle.net> Date: Sun, 28 Mar 2021 11:40:40 +0200 Subject: [PATCH] clarify API: allocate cnx in collection from a new fd, or add a fd to an existing connection in a collection --- collection.c | 28 +++++++++++++++++++--------- collection.h | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 collection.h diff --git a/collection.c b/collection.c index 928fce4..dc71654 100644 --- a/collection.c +++ b/collection.c @@ -132,12 +132,29 @@ static int extend_fd2cnx(cnx_collection* collection) return 0; } +/* Points the file descriptor to the specified connection index */ +int collection_add_fd(cnx_collection* collection, int fd, int cnx_index) +{ + if (fd > collection->num_fd) { + int res = extend_fd2cnx(collection); + if (res) { + log_message(LOG_ERR, "unable to extend fd2cnx -- dropping connection\n"); + return -1; + } + } + collection->fd2cnx[fd] = cnx_index; + return 0; +} -int collection_add_fd(struct cnx_collection* collection, int fd) + +/* Allocates a connection and inits it with specified file descriptor */ +int collection_alloc_cnx_from_fd(struct cnx_collection* collection, int fd) { int free, res; struct connection* cnx = collection->cnx; + if (cfg.verbose) fprintf(stderr, "collection_add_fd %d\n", fd); + /* Find an empty slot */ for (free = 0; (free < collection->num_cnx) && (cnx[free].q[0].fd != -1); free++) { /* nothing */ @@ -153,14 +170,7 @@ int collection_add_fd(struct cnx_collection* collection, int fd) collection->cnx[free].state = ST_PROBING; collection->cnx[free].probe_timeout = time(NULL) + cfg.timeout; - if (fd > collection->num_fd) { - res = extend_fd2cnx(collection); - if (res) { - log_message(LOG_ERR, "unable to extend fd2cnx -- dropping connection\n"); - return -1; - } - } - collection->fd2cnx[fd] = free; + collection_add_fd(collection, fd, free); if (cfg.verbose) fprintf(stderr, "accepted fd %d on slot %d\n", fd, free); diff --git a/collection.h b/collection.h new file mode 100644 index 0000000..c6efaac --- /dev/null +++ b/collection.h @@ -0,0 +1,21 @@ +#ifndef COLLECTION_H +#define COLLECTION_H + +typedef struct cnx_collection cnx_collection; + + +cnx_collection* collection_init(void); +void collection_destroy(cnx_collection* collection); + +int collection_alloc_cnx_from_fd(cnx_collection* collection, int fd); +int collection_add_fd(cnx_collection* collection, int fd, int cnx_index); + +/* Remove a connection from the collection */ +int collection_remove_cnx(cnx_collection* collection, struct connection *cnx); + +struct connection* collection_get_cnx(cnx_collection* collection, int index); + +/* Returns the number of connections in the collection */ +int collection_get_length(cnx_collection* collection); + +#endif