Continued refactoring code

master
Aadhavan Srinivasan 2 years ago
parent 70ecbae964
commit d46d250b84

@ -73,59 +73,50 @@ int main() {
for (int i=1; i < MAX_CONNECTIONS; i++) { for (int i=1; i < MAX_CONNECTIONS; i++) {
if (FD_ISSET(conn_sockets[i],&read_fd_set)) { if (FD_ISSET(conn_sockets[i],&read_fd_set)) {
do {
while ( strcmp(buffer,"END_OF_DATA\r\n") != 0 ) { while ( strstr(buffer,"END_OF_DATA") == NULL ) {
int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0); int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0);
if (num_bytes_read <= 0) { if (num_bytes_read <= 0) {
close(conn_sockets[i]); close(conn_sockets[i]);
conn_sockets[i] = 0; conn_sockets[i] = 0;
to_user[i] = NULL; to_user[i] = NULL;
GOTO continue_for_loop; goto continue_for_loop;
} }
strcat(data, buffer); strcat(data, buffer);
} }
strcat(data,"\0"); strcat(data,"\0");
if (num_bytes_read <= 0) {
close(conn_sockets[i]);
conn_sockets[i] = 0;
to_user[i] = NULL;
break;
} else {
if (from_user[i] == NULL) { if (from_user[i] == NULL) {
from_user[i] = fetch_user(fetch_sender_user_string(buffer)); from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
if (from_user[i] == NULL) { if (from_user[i] == NULL) {
printf("Please identify yourself.\n"); printf("Please identify yourself.\n");
raise(SIGINT); close(conn_sockets[i]);
return 150; continue;
} else { } else {
printf("You are %s\n",from_user[i]->username); printf("You are %s\n",from_user[i]->username);
} }
continue;
} }
if (to_user[i] == NULL) { if (to_user[i] == NULL) {
to_user[i] = fetch_user(fetch_dest_user_string(buffer)); to_user[i] = fetch_user(fetch_from_string(data,"TO"));
if (to_user[i] == NULL) { if (to_user[i] == NULL) {
printf("Invalid message format or User\n"); printf("Invalid user or message format.\n");
raise(SIGINT); close(conn_sockets[i]);
continue;
} else { } else {
printf("Message intended for %s\n",to_user[i]->username); printf("Message intended for %s\n",to_user[i]->username);
} }
continue;
} }
char* message = fetch_message_string(buffer); char* message = fetch_message_string(data);
close(conn_sockets[i]);
} }
} while (strcmp(buffer,"END_OF_DATA\r\n") != 0);
}
continue_for_loop: continue_for_loop:
data[0] = '\0'; /* This effectively clears the string, since the first element is null. */
to_user[i] = NULL;
} }
} }

Loading…
Cancel
Save