From 57be99113715a6023164633556ea1de50ceae6e1 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 11 Jul 2013 22:18:17 +0000 Subject: [PATCH] -adding API to associated user context with a server's client to avoid ugly hacks, such as linked list searches and casting pointers to integers (thanks to Peter-Vogginger for forcing me to fix this) --- src/include/gnunet_server_lib.h | 46 +++++++++++++++++++++++++++++++ src/util/server.c | 49 +++++++++++++++++++++++++++++---- 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/include/gnunet_server_lib.h b/src/include/gnunet_server_lib.h index f7b526416..e9137154b 100644 --- a/src/include/gnunet_server_lib.h +++ b/src/include/gnunet_server_lib.h @@ -297,6 +297,52 @@ GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client, struct GNUNET_TIME_Relative timeout); + +/** + * Return user context associated with the given client. + * Note: you should probably use the macro (call without the underscore). + * + * @param client client to query + * @param size number of bytes in user context struct (for verification only) + * @return pointer to user context + */ +void * +GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client, + size_t size); + + +/** + * Set user context to be associated with the given client. + * Note: you should probably use the macro (call without the underscore). + * + * @param client client to query + * @param ptr pointer to user context + * @param size number of bytes in user context struct (for verification only) + */ +void +GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client, + void *ptr, + size_t size); + + +/** + * Return user context associated with the given client. + * + * @param client client to query + * @param type expected return type (i.e. 'struct Foo') + * @return pointer to user context of type 'type *'. + */ +#define GNUNET_SERVER_client_get_user_context(client,type) (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type)) + +/** + * Set user context to be associated with the given client. + * + * @param client client to query + * @param ptr pointer to user context + */ +#define GNUNET_SERVER_client_set_user_context(client,value) GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value)) + + /** * Disable the warning the server issues if a message is not acknowledged * in a timely fashion. Use this call if a client is intentionally delayed diff --git a/src/util/server.c b/src/util/server.c index f62bf8539..6dbb5dc23 100644 --- a/src/util/server.c +++ b/src/util/server.c @@ -238,6 +238,12 @@ struct GNUNET_SERVER_Client */ struct GNUNET_CONNECTION_Handle *connection; + /** + * User context value, manipulated using + * 'GNUNET_SERVER_client_{get/set}_user_context' functions. + */ + void *user_context; + /** * ID of task used to restart processing. */ @@ -285,6 +291,12 @@ struct GNUNET_SERVER_Client */ unsigned int suspended; + /** + * Last size given when user context was initialized; used for + * sanity check. + */ + size_t user_context_size; + /** * Are we currently in the "process_client_buffer" function (and * will hence restart the receive job on exit if suspended == 0 once @@ -327,15 +339,40 @@ struct GNUNET_SERVER_Client }; + /** - * Scheduler says our listen socket is ready. Process it! + * Return user context associated with the given client. + * Note: you should probably use the macro (call without the underscore). * - * @param cls handle to our server for which we are processing the listen - * socket - * @param tc reason why we are running right now + * @param client client to query + * @param size number of bytes in user context struct (for verification only) + * @return pointer to user context */ -static void -process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); +void * +GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client, + size_t size) +{ + GNUNET_assert (size == client->user_context_size); + return client->user_context; +} + + +/** + * Set user context to be associated with the given client. + * Note: you should probably use the macro (call without the underscore). + * + * @param client client to query + * @param ptr pointer to user context + * @param size number of bytes in user context struct (for verification only) + */ +void +GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client, + void *ptr, + size_t size) +{ + client->user_context_size = size; + client->user_context = ptr; +} /** -- 2.25.1