From c36846a96fd45e57f37ce73e49af8275a305d4b4 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Tue, 19 Sep 2023 00:44:40 -0400 Subject: fix file descriptor leak --- snowcast_server_concurrent.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'snowcast_server_concurrent.c') diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index 3b71156..47b112a 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -240,8 +240,8 @@ void *send_udp_packet_routine(void *arg) { // get file path char* file_path = station_data[station_num].filePath; // get current seek chunk - int current_chunk = station_data[station_num].seekIndex; FILE* file_stream = fopen(file_path, "r"); + int current_chunk = station_data[station_num].seekIndex; if (fseek(file_stream, current_chunk, SEEK_SET) == -1) { perror("fseek"); return (NULL); @@ -253,6 +253,7 @@ void *send_udp_packet_routine(void *arg) { perror("fread"); return (NULL); } + fclose(file_stream); // printf("send data: thread %d \n", user_index); // int numbytes; // if ((numbytes = sendto(udp_sockfd, data, strlen(data), 0, @@ -273,7 +274,6 @@ void *send_udp_packet_routine(void *arg) { did_work = 1; - close(file_stream); usleep(400000); } @@ -507,16 +507,24 @@ void *init_user(int sockfd) { // this is to save memory space. // in general, the displacement of 4 is where a user "used to be" int user_index = max_active_users++; - if(user_data[(sockfd-4)/2].sockfd == -1) { - printf("reusing memory\n"); - user_index = (sockfd - 4)/2; - } else { - printf("making new memory\n"); - // have to make more memory + int running_index = 0; + while(running_index++ < max_active_users) + { + if (user_data[running_index].sockfd == -1) + { + user_index = running_index; + break; + } + // printf("reusing memory\n"); + } + // have to make more memory + if (user_index == max_active_users) { + ///printf("making new memory\n"); user_t *more_users = realloc(user_data, sizeof(user_t) * max_active_users); if (!more_users) { perror("realloc"); exit(1); } user_data = more_users; } + // map TCP sockfd to this user index user_data[user_index] = (user_t){-1, -1, sockfd, -1}; sockfd_to_user[sockfd] = user_index; @@ -585,4 +593,5 @@ void send_announce_message(int fd, int station_num) { printf("sent %d bytes\n", bytessent); free(send_buffer); + } \ No newline at end of file -- cgit v1.2.3-70-g09d2