Reference counting for client struct as well.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 23 Nov 2007 14:58:09 +0000 (15:58 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 23 Nov 2007 14:58:09 +0000 (15:58 +0100)
lib/client.c
lib/network.c
src/ctrlproxy.h
src/redirect.c

index e86fef2f7fd3178cc2d16149664b2060b0e353e8..c0f42bd4090cd5e780d6e5161722226eb8c184fb 100644 (file)
@@ -251,9 +251,10 @@ static void free_pending_line(void *_line, void *userdata)
 void disconnect_client(struct client *c, const char *reason) 
 {
        g_assert(c != NULL);
+       c->connected = FALSE;
        g_assert(c->incoming != NULL);
 
-       g_io_channel_ref(c->incoming);
+       g_io_channel_unref(c->incoming);
        g_source_remove(c->incoming_id);
        if (c->outgoing_id)
                g_source_remove(c->outgoing_id);
@@ -281,6 +282,12 @@ void disconnect_client(struct client *c, const char *reason)
        if (c->exit_on_close) 
                exit(0);
 
+       client_unref(c);
+}
+
+static void free_client(struct client *c)
+{
+       g_assert(c->connected == FALSE);
        g_free(c->charset);
        g_free(c->description);
        free_network_state(c->state);
@@ -787,6 +794,10 @@ struct client *client_ref(struct client *c)
 
 void client_unref(struct client *c) 
 {
-       if (c != NULL)
-               c->references--;
+       if (c == NULL)
+               return;
+
+       c->references--;
+       if (c->references == 0)
+               free_client(c);
 }
index 63487b1e0713ec60b82e8c413d9d5d9318fbb50d..2e259c4fd481e63ae61e1905f96b57856944afb0 100644 (file)
@@ -1299,30 +1299,6 @@ gboolean disconnect_network(struct network *s)
        return close_server(s);
 }
 
-/**
- * Check whether a client is still associated with a network.
- *
- * The client pointer does not have to point to a valid piece of memory.
- *
- * @param s Network the client should be associated with.
- * @param c The client.
- * @return Whether the client is still associated with the network.
- */
-gboolean verify_client(const struct network *s, const struct client *c)
-{
-       GList *gl;
-
-       g_assert(s);
-       g_assert(c);
-       
-       for (gl = s->clients; gl; gl = gl->next) {
-               struct client *nc = (struct client *)gl->data;
-               if (c == nc) return 1;
-       }
-
-       return 0;
-}
-
 /**
  * Register a new virtual network type.
  *
index 17074606440111010ec6f55d8d0afd73f9e3b1c8..6dcb7d2149799517c7d76ebee67090192576245d 100644 (file)
@@ -97,8 +97,6 @@ G_MODULE_EXPORT void register_save_config_notify(config_save_notify_fn fn);
 
 /* util.c */
 G_MODULE_EXPORT char *list_make_string(GList *);
-G_MODULE_EXPORT int verify_client(const struct network *s, 
-                                                                 const struct client *c);
 G_MODULE_EXPORT int str_rfc1459cmp(const char *a, const char *b);
 G_MODULE_EXPORT int str_strictrfc1459cmp(const char *a, const char *b);
 G_MODULE_EXPORT int str_asciicmp(const char *a, const char *b);
index 845924ad71f55bc8efeab1d8c192a85200c2120d..85f8695e3751c58ff89e99c8acc0d88dfae37f7b 100644 (file)
@@ -570,7 +570,7 @@ gboolean redirect_response(struct network *network, struct line *l)
                        is_reply(s->query->end_replies, n))) {
                        
                        /* Send to client that queried, if that client still exists */
-                       if (s->client != NULL && verify_client(s->network, s->client)) {
+                       if (s->client != NULL) {
                                c = s->client;
                                client_send_line(s->client, l);
                        }
@@ -579,6 +579,7 @@ gboolean redirect_response(struct network *network, struct line *l)
                                /* Remove from stack */
                                if (p == NULL)stack = s->next;  
                                else p->next = s->next;
+                               client_unref(s->client);
                                g_free(s);
                        }
 
@@ -635,6 +636,7 @@ void redirect_clear(const struct network *net)
                if (p == NULL)stack = q->next;  
                else p->next = q->next;
                n = q->next;
+               client_unref(q->client);
                g_free(q);
                q = n;
        }
@@ -674,7 +676,7 @@ static int handle_default(const struct line *l, const struct network *n,
        g_assert(n != NULL);
        g_assert(q != NULL);
        s->network = n;
-       s->client = c;
+       s->client = client_ref(c);
        s->time = time(NULL);
        s->query = q;
        s->next = stack;