1152
|
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
2 /*
|
1192
|
3 $Id: queue.c 1202 2000-12-04 06:22:48Z robflynn $
|
1152
|
4 $Log$
|
1192
|
5 Revision 1.2 2000/12/04 06:22:48 robflynn
|
|
6 Da numba one stunna!
|
|
7
|
1152
|
8 Revision 1.1 2000/11/28 02:22:42 warmenhoven
|
|
9 icq. whoop de doo
|
|
10
|
|
11 Revision 1.9 2000/07/10 01:31:17 bills
|
|
12 oops - removed #define LIST_TRACE and #define QUEUE_DEBUG
|
|
13
|
|
14 Revision 1.8 2000/07/10 01:26:56 bills
|
|
15 added more trace messages, reworked packet delete handling: now happens
|
|
16 during _icq_UDEQueueItemFree rather than during icq_UDPQueueDelSeq - fixes
|
|
17 memory leak
|
|
18
|
|
19 Revision 1.7 2000/07/09 22:07:37 bills
|
|
20 use new list_free
|
|
21
|
|
22 Revision 1.6 2000/06/25 16:30:05 denis
|
|
23 Some sanity checks were added to icq_UDPQueueDelete() and
|
|
24 icq_UDPQueueFree()
|
|
25
|
|
26 Revision 1.5 2000/05/10 19:06:59 denis
|
|
27 UDP outgoing packet queue was implemented.
|
|
28
|
|
29 Revision 1.4 2000/03/30 14:15:28 denis
|
|
30 Fixed FreeBSD warning about obsolete malloc.h header.
|
|
31
|
|
32 Revision 1.3 2000/01/16 03:59:10 bills
|
|
33 reworked list code so list_nodes don't need to be inside item structures,
|
|
34 removed strlist code and replaced with generic list calls
|
|
35
|
|
36 Revision 1.2 1999/09/29 17:06:47 denis
|
|
37 ICQLINK compatibility added.
|
|
38
|
|
39 Revision 1.1 1999/07/16 12:12:13 denis
|
|
40 Initial support for outgoing packet queue added.
|
|
41
|
|
42 */
|
|
43
|
|
44 #include <stdlib.h>
|
|
45 #include <time.h>
|
|
46
|
|
47 #include "queue.h"
|
|
48 #include "list.h"
|
|
49
|
|
50 void icq_UDPQueueNew(ICQLINK *link)
|
|
51 {
|
|
52 link->icq_UDPQueue = list_new();
|
|
53 link->icq_UDPExpireInterval = 15; /* expire interval = 15 sec */
|
|
54 }
|
|
55
|
|
56 void icq_UDPQueuePut(ICQLINK *link, icq_Packet *p, int attempt)
|
|
57 {
|
|
58 icq_UDPQueueItem *ptr = (icq_UDPQueueItem*)malloc(sizeof(icq_UDPQueueItem));
|
|
59 #ifdef QUEUE_DEBUG
|
|
60 printf("icq_UDPQueuePut(seq=0x%04X, cmd=0x%04X)\n", icq_PacketReadUDPOutSeq1(p),
|
|
61 icq_PacketReadUDPOutCmd(p));
|
|
62 #endif
|
|
63 ptr->attempts = attempt;
|
|
64 ptr->expire = time(0L)+link->icq_UDPExpireInterval;
|
|
65 ptr->pack = p;
|
|
66 #ifdef QUEUE_DEBUG
|
|
67 printf("enqueuing queueitem %p\n", ptr);
|
|
68 #endif
|
|
69 list_enqueue(link->icq_UDPQueue, ptr);
|
|
70 }
|
|
71
|
|
72 icq_Packet *icq_UDPQueueGet(ICQLINK *link)
|
|
73 {
|
|
74 icq_UDPQueueItem *ptr = (icq_UDPQueueItem*)list_first(link->icq_UDPQueue);
|
|
75 icq_Packet *pack = 0L;
|
|
76 if(ptr)
|
|
77 {
|
|
78 pack = ptr->pack;
|
|
79 list_remove(link->icq_UDPQueue, (list_node*)ptr);
|
|
80 }
|
|
81 #ifdef QUEUE_DEBUG
|
|
82 if(pack)
|
|
83 printf("icq_UDPQueueGet(cmd=0x%04X)\n", icq_PacketReadUDPOutCmd(pack));
|
|
84 #endif
|
|
85 return pack;
|
|
86 }
|
|
87
|
|
88 icq_Packet *icq_UDPQueuePeek(ICQLINK *link)
|
|
89 {
|
|
90 icq_UDPQueueItem *ptr = (icq_UDPQueueItem*)list_first(link->icq_UDPQueue);
|
|
91 if(ptr)
|
|
92 return ptr->pack;
|
|
93 else
|
|
94 return 0L;
|
|
95 }
|
|
96
|
|
97 void _icq_UDPQueueItemFree(void *p)
|
|
98 {
|
|
99 icq_UDPQueueItem *pitem=(icq_UDPQueueItem *)p;
|
|
100
|
|
101 #ifdef QUEUE_DEBUG
|
|
102 printf("_icq_UDPQueueItemFree(%p)\n", p);
|
|
103 #endif
|
|
104
|
|
105 if (pitem->pack)
|
|
106 icq_PacketDelete(pitem->pack);
|
|
107
|
|
108 free(p);
|
|
109 }
|
|
110
|
|
111 /* Frees the queue and dispose it */
|
|
112 void icq_UDPQueueDelete(ICQLINK *link)
|
|
113 {
|
|
114 #ifdef QUEUE_DEBUG
|
|
115 printf("icq_UDPQueueDelete\n");
|
|
116 #endif
|
|
117 if(link->icq_UDPQueue)
|
|
118 {
|
|
119 list_delete(link->icq_UDPQueue, _icq_UDPQueueItemFree);
|
|
120 link->icq_UDPQueue = 0;
|
|
121 }
|
|
122 }
|
|
123
|
|
124 /* Only frees the queue */
|
|
125 void icq_UDPQueueFree(ICQLINK *link)
|
|
126 {
|
|
127 #ifdef QUEUE_DEBUG
|
|
128 printf("icq_UDPQueueFree\n");
|
|
129 #endif
|
|
130 if(link->icq_UDPQueue)
|
|
131 list_free(link->icq_UDPQueue, _icq_UDPQueueItemFree);
|
|
132 }
|
|
133
|
|
134 int icq_UDPQueueFindSeq(void *p, va_list data)
|
|
135 {
|
1192
|
136 WORD seq=va_arg(data, int);
|
1152
|
137 return icq_PacketReadUDPOutSeq1(((icq_UDPQueueItem *)p)->pack) == seq;
|
|
138 }
|
|
139
|
|
140 void icq_UDPQueueDelSeq(ICQLINK *link, WORD seq)
|
|
141 {
|
|
142 icq_UDPQueueItem *ptr;
|
|
143 #ifdef QUEUE_DEBUG
|
|
144 printf("icq_UDPQueueDelSeq(seq=0x%04X", seq);
|
|
145 #endif
|
|
146 ptr = list_traverse(link->icq_UDPQueue, icq_UDPQueueFindSeq, seq);
|
|
147 if(ptr)
|
|
148 {
|
|
149 #ifdef QUEUE_DEBUG
|
|
150 printf(", cmd=0x%04X",icq_PacketReadUDPOutCmd(ptr->pack));
|
|
151 #endif
|
|
152 list_remove(link->icq_UDPQueue, ptr);
|
|
153 _icq_UDPQueueItemFree(ptr);
|
|
154 }
|
|
155 #ifdef QUEUE_DEBUG
|
|
156 printf(")\n");
|
|
157 #endif
|
|
158 }
|
|
159
|
|
160 long icq_UDPQueueInterval(ICQLINK *link)
|
|
161 {
|
|
162 long interval;
|
|
163 icq_UDPQueueItem *ptr = (icq_UDPQueueItem*)list_first(link->icq_UDPQueue);
|
|
164 if(ptr)
|
|
165 {
|
|
166 interval = ptr->expire - time(0L);
|
|
167 return interval>=0?interval:0;
|
|
168 }
|
|
169 return -1;
|
|
170 }
|