diff options
-rw-r--r-- | client.c | 3 | ||||
-rw-r--r-- | listener.c | 11 | ||||
-rw-r--r-- | protocol.c | 26 | ||||
-rw-r--r-- | server.c | 32 | ||||
-rwxr-xr-x | snowcast_control | bin | 35565 -> 22384 bytes | |||
-rw-r--r-- | snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control | bin | 14770 -> 15157 bytes | |||
-rwxr-xr-x | snowcast_listener | bin | 34654 -> 19120 bytes | |||
-rwxr-xr-x | snowcast_server | bin | 56124 -> 44360 bytes | |||
-rw-r--r-- | snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server | bin | 27644 -> 28332 bytes |
9 files changed, 51 insertions, 21 deletions
@@ -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"); @@ -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); @@ -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 @@ -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 Binary files differindex 4369180..2b99cdb 100755 --- a/snowcast_control +++ b/snowcast_control diff --git a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control Binary files differindex c9e1f06..3a596f0 100644 --- a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control +++ b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control diff --git a/snowcast_listener b/snowcast_listener Binary files differindex c8765c2..7accc61 100755 --- a/snowcast_listener +++ b/snowcast_listener diff --git a/snowcast_server b/snowcast_server Binary files differindex f3e7928..f829e64 100755 --- a/snowcast_server +++ b/snowcast_server diff --git a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server Binary files differindex 79dd8f6..2428c04 100644 --- a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server +++ b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server |