blob: e36bc31f96e4c0971ffa2b857220856be2e88a62 (
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
|
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// 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 <string.h>
#include "core/nng_impl.h"
// Protocol related stuff - generically.
typedef struct nni_protocol nni_protocol;
struct nni_protocol {
const nni_proto *p_proto;
nni_list_node p_link;
};
static nni_mtx nni_proto_lk;
static nni_list nni_proto_list;
static int nni_proto_inited = 0;
static int
nni_proto_init(const nni_proto *proto)
{
nni_protocol *p;
int rv;
nni_mtx_lock(&nni_proto_lk);
NNI_LIST_FOREACH (&nni_proto_list, p) {
if (p->p_proto == proto) {
nni_mtx_unlock(&nni_proto_lk);
return (0);
}
}
if ((p = NNI_ALLOC_STRUCT(p)) == NULL) {
nni_mtx_unlock(&nni_proto_lk);
return (NNG_ENOMEM);
}
NNI_LIST_NODE_INIT(&p->p_link);
p->p_proto = proto;
if ((proto->proto_init != NULL) && ((rv = proto->proto_init()) != 0)) {
NNI_FREE_STRUCT(p);
nni_mtx_unlock(&nni_proto_lk);
return (rv);
}
nni_list_append(&nni_proto_list, p);
nni_mtx_unlock(&nni_proto_lk);
return (0);
}
int
nni_proto_open(nng_socket *sockidp, const nni_proto *proto)
{
int rv;
nni_sock *sock;
if (((rv = nni_init()) != 0) || ((rv = nni_proto_init(proto)) != 0)) {
return (rv);
}
if ((rv = nni_sock_open(&sock, proto)) == 0) {
*sockidp = nni_sock_id(sock); // Keep socket held open.
}
return (rv);
}
int
nni_proto_sys_init(void)
{
NNI_LIST_INIT(&nni_proto_list, nni_protocol, p_link);
nni_mtx_init(&nni_proto_lk);
nni_proto_inited = 1;
return (0);
}
void
nni_proto_sys_fini(void)
{
if (nni_proto_inited) {
nni_protocol *p;
nni_mtx_lock(&nni_proto_lk);
while ((p = nni_list_first(&nni_proto_list)) != NULL) {
nni_list_remove(&nni_proto_list, p);
if (p->p_proto->proto_fini != NULL) {
p->p_proto->proto_fini();
}
NNI_FREE_STRUCT(p);
}
nni_mtx_unlock(&nni_proto_lk);
}
nni_proto_inited = 0;
nni_mtx_fini(&nni_proto_lk);
}
|