-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnstx_queue.c
113 lines (95 loc) · 1.84 KB
/
nstx_queue.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "nstx.h"
static struct nstxqueue *qhead = NULL;
static int qlen = 0;
static int qtimeout = QUEUETIMEOUT;
struct nstxqueue *finditem (unsigned short id)
{
struct nstxqueue *ptr;
for (ptr = qhead; ptr; ptr = ptr->next)
if (ptr->id == id)
break;
return ptr;
}
void queueitem (unsigned short id, char *name, struct sockaddr_in *peer)
{
struct nstxqueue *ptr, *tmp;
if (finditem(id))
return;
qlen++;
ptr = malloc(sizeof(struct nstxqueue));
memset(ptr, 0, sizeof(struct nstxqueue));
if (!qhead)
qhead = ptr;
else {
for (tmp = qhead; tmp->next; tmp = tmp->next)
;
tmp->next = ptr;
}
ptr->id = id;
if (name)
strcpy(ptr->name, name);
if (peer)
memcpy(&ptr->peer, peer, sizeof(struct sockaddr_in));
ptr->timeout = time(NULL) + qtimeout;
}
void queueid (unsigned short id)
{
queueitem(id, NULL, NULL);
}
struct nstxqueue *dequeueitem (int id)
{
static struct nstxqueue *tmp = NULL, *ptr;
if (!qhead)
return NULL;
if (tmp)
free(tmp);
if ((id < 0) || (qhead->id == id))
{
tmp = qhead;
qhead = qhead->next;
qlen--;
}
else
{
ptr = qhead;
for (tmp = qhead->next; tmp; tmp = tmp->next)
{
if (tmp->id == id)
{
ptr->next = tmp->next;
qlen--;
break;
}
ptr = tmp;
}
}
return tmp;
}
void timeoutqueue (void (*timeoutfn)(struct nstxqueue *))
{
struct nstxqueue *ptr;
time_t now;
now = time(NULL);
while (qhead && (qhead->timeout <= now))
{
if (timeoutfn)
timeoutfn(qhead);
ptr = qhead;
qhead = qhead->next;
qlen--;
free(ptr);
}
}
int queuelen (void)
{
return qlen;
}
void qsettimeout (int timeout)
{
qtimeout = timeout;
}