aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/defs.h4
-rw-r--r--src/core/endpt.c18
-rw-r--r--src/core/endpt.h11
-rw-r--r--src/core/pipe.h2
-rw-r--r--src/nng.c28
-rw-r--r--src/nng.h28
-rw-r--r--tests/trantest.h34
7 files changed, 82 insertions, 43 deletions
diff --git a/src/core/defs.h b/src/core/defs.h
index 50540102..eb1a6475 100644
--- a/src/core/defs.h
+++ b/src/core/defs.h
@@ -23,14 +23,14 @@
// These types are common but have names shared with user space.
typedef struct nng_socket nni_sock;
-typedef struct nng_endpoint nni_ep;
-typedef struct nng_pipe nni_pipe;
typedef struct nng_msg nni_msg;
typedef struct nng_sockaddr nni_sockaddr;
typedef struct nng_event nni_event;
typedef struct nng_notify nni_notify;
// These are our own names.
+typedef struct nni_ep nni_ep;
+typedef struct nni_pipe nni_pipe;
typedef struct nni_tran nni_tran;
typedef struct nni_tran_ep nni_tran_ep;
typedef struct nni_tran_pipe nni_tran_pipe;
diff --git a/src/core/endpt.c b/src/core/endpt.c
index 09ef0884..58b30b9f 100644
--- a/src/core/endpt.c
+++ b/src/core/endpt.c
@@ -38,6 +38,14 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr)
ep->ep_pipe = NULL;
ep->ep_tran = tran;
+ nni_mtx_lock(nni_idlock);
+ rv = nni_idhash_alloc(nni_endpoints, &ep->ep_id, ep);
+ nni_mtx_unlock(nni_idlock);
+ if (rv != 0) {
+ NNI_FREE_STRUCT(ep);
+ return (rv);
+ }
+
// Make a copy of the endpoint operations. This allows us to
// modify them (to override NULLs for example), and avoids an extra
// dereference on hot paths.
@@ -45,6 +53,9 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr)
NNI_LIST_NODE_INIT(&ep->ep_node);
if ((rv = nni_cv_init(&ep->ep_cv, &ep->ep_sock->s_mx)) != 0) {
+ nni_mtx_lock(nni_idlock);
+ nni_idhash_remove(nni_endpoints, ep->ep_id);
+ nni_mtx_unlock(nni_idlock);
NNI_FREE_STRUCT(ep);
return (NNG_ENOMEM);
}
@@ -54,6 +65,9 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr)
rv = ep->ep_ops.ep_init(&ep->ep_data, addr, nni_sock_proto(sock));
if (rv != 0) {
+ nni_mtx_lock(nni_idlock);
+ nni_idhash_remove(nni_endpoints, ep->ep_id);
+ nni_mtx_unlock(nni_idlock);
nni_cv_fini(&ep->ep_cv);
NNI_FREE_STRUCT(ep);
return (rv);
@@ -92,6 +106,10 @@ nni_ep_close(nni_ep *ep)
ep->ep_ops.ep_fini(ep->ep_data);
nni_cv_fini(&ep->ep_cv);
+ nni_mtx_lock(nni_idlock);
+ nni_idhash_remove(nni_endpoints, ep->ep_id);
+ nni_mtx_unlock(nni_idlock);
+
NNI_FREE_STRUCT(ep);
}
diff --git a/src/core/endpt.h b/src/core/endpt.h
index bff0ec6d..a3b2ce9e 100644
--- a/src/core/endpt.h
+++ b/src/core/endpt.h
@@ -15,11 +15,12 @@
// NB: This structure is supplied here for use by the CORE. Use of this
// OUSIDE of the core is STRICTLY VERBOTEN. NO DIRECT ACCESS BY PROTOCOLS
// OR TRANSPORTS.
-struct nng_endpoint {
- nni_tran_ep ep_ops;
- nni_tran * ep_tran;
- void * ep_data; // Transport private
- nni_list_node ep_node; // Per socket list
+struct nni_ep {
+ nni_tran_ep ep_ops; // transport ops
+ nni_tran * ep_tran; // transport pointer
+ void * ep_data; // transport private
+ uint32_t ep_id; // endpoint id
+ nni_list_node ep_node; // per socket list
nni_sock * ep_sock;
char ep_addr[NNG_MAXADDRLEN];
nni_thr ep_thr;
diff --git a/src/core/pipe.h b/src/core/pipe.h
index 65d2ede5..d54f1ce7 100644
--- a/src/core/pipe.h
+++ b/src/core/pipe.h
@@ -17,7 +17,7 @@
#include "core/defs.h"
#include "core/transport.h"
-struct nng_pipe {
+struct nni_pipe {
uint32_t p_id;
nni_tran_pipe p_tran_ops;
void * p_tran_data;
diff --git a/src/nng.c b/src/nng.c
index 723b230e..77ebebaf 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -113,18 +113,34 @@ nng_sendmsg(nng_socket *s, nng_msg *msg, int flags)
int
-nng_dial(nng_socket *s, const char *addr, nng_endpoint **epp, int flags)
+nng_dial(nng_socket *s, const char *addr, nng_endpoint *epp, int flags)
{
+ nni_ep *ep;
+ int rv;
+
NNI_INIT_INT();
- return (nni_sock_dial(s, addr, epp, flags));
+ if ((rv = nni_sock_dial(s, addr, &ep, flags)) == 0) {
+ if (epp != NULL) {
+ *epp = ep->ep_id;
+ }
+ }
+ return (rv);
}
int
-nng_listen(nng_socket *s, const char *addr, nng_endpoint **epp, int flags)
+nng_listen(nng_socket *s, const char *addr, nng_endpoint *epp, int flags)
{
+ nni_ep *ep;
+ int rv;
+
NNI_INIT_INT();
- return (nni_sock_listen(s, addr, epp, flags));
+ if ((rv = nni_sock_listen(s, addr, &ep, flags)) == 0) {
+ if (epp != NULL) {
+ *epp = ep->ep_id;
+ }
+ }
+ return (rv);
}
@@ -248,6 +264,7 @@ nng_strerror(int num)
}
+#if 0
int
nng_pipe_getopt(nng_pipe *pipe, int opt, void *val, size_t *sizep)
{
@@ -272,6 +289,9 @@ nng_pipe_close(nng_pipe *pipe)
}
+#endif
+
+
// Message handling.
int
nng_msg_alloc(nng_msg **msgp, size_t size)
diff --git a/src/nng.h b/src/nng.h
index 8dcfb4de..9db80f68 100644
--- a/src/nng.h
+++ b/src/nng.h
@@ -43,8 +43,8 @@ extern "C" {
// Types common to nng.
typedef struct nng_socket nng_socket;
-typedef struct nng_endpoint nng_endpoint;
-typedef struct nng_pipe nng_pipe;
+typedef uint32_t nng_endpoint;
+typedef uint32_t nng_pipe;
typedef struct nng_msg nng_msg;
typedef struct nng_event nng_event;
typedef struct nng_notify nng_notify;
@@ -126,8 +126,8 @@ NNG_DECL void nng_unsetnotify(nng_socket *, nng_notify *);
// the value returned will be NULL.
NNG_DECL int nng_event_type(nng_event *);
NNG_DECL nng_socket *nng_event_socket(nng_event *);
-NNG_DECL nng_endpoint *nng_event_endpoint(nng_event *);
-NNG_DECL nng_pipe *nng_event_pipe(nng_event *);
+NNG_DECL nng_endpoint nng_event_endpoint(nng_event *);
+NNG_DECL nng_pipe nng_event_pipe(nng_event *);
NNG_DECL const char *nng_event_reason(nng_event *);
// nng_listen creates a listening endpoint with no special options,
@@ -136,7 +136,7 @@ NNG_DECL const char *nng_event_reason(nng_event *);
// endpoint pointer, if it is not NULL. The flags may be NNG_FLAG_SYNCH to
// indicate that a failure setting the socket up should return an error
// back to the caller immediately.
-NNG_DECL int nng_listen(nng_socket *, const char *, nng_endpoint **, int);
+NNG_DECL int nng_listen(nng_socket *, const char *, nng_endpoint *, int);
// nng_dial creates a dialing endpoint, with no special options, and
// starts it dialing. Dialers have at most one active connection at a time
@@ -146,31 +146,31 @@ NNG_DECL int nng_listen(nng_socket *, const char *, nng_endpoint **, int);
// dial will be made synchronously, and a failure condition returned back
// to the caller. (If the connection is dropped, it will still be
// reconnected in the background -- only the initial connect is synchronous.)
-NNG_DECL int nng_dial(nng_socket *, const char *, nng_endpoint **, int);
+NNG_DECL int nng_dial(nng_socket *, const char *, nng_endpoint *, int);
// nng_endpoint_create creates an endpoint on the socket, but does not
// start it either dialing or listening.
-NNG_DECL int nng_endpoint_create(nng_endpoint **, nng_socket *, const char *);
+NNG_DECL int nng_endpoint_create(nng_endpoint *, nng_socket *, const char *);
// nng_endpoint_dial starts the endpoint dialing. This is only possible if
// the endpoint is not already dialing or listening.
-NNG_DECL int nng_endpoint_dial(nng_endpoint *, int);
+NNG_DECL int nng_endpoint_dial(nng_endpoint, int);
// nng_endpoint_listen starts the endpoint listening. This is only possible if
// the endpoint is not already dialing or listening.
-NNG_DECL int nng_endpoint_listen(nng_endpoint *, int);
+NNG_DECL int nng_endpoint_listen(nng_endpoint, int);
// nng_endpoint_close closes the endpoint, shutting down all underlying
// connections and releasing all associated resources. It is an error to
// refer to the endpoint after this is called.
-NNG_DECL int nng_endpoint_close(nng_endpoint *);
+NNG_DECL int nng_endpoint_close(nng_endpoint);
// nng_endpoint_setopt sets an option for a specific endpoint. Note
// endpoint options may not be altered on a running endpoint.
-NNG_DECL int nng_endpoint_setopt(nng_endpoint *, int, void *, size_t);
+NNG_DECL int nng_endpoint_setopt(nng_endpoint, int, void *, size_t);
// nng_endpoint_getopt obtains the option for an endpoint.
-NNG_DECL int nng_endpoint_getopt(nng_endpoint *, int, void *, size_t *);
+NNG_DECL int nng_endpoint_getopt(nng_endpoint, int, void *, size_t *);
// nng_strerror returns a human readable string associated with the error
// code supplied.
@@ -226,8 +226,8 @@ NNG_DECL int nng_msg_getopt(nng_msg *, int, void *, size_t *);
// we do permit an application to close a pipe. This can be useful, for
// example during a connection notification, to disconnect a pipe that
// is associated with an invalid or untrusted remote peer.
-NNG_DECL int nng_pipe_getopt(nng_pipe *, int, void *, size_t *);
-NNG_DECL int nng_pipe_close(nng_pipe *);
+NNG_DECL int nng_pipe_getopt(nng_pipe, int, void *, size_t *);
+NNG_DECL int nng_pipe_close(nng_pipe);
// Flags.
#define NNG_FLAG_ALLOC 1 // Recv to allocate receive buffer.
diff --git a/tests/trantest.h b/tests/trantest.h
index 6956c1fe..23081c35 100644
--- a/tests/trantest.h
+++ b/tests/trantest.h
@@ -55,12 +55,12 @@ void
trantest_conn_refused(trantest *tt)
{
Convey("Connection refused works", {
- nng_endpoint *ep = NULL;
+ nng_endpoint ep = 0;
So(nng_dial(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == NNG_ECONNREFUSED);
- So(ep == NULL);
+ So(ep == 0);
So(nng_dial(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == NNG_ECONNREFUSED);
- So(ep == NULL);
+ So(ep == 0);
})
}
@@ -68,12 +68,12 @@ void
trantest_duplicate_listen(trantest *tt)
{
Convey("Duplicate listen rejected", {
- nng_endpoint *ep;
+ nng_endpoint ep;
So(nng_listen(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
- So(ep != NULL);
- ep = NULL;
+ So(ep != 0);
+ ep = 0;
So(nng_listen(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == NNG_EADDRINUSE);
- So(ep == NULL);
+ So(ep == 0);
})
}
@@ -81,14 +81,14 @@ void
trantest_listen_accept(trantest *tt)
{
Convey("Listen and accept" ,{
- nng_endpoint *ep;
- ep = NULL;
+ nng_endpoint ep;
+ ep = 0;
So(nng_listen(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
- So(ep != NULL);
+ So(ep != 0);
- ep = NULL;
+ ep = 0;
So(nng_dial(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
- So(ep != NULL);
+ So(ep != 0);
})
}
@@ -96,17 +96,17 @@ void
trantest_send_recv(trantest *tt)
{
Convey("Send and recv", {
- nng_endpoint *ep = NULL;
+ nng_endpoint ep = 0;
nng_msg *send;
nng_msg *recv;
size_t len;
- ep = NULL;
+ ep = 0;
So(nng_listen(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
- So(ep != NULL);
- ep = NULL;
+ So(ep != 0);
+ ep = 0;
So(nng_dial(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
- So(ep != NULL);
+ So(ep != 0);
send = NULL;
So(nng_msg_alloc(&send, 0) == 0);