diff options
-rw-r--r-- | client.c | 2 | ||||
-rw-r--r-- | server.c | 83 | ||||
-rwxr-xr-x | snowcast_control | bin | 22384 -> 22384 bytes | |||
-rw-r--r-- | snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control | bin | 15157 -> 15199 bytes | |||
-rw-r--r-- | snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener | bin | 11651 -> 11649 bytes | |||
-rwxr-xr-x | snowcast_server | bin | 44360 -> 44576 bytes | |||
-rw-r--r-- | snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server | bin | 28332 -> 28763 bytes |
7 files changed, 67 insertions, 18 deletions
@@ -93,6 +93,8 @@ int main(int argc, char *argv[]) usleep(1000); + // sleep(1); + struct Hello hello; hello.commandType = 0; // convert updPort to an int @@ -546,10 +546,36 @@ void *select_thread(void *arg) { if (newfd == -1) { perror("accept"); } else { - FD_SET(newfd, &master); // add to master set - if (newfd > fdmax) { // keep track of the max - fdmax = newfd; + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = TIMEOUT; + if (setsockopt(newfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + perror("setsockopt"); } + + uint8_t command_type = -1; + if ((nbytes = recv(newfd, &command_type, 1, 0)) <= 0) + { + // got error or connection closed by client + if (nbytes == 0) { + // connection closed + // printf("selectserver: socket %d hung up\n", i); + } else { + perror("recv"); + } + // remove user from data structures + close(newfd); + // destroy_user(i); + continue; + } + + if (command_type != 0) { + char * message = "must send a Hello message first"; + send_invalid_command_reply(newfd, strlen(message), message); + close(newfd); + continue; + } + // printf("selectserver: new connection from %s on " // "socket %d\n.", // inet_ntop(remoteaddr.ss_family, @@ -557,8 +583,43 @@ void *select_thread(void *arg) { // remoteIP, INET6_ADDRSTRLEN), // newfd); // init user with this newfd + + // get the udp port + uint16_t udp_port = -1; + int bytes_to_read = sizeof(uint16_t); + if (recv_all(newfd, &udp_port, &bytes_to_read) == -1) { + perror("recv_all"); + close(newfd); + // destroy_user(i); + continue; + } + udp_port = ntohs(udp_port); + + // printf("udpPort (from Hello) for new connection is %d.\n", udp_port); + // update udp port of user + + // add a timeout to the socket + // struct timeval tv; + // tv.tv_sec = 0; + // tv.tv_usec = 0; + // if (setsockopt(newfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + // perror("setsockopt"); + // } + FD_SET(newfd, &master); // add to master set + if (newfd > fdmax) { // keep track of the max + fdmax = newfd; + } init_user(newfd); - } + update_user_udpPort(newfd, udp_port); + + // send the welcome message to client + struct Welcome welcome; + welcome.replyType = 2; + welcome.numStations = htons(num_stations); + int bytes_to_send = sizeof(struct Welcome); + if (send_all(newfd, &welcome, &bytes_to_send) == -1) + perror("send_all"); + } } else { // handle data from a client uint8_t command_type = -1; @@ -591,9 +652,7 @@ void *select_thread(void *arg) { destroy_user(i); continue; } - udp_port = ntohs(udp_port); - // check if user has a udpPort, if so, close connection if (user_data[sockfd_to_user[i]].udpPort != -1) { // send back in invalid command char * message = "must not sent more than one Hello message"; @@ -604,18 +663,6 @@ void *select_thread(void *arg) { destroy_user(i); continue; } - - // printf("udpPort (from Hello) for new connection is %d.\n", udp_port); - // update udp port of user - update_user_udpPort(i, udp_port); - - // send the welcome message to client - struct Welcome welcome; - welcome.replyType = 2; - welcome.numStations = htons(num_stations); - int bytes_to_send = sizeof(struct Welcome); - if (send_all(i, &welcome, &bytes_to_send) == -1) - perror("send_all"); } else if (command_type == 1) { // we got a SetStation command // get the station number diff --git a/snowcast_control b/snowcast_control Binary files differindex 2b99cdb..e8e8ad0 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 3a596f0..43f119b 100644 --- a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control +++ b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control diff --git a/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener b/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener Binary files differindex b6206fa..f811857 100644 --- a/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener +++ b/snowcast_listener.dSYM/Contents/Resources/DWARF/snowcast_listener diff --git a/snowcast_server b/snowcast_server Binary files differindex f829e64..2c41418 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 2428c04..df5780d 100644 --- a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server +++ b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server |