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 --- c | Bin 35294 -> 35294 bytes l | Bin 34638 -> 34638 bytes l.dSYM/Contents/Resources/DWARF/l | Bin 11651 -> 11651 bytes listener.c | 1 - mp3/chipOffTheBlock.mp3 | Bin 0 -> 4477733 bytes s | Bin 55198 -> 55198 bytes s.dSYM/Contents/Resources/DWARF/s | Bin 23594 -> 23649 bytes snowcast_server_concurrent.c | 25 +++++++++++++++++-------- 8 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 mp3/chipOffTheBlock.mp3 diff --git a/c b/c index 35031be..20b2474 100755 Binary files a/c and b/c differ diff --git a/l b/l index 281f522..d6eec69 100755 Binary files a/l and b/l differ diff --git a/l.dSYM/Contents/Resources/DWARF/l b/l.dSYM/Contents/Resources/DWARF/l index c55216a..283ecc9 100644 Binary files a/l.dSYM/Contents/Resources/DWARF/l and b/l.dSYM/Contents/Resources/DWARF/l differ diff --git a/listener.c b/listener.c index d5b4799..7bb8afe 100644 --- a/listener.c +++ b/listener.c @@ -102,7 +102,6 @@ int main(int argc, char *argv[]) //printf("listener: packet contains \"%s\"\n", buf); // print the size - write(STDOUT_FILENO, buf, numbytes); memset(buf, 0, MAXBUFLEN); diff --git a/mp3/chipOffTheBlock.mp3 b/mp3/chipOffTheBlock.mp3 new file mode 100644 index 0000000..c06c05f Binary files /dev/null and b/mp3/chipOffTheBlock.mp3 differ diff --git a/s b/s index 10bc785..9f86c9b 100755 Binary files a/s and b/s differ diff --git a/s.dSYM/Contents/Resources/DWARF/s b/s.dSYM/Contents/Resources/DWARF/s index 4db3832..489e060 100644 Binary files a/s.dSYM/Contents/Resources/DWARF/s and b/s.dSYM/Contents/Resources/DWARF/s differ 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