aboutsummaryrefslogtreecommitdiff
path: root/src/core/endpt.c
blob: b7181ce86ae193ddb0fa0c5254161036fbbe9df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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