comparison mcabber/src/events.c @ 751:4a7271e69694

Avoid reusing events ids
author Mikael Berthe <mikael@lilotux.net>
date Mon, 13 Mar 2006 23:41:29 +0100
parents 3a76c2d73606
children 584db5f21e43
comparison
equal deleted inserted replaced
750:938a8791658c 751:4a7271e69694
23 #include "events.h" 23 #include "events.h"
24 #include "logprint.h" 24 #include "logprint.h"
25 25
26 static GSList *evs_list; // Events list 26 static GSList *evs_list; // Events list
27 27
28 static eviqs *evs_find(const char *evid);
28 29
29 // evs_new(type, timeout) 30 // evs_new(type, timeout)
30 // Create an events structure. 31 // Create an events structure.
31 eviqs *evs_new(guint8 type, time_t timeout) 32 eviqs *evs_new(guint8 type, time_t timeout)
32 { 33 {
33 static guint evs_idn; 34 static guint evs_idn;
34 eviqs *new_evs; 35 eviqs *new_evs;
35 time_t now_t; 36 time_t now_t;
37 char *stridn;
36 38
37 if (!++evs_idn) 39 if (!++evs_idn)
38 evs_idn = 1; 40 evs_idn = 1;
39 /* TODO: check for wrapping, we shouldn't reuse ids */ 41 /* Check for wrapping, we shouldn't reuse ids */
42 stridn = g_strdup_printf("%d", evs_idn);
43 if (evs_find(stridn)) {
44 g_free(stridn);
45 // We could try another id but for now giving up should be fine...
46 return NULL;
47 }
40 48
41 new_evs = g_new0(eviqs, 1); 49 new_evs = g_new0(eviqs, 1);
42 time(&now_t); 50 time(&now_t);
43 new_evs->ts_create = now_t; 51 new_evs->ts_create = now_t;
44 if (timeout) 52 if (timeout)
45 new_evs->ts_expire = now_t + timeout; 53 new_evs->ts_expire = now_t + timeout;
46 new_evs->type = type; 54 new_evs->type = type;
47 new_evs->id = g_strdup_printf("%d", evs_idn); 55 new_evs->id = stridn;
48 56
49 evs_list = g_slist_append(evs_list, new_evs); 57 evs_list = g_slist_append(evs_list, new_evs);
50 return new_evs; 58 return new_evs;
51 } 59 }
52 60