From 4e668fdd5b5da0d46f97d835249dbe5f0ea319a7 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 19 Oct 2017 15:16:25 -0700 Subject: fixes #84 Consider using msec for durations There is now a public nng_duration type. We have also updated the zerotier work to work with the signed int64_t's that the latst ZeroTier dev branch is using. --- src/core/clock.c | 6 +-- src/core/clock.h | 3 +- src/core/defs.h | 6 +-- src/core/device.c | 2 +- src/core/options.c | 6 +-- src/core/options.h | 6 +-- src/core/platform.h | 4 +- src/core/socket.c | 24 +++++------ src/nng.c | 30 ++++++++------ src/nng.h | 23 +++++------ src/nng_compat.c | 84 ++++++++++----------------------------- src/platform/posix/posix_clock.c | 51 +++++++++++------------- src/platform/posix/posix_thread.c | 4 +- src/platform/windows/win_clock.c | 11 ++--- src/platform/windows/win_thread.c | 3 +- src/protocol/reqrep/req.c | 4 +- src/protocol/survey/survey.c | 4 +- src/transport/tcp/tcp.c | 6 +-- src/transport/zerotier/zerotier.c | 49 +++++++++++------------ src/transport/zerotier/zerotier.h | 2 +- 20 files changed, 140 insertions(+), 188 deletions(-) (limited to 'src') diff --git a/src/core/clock.c b/src/core/clock.c index 31678f67..f114daa1 100644 --- a/src/core/clock.c +++ b/src/core/clock.c @@ -16,7 +16,7 @@ nni_clock(void) } void -nni_usleep(nni_duration usec) +nni_msleep(nni_duration msec) { - nni_plat_usleep(usec); -} + nni_plat_sleep(msec); +} \ No newline at end of file diff --git a/src/core/clock.h b/src/core/clock.h index 3b607322..b369520b 100644 --- a/src/core/clock.h +++ b/src/core/clock.h @@ -1,5 +1,6 @@ // // Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -14,6 +15,6 @@ extern nni_time nni_clock(void); -extern void nni_usleep(nni_duration usec); +extern void nni_msleep(nni_duration); #endif // CORE_CLOCK_H diff --git a/src/core/defs.h b/src/core/defs.h index ff02b28b..57f7f06a 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -53,8 +53,8 @@ typedef struct nni_thr nni_thr; typedef void (*nni_thr_func)(void *); typedef int nni_signal; // Wakeup channel. -typedef uint64_t nni_time; // Abs. time (usec). -typedef int64_t nni_duration; // Rel. time (usec). +typedef uint64_t nni_time; // Abs. time (ms). +typedef int32_t nni_duration; // Rel. time (ms). typedef struct nni_aio nni_aio; @@ -76,7 +76,7 @@ typedef struct { // Some default timing things. #define NNI_TIME_NEVER ((nni_time) -1) #define NNI_TIME_ZERO ((nni_time) 0) -#define NNI_SECOND (1000000) +#define NNI_SECOND (1000) // Structure allocation conveniences. #define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof(*s)) diff --git a/src/core/device.c b/src/core/device.c index 9161e2f0..22ec086e 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -66,7 +66,7 @@ nni_device(nni_sock *sock1, nni_sock *sock2) { nni_device_pair pair; int rv; - nni_time never = NNI_TIME_NEVER; + nni_duration never = -1; size_t sz; memset(&pair, 0, sizeof(pair)); diff --git a/src/core/options.c b/src/core/options.c index aa744642..3b787b82 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -14,7 +14,7 @@ #include int -nni_chkopt_usec(const void *v, size_t sz) +nni_chkopt_ms(const void *v, size_t sz) { nni_duration val; if (sz != sizeof(val)) { @@ -56,7 +56,7 @@ nni_chkopt_size(const void *v, size_t sz, size_t minv, size_t maxv) } int -nni_setopt_usec(nni_duration *dp, const void *v, size_t sz) +nni_setopt_ms(nni_duration *dp, const void *v, size_t sz) { nni_duration dur; @@ -110,7 +110,7 @@ nni_setopt_size(size_t *sp, const void *v, size_t sz, size_t minv, size_t maxv) } int -nni_getopt_usec(nni_duration u, void *val, size_t *sizep) +nni_getopt_ms(nni_duration u, void *val, size_t *sizep) { size_t sz = sizeof(u); diff --git a/src/core/options.h b/src/core/options.h index cf2176b8..beeca951 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -23,10 +23,10 @@ extern int nni_getopt_buf(nni_msgq *, void *, size_t *); // nni_setopt_duration sets the duration. Durations must be legal, // either a positive value, 0, or -1 to indicate forever. -extern int nni_setopt_usec(nni_duration *, const void *, size_t); +extern int nni_setopt_ms(nni_duration *, const void *, size_t); // nni_getopt_duration gets the duration. -extern int nni_getopt_usec(nni_duration, void *, size_t *); +extern int nni_getopt_ms(nni_duration, void *, size_t *); // nni_setopt_int sets an integer, which must be between the minimum and // maximum values (inclusive). @@ -61,7 +61,7 @@ extern int nni_getopt_size(size_t, void *, size_t *); // nni_getopt_fd obtains a notification file descriptor. extern int nni_getopt_fd(nni_sock *, nni_notifyfd *, int, void *, size_t *); -extern int nni_chkopt_usec(const void *, size_t); +extern int nni_chkopt_ms(const void *, size_t); extern int nni_chkopt_int(const void *, size_t, int, int); extern int nni_chkopt_size(const void *, size_t, size_t, size_t); diff --git a/src/core/platform.h b/src/core/platform.h index 8b709e93..bff7f709 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -153,8 +153,8 @@ extern void nni_plat_thr_fini(nni_plat_thr *); // of using negative values for other purposes in the future.) extern nni_time nni_plat_clock(void); -// nni_plat_usleep sleeps for the specified number of microseconds (at least). -extern void nni_plat_usleep(nni_duration); +// nni_plat_sleep sleeps for the specified number of milliseconds (at least). +extern void nni_plat_sleep(nni_duration); // // Entropy Support diff --git a/src/core/socket.c b/src/core/socket.c index c64ab995..8895f7a7 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -95,49 +95,49 @@ nni_sock_getopt_recvfd(nni_sock *s, void *buf, size_t *szp) static int nni_sock_setopt_recvtimeo(nni_sock *s, const void *buf, size_t sz) { - return (nni_setopt_usec(&s->s_rcvtimeo, buf, sz)); + return (nni_setopt_ms(&s->s_rcvtimeo, buf, sz)); } static int nni_sock_getopt_recvtimeo(nni_sock *s, void *buf, size_t *szp) { - return (nni_getopt_usec(s->s_rcvtimeo, buf, szp)); + return (nni_getopt_ms(s->s_rcvtimeo, buf, szp)); } static int nni_sock_setopt_sendtimeo(nni_sock *s, const void *buf, size_t sz) { - return (nni_setopt_usec(&s->s_sndtimeo, buf, sz)); + return (nni_setopt_ms(&s->s_sndtimeo, buf, sz)); } static int nni_sock_getopt_sendtimeo(nni_sock *s, void *buf, size_t *szp) { - return (nni_getopt_usec(s->s_sndtimeo, buf, szp)); + return (nni_getopt_ms(s->s_sndtimeo, buf, szp)); } static int nni_sock_setopt_reconnmint(nni_sock *s, const void *buf, size_t sz) { - return (nni_setopt_usec(&s->s_reconn, buf, sz)); + return (nni_setopt_ms(&s->s_reconn, buf, sz)); } static int nni_sock_getopt_reconnmint(nni_sock *s, void *buf, size_t *szp) { - return (nni_getopt_usec(s->s_reconn, buf, szp)); + return (nni_getopt_ms(s->s_reconn, buf, szp)); } static int nni_sock_setopt_reconnmaxt(nni_sock *s, const void *buf, size_t sz) { - return (nni_setopt_usec(&s->s_reconnmax, buf, sz)); + return (nni_setopt_ms(&s->s_reconnmax, buf, sz)); } static int nni_sock_getopt_reconnmaxt(nni_sock *s, void *buf, size_t *szp) { - return (nni_getopt_usec(s->s_reconnmax, buf, szp)); + return (nni_getopt_ms(s->s_reconnmax, buf, szp)); } static int @@ -500,8 +500,8 @@ nni_sock_create(nni_sock **sp, const nni_proto *proto) return (NNG_ENOMEM); } s->s_linger = 0; - s->s_sndtimeo = NNI_TIME_NEVER; - s->s_rcvtimeo = NNI_TIME_NEVER; + s->s_sndtimeo = -1; + s->s_rcvtimeo = -1; s->s_closing = 0; s->s_reconn = NNI_SECOND; s->s_reconnmax = 0; @@ -1026,7 +1026,7 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *val, size_t size) // was found, or even if a transport rejected one of the settings. if ((rv == NNG_ENOTSUP) || (rv == 0)) { if ((strcmp(name, NNG_OPT_LINGER) == 0)) { - rv = nni_chkopt_usec(val, size); + rv = nni_chkopt_ms(val, size); } else if (strcmp(name, NNG_OPT_RECVMAXSZ) == 0) { // just a sanity test on the size; it also ensures that // a size can be set even with no transport configured. @@ -1090,7 +1090,7 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *val, size_t size) // will already have had a chance to veto this. if (strcmp(name, NNG_OPT_LINGER) == 0) { - rv = nni_setopt_usec(&s->s_linger, val, size); + rv = nni_setopt_ms(&s->s_linger, val, size); } if (rv == 0) { diff --git a/src/nng.c b/src/nng.c index ad9651d3..9da78905 100644 --- a/src/nng.c +++ b/src/nng.c @@ -389,7 +389,7 @@ nng_dialer_setopt_size(nng_dialer id, const char *name, size_t val) } int -nng_dialer_setopt_usec(nng_dialer id, const char *name, uint64_t val) +nng_dialer_setopt_ms(nng_dialer id, const char *name, nng_duration val) { return (nng_dialer_setopt(id, name, &val, sizeof(val))); } @@ -428,9 +428,10 @@ nng_dialer_getopt_uint64(nng_dialer id, const char *name, uint64_t *valp) } int -nng_dialer_getopt_usec(nng_dialer id, const char *name, uint64_t *valp) +nng_dialer_getopt_ms(nng_dialer id, const char *name, nng_duration *valp) { - return (nng_dialer_getopt_uint64(id, name, valp)); + size_t sz = sizeof(*valp); + return (nng_dialer_getopt(id, name, valp, &sz)); } int @@ -453,7 +454,7 @@ nng_listener_setopt_size(nng_listener id, const char *name, size_t val) } int -nng_listener_setopt_usec(nng_listener id, const char *name, uint64_t val) +nng_listener_setopt_ms(nng_listener id, const char *name, nng_duration val) { return (nng_listener_setopt(id, name, &val, sizeof(val))); } @@ -492,9 +493,10 @@ nng_listener_getopt_uint64(nng_listener id, const char *name, uint64_t *valp) } int -nng_listener_getopt_usec(nng_listener id, const char *name, uint64_t *valp) +nng_listener_getopt_ms(nng_listener id, const char *name, nng_duration *valp) { - return (nng_listener_getopt_uint64(id, name, valp)); + size_t sz = sizeof(*valp); + return (nng_listener_getopt(id, name, valp, &sz)); } static int @@ -570,7 +572,7 @@ nng_setopt_size(nng_socket sid, const char *name, size_t val) } int -nng_setopt_usec(nng_socket sid, const char *name, uint64_t val) +nng_setopt_ms(nng_socket sid, const char *name, nng_duration val) { return (nng_setopt(sid, name, &val, sizeof(val))); } @@ -603,9 +605,10 @@ nng_getopt_uint64(nng_socket sid, const char *name, uint64_t *valp) } int -nng_getopt_usec(nng_socket sid, const char *name, uint64_t *valp) +nng_getopt_ms(nng_socket sid, const char *name, nng_duration *valp) { - return (nng_getopt_uint64(sid, name, valp)); + size_t sz = sizeof(*valp); + return (nng_getopt(sid, name, valp, &sz)); } nng_notify * @@ -775,9 +778,10 @@ nng_pipe_getopt_uint64(nng_pipe id, const char *name, uint64_t *valp) } int -nng_pipe_getopt_usec(nng_pipe id, const char *name, uint64_t *valp) +nng_pipe_getopt_ms(nng_pipe id, const char *name, nng_duration *valp) { - return (nng_pipe_getopt_uint64(id, name, valp)); + size_t sz = sizeof(*valp); + return (nng_pipe_getopt(id, name, valp, &sz)); } int @@ -1042,9 +1046,9 @@ nng_stat_value(nng_stat *stat) // API, and applications should refrain from their use. void -nng_usleep(uint64_t usec) +nng_msleep(nng_duration ms) { - nni_usleep(usec); + nni_msleep(ms); } uint64_t diff --git a/src/nng.h b/src/nng.h index 700ee869..d740dfc0 100644 --- a/src/nng.h +++ b/src/nng.h @@ -46,6 +46,7 @@ typedef uint32_t nng_socket; typedef uint32_t nng_dialer; typedef uint32_t nng_listener; typedef uint32_t nng_pipe; +typedef int32_t nng_duration; // in milliseconds typedef struct nng_msg nng_msg; typedef struct nng_event nng_event; typedef struct nng_notify nng_notify; @@ -88,14 +89,14 @@ NNG_DECL uint16_t nng_peer(nng_socket); // nng_setopt sets an option for a specific socket. NNG_DECL int nng_setopt(nng_socket, const char *, const void *, size_t); NNG_DECL int nng_setopt_int(nng_socket, const char *, int); -NNG_DECL int nng_setopt_usec(nng_socket, const char *, uint64_t); +NNG_DECL int nng_setopt_ms(nng_socket, const char *, nng_duration); NNG_DECL int nng_setopt_size(nng_socket, const char *, size_t); NNG_DECL int nng_setopt_uint64(nng_socket, const char *, uint64_t); // nng_socket_getopt obtains the option for a socket. NNG_DECL int nng_getopt(nng_socket, const char *, void *, size_t *); NNG_DECL int nng_getopt_int(nng_socket, const char *, int *); -NNG_DECL int nng_getopt_usec(nng_socket, const char *, uint64_t *); +NNG_DECL int nng_getopt_ms(nng_socket, const char *, nng_duration *); NNG_DECL int nng_getopt_size(nng_socket, const char *, size_t *); NNG_DECL int nng_getopt_uint64(nng_socket, const char *, uint64_t *); @@ -203,7 +204,7 @@ NNG_DECL int nng_listener_close(nng_listener); // dialer options may not be altered on a running dialer. NNG_DECL int nng_dialer_setopt(nng_dialer, const char *, const void *, size_t); NNG_DECL int nng_dialer_setopt_int(nng_dialer, const char *, int); -NNG_DECL int nng_dialer_setopt_usec(nng_dialer, const char *, uint64_t); +NNG_DECL int nng_dialer_setopt_ms(nng_dialer, const char *, nng_duration); NNG_DECL int nng_dialer_setopt_size(nng_dialer, const char *, size_t); NNG_DECL int nng_dialer_setopt_uint64(nng_dialer, const char *, uint64_t); @@ -212,7 +213,7 @@ NNG_DECL int nng_dialer_setopt_uint64(nng_dialer, const char *, uint64_t); // even if they were set on the socket. NNG_DECL int nng_dialer_getopt(nng_dialer, const char *, void *, size_t *); NNG_DECL int nng_dialer_getopt_int(nng_dialer, const char *, int *); -NNG_DECL int nng_dialer_getopt_usec(nng_dialer, const char *, uint64_t *); +NNG_DECL int nng_dialer_getopt_ms(nng_dialer, const char *, nng_duration *); NNG_DECL int nng_dialer_getopt_size(nng_dialer, const char *, size_t *); NNG_DECL int nng_dialer_getopt_uint64(nng_dialer, const char *, uint64_t *); @@ -223,7 +224,7 @@ NNG_DECL int nng_dialer_getopt_uint64(nng_dialer, const char *, uint64_t *); NNG_DECL int nng_listener_setopt( nng_listener, const char *, const void *, size_t); NNG_DECL int nng_listener_setopt_int(nng_listener, const char *, int); -NNG_DECL int nng_listener_setopt_usec(nng_listener, const char *, uint64_t); +NNG_DECL int nng_listener_setopt_ms(nng_listener, const char *, nng_duration); NNG_DECL int nng_listener_setopt_size(nng_listener, const char *, size_t); NNG_DECL int nng_listener_setopt_uint64(nng_listener, const char *, uint64_t); @@ -232,7 +233,8 @@ NNG_DECL int nng_listener_setopt_uint64(nng_listener, const char *, uint64_t); // even if they were set on the socket. NNG_DECL int nng_listener_getopt(nng_listener, const char *, void *, size_t *); NNG_DECL int nng_listener_getopt_int(nng_listener, const char *, int *); -NNG_DECL int nng_listener_getopt_usec(nng_listener, const char *, uint64_t *); +NNG_DECL int nng_listener_getopt_ms( + nng_listener, const char *, nng_duration *); NNG_DECL int nng_listener_getopt_size(nng_listener, const char *, size_t *); NNG_DECL int nng_listener_getopt_uint64( nng_listener, const char *, uint64_t *); @@ -330,7 +332,7 @@ NNG_DECL const char *nng_option_name(int); // is associated with an invalid or untrusted remote peer. NNG_DECL int nng_pipe_getopt(nng_pipe, const char *, void *, size_t *); NNG_DECL int nng_pipe_getopt_int(nng_pipe, const char *, int *); -NNG_DECL int nng_pipe_getopt_usec(nng_pipe, const char *, uint64_t *); +NNG_DECL int nng_pipe_getopt_ms(nng_pipe, const char *, nng_duration *); NNG_DECL int nng_pipe_getopt_size(nng_pipe, const char *, size_t *); NNG_DECL int nng_pipe_getopt_uint64(nng_pipe, const char *, uint64_t *); NNG_DECL int nng_pipe_close(nng_pipe); @@ -517,11 +519,8 @@ NNG_DECL int nng_device(nng_socket, nng_socket); #ifdef NNG_PRIVATE -// Sleep for specified usecs. -NNG_DECL void nng_usleep(uint64_t); - -// Return usecs since some arbitrary time in past. -NNG_DECL uint64_t nng_clock(void); +// Sleep for specified msecs. +NNG_DECL void nng_msleep(nng_duration); // Create and start a thread. NNG_DECL int nng_thread_create(void **, void (*)(void *), void *); diff --git a/src/nng_compat.c b/src/nng_compat.c index dedbda0d..e9a3e3a3 100644 --- a/src/nng_compat.c +++ b/src/nng_compat.c @@ -569,7 +569,6 @@ static struct { int nnlevel; int nnopt; const char *opt; - int mscvt; } options[] = { // clang-format off { NN_SOL_SOCKET, NN_LINGER }, // review @@ -604,74 +603,72 @@ init_opts(void) if (options[i].opt > 0) { continue; } -#define SETOPT(n, ms) \ - options[i].opt = n; \ - options[i].mscvt = ms +#define SETOPT(n) options[i].opt = n; switch (options[i].nnlevel) { case NN_SOL_SOCKET: switch (options[i].nnopt) { case NN_LINGER: - SETOPT(NNG_OPT_LINGER, 1); + SETOPT(NNG_OPT_LINGER); break; case NN_SNDBUF: - SETOPT(NNG_OPT_SENDBUF, 0); + SETOPT(NNG_OPT_SENDBUF); break; case NN_RCVBUF: - SETOPT(NNG_OPT_RECVBUF, 0); + SETOPT(NNG_OPT_RECVBUF); break; case NN_RECONNECT_IVL: - SETOPT(NNG_OPT_RECONNMINT, 1); + SETOPT(NNG_OPT_RECONNMINT); break; case NN_RECONNECT_IVL_MAX: - SETOPT(NNG_OPT_RECONNMAXT, 1); + SETOPT(NNG_OPT_RECONNMAXT); break; case NN_SNDFD: - SETOPT(NNG_OPT_SENDFD, 0); + SETOPT(NNG_OPT_SENDFD); break; case NN_RCVFD: - SETOPT(NNG_OPT_RECVFD, 0); + SETOPT(NNG_OPT_RECVFD); break; case NN_RCVMAXSIZE: - SETOPT(NNG_OPT_RECVMAXSZ, 0); + SETOPT(NNG_OPT_RECVMAXSZ); break; case NN_MAXTTL: - SETOPT(NNG_OPT_MAXTTL, 0); + SETOPT(NNG_OPT_MAXTTL); break; case NN_RCVTIMEO: - SETOPT(NNG_OPT_RECVTIMEO, 1); + SETOPT(NNG_OPT_RECVTIMEO); break; case NN_SNDTIMEO: - SETOPT(NNG_OPT_SENDTIMEO, 1); + SETOPT(NNG_OPT_SENDTIMEO); break; case NN_SOCKET_NAME: - SETOPT(NNG_OPT_SOCKNAME, 0); + SETOPT(NNG_OPT_SOCKNAME); break; case NN_DOMAIN: - SETOPT(NNG_OPT_DOMAIN, 0); + SETOPT(NNG_OPT_DOMAIN); break; } break; case NN_REQ: switch (options[i].nnopt) { case NN_REQ_RESEND_IVL: - SETOPT(NNG_OPT_REQ_RESENDTIME, 1); + SETOPT(NNG_OPT_REQ_RESENDTIME); break; } break; case NN_SUB: switch (options[i].nnopt) { case NN_SUB_SUBSCRIBE: - SETOPT(NNG_OPT_SUB_SUBSCRIBE, 0); + SETOPT(NNG_OPT_SUB_SUBSCRIBE); break; case NN_SUB_UNSUBSCRIBE: - SETOPT(NNG_OPT_SUB_UNSUBSCRIBE, 0); + SETOPT(NNG_OPT_SUB_UNSUBSCRIBE); break; } case NN_SURVEYOR: switch (options[i].nnopt) { case NN_SURVEYOR_DEADLINE: - SETOPT(NNG_OPT_SURVEYOR_SURVEYTIME, 1); + SETOPT(NNG_OPT_SURVEYOR_SURVEYTIME); break; } break; @@ -683,10 +680,7 @@ init_opts(void) int nn_getsockopt(int s, int nnlevel, int nnopt, void *valp, size_t *szp) { - const char *name = NULL; - int mscvt = 0; - uint64_t usec; - int * msecp; + const char *name = NULL; int rv; init_opts(); @@ -694,8 +688,7 @@ nn_getsockopt(int s, int nnlevel, int nnopt, void *valp, size_t *szp) for (int i = 0; i < sizeof(options) / sizeof(options[0]); i++) { if ((options[i].nnlevel == nnlevel) && (options[i].nnopt == nnopt)) { - mscvt = options[i].mscvt; - name = options[i].opt; + name = options[i].opt; break; } } @@ -705,37 +698,18 @@ nn_getsockopt(int s, int nnlevel, int nnopt, void *valp, size_t *szp) return (-1); } - if (mscvt) { - if (*szp != sizeof(int)) { - errno = EINVAL; - return (-1); - } - - msecp = valp; - valp = &usec; - *szp = sizeof(uint64_t); - } - if ((rv = nng_getopt((nng_socket) s, name, valp, szp)) != 0) { nn_seterror(rv); return (-1); } - if (mscvt) { - // We have to convert value to ms... - *msecp = (int) (usec / 1000); - *szp = sizeof(int); - } - return (0); } int nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) { - const char *name = NULL; - int mscvt = 0; - uint64_t usec; + const char *name = NULL; int rv; init_opts(); @@ -743,8 +717,7 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) for (int i = 0; i < sizeof(options) / sizeof(options[0]); i++) { if ((options[i].nnlevel == nnlevel) && (options[i].nnopt == nnopt)) { - mscvt = options[i].mscvt; - name = options[i].opt; + name = options[i].opt; break; } } @@ -752,19 +725,6 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) return (ENOPROTOOPT); } - if (mscvt) { - // We have to convert value to ms... - - if (sz != sizeof(int)) { - errno = EINVAL; - return (-1); - } - usec = *(int *) valp; - usec *= 1000; - valp = &usec; - sz = sizeof(usec); - } - if ((rv = nng_setopt((nng_socket) s, name, valp, sz)) != 0) { nn_seterror(rv); return (-1); diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c index 3e46f787..289638ca 100644 --- a/src/platform/posix/posix_clock.c +++ b/src/platform/posix/posix_clock.c @@ -1,5 +1,6 @@ // -// Copyright 2016 Garrett D'Amore +// Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -23,28 +24,28 @@ nni_time nni_plat_clock(void) { struct timespec ts; - nni_time usec; + nni_time msec; if (clock_gettime(NNG_USE_CLOCKID, &ts) != 0) { - /* This should never ever occur. */ + // This should never ever occur. nni_panic("clock_gettime failed: %s", strerror(errno)); } - usec = ts.tv_sec; - usec *= 1000000; - usec += (ts.tv_nsec / 1000); - return (usec); + msec = ts.tv_sec; + msec *= 1000; + msec += (ts.tv_nsec / 1000000); + return (msec); } void -nni_plat_usleep(nni_duration usec) +nni_plat_sleep(nni_duration ms) { struct timespec ts; - ts.tv_sec = usec / 1000000; - ts.tv_nsec = (usec % 1000000) * 1000; + ts.tv_sec = ms / 1000; + ts.tv_nsec = (ms % 1000) * 1000000; - /* Do this in a loop, so that interrupts don't actually wake us. */ + // Do this in a loop, so that interrupts don't actually wake us. while (ts.tv_sec || ts.tv_nsec) { if (nanosleep(&ts, &ts) == 0) { break; @@ -67,10 +68,10 @@ nni_plat_usleep(nni_duration usec) #include #include -nni_time +static nni_time nni_plat_clock(void) { - nni_time usec; + nni_time ms; struct timeval tv; @@ -78,14 +79,14 @@ nni_plat_clock(void) nni_panic("gettimeofday failed: %s", strerror(errno)); } - usec = tv.tv_sec; - usec *= 1000000; - usec += tv.tv_usec; - return (usec); + ms = tv.tv_sec; + msec *= 1000; + msec += (tv.tv_usec / 1000); + return (msec); } void -nni_plat_usleep(nni_duration usec) +nni_plat_sleep(nni_duration ms) { // So probably there is no nanosleep. We could in theory use // pthread condition variables, but that means doing memory @@ -93,8 +94,7 @@ nni_plat_usleep(nni_duration usec) // might be preferring the use of another threading package. // Additionally, use of pthreads means that we cannot use // relative times in a clock_settime safe manner. - // So we can use poll() instead, which is rather coarse, but - // pretty much guaranteed to work. + // So we can use poll() instead. struct pollfd pfd; nni_time now; nni_time expire; @@ -105,16 +105,11 @@ nni_plat_usleep(nni_duration usec) pfd.fd = -1; pfd.events = 0; - now = nni_clock(); - expire = now + usec; + now = nni_plat_clock(); // XXX: until nni_plat_clock returns ms. + expire = now + ms; while (now < expire) { - // In theory we could round up to a whole number of msec, - // but under the covers poll already does some rounding up, - // and the loop above guarantees that we will not bail out - // early. So this gives us a better chance to avoid adding - // nearly an extra unneeded millisecond to the wait. - (void) poll(&pfd, 0, (int) ((expire - now) / 1000)); + (void) poll(&pfd, 0, (int) (expire - now)); now = nni_clock(); } } diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index 4278a057..115768dc 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.c @@ -354,8 +354,8 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until) NNI_ASSERT(cv->mtx->owner == pthread_self()); // Our caller has already guaranteed a sane value for until. - ts.tv_sec = until / 1000000; - ts.tv_nsec = (until % 1000000) * 1000; + ts.tv_sec = until / 1000; + ts.tv_nsec = (until % 1000) * 1000000; if (cv->fallback) { rv = nni_plat_cv_until_fallback(cv, &ts); diff --git a/src/platform/windows/win_clock.c b/src/platform/windows/win_clock.c index 949b2dfc..613af4be 100644 --- a/src/platform/windows/win_clock.c +++ b/src/platform/windows/win_clock.c @@ -1,5 +1,6 @@ // -// Copyright 2016 Garrett D'Amore +// Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -15,18 +16,14 @@ nni_time nni_plat_clock(void) { // We are limited by the system clock, but that is ok. - return (GetTickCount64() * 1000); + return (GetTickCount64()); } void -nni_plat_usleep(nni_duration dur) +nni_plat_sleep(nni_duration dur) { uint64_t exp; - // Convert duration to msec, rounding up. - dur += 999; - dur /= 1000; - exp = (uint64_t) GetTickCount64() + dur; // Sleep() would be our preferred API, if it didn't have a nasty diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 41ba721c..0d9e7387 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -91,8 +91,7 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until) if (now > until) { msec = 0; } else { - // times are in usec, but win32 wants millis - msec = (DWORD)(((until - now) + 999) / 1000); + msec = (DWORD)(until - now); } ok = SleepConditionVariableSRW(&cv->cv, cv->srl, msec, 0); diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index 7d47b0b3..24d01df2 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -282,14 +282,14 @@ static int req_sock_setopt_resendtime(void *arg, const void *buf, size_t sz) { req_sock *s = arg; - return (nni_setopt_usec(&s->retry, buf, sz)); + return (nni_setopt_ms(&s->retry, buf, sz)); } static int req_sock_getopt_resendtime(void *arg, void *buf, size_t *szp) { req_sock *s = arg; - return (nni_getopt_usec(s->retry, buf, szp)); + return (nni_getopt_ms(s->retry, buf, szp)); } // Raw and cooked mode differ in the way they send messages out. diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c index de9df7a5..1205402e 100644 --- a/src/protocol/survey/survey.c +++ b/src/protocol/survey/survey.c @@ -292,14 +292,14 @@ static int surv_sock_setopt_surveytime(void *arg, const void *buf, size_t sz) { surv_sock *s = arg; - return (nni_setopt_usec(&s->survtime, buf, sz)); + return (nni_setopt_ms(&s->survtime, buf, sz)); } static int surv_sock_getopt_surveytime(void *arg, void *buf, size_t *szp) { surv_sock *s = arg; - return (nni_getopt_usec(s->survtime, buf, szp)); + return (nni_getopt_ms(s->survtime, buf, szp)); } static void diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c index 2e0b023a..5ed90ca4 100644 --- a/src/transport/tcp/tcp.c +++ b/src/transport/tcp/tcp.c @@ -800,16 +800,16 @@ nni_tcp_ep_setopt_linger(void *arg, const void *v, size_t sz) { nni_tcp_ep *ep = arg; if (ep == NULL) { - return (nni_chkopt_usec(v, sz)); + return (nni_chkopt_ms(v, sz)); } - return (nni_setopt_usec(&ep->linger, v, sz)); + return (nni_setopt_ms(&ep->linger, v, sz)); } static int nni_tcp_ep_getopt_linger(void *arg, void *v, size_t *szp) { nni_tcp_ep *ep = arg; - return (nni_getopt_usec(ep->linger, v, szp)); + return (nni_getopt_ms(ep->linger, v, szp)); } static nni_tran_pipe_option nni_tcp_pipe_options[] = { diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c index e306c85a..607c353c 100644 --- a/src/transport/zerotier/zerotier.c +++ b/src/transport/zerotier/zerotier.c @@ -91,14 +91,14 @@ static const uint32_t zt_port_shift = 24; // These are compile time tunables for now. enum zt_tunables { zt_listenq = 128, // backlog queue length - zt_listen_expire = 60000000, // maximum time in backlog + zt_listen_expire = 60000, // maximum time in backlog (msec) zt_rcv_bufsize = ZT_MAX_PHYSMTU, // max UDP recv zt_conn_attempts = 12, // connection attempts (default) - zt_conn_interval = 5000000, // between attempts (usec) + zt_conn_interval = 5000, // between attempts (msec) zt_udp_sendq = 16, // outgoing UDP queue length zt_recvq = 2, // max pending recv (per pipe) - zt_recv_stale = 1000000, // frags older than are stale - zt_ping_time = 60000000, // keepalive time (usec) + zt_recv_stale = 1000, // frags older than are stale (msec) + zt_ping_time = 60000, // keepalive time (msec) zt_ping_count = 5, // keepalive attempts }; @@ -292,22 +292,22 @@ static void zt_virtual_recv(ZT_Node *, void *, void *, uint64_t, void **, uint64_t, uint64_t, unsigned int, unsigned int, const void *, unsigned int); -static uint64_t +static int64_t zt_now(void) { // We return msec - return (nni_clock() / 1000); + return ((int64_t) nni_clock()); } static void zt_bgthr(void *arg) { zt_node *ztn = arg; - nni_time now; + int64_t now; nni_mtx_lock(&zt_lk); for (;;) { - now = nni_clock(); + now = zt_now(); if (ztn->zn_closed) { break; @@ -318,18 +318,21 @@ zt_bgthr(void *arg) continue; } - now /= 1000; // usec -> msec + ztn->zn_bgtime = 0; ZT_Node_processBackgroundTasks(ztn->zn_znode, NULL, now, &now); - ztn->zn_bgtime = now * 1000; // usec + ztn->zn_bgtime = now; } nni_mtx_unlock(&zt_lk); } static void -zt_node_resched(zt_node *ztn, uint64_t msec) +zt_node_resched(zt_node *ztn, int64_t msec) { - ztn->zn_bgtime = msec * 1000; // convert to usec + if (msec > ztn->zn_bgtime && ztn->zn_bgtime != 0) { + return; + } + ztn->zn_bgtime = msec; nni_cv_wake1(&ztn->zn_bgcv); } @@ -341,7 +344,7 @@ zt_node_rcv4_cb(void *arg) struct sockaddr_storage sa; struct sockaddr_in * sin; nng_sockaddr_in * nsin; - uint64_t now; + int64_t now; if (nni_aio_result(aio) != 0) { // Outside of memory exhaustion, we can't really think @@ -392,7 +395,7 @@ zt_node_rcv6_cb(void *arg) struct sockaddr_storage sa; struct sockaddr_in6 * sin6; struct nng_sockaddr_in6 *nsin6; - uint64_t now; + int64_t now; if (nni_aio_result(aio) != 0) { // Outside of memory exhaustion, we can't really think @@ -558,7 +561,7 @@ zt_send(zt_node *ztn, uint64_t nwid, uint8_t op, uint64_t raddr, { uint64_t srcmac = zt_node_to_mac(laddr >> 24, nwid); uint64_t dstmac = zt_node_to_mac(raddr >> 24, nwid); - uint64_t now = zt_now(); + int64_t now = zt_now(); NNI_ASSERT(len >= zt_size_headers); data[zt_offset_op] = op; @@ -569,13 +572,6 @@ zt_send(zt_node *ztn, uint64_t nwid, uint8_t op, uint64_t raddr, ZT_PUT24(data + zt_offset_dst_port, raddr & zt_port_mask); ZT_PUT24(data + zt_offset_src_port, laddr & zt_port_mask); - // If we are looping back, bypass ZT. - if (srcmac == dstmac) { - zt_virtual_recv(ztn->zn_znode, ztn, NULL, nwid, NULL, srcmac, - dstmac, zt_ethertype, 0, data, len); - return; - } - (void) ZT_Node_processVirtualNetworkFrame(ztn->zn_znode, NULL, now, nwid, srcmac, dstmac, zt_ethertype, 0, data, len, &now); @@ -1877,6 +1873,8 @@ zt_pipe_dorecv(zt_pipe *p) msg = fl->fl_msg; fl->fl_msg = NULL; NNI_ASSERT(msg != NULL); + + p->zp_user_rxaio = NULL; nni_aio_finish_msg(aio, msg); zt_fraglist_clear(fl); return; @@ -2375,7 +2373,6 @@ zt_ep_doaccept(zt_ep *ep) continue; } p->zp_peer = creq.cr_proto; - zt_pipe_send_conn_ack(p); nni_aio_finish_pipe(aio, p); } @@ -2645,16 +2642,16 @@ zt_ep_setopt_ping_time(void *arg, const void *data, size_t sz) { zt_ep *ep = arg; if (ep == NULL) { - return (nni_chkopt_usec(data, sz)); + return (nni_chkopt_ms(data, sz)); } - return (nni_setopt_usec(&ep->ze_ping_time, data, sz)); + return (nni_setopt_ms(&ep->ze_ping_time, data, sz)); } static int zt_ep_getopt_ping_time(void *arg, void *data, size_t *szp) { zt_ep *ep = arg; - return (nni_getopt_usec(ep->ze_ping_time, data, szp)); + return (nni_getopt_ms(ep->ze_ping_time, data, szp)); } static int diff --git a/src/transport/zerotier/zerotier.h b/src/transport/zerotier/zerotier.h index 1b3ee8b6..4f10f9be 100644 --- a/src/transport/zerotier/zerotier.h +++ b/src/transport/zerotier/zerotier.h @@ -81,7 +81,7 @@ // is sent. This will be done up to ping-count times. If no traffic from // the remote peer is seen after all ping requests are sent, then the peer // is assumed to be dead or offline, and the session is closed. The -// NNG_OPT_ZT_PING_TIME is a duration (usec, stored as an nng_duration, and +// NNG_OPT_ZT_PING_TIME is a duration (msec, stored as an nng_duration, and // NNG_OPT_ZT_PING_COUNT is an integer.) This ping process can be disabled // by setting either ping-time or ping-count to zero. #define NNG_OPT_ZT_PING_TIME "zt:ping-time" -- cgit v1.2.3-70-g09d2