From d8ff72387d5474e56228a3403dc91354d9916189 Mon Sep 17 00:00:00 2001 From: Michael Grunder Date: Wed, 29 Jul 2020 11:53:03 -0700 Subject: Move SSL management to a distinct private pointer. (#855) We need to allow our users to use redisContext->privdata as context for any RESP3 PUSH messages, which means we can't use it for managing SSL connections. Bulletpoints: * Create a secondary redisContext member for internal use only called privctx and rename the redisContextFuncs->free_privdata accordingly. * Adds a `free_privdata` function pointer so the user can tie allocated memory to the lifetime of a redisContext (like they can already do with redisAsyncContext) * Enables SSL tests in .travis.yml --- examples/example-push.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/example-push.c b/examples/example-push.c index 23dbd17..2d4ab4d 100644 --- a/examples/example-push.c +++ b/examples/example-push.c @@ -97,6 +97,13 @@ void pushReplyHandler(void *privdata, void *r) { freeReplyObject(reply); } +/* We aren't actually freeing anything here, but it is included to show that we can + * have hiredis call our data destructor when freeing the context */ +void privdata_dtor(void *privdata) { + unsigned int *icount = privdata; + printf("privdata_dtor(): In context privdata dtor (invalidations: %u)\n", *icount); +} + int main(int argc, char **argv) { unsigned int j, invalidations = 0; redisContext *c; @@ -108,6 +115,16 @@ int main(int argc, char **argv) { redisOptions o = {0}; REDIS_OPTIONS_SET_TCP(&o, hostname, port); + /* Set our context privdata to the address of our invalidation counter. Each + * time our PUSH handler is called, hiredis will pass the privdata for context. + * + * This could also be done after we create the context like so: + * + * c->privdata = &invalidations; + * c->free_privdata = privdata_dtor; + */ + REDIS_OPTIONS_SET_PRIVDATA(&o, &invalidations, privdata_dtor); + /* Set our custom PUSH message handler */ o.push_cb = pushReplyHandler; @@ -118,10 +135,6 @@ int main(int argc, char **argv) { /* Enable RESP3 and turn on client tracking */ enableClientTracking(c); - /* Set our context privdata to the address of our invalidation counter. Each - * time our PUSH handler is called, hiredis will pass the privdata for context */ - c->privdata = &invalidations; - /* Set some keys and then read them back. Once we do that, Redis will deliver * invalidation push messages whenever the key is modified */ for (j = 0; j < KEY_COUNT; j++) { -- cgit v1.2.3