aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-14 15:27:38 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-14 15:27:38 -0700
commit63479c2938cbc80c1aac9367cb95564f6e7540e1 (patch)
tree495584b637f73b593d25f01577eeaa944477f159 /src
parent343417234aa3fd86e8ae0b56ae500a1ed3411cfc (diff)
downloadnng-63479c2938cbc80c1aac9367cb95564f6e7540e1.tar.gz
nng-63479c2938cbc80c1aac9367cb95564f6e7540e1.tar.bz2
nng-63479c2938cbc80c1aac9367cb95564f6e7540e1.zip
fixes #63 NNG_FLAG_SYNCH should be the default
Also enables creating endpoints that are idle (first part of endpoint options API) and shutting down endpoints.
Diffstat (limited to 'src')
-rw-r--r--src/core/endpt.c39
-rw-r--r--src/nng.c60
-rw-r--r--src/nng.h7
-rw-r--r--src/nng_compat.c5
-rw-r--r--src/transport/inproc/inproc.c1
5 files changed, 86 insertions, 26 deletions
diff --git a/src/core/endpt.c b/src/core/endpt.c
index 6e5f7e8a..0ab35ea3 100644
--- a/src/core/endpt.c
+++ b/src/core/endpt.c
@@ -23,9 +23,9 @@ struct nni_ep {
nni_sock * ep_sock;
char ep_addr[NNG_MAXADDRLEN];
int ep_mode;
+ int ep_started;
int ep_closed; // full shutdown
int ep_closing; // close pending (waiting on refcnt)
- int ep_bound; // true if we bound locally
int ep_refcnt;
nni_mtx ep_mtx;
nni_cv ep_cv;
@@ -132,13 +132,13 @@ nni_ep_create(nni_ep **epp, nni_sock *s, const char *addr, int mode)
if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) {
return (NNG_ENOMEM);
}
- ep->ep_closed = 0;
- ep->ep_bound = 0;
- ep->ep_data = NULL;
- ep->ep_refcnt = 1;
- ep->ep_sock = s;
- ep->ep_tran = tran;
- ep->ep_mode = mode;
+ ep->ep_closed = 0;
+ ep->ep_started = 0;
+ ep->ep_data = NULL;
+ ep->ep_refcnt = 1;
+ ep->ep_sock = s;
+ ep->ep_tran = tran;
+ ep->ep_mode = mode;
// Make a copy of the endpoint operations. This allows us to
// modify them (to override NULLs for example), and avoids an extra
@@ -411,7 +411,12 @@ nni_ep_dial(nni_ep *ep, int flags)
return (NNG_ECLOSED);
}
- if ((flags & NNG_FLAG_SYNCH) == 0) {
+ if (ep->ep_started) {
+ nni_mtx_unlock(&ep->ep_mtx);
+ return (NNG_ESTATE);
+ }
+
+ if ((flags & NNG_FLAG_NONBLOCK) != 0) {
nni_ep_con_start(ep);
nni_mtx_unlock(&ep->ep_mtx);
return (0);
@@ -421,16 +426,18 @@ nni_ep_dial(nni_ep *ep, int flags)
aio = &ep->ep_con_syn;
aio->a_endpt = ep->ep_data;
ep->ep_ops.ep_connect(ep->ep_data, aio);
+ ep->ep_started = 1;
nni_mtx_unlock(&ep->ep_mtx);
nni_aio_wait(aio);
// As we're synchronous, we also have to handle the completion.
- if ((rv = nni_aio_result(aio)) == 0) {
- NNI_ASSERT(aio->a_pipe != NULL);
- rv = nni_pipe_create(ep, aio->a_pipe);
+ if (((rv = nni_aio_result(aio)) != 0) ||
+ ((rv = nni_pipe_create(ep, aio->a_pipe)) != 0)) {
+ nni_mtx_lock(&ep->ep_mtx);
+ ep->ep_started = 0;
+ nni_mtx_unlock(&ep->ep_mtx);
}
-
return (rv);
}
@@ -503,14 +510,18 @@ nni_ep_listen(nni_ep *ep, int flags)
nni_mtx_unlock(&ep->ep_mtx);
return (NNG_ECLOSED);
}
+ if (ep->ep_started) {
+ nni_mtx_unlock(&ep->ep_mtx);
+ return (NNG_ESTATE);
+ }
rv = ep->ep_ops.ep_bind(ep->ep_data);
if (rv != 0) {
nni_mtx_unlock(&ep->ep_mtx);
return (rv);
}
- ep->ep_bound = 1;
+ ep->ep_started = 1;
nni_ep_acc_start(ep);
nni_mtx_unlock(&ep->ep_mtx);
diff --git a/src/nng.c b/src/nng.c
index 421ce74a..21543dbc 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -278,18 +278,68 @@ nng_listen(nng_socket sid, const char *addr, nng_listener *lp, int flags)
}
int
+nng_listener_create(nng_listener *lp, nng_socket sid, const char *addr)
+{
+ nni_sock *s;
+ nni_ep * ep;
+ int rv;
+
+ if ((rv = nni_sock_find(&s, sid)) != 0) {
+ return (rv);
+ }
+ if ((rv = nni_ep_create_listener(&ep, s, addr)) != 0) {
+ nni_sock_rele(s);
+ return (rv);
+ }
+ *lp = nni_ep_id(ep);
+ nni_ep_rele(ep);
+ nni_sock_rele(s);
+ return (0);
+}
+
+int
+nng_dialer_create(nng_dialer *dp, nng_socket sid, const char *addr)
+{
+ nni_sock *s;
+ nni_ep * ep;
+ int rv;
+
+ if ((rv = nni_sock_find(&s, sid)) != 0) {
+ return (rv);
+ }
+ if ((rv = nni_ep_create_dialer(&ep, s, addr)) != 0) {
+ nni_sock_rele(s);
+ return (rv);
+ }
+ *dp = nni_ep_id(ep);
+ nni_ep_rele(ep);
+ nni_sock_rele(s);
+ return (0);
+}
+
+static int
+nng_ep_close(uint32_t id)
+{
+ nni_ep *ep;
+ int rv;
+
+ if ((rv = nni_ep_find(&ep, id)) != 0) {
+ return (rv);
+ }
+ nni_ep_close(ep);
+ return (0);
+}
+
+int
nng_dialer_close(nng_dialer d)
{
- // return (nni_ep_close());
- NNI_ARG_UNUSED(d);
- return (NNG_ENOTSUP);
+ return (nng_ep_close((uint32_t) d));
}
int
nng_listener_close(nng_listener l)
{
- NNI_ARG_UNUSED(l);
- return (NNG_ENOTSUP);
+ return (nng_ep_close((uint32_t) l));
}
int
diff --git a/src/nng.h b/src/nng.h
index 6cf581a2..8d9a24c2 100644
--- a/src/nng.h
+++ b/src/nng.h
@@ -176,10 +176,10 @@ NNG_DECL int nng_listen(nng_socket, const char *, nng_listener *, int);
NNG_DECL int nng_dial(nng_socket, const char *, nng_dialer *, int);
// nng_dialer_create creates a new dialer, that is not yet started.
-NNG_DECL int nng_dialer_create(nng_socket, const char *, nng_dialer *);
+NNG_DECL int nng_dialer_create(nng_dialer *, nng_socket, const char *);
// nng_listener_create creates a new listener, that is not yet started.
-NNG_DECL int nng_listener_create(nng_socket, const char *, nng_listener *);
+NNG_DECL int nng_listener_create(nng_listener *, nng_socket, const char *);
// nng_dialer_start starts the endpoint dialing. This is only possible if
// the dialer is not already dialing.
@@ -301,8 +301,7 @@ NNG_DECL int nng_pipe_close(nng_pipe);
// Flags.
enum nng_flag_enum {
NNG_FLAG_ALLOC = 1, // Recv to allocate receive buffer.
- NNG_FLAG_NONBLOCK = 2, // Non-block send/recv.
- NNG_FLAG_SYNCH = 4, // Synchronous dial / listen
+ NNG_FLAG_NONBLOCK = 2, // Non-blocking operations.
};
// Protocol numbers. These are to be used with nng_socket_create().
diff --git a/src/nng_compat.c b/src/nng_compat.c
index 48b406eb..bb0c9faa 100644
--- a/src/nng_compat.c
+++ b/src/nng_compat.c
@@ -162,7 +162,7 @@ nn_bind(int s, const char *addr)
int rv;
nng_listener l;
- if ((rv = nng_listen((nng_socket) s, addr, &l, NNG_FLAG_SYNCH)) != 0) {
+ if ((rv = nng_listen((nng_socket) s, addr, &l, 0)) != 0) {
nn_seterror(rv);
return (-1);
}
@@ -175,7 +175,8 @@ nn_connect(int s, const char *addr)
int rv;
nng_dialer d;
- if ((rv = nng_dial((nng_socket) s, addr, &d, 0)) != 0) {
+ if ((rv = nng_dial((nng_socket) s, addr, &d, NNG_FLAG_NONBLOCK)) !=
+ 0) {
nn_seterror(rv);
return (-1);
}
diff --git a/src/transport/inproc/inproc.c b/src/transport/inproc/inproc.c
index 37d7153f..3bc24c41 100644
--- a/src/transport/inproc/inproc.c
+++ b/src/transport/inproc/inproc.c
@@ -233,7 +233,6 @@ nni_inproc_ep_fini(void *arg)
{
nni_inproc_ep *ep = arg;
- NNI_ASSERT(ep->closed);
NNI_FREE_STRUCT(ep);
}