Permalink
Browse files

user structs are removed on communication closure

  • Loading branch information...
1 parent 8e3362d commit 9d8a70d0407cf74c851f517d9eaf0f6dd264a924 Robert A committed Jun 7, 2009
Showing with 49 additions and 7 deletions.
  1. +49 −7 duoit.c
View
56 duoit.c
@@ -153,6 +153,28 @@ int find_user(int socket, usr *head, usr **result) {
}
/*
+ * find ancestor of user with csocket number socket
+ */
+int find_pre_user(int socket, usr *head, usr **result) {
+ usr *pre;
+
+ if (head->csocket == socket) {
+ //only one user in user-list
+ return 1;
+ }
+ while (head) {
+ if (head->csocket == socket) {
+ *result = pre;
+ return 0;
+ }
+ pre = head;
+ head = head->next;
+ }
+
+ return 1;
+}
+
+/*
* Handle new connection
*
* accepts new connections and adds them to the file discriptors
@@ -191,7 +213,27 @@ void handle_new_connection(int *listener, fd_set *master, int *fdmax, usr **user
/*
* close connection and remove it from the file discriptors list
*/
-void close_connection(int socket, fd_set *master) {
+void close_connection(int socket, fd_set *master, usr **head) {
+ usr *user;
+ usr *save;
+
+ find_user(socket, *head, &user);
+ if (user == *head) {
+ //remove user-list head
+ *head = user->next;
+ free(user);
+ } else if (user->next == NULL) {
+ //remove end of user-list
+ find_pre_user(socket, *head, &save);
+ save->next = NULL;
+ free(user);
+ } else {
+ //remove entry in user-list
+ find_pre_user(socket, *head, &save);
+ save->next = user->next;
+ free(user);
+ }
+
close(socket);
FD_CLR(socket, master);
}
@@ -278,10 +320,10 @@ int user_command(instruc *instruction, usr *user) {
* quits the connection with client
* TODO: usr struct freigeben
*/
-int quit_command(instruc *instruction, usr *user, fd_set *master) {
+int quit_command(instruc *instruction, usr *user, fd_set *master, usr **head) {
send_message(user->csocket, "221 Goodbye.");
- close_connection(user->csocket, master);
+ close_connection(user->csocket, master, head);
return 0;
}
@@ -357,7 +399,7 @@ int log_action(usr *user, char *message) {
*
* TODO
*/
-int dispatch(usr *user, char *received, int received_len, fd_set *master) {
+int dispatch(usr *user, char *received, int received_len, fd_set *master, usr **head) {
instruc instruction;
if (parse_command(received, received_len, &instruction)) {
@@ -370,7 +412,7 @@ int dispatch(usr *user, char *received, int received_len, fd_set *master) {
return 1;
}
} else if (!strcmp(instruction.command, "QUIT")) {
- quit_command(&instruction, user, master);
+ quit_command(&instruction, user, master, head);
} else {
send_message(user->csocket, "502 Command not implemented.");
}
@@ -475,7 +517,7 @@ int main(int argc, char *argv[]) {
} else {
perror("recv");
}
- close_connection(i, &master);
+ close_connection(i, &master, &head);
} else {
// client sends message
if (find_user(i, head, &current_user)) {
@@ -485,7 +527,7 @@ int main(int argc, char *argv[]) {
if (log_action(current_user, "Zauberei!")) {
printf("*ERR* logging error\n");
}
- dispatch(current_user, buf, nbytes, &master);
+ dispatch(current_user, buf, nbytes, &master, &head);
}
}

0 comments on commit 9d8a70d

Please sign in to comment.