aboutsummaryrefslogtreecommitdiff
path: root/src/include/gnunet_service_lib.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/gnunet_service_lib.h')
-rw-r--r--src/include/gnunet_service_lib.h132
1 files changed, 111 insertions, 21 deletions
diff --git a/src/include/gnunet_service_lib.h b/src/include/gnunet_service_lib.h
index ad238a0fa..0ad4fcc3c 100644
--- a/src/include/gnunet_service_lib.h
+++ b/src/include/gnunet_service_lib.h
@@ -18,7 +18,19 @@
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20 20
21
22#if ! defined (__GNUNET_UTIL_LIB_H_INSIDE__)
23#error "Only <gnunet_util_lib.h> can be included directly."
24#endif
25
21/** 26/**
27 * @addtogroup libgnunetutil
28 * Multi-function utilities library for GNUnet programs
29 * @{
30 *
31 * @addtogroup networking
32 * @{
33 *
22 * @author Christian Grothoff 34 * @author Christian Grothoff
23 * 35 *
24 * @file 36 * @file
@@ -43,8 +55,8 @@ extern "C"
43#endif 55#endif
44#endif 56#endif
45 57
58#include "gnunet_util_lib.h"
46#include "gnunet_configuration_lib.h" 59#include "gnunet_configuration_lib.h"
47#include "gnunet_mq_lib.h"
48 60
49 61
50/** 62/**
@@ -191,7 +203,6 @@ GNUNET_SERVICE_start (const char *service_name,
191void 203void
192GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Handle *srv); 204GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Handle *srv);
193 205
194
195/** 206/**
196 * Creates the "main" function for a GNUnet service. You 207 * Creates the "main" function for a GNUnet service. You
197 * should almost always use the #GNUNET_SERVICE_MAIN macro 208 * should almost always use the #GNUNET_SERVICE_MAIN macro
@@ -246,6 +257,56 @@ GNUNET_SERVICE_run_ (int argc,
246 257
247 258
248/** 259/**
260 * Registers the GNUnet service to be scheduled as part of a monilithic
261 * libgnunet.
262 * You should almost always use the #GNUNET_SERVICE_MAIN macro
263 * instead of calling this function directly.
264 *
265 * The function will launch the service with the name @a service_name
266 * using the @a service_options to configure its shutdown
267 * behavior. Once the service is ready, the @a init_cb will be called
268 * for service-specific initialization. @a init_cb will be given the
269 * service handler which can be used to control the service's
270 * availability. When clients connect or disconnect, the respective
271 * @a connect_cb or @a disconnect_cb functions will be called. For
272 * messages received from the clients, the respective @a handlers will
273 * be invoked; for the closure of the handlers we use the return value
274 * from the @a connect_cb invocation of the respective client.
275 *
276 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
277 * message to receive further messages from this client. If
278 * #GNUNET_SERVICE_client_continue() is not called within a short
279 * time, a warning will be logged. If delays are expected, services
280 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
281 * disable the warning.
282 *
283 * Clients sending invalid messages (based on @a handlers) will be
284 * dropped. Additionally, clients can be dropped at any time using
285 * #GNUNET_SERVICE_client_drop().
286 *
287 * @param service_name name of the service to run
288 * @param options options controlling shutdown of the service
289 * @param service_init_cb function to call once the service is ready
290 * @param connect_cb function to call whenever a client connects
291 * @param disconnect_cb function to call whenever a client disconnects
292 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
293 * @param handlers NULL-terminated array of message handlers for the service,
294 * the closure will be set to the value returned by
295 * the @a connect_cb for the respective connection
296 * @return 0 on success, non-zero on error
297 */
298int
299GNUNET_SERVICE_register_ (
300 const char *service_name,
301 enum GNUNET_SERVICE_Options options,
302 GNUNET_SERVICE_InitCallback service_init_cb,
303 GNUNET_SERVICE_ConnectHandler connect_cb,
304 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
305 void *cls,
306 const struct GNUNET_MQ_MessageHandler *handlers);
307
308
309/**
249 * Creates the "main" function for a GNUnet service. You 310 * Creates the "main" function for a GNUnet service. You
250 * MUST use this macro to define GNUnet services (except 311 * MUST use this macro to define GNUnet services (except
251 * for ARM, which MUST NOT use the macro). The reason is 312 * for ARM, which MUST NOT use the macro). The reason is
@@ -275,8 +336,8 @@ GNUNET_SERVICE_run_ (int argc,
275 * #GNUNET_SERVICE_client_drop(). 336 * #GNUNET_SERVICE_client_drop().
276 * 337 *
277 * @param service_name name of the service to run 338 * @param service_name name of the service to run
278 * @param options options controlling shutdown of the service 339 * @param service_options options controlling shutdown of the service
279 * @param service_init_cb function to call once the service is ready 340 * @param init_cb function to call once the service is ready
280 * @param connect_cb function to call whenever a client connects 341 * @param connect_cb function to call whenever a client connects
281 * @param disconnect_cb function to call whenever a client disconnects 342 * @param disconnect_cb function to call whenever a client disconnects
282 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb 343 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
@@ -302,26 +363,51 @@ GNUNET_SERVICE_run_ (int argc,
302 * GNUNET_MQ_handler_end ()); 363 * GNUNET_MQ_handler_end ());
303 * </code> 364 * </code>
304 */ 365 */
366#ifndef HAVE_GNUNET_MONOLITH
367#define GNUNET_SERVICE_MAIN(service_name, service_options, init_cb, connect_cb, \
368 disconnect_cb, cls, ...) \
369 int \
370 main (int argc, \
371 char *const *argv) \
372 { \
373 struct GNUNET_MQ_MessageHandler mh[] = { \
374 __VA_ARGS__ \
375 }; \
376 return GNUNET_SERVICE_run_ (argc, \
377 argv, \
378 service_name, \
379 service_options, \
380 init_cb, \
381 connect_cb, \
382 disconnect_cb, \
383 cls, \
384 mh); \
385 }
386#else
305#define GNUNET_SERVICE_MAIN(service_name, service_options, init_cb, connect_cb, \ 387#define GNUNET_SERVICE_MAIN(service_name, service_options, init_cb, connect_cb, \
306 disconnect_cb, cls, ...) \ 388 disconnect_cb, cls, ...) \
307 int \ 389 static int __attribute__ ((constructor)) \
308 main (int argc, \ 390 init (void) \
309 char *const *argv) \ 391 { \
310 { \ 392 struct GNUNET_MQ_MessageHandler mh[] = { \
311 struct GNUNET_MQ_MessageHandler mh[] = { \ 393 __VA_ARGS__ \
312 __VA_ARGS__ \ 394 }; \
313 }; \ 395 return GNUNET_SERVICE_register_ (service_name, \
314 return GNUNET_SERVICE_run_ (argc, \ 396 service_options, \
315 argv, \ 397 init_cb, \
316 service_name, \ 398 connect_cb, \
317 service_options, \ 399 disconnect_cb, \
318 init_cb, \ 400 cls, \
319 connect_cb, \ 401 mh); \
320 disconnect_cb, \ 402 }
321 cls, \ 403#endif
322 mh); \
323 }
324 404
405/**
406 * Run the mainloop in a monolithic libgnunet.
407 * Must be called such that services are actually launched.
408 */
409void
410GNUNET_SERVICE_main (int argc, char *const *argv);
325 411
326/** 412/**
327 * Suspend accepting connections from the listen socket temporarily. 413 * Suspend accepting connections from the listen socket temporarily.
@@ -442,4 +528,8 @@ GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
442 528
443/** @} */ /* end of group service */ 529/** @} */ /* end of group service */
444 530
531/** @} */ /* end of group addition to networking*/
532
533/** @} */ /* end of group addition to libgnunetutil */
534
445/* end of gnunet_service_lib.h */ 535/* end of gnunet_service_lib.h */