- added another callback closed() and use this as the current
destroyed_connection() has been used.
- the destroyed() event changes meaning to
"the connection object is about to be free'ed"
and the user can free the context memory.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/qb/qbipcs.h | 9 ++++++++-
lib/ipcs.c | 13 +++++++------
tests/bms.c | 8 +++++++-
tests/check_ipc.c | 1 +
4 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/include/qb/qbipcs.h b/include/qb/qbipcs.h
index 7d14ef5..3e175db 100644
--- a/include/qb/qbipcs.h
+++ b/include/qb/qbipcs.h
@@ -109,7 +109,13 @@ typedef void (*qb_ipcs_connection_created_fn) (qb_ipcs_connection_t
*c);
* @note if you return anything but 0 this function will be
* repeativily called (until 0 is returned).
*/
-typedef int32_t (*qb_ipcs_connection_destroyed_fn) (qb_ipcs_connection_t *c);
+typedef int32_t (*qb_ipcs_connection_closed_fn) (qb_ipcs_connection_t *c);
+
+/**
+ * This is called just before a connection is freed.
+ */
+typedef void (*qb_ipcs_connection_destroyed_fn) (qb_ipcs_connection_t *c);
+
/**
* This is the message processing calback.
* It is called with the message data.
@@ -121,6 +127,7 @@ struct qb_ipcs_service_handlers {
qb_ipcs_connection_accept_fn connection_accept;
qb_ipcs_connection_created_fn connection_created;
qb_ipcs_msg_process_fn msg_process;
+ qb_ipcs_connection_closed_fn connection_closed;
qb_ipcs_connection_destroyed_fn connection_destroyed;
};
diff --git a/lib/ipcs.c b/lib/ipcs.c
index 0b1d3d9..3152dfe 100644
--- a/lib/ipcs.c
+++ b/lib/ipcs.c
@@ -52,6 +52,7 @@ qb_ipcs_service_t* qb_ipcs_create(const char *name,
s->serv_fns.connection_accept = handlers->connection_accept;
s->serv_fns.connection_created = handlers->connection_created;
s->serv_fns.msg_process = handlers->msg_process;
+ s->serv_fns.connection_closed = handlers->connection_closed;
s->serv_fns.connection_destroyed = handlers->connection_destroyed;
qb_list_init(&s->connections);
@@ -362,13 +363,13 @@ void qb_ipcs_connection_ref_dec(struct qb_ipcs_connection *c)
free_it = qb_atomic_int_dec_and_test(&c->refcount);
if (free_it) {
qb_list_del(&c->list);
+ if (c->service->serv_fns.connection_destroyed) {
+ c->service->serv_fns.connection_destroyed(c);
+ }
c->service->funcs.disconnect(c);
if (c->receive_buf) {
free(c->receive_buf);
}
- if (c->context) {
- free(c->context);
- }
free(c);
}
}
@@ -393,13 +394,13 @@ void qb_ipcs_disconnect(struct qb_ipcs_connection *c)
}
if (c->state == QB_IPCS_CONNECTION_DOWN) {
res = 0;
- if (c->service->serv_fns.connection_destroyed) {
- res = c->service->serv_fns.connection_destroyed(c);
+ if (c->service->serv_fns.connection_closed) {
+ res = c->service->serv_fns.connection_closed(c);
}
if (res == 0) {
qb_ipcs_connection_ref_dec(c);
} else {
- /* ok, so they want the connection_destroyed()
+ /* ok, so they want the connection_closedd()
* function re-run */
rerun_job = (qb_loop_job_dispatch_fn)qb_ipcs_disconnect;
res = c->service->poll_fns.job_add(QB_LOOP_LOW, c,
diff --git a/tests/bms.c b/tests/bms.c
index 65010dc..c8a6c3b 100644
--- a/tests/bms.c
+++ b/tests/bms.c
@@ -69,7 +69,12 @@ static void s1_connection_created_fn(qb_ipcs_connection_t *c)
srv_stats.closed_connections);
}
-static int32_t s1_connection_destroyed_fn(qb_ipcs_connection_t *c)
+static void s1_connection_destroyed_fn(qb_ipcs_connection_t *c)
+{
+ printf("connection about to be freed\n");
+}
+
+static int32_t s1_connection_closed_fn(qb_ipcs_connection_t *c)
{
struct qb_ipcs_connection_stats stats;
struct qb_ipcs_stats srv_stats;
@@ -265,6 +270,7 @@ int32_t main(int32_t argc, char *argv[])
.connection_created = s1_connection_created_fn,
.msg_process = s1_msg_process_fn,
.connection_destroyed = s1_connection_destroyed_fn,
+ .connection_closed = s1_connection_closed_fn,
};
struct qb_ipcs_poll_handlers ph = {
.job_add = my_job_add,
diff --git a/tests/check_ipc.c b/tests/check_ipc.c
index ca20239..ebb138e 100644
--- a/tests/check_ipc.c
+++ b/tests/check_ipc.c
@@ -144,6 +144,7 @@ static void run_ipc_server(void)
.connection_created = NULL,
.msg_process = s1_msg_process_fn,
.connection_destroyed = NULL,
+ .connection_closed = NULL,
};
struct qb_ipcs_poll_handlers ph = {
--
1.7.3.1