-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientList.c
81 lines (60 loc) · 1.53 KB
/
clientList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "clientList.h"
clientList *cl_create() {
clientList *list = (clientList *)malloc(sizeof(clientList));
list->head = cl_createItem(-1, -1, NULL, NULL);
list->tail = cl_createItem(-1, -1, NULL, NULL);
list->head->next = list->tail;
list->tail->prev = list->head;
return list;
}
client *cl_createItem(int id, int socket_fd, struct sockaddr_in *address, fileLinkList *files) {
client *c = (client *)malloc(sizeof(client));
c->id = id;
c->socket_fd = socket_fd;
if (address != NULL) {
c->address = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
memcpy(c->address, address, sizeof(struct sockaddr_in));
} else {
c->address = NULL;
}
c->files = files;
c->next = NULL;
c->prev = NULL;
return c;
}
client *cl_find(clientList *list, int id) {
client *c;
if (list != NULL) {
c = list->head;
for (; c != list->tail; c = c->next) {
if (c->id == id) return c;
}
}
return NULL;
}
void cl_add(clientList *list, client *newClient) {
newClient->next = list->head->next;
newClient->prev = list->head;
list->head->next->prev = newClient;
list->head->next = newClient;
}
void cl_remove(client *c) {
if (c != NULL) {
c->prev->next = c->next;
c->next->prev = c->prev;
free(c->address);
if (c->files != NULL) fll_clear(c->files->head);
free(c);
}
}
void cl_clear(client *head) {
if (head != NULL) {
cl_clear(head->next);
free(head->address);
if (head->files != NULL) fll_clear(head->files->head);
free(head);
}
}
int cl_isEmpty(clientList *list) {
return (list->head->next == list->tail);
}