aboutsummaryrefslogtreecommitdiff
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
parent433f6eb3a54881913286aa620db93c003c436c16 (diff)
add timeout
-rw-r--r--client.c3
-rw-r--r--listener.c11
-rw-r--r--protocol.c26
-rw-r--r--server.c32
-rwxr-xr-xsnowcast_controlbin35565 -> 22384 bytes
-rw-r--r--snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_controlbin14770 -> 15157 bytes
-rwxr-xr-xsnowcast_listenerbin34654 -> 19120 bytes
-rwxr-xr-xsnowcast_serverbin56124 -> 44360 bytes
-rw-r--r--snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_serverbin27644 -> 28332 bytes
9 files changed, 51 insertions, 21 deletions
diff --git a/client.c b/client.c
index 852e7ff..760b494 100644
--- a/client.c
+++ b/client.c
@@ -170,6 +170,8 @@ void *reply_thread_routine(void* args) {
exit(1);
}
char *song_name = malloc(string_size);
+ if(song_name == NULL) { perror("malloc in song name"); }
+
int bytes_to_read = string_size;
if (recv_all(sockfd, song_name, &bytes_to_read) == -1) {
perror("recv_all");
@@ -186,6 +188,7 @@ void *reply_thread_routine(void* args) {
exit(1);
}
char *message = malloc(string_size);
+ if(message == NULL) { perror("malloc in message"); }
int bytes_to_read = string_size;
if (recv_all(sockfd, message, &bytes_to_read) == -1) {
perror("recv_all");
diff --git a/listener.c b/listener.c
index 7bb8afe..9cf1c00 100644
--- a/listener.c
+++ b/listener.c
@@ -34,7 +34,6 @@ int main(int argc, char *argv[])
int rv;
int numbytes;
struct sockaddr_storage their_addr;
- char buf[MAXBUFLEN];
socklen_t addr_len;
char s[INET6_ADDRSTRLEN];
@@ -82,6 +81,7 @@ int main(int argc, char *argv[])
int count = 0;
+ char buf[MAXBUFLEN];
while(1) {
// printf("\nlistener: waiting to recvfrom... %d times\n", count++);
@@ -91,15 +91,6 @@ int main(int argc, char *argv[])
perror("recvfrom");
exit(1);
}
- // buf[numbytes] = '\0';
-
- //printf("listener: got packet from %s\n",
- // inet_ntop(their_addr.ss_family,
- // get_in_addr((struct sockaddr *)&their_addr),
- // s, sizeof s));
- //printf("listener: packet is %d bytes long\n", numbytes);
- //buf[numbytes] = '\0';
- //printf("listener: packet contains \"%s\"\n", buf);
// print the size
write(STDOUT_FILENO, buf, numbytes);
diff --git a/protocol.c b/protocol.c
index 864afd8..d350221 100644
--- a/protocol.c
+++ b/protocol.c
@@ -3,8 +3,17 @@
#include "protocol.h"
+#define TIMEOUT 100000 // 100ms in microseconds
+
int send_all(int sock, char *buf, int *len)
{
+ struct timeval timeout;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 100000;
+ // if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+ // sizeof timeout) < 0)
+ // perror("setsockopt failed\n");
+
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
@@ -23,17 +32,34 @@ int send_all(int sock, char *buf, int *len)
int recv_all(int sock, char *buf, int *len)
{
+ // setup the timeout on the socket
+ struct timeval timeout;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = TIMEOUT;
+ if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+ sizeof timeout) < 0)
+ perror("setsockopt failed\n");
+
+ // printf("start: %ld\n", start);
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while(total < *len) {
n = recv(sock, buf+total, bytesleft, 0);
+ // start = time(NULL);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0;
+ if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+ sizeof timeout) < 0)
+ perror("setsockopt failed\n");
+
+
*len = total; // return number actually sent here
return n==-1?-1:0; // return -1 on failure, 0 on success
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;
}
diff --git a/snowcast_control b/snowcast_control
index 4369180..2b99cdb 100755
--- a/snowcast_control
+++ b/snowcast_control
Binary files differ
diff --git a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control
index c9e1f06..3a596f0 100644
--- a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control
+++ b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control
Binary files differ
diff --git a/snowcast_listener b/snowcast_listener
index c8765c2..7accc61 100755
--- a/snowcast_listener
+++ b/snowcast_listener
Binary files differ
diff --git a/snowcast_server b/snowcast_server
index f3e7928..f829e64 100755
--- a/snowcast_server
+++ b/snowcast_server
Binary files differ
diff --git a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server
index 79dd8f6..2428c04 100644
--- a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server
+++ b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server
Binary files differ