From: Benjamin S. <cr...@ap...> - 2002-02-08 11:07:34
|
Update of /var/lib/cvs/bccs/src In directory apollo:/tmp/cvs-serv30974/src Modified Files: bccs_clients.c bccs_server.c Log Message: New way to send to clients Index: bccs_clients.c =================================================================== RCS file: /var/lib/cvs/bccs/src/bccs_clients.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- bccs_clients.c 8 Feb 2002 09:17:50 -0000 1.2 +++ bccs_clients.c 8 Feb 2002 11:05:31 -0000 1.3 @@ -17,56 +17,97 @@ #include <global.h> -pthread_mutex_t mto_send_mutex; +struct S_user{ + int sockfd; + char username[25]; + int userid; +}; + +pthread_mutex_t mto_send_mutex, msending; pthread_cond_t send_allow; char *to_send; +struct S_user *user; +int num_user; -void client_init(){ - pt_mutex_init(&mto_send_mutex, "init to_send_mutex"); // Initialize mutex - to_send = malloc(16); // allocating some memory :) () - memset(to_send, 0x0, 16); +void add_client (struct S_user newuser){ + struct S_user *dummy_user; + int usercount, dummy_usercount = 0; + + dummy_user = malloc(sizeof(struct S_user)*(num_user+1)); + for (usercount = 0; usercount < num_user; usercount++){ + dummy_user[dummy_usercount].sockfd = user[usercount].sockfd; + dummy_user[dummy_usercount].userid= user[usercount].userid; + strcpy(dummy_user[dummy_usercount].username, user[usercount].username); + } + + dummy_user[num_user].sockfd = newuser.sockfd; + dummy_user[num_user].userid = newuser.userid; + strcpy(dummy_user[num_user].username, newuser.username); + + free (user); + user = malloc(sizeof(struct S_user)*(num_user+1)); + memcpy(user, dummy_user, malloc_usable_size(dummy_user)); + free (dummy_user); + num_user++; } -void client_destroy(){ - pt_mutex_destroy(&mto_send_mutex, "detroying to_send_mutex"); +void delete_client_by_sockfd(int sockfd){ + struct S_user *dummy_user; + int usercount, dummy_usercount = 0; + + dummy_user = malloc(sizeof(struct S_user)*(num_user-1)); + for (usercount = 0; usercount < num_user; usercount++){ + if (user[usercount].sockfd != sockfd){ + dummy_user[dummy_usercount].sockfd = user[usercount].sockfd; + dummy_user[dummy_usercount].userid= user[usercount].userid; + strcpy(dummy_user[dummy_usercount].username, user[usercount].username); + } + } + free (user); + user = malloc(sizeof(struct S_user)*(num_user-1)); + memcpy(user, dummy_user, malloc_usable_size(dummy_user)); + free (dummy_user); + num_user--; } void client_disconnect(int fd){ + delete_client_by_sockfd(fd); close(fd); } -void *thread_clients_send(void * arg){ - int clientsocket=*(int *) arg, outbytes; - char oldsend[2048], log_message[2048]; - - memcpy(oldsend, to_send, 2048); - while (1){ - pt_mutex_lock(&mto_send_mutex, "locking mutex"); - while (memcmp(oldsend, to_send, 2048) == 0){ - pthread_cond_wait(&send_allow, &mto_send_mutex); - } - memcpy(oldsend, to_send, 2048); - if ((outbytes = send(clientsocket, oldsend, strlen(oldsend), 0)) <= 0) { +void *clients_send(char *sendstring){ + int clientcounter, outbytes; + char log_message[2048]; + + pt_mutex_lock(&msending, "locking sending"); + for (clientcounter = 0; clientcounter < num_user; clientcounter++){ + if ((outbytes = send(user[clientcounter].sockfd, sendstring, strlen(sendstring), 0)) <= 0){ if (outbytes == 0) { - sprintf(log_message, "THREAD_CLIENTS_SEND: socket %d hung up\n", clientsocket); + sprintf(log_message, "CLIENTS_SEND: socket %d hung up\n", user[clientcounter].sockfd); bccs_log(log_message, LOG_INFO); } else { - bccs_log("THREAD_CLIENTS_SEND: send: %m", LOG_ERR); + bccs_log("CLIENTS_SEND: recv: %m", LOG_ERR); } - client_disconnect(clientsocket); - pt_mutex_unlock(&mto_send_mutex, "unlocking mutex"); - return; + client_disconnect(user[clientcounter].sockfd); } else { - sprintf(log_message, "THREAD_CLIENTS_SEND: sent '%s' to %d\n", oldsend, clientsocket); + sprintf(log_message, "CLIENTS_SEND: sent '%s' to '%s' on %d\n", sendstring, user[clientcounter].username, user[clientcounter].sockfd); bccs_log(log_message, LOG_INFO); } - pt_mutex_unlock(&mto_send_mutex, "unlocking mutex"); + } + pt_mutex_unlock(&msending, "unlocking sending"); } void *thread_clients_read(void * arg){ int clientsocket=*(int *) arg, inbytes, auth_id; char inbuf[2048], log_message[2048]; + struct S_user newuser; + + newuser.sockfd = clientsocket; + newuser.userid = -1; + strcpy(newuser.username, ""); + add_client(newuser); + while (1){ memset(inbuf, 0x0, 2048); if ((inbytes = recv(clientsocket, inbuf, 2048, 0)) <= 0) { @@ -83,8 +124,20 @@ bccs_log(log_message, LOG_INFO); pt_mutex_lock(&mto_send_mutex, "locking mutex"); strcpy(to_send, inbuf); + clients_send(to_send); pt_mutex_unlock(&mto_send_mutex, "unlocking mutex"); - pthread_cond_broadcast(&send_allow); } } +} + +void client_init(){ + pt_mutex_init(&mto_send_mutex, "init to_send_mutex"); // Initialize mutex + pt_mutex_init(&msending, "init sending_muitex"); // Initialize mutex + to_send = malloc(16); // allocating some memory :) + user = malloc(sizeof(struct S_user)); // allocating some memory :) + memset(to_send, 0x0, 16); +} + +void client_destroy(){ + pt_mutex_destroy(&mto_send_mutex, "destroying to_send_mutex"); } Index: bccs_server.c =================================================================== RCS file: /var/lib/cvs/bccs/src/bccs_server.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- bccs_server.c 8 Feb 2002 09:17:50 -0000 1.2 +++ bccs_server.c 8 Feb 2002 11:05:31 -0000 1.3 @@ -50,12 +50,5 @@ } else { bccs_log("Connection received, spawning read-child\n", LOG_INFO); } - if ((errcode=pthread_create(&newthread, NULL, thread_clients_send, &newfd))){ - bccs_log("Error spawning send-client-thread\n", LOG_ALERT); - exit(1); - } else { - bccs_log("Connection received, spawning write-child\n", LOG_INFO); - current_users++; - } } } |