aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-04-25 07:48:41 -0700
committerGarrett D'Amore <garrett@damore.org>2018-04-25 10:56:37 -0700
commit7496c64c12d7a22abc2bd7cbdbe8198832338cf1 (patch)
tree725b1679b355cb2d733103f305e148d587c4e58b
parent90267002f229322c82a43c160ae699a4791efe50 (diff)
downloadnng-7496c64c12d7a22abc2bd7cbdbe8198832338cf1.tar.gz
nng-7496c64c12d7a22abc2bd7cbdbe8198832338cf1.tar.bz2
nng-7496c64c12d7a22abc2bd7cbdbe8198832338cf1.zip
fixes #347 Legacy option NN_SNDBUF and NN_RCVBUF in bytes not messages
-rw-r--r--src/compat/nanomsg/nn.c88
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/bufsz.c106
3 files changed, 191 insertions, 4 deletions
diff --git a/src/compat/nanomsg/nn.c b/src/compat/nanomsg/nn.c
index c154ec6b..e99c0ff3 100644
--- a/src/compat/nanomsg/nn.c
+++ b/src/compat/nanomsg/nn.c
@@ -716,6 +716,84 @@ nn_setignore(nng_socket s, const void *valp, size_t sz)
return (0);
}
+static int
+nn_getrcvbuf(nng_socket s, void *valp, size_t *szp)
+{
+ int cnt;
+ int rv;
+
+ if ((rv = nng_getopt_int(s, NNG_OPT_RECVBUF, &cnt)) != 0) {
+ nn_seterror(rv);
+ return (-1);
+ }
+ cnt *= 1024;
+ memcpy(valp, &cnt, *szp < sizeof(cnt) ? *szp : sizeof(cnt));
+ *szp = sizeof(cnt);
+ return (0);
+}
+
+static int
+nn_setrcvbuf(nng_socket s, const void *valp, size_t sz)
+{
+ int cnt;
+ int rv;
+
+ if (sz != sizeof(cnt)) {
+ nn_seterror(NNG_EINVAL);
+ return (-1);
+ }
+ memcpy(&cnt, valp, sizeof(cnt));
+ // Round up to a whole number of kilobytes, then divide by kB to
+ // go from buffer size in bytes to messages. This is a coarse
+ // estimate, and assumes messages are 1kB on average.
+ cnt += 1023;
+ cnt /= 1024;
+ if ((rv = nng_setopt_int(s, NNG_OPT_RECVBUF, cnt)) != 0) {
+ nn_seterror(rv);
+ return (-1);
+ }
+ return (0);
+}
+
+static int
+nn_getsndbuf(nng_socket s, void *valp, size_t *szp)
+{
+ int cnt;
+ int rv;
+
+ if ((rv = nng_getopt_int(s, NNG_OPT_SENDBUF, &cnt)) != 0) {
+ nn_seterror(rv);
+ return (-1);
+ }
+ cnt *= 1024;
+ memcpy(valp, &cnt, *szp < sizeof(cnt) ? *szp : sizeof(cnt));
+ *szp = sizeof(cnt);
+ return (0);
+}
+
+static int
+nn_setsndbuf(nng_socket s, const void *valp, size_t sz)
+{
+ int cnt;
+ int rv;
+
+ if (sz != sizeof(cnt)) {
+ nn_seterror(NNG_EINVAL);
+ return (-1);
+ }
+ memcpy(&cnt, valp, sizeof(cnt));
+ // Round up to a whole number of kilobytes, then divide by kB to
+ // go from buffer size in bytes to messages. This is a coarse
+ // estimate, and assumes messages are 1kB on average.
+ cnt += 1023;
+ cnt /= 1024;
+ if ((rv = nng_setopt_int(s, NNG_OPT_SENDBUF, cnt)) != 0) {
+ nn_seterror(rv);
+ return (-1);
+ }
+ return (0);
+}
+
// options which we convert -- most of the array is initialized at run time.
static const struct {
int nnlevel;
@@ -738,13 +816,15 @@ static const struct {
},
{
.nnlevel = NN_SOL_SOCKET,
- .nnopt = NN_SNDBUF,
- .opt = NNG_OPT_SENDBUF,
+ .nnopt = NN_RCVBUF,
+ .get = nn_getrcvbuf,
+ .set = nn_setrcvbuf,
},
{
.nnlevel = NN_SOL_SOCKET,
- .nnopt = NN_RCVBUF,
- .opt = NNG_OPT_RECVBUF,
+ .nnopt = NN_SNDBUF,
+ .get = nn_getsndbuf,
+ .set = nn_setsndbuf,
},
{
.nnlevel = NN_SOL_SOCKET,
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index af570daa..d658dd65 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -126,6 +126,7 @@ else ()
endif ()
add_nng_test(aio 5 ON)
+add_nng_test(bufsz 5 NNG_PROTO_PAIR0)
add_nng_test(base64 5 NNG_SUPP_BASE64)
add_nng_test(device 5 ON)
add_nng_test(errors 2 ON)
diff --git a/tests/bufsz.c b/tests/bufsz.c
new file mode 100644
index 00000000..4686a4e5
--- /dev/null
+++ b/tests/bufsz.c
@@ -0,0 +1,106 @@
+//
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 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 "compat/nanomsg/nn.h"
+#include "convey.h"
+#include "nng.h"
+#include "trantest.h"
+
+#include "protocol/pubsub0/sub.h"
+
+#include "protocol/pair1/pair.h"
+
+#include "supplemental/util/platform.h"
+
+#include "stubs.h"
+
+#include <string.h>
+
+#define SECONDS(x) ((x) *1000)
+
+TestMain("Buffer Options", {
+
+ atexit(nng_fini);
+
+ Convey("We are able to open a PAIR socket", {
+ nng_socket s1;
+
+ So(nng_pair_open(&s1) == 0);
+
+ Reset({ nng_close(s1); });
+
+ Convey("Set/Get Recv Buf Option", {
+ int cnt;
+ So(nng_setopt_int(s1, NNG_OPT_RECVBUF, 10) == 0);
+ So(nng_getopt_int(s1, NNG_OPT_RECVBUF, &cnt) == 0);
+ So(cnt == 10);
+ So(nng_setopt_size(s1, NNG_OPT_RECVBUF, 42) ==
+ NNG_EBADTYPE);
+
+ });
+ Convey("Set/Get Send Buf Option", {
+ int cnt;
+ So(nng_setopt_int(s1, NNG_OPT_SENDBUF, 10) == 0);
+ So(nng_getopt_int(s1, NNG_OPT_SENDBUF, &cnt) == 0);
+ So(cnt == 10);
+ So(nng_setopt_size(s1, NNG_OPT_SENDBUF, 42) ==
+ NNG_EBADTYPE);
+
+ });
+
+ // NOTE: We are going to use the compat mode, but
+ // this assumes that the socket is the same between compat
+ // and current mode. This is true, but normal applications
+ // MUST NOT assume this. We only do so for testing.
+ Convey("Legacy Recv Buf Option", {
+ int cnt;
+ int os = (int) s1;
+ size_t sz = sizeof(cnt);
+ So(nng_setopt_int(s1, NNG_OPT_RECVBUF, 10) == 0);
+ So(nn_getsockopt(
+ os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, &sz) == 0);
+ So(cnt == 10240);
+ cnt = 1;
+ So(nn_setsockopt(
+ os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, sz) == 0);
+ So(nn_getsockopt(
+ os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, &sz) == 0);
+ So(cnt == 1024); // round up!
+ So(nng_getopt_int(s1, NNG_OPT_RECVBUF, &cnt) == 0);
+ So(cnt == 1);
+
+ So(nn_setsockopt(
+ os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, 100) == -1);
+ So(nn_errno() == EINVAL);
+ });
+ Convey("Legacy Send Buf Option", {
+ int cnt;
+ int os = (int) s1;
+ size_t sz = sizeof(cnt);
+ So(nng_setopt_int(s1, NNG_OPT_SENDBUF, 10) == 0);
+ So(nn_getsockopt(
+ os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, &sz) == 0);
+ So(cnt == 10240);
+ cnt = 1;
+ So(nn_setsockopt(
+ os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, sz) == 0);
+ So(nn_getsockopt(
+ os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, &sz) == 0);
+ So(cnt == 1024); // round up!
+ So(nng_getopt_int(s1, NNG_OPT_SENDBUF, &cnt) == 0);
+ So(cnt == 1);
+
+ So(nn_setsockopt(
+ os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, 100) == -1);
+ So(nn_errno() == EINVAL);
+ });
+
+ });
+})