implemented why2_connection_t in linked list

This commit is contained in:
Václav Šmejkal 2023-02-22 10:09:58 +01:00
parent 493724626c
commit b4c0af2739
Signed by: ENGO150
GPG Key ID: F6D6DF86242C5A59

View File

@ -28,18 +28,20 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
typedef struct node typedef struct node
{ {
int connection; int connection;
pthread_t thread;
struct node *next; struct node *next;
} node_t; //SINGLE LINKED LIST } node_t; //SINGLE LINKED LIST
node_t *head = NULL; node_t *head = NULL;
void push_to_list(int connection) void push_to_list(int connection, pthread_t thread)
{ {
//CREATE NODE //CREATE NODE
node_t *new_node = malloc(sizeof(node_t)); node_t *new_node = malloc(sizeof(node_t));
node_t *buffer = head; node_t *buffer = head;
new_node -> connection = connection; new_node -> connection = connection;
new_node -> thread = thread;
new_node -> next = NULL; new_node -> next = NULL;
if (head == NULL) //INIT LIST if (head == NULL) //INIT LIST
@ -128,16 +130,18 @@ void why2_send_socket(char *text, int socket)
void *why2_communicate_thread(void *arg) void *why2_communicate_thread(void *arg)
{ {
printf("User connected.\t%d\n", *((int*) arg)); why2_connection_t connection = *(why2_connection_t*) arg;
push_to_list(*((int*) arg)); printf("User connected.\t%d\n", connection.connection);
push_to_list(connection.connection, connection.thread);
const time_t startTime = time(NULL); const time_t startTime = time(NULL);
char *received = NULL; char *received = NULL;
while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS
{ {
received = why2_read_socket(*((int*) arg)); //READ received = why2_read_socket(connection.connection); //READ
if (received == NULL) return NULL; //FAILED; EXIT THREAD if (received == NULL) return NULL; //FAILED; EXIT THREAD
@ -148,10 +152,10 @@ void *why2_communicate_thread(void *arg)
why2_deallocate(received); why2_deallocate(received);
} }
printf("User exited.\t%d\n", *((int*) arg)); printf("User exited.\t%d\n", connection.connection);
//DEALLOCATION //DEALLOCATION
remove_node(get_node(*((int*) arg))); remove_node(get_node(connection.connection));
close(*((int*) arg)); close(*((int*) arg));
why2_deallocate(received); why2_deallocate(received);
@ -190,6 +194,8 @@ void *why2_accept_thread(void *socket)
int accepted; int accepted;
pthread_t thread; pthread_t thread;
why2_connection_t connection_buffer;
//LOOP ACCEPT //LOOP ACCEPT
for (;;) for (;;)
{ {
@ -197,6 +203,12 @@ void *why2_accept_thread(void *socket)
if (accepted == -1) continue; if (accepted == -1) continue;
pthread_create(&thread, NULL, why2_communicate_thread, &accepted); connection_buffer = (why2_connection_t)
{
accepted,
thread
};
pthread_create(&thread, NULL, why2_communicate_thread, &connection_buffer);
} }
} }