diff options
author | fadhil riyanto <me@fadev.org> | 2024-09-29 00:49:02 +0700 |
---|---|---|
committer | fadhil riyanto <me@fadev.org> | 2024-09-29 00:49:02 +0700 |
commit | 4b401b41e83a4094fd5411b57d71c9a162368fb8 (patch) | |
tree | 9aaac6cb3b1a5b8649776d0868cb46e10b802bf9 | |
parent | 318d8bc772b496672150724e294a113ce6230a95 (diff) |
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 <me@fadev.org>
-rw-r--r-- | main.c | 48 |
1 files changed, 46 insertions, 2 deletions
@@ -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); |