diff options
Diffstat (limited to 'resource.c')
-rw-r--r-- | resource.c | 34 |
1 files changed, 28 insertions, 6 deletions
@@ -73,6 +73,20 @@ static int object_v1_get(struct client *c, struct resource *r) return 1; } +static void object_v1_close(struct client *c, struct resource *r) +{ + struct resource_object *obj = r->data; + + // Remove this client from the object list + resource_obj_del_client(obj, c); +} + +static void object_v1_update_open(struct client *c, struct resource *r) +{ + // Keep track of the latest updater + r->updater = c; +} + // Update all attached clients with the new resource object data static int object_v1_update(struct client *c, struct resource *r, const char *m) { @@ -80,6 +94,12 @@ static int object_v1_update(struct client *c, struct resource *r, const char *m) GString *string; char *n; + // Only allow the latest updater to provide updates, in case we + // end up with multiple connections. This also allows us to quickly + // release the resources of a stale connection. + if (r->updater != c) + return -1; + // Remove any trailing whitespace. n = g_strchomp(g_strdup(m)); @@ -97,18 +117,20 @@ static int object_v1_update(struct client *c, struct resource *r, const char *m) return 0; } -static void object_v1_close(struct client *c, struct resource *r) +static void object_v1_update_close(struct client *c, struct resource *r) { - struct resource_object *obj = r->data; - - // Remove this client from the object list - resource_obj_del_client(obj, c); + // If the updater goes away, clear the data so we don't serve + // stale data. + if (r->updater == c) + r->updater = NULL; } static const struct resource_ops resource_v1_ops = { .get = object_v1_get, - .update = object_v1_update, .close = object_v1_close, + .update_open = object_v1_update_open, + .update = object_v1_update, + .update_close = object_v1_update_close, }; static struct resource resource_position1 = { |