From d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 8 Aug 2017 21:19:09 -0700 Subject: fixes #44 open protocol by "name" (symbol) instead number fixes #38 Make protocols "pluggable", or at least optional This is a breaking change, as we've done away with the central registered list of protocols, and instead demand the user call nng_xxx_open() where xxx is a protocol name. (We did keep a table around in the compat framework though.) There is a nice way for protocols to plug in via an nni_proto_open(), where they can use a generic constructor that they use to build a protocol specific constructor (passing their ops vector in.) --- src/core/protocol.c | 83 ++++++----------------------------------------------- 1 file changed, 8 insertions(+), 75 deletions(-) (limited to 'src/core/protocol.c') diff --git a/src/core/protocol.c b/src/core/protocol.c index a60454de..7faf9068 100644 --- a/src/core/protocol.c +++ b/src/core/protocol.c @@ -1,5 +1,6 @@ // // Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -13,82 +14,14 @@ // Protocol related stuff - generically. -// The list of protocols is hardwired. This is reasonably unlikely to -// change, as adding new protocols is not something intended to be done -// outside of the core. -extern nni_proto nni_bus_proto; -extern nni_proto nni_pair_proto; -extern nni_proto nni_rep_proto; -extern nni_proto nni_req_proto; -extern nni_proto nni_pub_proto; -extern nni_proto nni_sub_proto; -extern nni_proto nni_push_proto; -extern nni_proto nni_pull_proto; -extern nni_proto nni_surveyor_proto; -extern nni_proto nni_respondent_proto; - -static nni_proto *protocols[] = { - // clang-format off - &nni_bus_proto, - &nni_pair_proto, - &nni_rep_proto, - &nni_req_proto, - &nni_pub_proto, - &nni_sub_proto, - &nni_push_proto, - &nni_pull_proto, - &nni_surveyor_proto, - &nni_respondent_proto, - NULL - // clang-format on -}; - -nni_proto * -nni_proto_find(uint16_t num) -{ - int i; - nni_proto *p; - - for (i = 0; (p = protocols[i]) != NULL; i++) { - if (p->proto_self == num) { - break; - } - } - return (p); -} - -const char * -nni_proto_name(uint16_t num) -{ - nni_proto *p; - - if ((p = nni_proto_find(num)) == NULL) { - return (NULL); - } - return (p->proto_name); -} - -uint16_t -nni_proto_number(const char *name) -{ - nni_proto *p; - int i; - - for (i = 0; (p = protocols[i]) != NULL; i++) { - if (strcmp(p->proto_name, name) == 0) { - return (p->proto_self); - } - } - return (NNG_PROTO_NONE); -} - -uint16_t -nni_proto_peer(uint16_t num) +int +nni_proto_open(nng_socket *sockidp, const nni_proto *proto) { - nni_proto *p; + int rv; + nni_sock *sock; - if ((p = nni_proto_find(num)) == NULL) { - return (NNG_PROTO_NONE); + if ((rv = nni_sock_open(&sock, proto)) == 0) { + *sockidp = nni_sock_id(sock); // Keep socket held open. } - return (p->proto_peer); + return (rv); } -- cgit v1.2.3-70-g09d2