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/nng_compat.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src/nng_compat.c') diff --git a/src/nng_compat.c b/src/nng_compat.c index cb8d29ab..34fc6553 100644 --- a/src/nng_compat.c +++ b/src/nng_compat.c @@ -87,17 +87,48 @@ nn_errno(void) return (errno); } +static const struct { + uint16_t p_id; + int (*p_open)(nng_socket *); +} nn_protocols[] = { + // clang-format off + { NNG_PROTO_BUS_V0, nng_bus_open }, + { NNG_PROTO_PAIR_V0, nng_pair_open }, + { NNG_PROTO_PUSH_V0, nng_push_open }, + { NNG_PROTO_PULL_V0, nng_pull_open }, + { NNG_PROTO_PUB_V0, nng_pub_open }, + { NNG_PROTO_SUB_V0, nng_sub_open }, + { NNG_PROTO_REQ_V0, nng_req_open }, + { NNG_PROTO_REP_V0, nng_rep_open }, + { NNG_PROTO_SURVEYOR_V0, nng_surveyor_open }, + { NNG_PROTO_RESPONDENT_V0, nng_respondent_open }, + { NNG_PROTO_NONE, NULL }, + // clang-format on +}; + int nn_socket(int domain, int protocol) { nng_socket sock; int rv; + int i; if ((domain != AF_SP) && (domain != AF_SP_RAW)) { - errno = EAFNOSUPPORT; + nn_seterror(EAFNOSUPPORT); return (-1); } - if ((rv = nng_open(&sock, protocol)) != 0) { + + for (i = 0; nn_protocols[i].p_id != NNG_PROTO_NONE; i++) { + if (nn_protocols[i].p_id == protocol) { + break; + } + } + if (nn_protocols[i].p_open == NULL) { + nn_seterror(ENOTSUP); + return (-1); + } + + if ((rv = nn_protocols[i].p_open(&sock)) != 0) { nn_seterror(rv); return (-1); } -- cgit v1.2.3-70-g09d2