aboutsummaryrefslogtreecommitdiff
path: root/src/core/endpt.c
blob: ba557e3ac2ae8f988ffb6caaee136308834877b2 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// 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;
	ep->ep_start = 0;
	ep->ep_pipe = NULL;
	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);
	}
	*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 != NULL) {
		nni_thread_reap(ep->ep_dialer);
	}
	if (ep->ep_listener != NULL) {
		nni_thread_reap(ep->ep_listener);
	}

	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_pipe *pipe;

	nni_mutex_enter(&ep->ep_mx);
	if (ep->ep_close) {
		nni_mutex_exit(&ep->ep_mx);
		return;
	}
	ep->ep_close = 1;
	ep->ep_ops.ep_close(ep->ep_data);
	if ((pipe = ep->ep_pipe) != NULL) {
		pipe->p_ep = NULL;
		ep->ep_pipe = NULL;
	}
	nni_cond_broadcast(&ep->ep_cv);
	nni_mutex_exit(&ep->ep_mx);
}


int
nni_endpt_listen(nni_endpt *ep)
{
	if (ep->ep_close) {
		return (NNG_ECLOSED);
	}
	return (ep->ep_ops.ep_listen(ep->ep_data));
}


int
nni_endpt_dial(nni_endpt *ep, nni_pipe **pp)
{
	nni_pipe *pipe;
	int rv;

	if (ep->ep_close) {
		return (NNG_ECLOSED);
	}
	if ((rv = nni_pipe_create(&pipe, ep->ep_ops.ep_pipe_ops)) != 0) {
		return (rv);
	}
	if ((rv = ep->ep_ops.ep_dial(ep->ep_data, &pipe->p_data)) != 0) {
		nni_pipe_destroy(pipe);
		return (rv);
	}
	pipe->p_ep = ep;
	*pp = pipe;
	return (0);
}


int
nni_endpt_accept(nni_endpt *ep, nni_pipe **pp)
{
	nni_pipe *pipe;
	int rv;

	if (ep->ep_close) {
		return (NNG_ECLOSED);
	}
	if ((rv = nni_pipe_create(&pipe, ep->ep_ops.ep_pipe_ops)) != 0) {
		return (rv);
	}
	if ((rv = ep->ep_ops.ep_accept(ep->ep_data, &pipe->p_data)) != 0) {
		nni_pipe_destroy(pipe);
		return (rv);
	}
	pipe->p_ep = ep;
	*pp = pipe;
	return (0);
}