diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-08-08 21:19:09 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-08-09 02:38:55 -0700 |
| commit | d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba (patch) | |
| tree | f6bdac79578176f0d00528d191f862009e761eac /src/nng_compat.c | |
| parent | 5f0398de8edd1ed4ddbf6455c66273a6608aad9a (diff) | |
| download | nng-d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba.tar.gz nng-d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba.tar.bz2 nng-d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba.zip | |
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.)
Diffstat (limited to 'src/nng_compat.c')
| -rw-r--r-- | src/nng_compat.c | 35 |
1 files changed, 33 insertions, 2 deletions
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); } |
