aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-21 07:06:14 +0000
committersotech117 <michael_foiani@brown.edu>2023-09-21 07:06:14 +0000
commit9ba49f755e152d0996a43e8da7d146b8d2069051 (patch)
treedde7fe228fc6ab44c3c462576a391193e3189da5 /server.c
parent433f6eb3a54881913286aa620db93c003c436c16 (diff)
add timeout
Diffstat (limited to 'server.c')
-rw-r--r--server.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/server.c b/server.c
index f7697a3..c2d8208 100644
--- a/server.c
+++ b/server.c
@@ -73,7 +73,7 @@ void send_invalid_command_reply(int fd, size_t message_size, char* message);
// void *load_file(void* arg);
-int main(int argc, char *argv[])
+main(int argc, char *argv[])
{
// threads to control reading files at chunks while the other threads sleep
// station_data = malloc(sizeof(station_t) * NUM_STATIONS);
@@ -98,6 +98,7 @@ int main(int argc, char *argv[])
totalSize += sizeof(int) + strlen(argv[i]);
}
station_data = malloc(totalSize);
+ if (!station_data) { perror("malloc station data"); return 1; }
// assign the stations
for (int i = 2; i < argc; i++)
{
@@ -112,7 +113,7 @@ int main(int argc, char *argv[])
// make array of user data
user_data = malloc(sizeof(user_t) * max_active_users);
- if (!user_data) { perror("malloc"); return 1; }
+ if (!user_data) { perror("malloc userdata"); return 1; }
// make and start "select" thread that manages:
// 1) new connections, 2) requests from current connections, 3)cloing connections
@@ -181,7 +182,7 @@ int main(int argc, char *argv[])
void write_int_to_fd(int fd, int n) {
int l = snprintf(NULL, 0, "%d", n);
char *num = malloc(l + 1);
- if (!num) { perror("malloc"); return; }
+ if (!num) { perror("malloc write to fd"); return; }
snprintf(num, l + 1, "%d", n);
if (write(fd, num, strlen(num)) == -1) {
@@ -274,8 +275,8 @@ void *send_udp_packet_routine(void *arg) {
int int_port = user_data[user_index].udpPort;
int length = snprintf( NULL, 0, "%d", int_port );
char* port = malloc( length + 1 );
- if (!port) { perror("malloc"); return (NULL); }
- snprintf( port, length + 1, "%d", int_port );
+ if (!port) { perror("malloc on port"); return (NULL); }
+ snprintf(port, length + 1, "%d", int_port);
sprintf(port, "%d", int_port);
if (error_code = getaddrinfo(NULL, port, &thread_hints, &thread_servinfo) != 0)
@@ -585,7 +586,10 @@ void *select_thread(void *arg) {
int bytes_to_read = sizeof(uint16_t);
if (recv_all(i, &udp_port, &bytes_to_read) == -1) {
perror("recv_all");
- exit(1);
+ close(i);
+ FD_CLR(i, &master);
+ destroy_user(i);
+ continue;
}
udp_port = ntohs(udp_port);
@@ -615,11 +619,15 @@ void *select_thread(void *arg) {
}
else if (command_type == 1) { // we got a SetStation command
// get the station number
+
uint16_t station_number = -1;
int bytes_to_read = sizeof(uint16_t);
if (recv_all(i, &station_number, &bytes_to_read) == -1) {
perror("recv_all");
- exit(1);
+ close(i);
+ FD_CLR(i, &master);
+ destroy_user(i);
+ continue;
}
station_number = ntohs(station_number);
@@ -703,7 +711,6 @@ void *init_user(int sockfd) {
user_data = more_users;
}
-
// map TCP sockfd to this user index
user_data[running_index] = (user_t){-1, -1, sockfd, -1};
sockfd_to_user[sockfd] = running_index;
@@ -734,7 +741,10 @@ void destroy_user(int sockfd) {
pthread_mutex_lock(&mutex_user_data);
// stop the thread streaming to the user
// TODO: close the FD in the stream thread
- pthread_cancel(user_data[sockfd_to_user[sockfd]].streamThread);
+ pthread_t thread = user_data[sockfd_to_user[sockfd]].streamThread;
+ if (thread != -1) {
+ pthread_cancel(thread);
+ }
// close(user_data[sockfd_to_user[sockfd]].udpPort);
// pthread_kill(user_data[sockfd_to_user[sockfd]].streamThread, SIGINT);
// "remove" the user from the list of user data
@@ -762,7 +772,7 @@ void send_announce_reply(int fd, int station_num) {
char *send_buffer = malloc(len_file_path+2);
if (!send_buffer) {
- perror("malloc");
+ perror("malloc in send announce");
return;
}
send_buffer[0] = 3;
@@ -784,7 +794,7 @@ void send_announce_reply(int fd, int station_num) {
void send_invalid_command_reply(int fd, size_t message_size, char* message) {
char *send_buffer = malloc(message_size+2);
if (!send_buffer) {
- perror("malloc");
+ perror("malloc in send ancalid command");
return;
}