summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-04-30 21:41:40 -0700
committerGarrett D'Amore <garrett@damore.org>2018-05-01 12:27:19 -0700
commit4998964a435fe0f02a2d81b01fdb837214674e72 (patch)
treedc5fe29da0574fc1510ab6f9b23b54ffd68cd97b /src
parent63bdb2c28bc185096e579d1922d57cb71ecaa36b (diff)
downloadnng-4998964a435fe0f02a2d81b01fdb837214674e72.tar.gz
nng-4998964a435fe0f02a2d81b01fdb837214674e72.tar.bz2
nng-4998964a435fe0f02a2d81b01fdb837214674e72.zip
fixes #381 Want comparators for various types
Diffstat (limited to 'src')
-rw-r--r--src/core/pipe.c15
-rw-r--r--src/nng.c62
-rw-r--r--src/nng.h27
3 files changed, 83 insertions, 21 deletions
diff --git a/src/core/pipe.c b/src/core/pipe.c
index a4148c65..c619fca0 100644
--- a/src/core/pipe.c
+++ b/src/core/pipe.c
@@ -26,7 +26,7 @@ struct nni_pipe {
nni_list_node p_ep_node;
nni_sock * p_sock;
nni_ep * p_ep;
- int p_reap;
+ bool p_reap;
int p_stop;
int p_refcnt;
nni_mtx p_mtx;
@@ -142,8 +142,12 @@ nni_pipe_find(nni_pipe **pp, uint32_t id)
nni_pipe *p;
nni_mtx_lock(&nni_pipe_lk);
if ((rv = nni_idhash_find(nni_pipes, id, (void **) &p)) == 0) {
- p->p_refcnt++;
- *pp = p;
+ if (p->p_reap) {
+ rv = NNG_ECLOSED;
+ } else {
+ p->p_refcnt++;
+ *pp = p;
+ }
}
nni_mtx_unlock(&nni_pipe_lk);
return (rv);
@@ -186,12 +190,12 @@ void
nni_pipe_close(nni_pipe *p)
{
nni_mtx_lock(&p->p_mtx);
- if (p->p_reap == 1) {
+ if (p->p_reap) {
// We already did a close.
nni_mtx_unlock(&p->p_mtx);
return;
}
- p->p_reap = 1;
+ p->p_reap = true;
// Close the underlying transport.
if (p->p_tran_data != NULL) {
@@ -267,6 +271,7 @@ nni_pipe_create(nni_ep *ep, void *tdata)
p->p_proto_data = NULL;
p->p_ep = ep;
p->p_sock = sock;
+ p->p_reap = false;
NNI_LIST_NODE_INIT(&p->p_reap_node);
NNI_LIST_NODE_INIT(&p->p_sock_node);
diff --git a/src/nng.c b/src/nng.c
index a4b8fd27..4dc6c3fa 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -46,6 +46,12 @@ nng_close(nng_socket s)
return (rv);
}
+int
+nng_socket_id(nng_socket s)
+{
+ return (((int) s.id > 0) ? (int) s.id : -1);
+}
+
void
nng_closeall(void)
{
@@ -230,12 +236,12 @@ nng_send_aio(nng_socket s, nng_aio *aio)
}
int
-nng_ctx_open(nng_ctx *idp, nng_socket s)
+nng_ctx_open(nng_ctx *cp, nng_socket s)
{
nni_sock *sock;
nni_ctx * ctx;
int rv;
- nng_ctx id;
+ nng_ctx c;
if ((rv = nni_sock_find(&sock, s.id)) != 0) {
return (rv);
@@ -244,20 +250,20 @@ nng_ctx_open(nng_ctx *idp, nng_socket s)
nni_sock_rele(sock);
return (rv);
}
- id.id = nni_ctx_id(ctx);
- *idp = id;
+ c.id = nni_ctx_id(ctx);
nni_ctx_rele(ctx);
nni_sock_rele(sock);
+ *cp = c;
return (0);
}
int
-nng_ctx_close(nng_ctx cid)
+nng_ctx_close(nng_ctx c)
{
int rv;
nni_ctx *ctx;
- if ((rv = nni_ctx_find(&ctx, cid.id, true)) != 0) {
+ if ((rv = nni_ctx_find(&ctx, c.id, true)) != 0) {
return (rv);
}
// no release, close releases implicitly.
@@ -265,6 +271,12 @@ nng_ctx_close(nng_ctx cid)
return (0);
}
+int
+nng_ctx_id(nng_ctx c)
+{
+ return (((int) c.id > 0) ? (int) c.id : -1);
+}
+
void
nng_ctx_recv(nng_ctx cid, nng_aio *aio)
{
@@ -497,6 +509,12 @@ nng_listener_start(nng_listener l, int flags)
}
int
+nng_listener_id(nng_listener l)
+{
+ return (((int) l.id > 0) ? (int) l.id : -1);
+}
+
+int
nng_dialer_create(nng_dialer *dp, nng_socket s, const char *addr)
{
nni_sock * sock;
@@ -532,6 +550,12 @@ nng_dialer_start(nng_dialer d, int flags)
return (rv);
}
+int
+nng_dialer_id(nng_dialer d)
+{
+ return (((int) d.id > 0) ? (int) d.id : -1);
+}
+
static int
nng_ep_setx(
uint32_t id, const char *n, const void *v, size_t sz, int mode, int t)
@@ -1088,19 +1112,19 @@ nng_strerror(int num)
}
static int
-nng_pipe_getx(nng_pipe id, const char *name, void *val, size_t *szp, int t)
+nng_pipe_getx(nng_pipe p, const char *name, void *val, size_t *szp, int t)
{
int rv;
- nni_pipe *p;
+ nni_pipe *pipe;
if ((rv = nni_init()) < 0) {
return (rv);
}
- if ((rv = nni_pipe_find(&p, id.id)) != 0) {
+ if ((rv = nni_pipe_find(&pipe, p.id)) != 0) {
return (rv);
}
- rv = nni_pipe_getopt(p, name, val, szp, t);
- nni_pipe_rele(p);
+ rv = nni_pipe_getopt(pipe, name, val, szp, t);
+ nni_pipe_rele(pipe);
return (rv);
}
@@ -1167,19 +1191,25 @@ nng_pipe_getopt_string(nng_pipe p, const char *name, char **valp)
}
int
-nng_pipe_close(nng_pipe pid)
+nng_pipe_close(nng_pipe p)
{
int rv;
- nni_pipe *p;
+ nni_pipe *pipe;
- if ((rv = nni_pipe_find(&p, pid.id)) != 0) {
+ if ((rv = nni_pipe_find(&pipe, p.id)) != 0) {
return (rv);
}
- nni_pipe_close(p);
- nni_pipe_rele(p);
+ nni_pipe_close(pipe);
+ nni_pipe_rele(pipe);
return (0);
}
+int
+nng_pipe_id(nng_pipe p)
+{
+ return (((int) p.id > 0) ? (int) p.id : -1);
+}
+
// Message handling.
int
nng_msg_alloc(nng_msg **msgp, size_t size)
diff --git a/src/nng.h b/src/nng.h
index f45c5bff..c3ae48ac 100644
--- a/src/nng.h
+++ b/src/nng.h
@@ -92,6 +92,15 @@ typedef struct nng_snapshot nng_snapshot;
typedef struct nng_stat nng_stat;
typedef struct nng_aio nng_aio;
+// Initializers.
+// clang-format off
+#define NNG_PIPE_INITIALIZER { 0 }
+#define NNG_SOCKET_INITIALIZER { 0 }
+#define NNG_DIALER_INITIALIZER { 0 }
+#define NNG_LISTENER_INITIALIZER { 0 }
+#define NNG_CTX_INITIALIZER { 0 }
+// clang-format on
+
// Some address details. This is in some ways like a traditional sockets
// sockaddr, but we have our own to cope with our unique families, etc.
// The details of this structure are directly exposed to applications.
@@ -181,6 +190,10 @@ NNG_DECL void nng_fini(void);
// resources.
NNG_DECL int nng_close(nng_socket);
+// nng_socket_id returns the positive socket id for the socket, or -1
+// if the socket is not valid.
+NNG_DECL int nng_socket_id(nng_socket);
+
// nng_closeall closes all open sockets. Do not call this from
// a library; it will affect all sockets.
NNG_DECL void nng_closeall(void);
@@ -249,6 +262,14 @@ NNG_DECL int nng_dialer_close(nng_dialer);
// connections and releasing all associated resources.
NNG_DECL int nng_listener_close(nng_listener);
+// nng_dialer_id returns the positive dialer ID, or -1 if the dialer is
+// invalid.
+NNG_DECL int nng_dialer_id(nng_dialer);
+
+// nng_listener_id returns the positive listener ID, or -1 if the listener is
+// invalid.
+NNG_DECL int nng_listener_id(nng_listener);
+
// nng_dialer_setopt sets an option for a specific dialer. Note
// dialer options may not be altered on a running dialer.
NNG_DECL int nng_dialer_setopt(nng_dialer, const char *, const void *, size_t);
@@ -379,6 +400,11 @@ NNG_DECL int nng_ctx_open(nng_ctx *, nng_socket);
// nng_ctx_close closes the context.
NNG_DECL int nng_ctx_close(nng_ctx);
+// nng_ctx_id returns the numeric id for the context; this will be
+// a postive value for a valid context, or < 0 for an invalid context.
+// A valid context is not necessarily an *open* context.
+NNG_DECL int nng_ctx_id(nng_ctx);
+
// nng_ctx_recv receives asynchronously. It works like nng_recv_aio, but
// uses a local context instead of the socket global context.
NNG_DECL void nng_ctx_recv(nng_ctx, nng_aio *);
@@ -573,6 +599,7 @@ NNG_DECL int nng_pipe_getopt_uint64(nng_pipe, const char *, uint64_t *);
NNG_DECL int nng_pipe_getopt_ptr(nng_pipe, const char *, void **);
NNG_DECL int nng_pipe_getopt_string(nng_pipe, const char *, char **);
NNG_DECL int nng_pipe_close(nng_pipe);
+NNG_DECL int nng_pipe_id(nng_pipe);
// Flags.
enum nng_flag_enum {