From: Lennart Poettering Date: Tue, 19 Jul 2005 18:36:14 +0000 (+0000) Subject: * Change timeval arithmetic functions to use 64bit integers. This fixes some problems... X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=ac082e8eb624bc5ae0c19e91f412c8c5f12bfddf;p=catta * Change timeval arithmetic functions to use 64bit integers. This fixes some problems with long lived queries git-svn-id: file:///home/lennart/svn/public/avahi/trunk@170 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-common/Makefile.am b/avahi-common/Makefile.am index b1188cd..208aa94 100644 --- a/avahi-common/Makefile.am +++ b/avahi-common/Makefile.am @@ -41,7 +41,8 @@ noinst_HEADERS = \ noinst_PROGRAMS = \ strlst-test \ domain-test \ - alternative-test + alternative-test \ + timeval-test lib_LTLIBRARIES = \ libavahi-common.la @@ -80,3 +81,9 @@ domain_test_SOURCES = \ domain_test_CFLAGS = $(AM_CFLAGS) domain_test_LDADD = $(AM_LDADD) +timeval_test_SOURCES = \ + util.c util.h \ + timeval-test.c +timeval_test_CFLAGS = $(AM_CFLAGS) +timeval_test_LDADD = $(AM_LDADD) + diff --git a/avahi-common/timeval-test.c b/avahi-common/timeval-test.c new file mode 100644 index 0000000..a0392eb --- /dev/null +++ b/avahi-common/timeval-test.c @@ -0,0 +1,15 @@ +#include "util.h" + +int main(int argc, char *argv[]) { + + GTimeVal a = { 5, 5 }, b; + + b = a; + + g_message("%li.%li", a.tv_sec, a.tv_usec); + avahi_timeval_add(&a, -50); + + g_message("%li.%li", a.tv_sec, a.tv_usec); + + g_message("%lli", avahi_timeval_diff(&a, &b)); +} diff --git a/avahi-common/util.c b/avahi-common/util.c index baf5527..c9a3143 100644 --- a/avahi-common/util.c +++ b/avahi-common/util.c @@ -112,16 +112,56 @@ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) { return 0; } -glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { +AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { g_assert(a); g_assert(b); if (avahi_timeval_compare(a, b) < 0) - return avahi_timeval_diff(b, a); + return - avahi_timeval_diff(b, a); - return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; + return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; } +GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) { + AvahiUsec u; + g_assert(a); + + u = usec + a->tv_usec; + + if (u < 0) { + a->tv_usec = (glong) (1000000 + (u % 1000000)); + a->tv_sec += (glong) (-1 + (u / 1000000)); + } else { + a->tv_usec = (glong) (u % 1000000); + a->tv_sec += (glong) (u / 1000000); + } + + return a; +} + +AvahiUsec avahi_age(const GTimeVal *a) { + GTimeVal now; + + g_assert(a); + + g_get_current_time(&now); + + return avahi_timeval_diff(&now, a); +} + +GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) { + g_assert(tv); + + g_get_current_time(tv); + + if (msec) + avahi_timeval_add(tv, (AvahiUsec) msec*1000); + + if (jitter) + avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000); + + return tv; +} gint avahi_set_cloexec(gint fd) { gint n; @@ -169,30 +209,6 @@ gint avahi_wait_for_write(gint fd) { return 0; } -GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) { - g_assert(tv); - - g_get_current_time(tv); - - if (msec) - g_time_val_add(tv, msec*1000); - - if (jitter) - g_time_val_add(tv, g_random_int_range(0, jitter) * 1000); - - return tv; -} - -glong avahi_age(const GTimeVal *a) { - GTimeVal now; - - g_assert(a); - - g_get_current_time(&now); - - return avahi_timeval_diff(&now, a); -} - /* Read the first label from string *name, unescape "\" and write it to dest */ gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) { guint i = 0; diff --git a/avahi-common/util.h b/avahi-common/util.h index d590423..731a5a1 100644 --- a/avahi-common/util.h +++ b/avahi-common/util.h @@ -28,20 +28,22 @@ AVAHI_C_DECL_BEGIN +typedef gint64 AvahiUsec; + gchar *avahi_normalize_name(const gchar *s); /* g_free() the result! */ gchar *avahi_get_host_name(void); /* g_free() the result! */ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b); -glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b); +AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b); +GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec); + +AvahiUsec avahi_age(const GTimeVal *a); +GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter); gint avahi_set_cloexec(gint fd); gint avahi_set_nonblock(gint fd); gint avahi_wait_for_write(gint fd); -GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter); - -glong avahi_age(const GTimeVal *a); - gboolean avahi_domain_equal(const gchar *a, const gchar *b); gint avahi_binary_domain_cmp(const gchar *a, const gchar *b); diff --git a/avahi-core/cache.c b/avahi-core/cache.c index 52304a8..0a39194 100644 --- a/avahi-core/cache.c +++ b/avahi-core/cache.c @@ -161,7 +161,7 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) { if (e->state == AVAHI_CACHE_FINAL) { remove_entry(e->cache, e); -/* avahi_log_debug("Removing entry from cache due to expiration (%s)", txt); */ +/* avahi_log_debug("Removing entry from cache due to expiration (%s)", txt); */ } else { guint percent = 0; @@ -193,7 +193,7 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) { /* Request a cache update, if we are subscribed to this entry */ if (avahi_is_subscribed(e->cache->server, e->cache->interface, e->record->key)) { -/* avahi_log_debug("Requesting cache entry update at %i%% for %s.", percent, txt); */ +/* avahi_log_debug("Requesting cache entry update at %i%% for %s.", percent, txt); */ avahi_interface_post_query(e->cache->interface, e->record->key, TRUE); } @@ -218,20 +218,19 @@ static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) { g_assert(c); g_assert(e); g_assert(percent > 0 && percent <= 100); - gdouble usec; + AvahiUsec usec; /* gchar *txt; */ - - usec = (gdouble) e->record->ttl * 10000; + usec = ((AvahiUsec) e->record->ttl) * 10000; /* 2% jitter */ - usec = g_random_double_range(usec*percent, usec*(percent+2)); -/* g_message("next expiry: %g (%s)", usec / 1000000.0, txt = avahi_record_to_string(e->record)); */ + usec = (AvahiUsec) g_random_double_range(usec*percent, usec*(percent+2)); +/* g_message("next expiry: %lli (%s)", usec / 1000000, txt = avahi_record_to_string(e->record)); */ /* g_free(txt); */ e->expiry = e->timestamp; - g_time_val_add(&e->expiry, (glong) usec); - + avahi_timeval_add(&e->expiry, usec); + /* g_message("wake up in +%lu seconds", e->expiry.tv_sec - e->timestamp.tv_sec); */ update_time_event(c, e); @@ -243,7 +242,7 @@ static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) { e->state = AVAHI_CACHE_FINAL; g_get_current_time(&e->expiry); - g_time_val_add(&e->expiry, 1000000); /* 1s */ + avahi_timeval_add(&e->expiry, 1000000); /* 1s */ update_time_event(c, e); } @@ -253,8 +252,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con g_assert(c); g_assert(r && r->ref >= 1); -/* avahi_log_debug("cache update: %s", (txt = avahi_record_to_string(r))); */ -/* g_free(txt); */ +/* txt = avahi_record_to_string(r); */ if (r->ttl == 0) { /* This is a goodbye request */ @@ -278,7 +276,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con /* For unique entries drop all entries older than one second */ for (e = first; e; e = e->by_key_next) { - glong t; + AvahiUsec t; t = avahi_timeval_diff(&now, &e->timestamp); @@ -306,10 +304,12 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con avahi_record_unref(e->record); e->record = avahi_record_ref(r); +/* avahi_log_debug("cache: updating %s", txt); */ + } else { /* No entry found, therefore we create a new one */ -/* avahi_log_debug("couldn't find matching cache entry"); */ +/* avahi_log_debug("cache: couldn't find matching cache entry for %s", txt); */ if (c->n_entries >= AVAHI_MAX_CACHE_ENTRIES) return; @@ -338,6 +338,8 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con e->state = AVAHI_CACHE_VALID; e->cache_flush = cache_flush; } + +/* g_free(txt); */ } static void dump_callback(gpointer key, gpointer data, gpointer userdata) { @@ -364,7 +366,7 @@ void avahi_cache_dump(AvahiCache *c, FILE *f) { gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { GTimeVal now; - guint age; + AvahiUsec age; g_assert(c); g_assert(e); @@ -373,7 +375,7 @@ gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { age = avahi_timeval_diff(&now, &e->timestamp)/1000000; -/* avahi_log_debug("age: %u, ttl/2: %u", age, e->record->ttl); */ +/* avahi_log_debug("age: %lli, ttl/2: %u", age, e->record->ttl); */ return age >= e->record->ttl/2; } diff --git a/avahi-core/iface.c b/avahi-core/iface.c index fb491c9..a4c7de8 100644 --- a/avahi-core/iface.c +++ b/avahi-core/iface.c @@ -429,7 +429,6 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { addr->prefix_len = ifaddrmsg->ifa_prefixlen; update_address_rr(m, addr, FALSE); - check_interface_relevant(m, i); } else { AvahiInterfaceAddress *addr; @@ -438,10 +437,10 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { update_address_rr(m, addr, TRUE); free_address(m, addr); - - check_interface_relevant(m, i); } - + + check_interface_relevant(m, i); + } else if (n->nlmsg_type == NLMSG_DONE) { if (m->list == LIST_IFACE) {