diff options
Diffstat (limited to 'src/core/endpt.c')
| -rw-r--r-- | src/core/endpt.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/core/endpt.c b/src/core/endpt.c new file mode 100644 index 00000000..b7181ce8 --- /dev/null +++ b/src/core/endpt.c @@ -0,0 +1,123 @@ +// +// Copyright 2016 Garrett D'Amore <garrett@damore.org> +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include "core/nng_impl.h" + +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +// Functionality realited to end points. +#if 0 +struct nng_endpt { + struct nni_endpt_ops ep_ops; + void * ep_data; + nni_list_node_t ep_sock_node; + nni_socket * ep_sock; + char ep_addr[NNG_MAXADDRLEN]; + nni_thread * ep_dialer; + nni_thread * ep_listener; + int ep_close; + nni_mutex ep_mx; + nni_cond ep_cv; +}; +#endif + +int +nni_endpt_create(nni_endpt **epp, nni_socket *sock, const char *addr) +{ + nni_transport *tran; + nni_endpt *ep; + int rv; + + if ((tran = nni_transport_find(addr)) == NULL) { + return (NNG_EINVAL); + } + if (strlen(addr) >= NNG_MAXADDRLEN) { + return (NNG_EINVAL); + } + + if ((ep = nni_alloc(sizeof (*ep))) == NULL) { + return (NNG_ENOMEM); + } + ep->ep_dialer = NULL; + ep->ep_listener = NULL; + ep->ep_close = 0; + if ((rv = nni_mutex_init(&ep->ep_mx)) != 0) { + nni_free(ep, sizeof (*ep)); + return (NNG_ENOMEM); + } + if ((rv = nni_cond_init(&ep->ep_cv, &ep->ep_mx)) != 0) { + nni_mutex_fini(&ep->ep_mx); + nni_free(ep, sizeof (*ep)); + return (NNG_ENOMEM); + } + + // Could safely use strcpy here, but this avoids discussion. + (void) snprintf(ep->ep_addr, sizeof (ep->ep_addr), "%s", addr); + ep->ep_sock = sock; + ep->ep_ops = *tran->tran_ep_ops; + + rv = ep->ep_ops.ep_create(&ep->ep_data, addr, nni_socket_proto(sock)); + if (rv != 0) { + nni_cond_fini(&ep->ep_cv); + nni_mutex_fini(&ep->ep_mx); + nni_free(ep, sizeof (*ep)); + return (rv); + } + NNI_LIST_INIT(&ep->ep_pipes, nni_pipe, p_ep_node); + *epp = ep; + return (0); +} + +void +nni_endpt_destroy(nni_endpt *ep) +{ + // We should already have been closed at this point, so this + // should proceed very quickly. + if (ep->ep_dialer) { + nni_thread_reap(ep->ep_dialer); + } + if (ep->ep_listener) { + nni_thread_reap(ep->ep_listener); + } + nni_mutex_enter(&ep->ep_mx); + while (nni_list_first(&ep->ep_pipes) != NULL) { + nni_cond_wait(&ep->ep_cv); + } + nni_mutex_exit(&ep->ep_mx); + + ep->ep_ops.ep_destroy(ep->ep_data); + + nni_cond_fini(&ep->ep_cv); + nni_mutex_fini(&ep->ep_mx); + nni_free(ep, sizeof (*ep)); +} + +void +nni_endpt_close(nni_endpt *ep) +{ + nni_mutex_enter(&ep->ep_mx); + if (ep->ep_close) { + nni_mutex_exit(&ep->ep_mx); + return; + } + ep->ep_close = 1; + nni_cond_broadcast(&ep->ep_cv); + nni_mutex_exit(&ep->ep_mx); + ep->ep_ops.ep_close(ep->ep_data); +} + + +#if 0 +int nni_endpt_dial(nni_endpt *, nni_pipe **); +int nni_endpt_listen(nni_endpt *); +int nni_endpt_accept(nni_endpt *, nni_pipe **); +int nni_endpt_close(nni_endpt *); +#endif |
