From 4b401b41e83a4094fd5411b57d71c9a162368fb8 Mon Sep 17 00:00:00 2001 From: fadhil riyanto Date: Sun, 29 Sep 2024 00:49:02 +0700 Subject: add del_fd_sockaddr method while data being added by increasing fdsocklist->size we decrement that variable and loop all data and jump spesific fd. and re-pack the array into new memory area that created by realloc. purpose: delete old sockaddr_in data by fd Signed-off-by: fadhil riyanto --- main.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 77db7c0..57d3df5 100644 --- a/main.c +++ b/main.c @@ -305,6 +305,43 @@ static struct sockaddr_in* get_by_fd_sockaddr(struct fd_sockaddr_list *fdsocklis return NULL; } +static struct sockaddr_in* del_fd_sockaddr(struct fd_sockaddr_list *fdsocklist, int fd_num) +{ + if (fdsocklist->size != 0) { + + + // fdsocklist->size = fdsocklist->size - 1; + + struct _fd_sockaddr_list* dummymem = realloc( + fdsocklist->list, sizeof(struct _fd_sockaddr_list) * (fdsocklist->size - 1)); + + if (dummymem == NULL) { + perror("realloc"); + + return NULL; + } + + int i_n = 0; + for(int i = 0; i < fdsocklist->size; i++) { + if (fdsocklist->list[i].fd == fd_num) { + /* pass, do not add instead do free */ + free(fdsocklist->list[i].sockaddr); + } else { + dummymem[i_n] = fdsocklist->list[i]; + + } + i_n = i_n + 1; + } + + fdsocklist->list = dummymem; + fdsocklist->size = fdsocklist->size - 1; + + + } + + return NULL; +} + static int free_fd_sockaddr(struct fd_sockaddr_list *fdsocklist) { free(fdsocklist->list); @@ -357,8 +394,6 @@ static void* start_long_poll(void *srv_ctx_voidptr) { } } - - printf("ret %d\n", n_ready_conn); // server_accept_to_epoll(srv_ctx, tcpfd_event_list, &ev); } } @@ -379,6 +414,15 @@ static void* start_long_poll_receiver(void *srv_ctx_voidptr) for (int i = 0; i < n_ready_read; i++) { get_by_fd_sockaddr(srv_ctx->fd_sockaddr_list, srv_ctx->acceptfd_watchlist_event[i].data.fd); + + char tempbuf[100]; + memset(tempbuf, 0, 100); + read(srv_ctx->acceptfd_watchlist_event[i].data.fd, tempbuf, 100); + + printf("%s\n", tempbuf); + + del_fd_sockaddr(srv_ctx->fd_sockaddr_list, srv_ctx->acceptfd_watchlist_event[i].data.fd); + close(srv_ctx->acceptfd_watchlist_event[i].data.fd); } printf("event available to read %d\n", n_ready_read); -- cgit v1.2.3