diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-07-11 22:18:17 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-07-11 22:18:17 +0000 |
commit | 57be99113715a6023164633556ea1de50ceae6e1 (patch) | |
tree | be1c6003207ec50b17483267f21ad10cbc85d976 | |
parent | 4f0cfaff0e3e5cc0c59801279714cb0467fc0385 (diff) | |
download | gnunet-57be99113715a6023164633556ea1de50ceae6e1.tar.gz gnunet-57be99113715a6023164633556ea1de50ceae6e1.zip |
-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)
-rw-r--r-- | src/include/gnunet_server_lib.h | 46 | ||||
-rw-r--r-- | 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, | |||
297 | struct GNUNET_TIME_Relative timeout); | 297 | struct GNUNET_TIME_Relative timeout); |
298 | 298 | ||
299 | 299 | ||
300 | |||
301 | /** | ||
302 | * Return user context associated with the given client. | ||
303 | * Note: you should probably use the macro (call without the underscore). | ||
304 | * | ||
305 | * @param client client to query | ||
306 | * @param size number of bytes in user context struct (for verification only) | ||
307 | * @return pointer to user context | ||
308 | */ | ||
309 | void * | ||
310 | GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client, | ||
311 | size_t size); | ||
312 | |||
313 | |||
314 | /** | ||
315 | * Set user context to be associated with the given client. | ||
316 | * Note: you should probably use the macro (call without the underscore). | ||
317 | * | ||
318 | * @param client client to query | ||
319 | * @param ptr pointer to user context | ||
320 | * @param size number of bytes in user context struct (for verification only) | ||
321 | */ | ||
322 | void | ||
323 | GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client, | ||
324 | void *ptr, | ||
325 | size_t size); | ||
326 | |||
327 | |||
328 | /** | ||
329 | * Return user context associated with the given client. | ||
330 | * | ||
331 | * @param client client to query | ||
332 | * @param type expected return type (i.e. 'struct Foo') | ||
333 | * @return pointer to user context of type 'type *'. | ||
334 | */ | ||
335 | #define GNUNET_SERVER_client_get_user_context(client,type) (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type)) | ||
336 | |||
337 | /** | ||
338 | * Set user context to be associated with the given client. | ||
339 | * | ||
340 | * @param client client to query | ||
341 | * @param ptr pointer to user context | ||
342 | */ | ||
343 | #define GNUNET_SERVER_client_set_user_context(client,value) GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value)) | ||
344 | |||
345 | |||
300 | /** | 346 | /** |
301 | * Disable the warning the server issues if a message is not acknowledged | 347 | * Disable the warning the server issues if a message is not acknowledged |
302 | * in a timely fashion. Use this call if a client is intentionally delayed | 348 | * 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 | |||
@@ -239,6 +239,12 @@ struct GNUNET_SERVER_Client | |||
239 | struct GNUNET_CONNECTION_Handle *connection; | 239 | struct GNUNET_CONNECTION_Handle *connection; |
240 | 240 | ||
241 | /** | 241 | /** |
242 | * User context value, manipulated using | ||
243 | * 'GNUNET_SERVER_client_{get/set}_user_context' functions. | ||
244 | */ | ||
245 | void *user_context; | ||
246 | |||
247 | /** | ||
242 | * ID of task used to restart processing. | 248 | * ID of task used to restart processing. |
243 | */ | 249 | */ |
244 | GNUNET_SCHEDULER_TaskIdentifier restart_task; | 250 | GNUNET_SCHEDULER_TaskIdentifier restart_task; |
@@ -286,6 +292,12 @@ struct GNUNET_SERVER_Client | |||
286 | unsigned int suspended; | 292 | unsigned int suspended; |
287 | 293 | ||
288 | /** | 294 | /** |
295 | * Last size given when user context was initialized; used for | ||
296 | * sanity check. | ||
297 | */ | ||
298 | size_t user_context_size; | ||
299 | |||
300 | /** | ||
289 | * Are we currently in the "process_client_buffer" function (and | 301 | * Are we currently in the "process_client_buffer" function (and |
290 | * will hence restart the receive job on exit if suspended == 0 once | 302 | * will hence restart the receive job on exit if suspended == 0 once |
291 | * we are done?). If this is set, then "receive_done" will | 303 | * we are done?). If this is set, then "receive_done" will |
@@ -327,15 +339,40 @@ struct GNUNET_SERVER_Client | |||
327 | }; | 339 | }; |
328 | 340 | ||
329 | 341 | ||
342 | |||
330 | /** | 343 | /** |
331 | * Scheduler says our listen socket is ready. Process it! | 344 | * Return user context associated with the given client. |
345 | * Note: you should probably use the macro (call without the underscore). | ||
332 | * | 346 | * |
333 | * @param cls handle to our server for which we are processing the listen | 347 | * @param client client to query |
334 | * socket | 348 | * @param size number of bytes in user context struct (for verification only) |
335 | * @param tc reason why we are running right now | 349 | * @return pointer to user context |
336 | */ | 350 | */ |
337 | static void | 351 | void * |
338 | process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | 352 | GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client, |
353 | size_t size) | ||
354 | { | ||
355 | GNUNET_assert (size == client->user_context_size); | ||
356 | return client->user_context; | ||
357 | } | ||
358 | |||
359 | |||
360 | /** | ||
361 | * Set user context to be associated with the given client. | ||
362 | * Note: you should probably use the macro (call without the underscore). | ||
363 | * | ||
364 | * @param client client to query | ||
365 | * @param ptr pointer to user context | ||
366 | * @param size number of bytes in user context struct (for verification only) | ||
367 | */ | ||
368 | void | ||
369 | GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client, | ||
370 | void *ptr, | ||
371 | size_t size) | ||
372 | { | ||
373 | client->user_context_size = size; | ||
374 | client->user_context = ptr; | ||
375 | } | ||
339 | 376 | ||
340 | 377 | ||
341 | /** | 378 | /** |