clarify API: allocate cnx in collection from a new fd, or add a fd to an existing connection in a collection

This commit is contained in:
yrutschle 2021-03-28 11:40:40 +02:00
parent 654ff0b5b0
commit 758be2a8ab
2 changed files with 40 additions and 9 deletions

View File

@ -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);

21
collection.h Normal file
View File

@ -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