-
Notifications
You must be signed in to change notification settings - Fork 2
/
IPCClient.c
66 lines (53 loc) · 1.2 KB
/
IPCClient.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
#include "IPCClient.h"
#include <string.h>
#include <sys/epoll.h>
#include "util.h"
IPCClient *
ipc_client_new(int fd)
{
IPCClient *c = (IPCClient *)malloc(sizeof(IPCClient));
if (c == NULL) return NULL;
// Initialize struct
memset(&c->event, 0, sizeof(struct epoll_event));
c->buffer_size = 0;
c->buffer = NULL;
c->fd = fd;
c->event.data.fd = fd;
c->next = NULL;
c->prev = NULL;
c->subscriptions = 0;
return c;
}
void
ipc_list_add_client(IPCClientList *list, IPCClient *nc)
{
DEBUG("Adding client with fd %d to list\n", nc->fd);
if (*list == NULL) {
// List is empty, point list at first client
*list = nc;
} else {
IPCClient *c;
// Go to last client in list
for (c = *list; c && c->next; c = c->next)
;
c->next = nc;
nc->prev = c;
}
}
void
ipc_list_remove_client(IPCClientList *list, IPCClient *c)
{
IPCClient *cprev = c->prev;
IPCClient *cnext = c->next;
if (cprev != NULL) cprev->next = c->next;
if (cnext != NULL) cnext->prev = c->prev;
if (c == *list) *list = c->next;
}
IPCClient *
ipc_list_get_client(IPCClientList list, int fd)
{
for (IPCClient *c = list; c; c = c->next) {
if (c->fd == fd) return c;
}
return NULL;
}