From 9b4d9e71a63cafcee0edee734847bba28d9fea35 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 12 Aug 2017 10:16:54 -0700 Subject: Convenience option accesor functions. This adds functions that know about option sizes and make them easier to use. While here I added some validation of those, and cleaned up a few tests slightly. Note that we do not need to use the nng_impl.h for most tests. More of them need to be cleaned up. --- src/nng.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/nng.h | 18 +++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nng.c b/src/nng.c index 61aaa9c4..a34619da 100644 --- a/src/nng.c +++ b/src/nng.c @@ -296,6 +296,46 @@ nng_getopt(nng_socket sid, int opt, void *val, size_t *szp) return (rv); } +// Convenience option wrappers. +int +nng_setopt_int(nng_socket sid, int opt, int val) +{ + return (nng_setopt(sid, opt, &val, sizeof(val))); +} + +int +nng_setopt_size(nng_socket sid, int opt, size_t val) +{ + return (nng_setopt(sid, opt, &val, sizeof(val))); +} + +int +nng_setopt_duration(nng_socket sid, int opt, uint64_t val) +{ + return (nng_setopt(sid, opt, &val, sizeof(val))); +} + +int +nng_getopt_int(nng_socket sid, int opt, int *valp) +{ + size_t sz = sizeof(*valp); + return (nng_getopt(sid, opt, valp, &sz)); +} + +int +nng_getopt_size(nng_socket sid, int opt, size_t *valp) +{ + size_t sz = sizeof(*valp); + return (nng_getopt(sid, opt, valp, &sz)); +} + +int +nng_getopt_duration(nng_socket sid, int opt, uint64_t *valp) +{ + size_t sz = sizeof(*valp); + return (nng_getopt(sid, opt, valp, &sz)); +} + nng_notify * nng_setnotify(nng_socket sid, int mask, nng_notify_func fn, void *arg) { diff --git a/src/nng.h b/src/nng.h index 880614ea..9b2618eb 100644 --- a/src/nng.h +++ b/src/nng.h @@ -54,10 +54,11 @@ typedef struct nng_stat nng_stat; typedef uint32_t nng_endpoint; // XXX: REMOVE ME. // nng_fini is used to terminate the library, freeing certain global resources. -// Its a good idea to call this with atexit() or during application shutdown. // For most cases, this call is optional, but failure to do so may cause -// memory checkers like valgrind to incorrectly flag memory leaks. Note that -// this particular API is NOT THREADSAFE, and MUST NOT BE CALLED WHILE ANY +// memory checkers like valgrind to incorrectly flag memory leaks associated +// with global library resources. +// +// NOTE: THIS API IS NOT THREADSAFE, and MUST NOT BE CALLED WHILE ANY // OTHER APIS ARE IN USE. (It is safe however to call other functions such // as nng_open *after* this function returns, provided that the functions do // not run concurrently!) @@ -90,12 +91,23 @@ NNG_DECL uint16_t nng_peer(nng_socket); // nng_setopt sets an option for a specific socket. NNG_DECL int nng_setopt(nng_socket, int, const void *, size_t); +NNG_DECL int nng_setopt_int(nng_socket, int, int); +NNG_DECL int nng_setopt_duration(nng_socket, int, uint64_t); +NNG_DECL int nng_setopt_size(nng_socket, int, size_t); // nng_socket_getopt obtains the option for a socket. NNG_DECL int nng_getopt(nng_socket, int, void *, size_t *); +NNG_DECL int nng_getopt_int(nng_socket, int, int *); +NNG_DECL int nng_getopt_duration(nng_socket, int, uint64_t *); +NNG_DECL int nng_getopt_size(nng_socket, int, size_t *); // nng_notify_func is a user function that is executed upon certain // events. See below. +// +// NOTE WELL: This API is to be replaced in the future with an +// alternate API based on our AIO async I/O handles. We recommend +// against building this API too firmly into application code at +// this juncture. typedef void (*nng_notify_func)(nng_event *, void *); // nng_setnotify sets a notification callback. The callback will be -- cgit v1.2.3-70-g09d2