diff options
| author | Garrett D'Amore <garrett@damore.org> | 2021-07-30 17:37:30 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2021-07-31 11:18:28 -0700 |
| commit | 5e5881391bfa6e261ab0f6349a5f12a526e2f293 (patch) | |
| tree | 4e89313b00110a31bfc1736b565b154c81c6f409 /src/sp/transport.c | |
| parent | 2c3700cce723f964cab23ae1d8b73aaa66702f50 (diff) | |
| download | nng-5e5881391bfa6e261ab0f6349a5f12a526e2f293.tar.gz nng-5e5881391bfa6e261ab0f6349a5f12a526e2f293.tar.bz2 nng-5e5881391bfa6e261ab0f6349a5f12a526e2f293.zip | |
Simplify the SP transport initialization process.
Diffstat (limited to 'src/sp/transport.c')
| -rw-r--r-- | src/sp/transport.c | 133 |
1 files changed, 33 insertions, 100 deletions
diff --git a/src/sp/transport.c b/src/sp/transport.c index 7f76af16..dba530e0 100644 --- a/src/sp/transport.c +++ b/src/sp/transport.c @@ -33,132 +33,67 @@ #include <stdio.h> #include <string.h> -// For now the list of transports is hard-wired. Adding new transports -// to the system dynamically is something that might be considered later. -extern nni_sp_tran nni_tcp_tran; -extern nni_sp_tran nni_ipc_tran; +static nni_list sp_tran_list; +static nni_rwlock sp_tran_lk; -typedef struct nni_sp_transport { - nni_sp_tran t_tran; - nni_list_node t_node; -} nni_sp_transport; - -static nni_list nni_sp_tran_list; -static nni_rwlock nni_sp_tran_lk; -static int nni_sp_tran_inited; - -int -nni_sp_tran_register(const nni_sp_tran *tran) +void +nni_sp_tran_register(nni_sp_tran *tran) { - nni_sp_transport *t; - int rv; - - // Its entirely possible that we are called before any sockets - // are opened. Make sure we are initialized. This has to be - // protected by a guard to prevent infinite recursion, since - // nni_init also winds up calling us. - if (!nni_sp_tran_inited) { - nni_init(); - } - - if (tran->tran_version != NNI_TRANSPORT_VERSION) { - return (NNG_ENOTSUP); - } - - nni_rwlock_wrlock(&nni_sp_tran_lk); - // Check to see if the transport is already registered... - NNI_LIST_FOREACH (&nni_sp_tran_list, t) { - if (strcmp(tran->tran_scheme, t->t_tran.tran_scheme) == 0) { - if (tran->tran_init == t->t_tran.tran_init) { - // duplicate. - nni_rwlock_unlock(&nni_sp_tran_lk); - return (0); - } - nni_rwlock_unlock(&nni_sp_tran_lk); - return (NNG_ESTATE); - } - } - if ((t = NNI_ALLOC_STRUCT(t)) == NULL) { - nni_rwlock_unlock(&nni_sp_tran_lk); - return (NNG_ENOMEM); - } - - t->t_tran = *tran; - if ((rv = t->t_tran.tran_init()) != 0) { - nni_rwlock_unlock(&nni_sp_tran_lk); - NNI_FREE_STRUCT(t); - return (rv); + nni_rwlock_wrlock(&sp_tran_lk); + if (!nni_list_node_active(&tran->tran_link)) { + tran->tran_init(); + nni_list_append(&sp_tran_list, tran); } - nni_list_append(&nni_sp_tran_list, t); - nni_rwlock_unlock(&nni_sp_tran_lk); - return (0); + nni_rwlock_unlock(&sp_tran_lk); } nni_sp_tran * nni_sp_tran_find(nni_url *url) { // address is of the form "<scheme>://blah..." - nni_sp_transport *t; + nni_sp_tran *t; - nni_rwlock_rdlock(&nni_sp_tran_lk); - NNI_LIST_FOREACH (&nni_sp_tran_list, t) { - if (strcmp(url->u_scheme, t->t_tran.tran_scheme) == 0) { - nni_rwlock_unlock(&nni_sp_tran_lk); - return (&t->t_tran); + nni_rwlock_rdlock(&sp_tran_lk); + NNI_LIST_FOREACH (&sp_tran_list, t) { + if (strcmp(url->u_scheme, t->tran_scheme) == 0) { + nni_rwlock_unlock(&sp_tran_lk); + return (t); } } - nni_rwlock_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&sp_tran_lk); return (NULL); } // nni_sp_tran_sys_init initializes the entire transport subsystem, including // each individual transport. -typedef int (*nni_sp_tran_ctor)(void); +int +nni_sp_tran_sys_init(void) +{ + NNI_LIST_INIT(&sp_tran_list, nni_sp_tran, tran_link); + nni_rwlock_init(&sp_tran_lk); -// These are just the statically compiled in constructors. -// In the future we might want to support dynamic additions. -static nni_sp_tran_ctor nni_sp_tran_ctors[] = { #ifdef NNG_TRANSPORT_INPROC - nng_inproc_register, + nng_inproc_register(); #endif #ifdef NNG_TRANSPORT_IPC - nng_ipc_register, + nng_ipc_register(); #endif #ifdef NNG_TRANSPORT_TCP - nng_tcp_register, + nng_tcp_register(); #endif #ifdef NNG_TRANSPORT_TLS - nng_tls_register, + nng_tls_register(); #endif #ifdef NNG_TRANSPORT_WS - nng_ws_register, + nng_ws_register(); #endif #ifdef NNG_TRANSPORT_WSS - nng_wss_register, + nng_wss_register(); #endif #ifdef NNG_TRANSPORT_ZEROTIER - nng_zt_register, + nng_zt_register(); #endif - NULL, -}; - -int -nni_sp_tran_sys_init(void) -{ - int i; - - nni_sp_tran_inited = 1; - NNI_LIST_INIT(&nni_sp_tran_list, nni_sp_transport, t_node); - nni_rwlock_init(&nni_sp_tran_lk); - - for (i = 0; nni_sp_tran_ctors[i] != NULL; i++) { - int rv; - if ((rv = (nni_sp_tran_ctors[i]) ()) != 0) { - nni_sp_tran_sys_fini(); - return (rv); - } - } return (0); } @@ -167,13 +102,11 @@ nni_sp_tran_sys_init(void) void nni_sp_tran_sys_fini(void) { - nni_sp_transport *t; + nni_sp_tran *t; - while ((t = nni_list_first(&nni_sp_tran_list)) != NULL) { - nni_list_remove(&nni_sp_tran_list, t); - t->t_tran.tran_fini(); - NNI_FREE_STRUCT(t); + while ((t = nni_list_first(&sp_tran_list)) != NULL) { + nni_list_remove(&sp_tran_list, t); + t->tran_fini(); } - nni_rwlock_fini(&nni_sp_tran_lk); - nni_sp_tran_inited = 0; + nni_rwlock_fini(&sp_tran_lk); } |
