aboutsummaryrefslogtreecommitdiff
path: root/src/net/zmq.cpp
diff options
context:
space:
mode:
authorLee Clagett <code@leeclagett.com>2019-11-17 06:06:10 +0000
committerLee Clagett <code@leeclagett.com>2020-04-03 01:56:17 +0000
commitda9915746219482c25b96406d475c6fde9b02a31 (patch)
tree5b1c6be33c9511dc6af48b63d5f87877b9bd06ae /src/net/zmq.cpp
parentMerge pull request #6470 (diff)
downloadmonero-da9915746219482c25b96406d475c6fde9b02a31.tar.xz
Use byte_slice for sending zmq messages - removes data copy within zmq
Diffstat (limited to 'src/net/zmq.cpp')
-rw-r--r--src/net/zmq.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/net/zmq.cpp b/src/net/zmq.cpp
index d02a22983..7ea80b907 100644
--- a/src/net/zmq.cpp
+++ b/src/net/zmq.cpp
@@ -33,6 +33,8 @@
#include <limits>
#include <utility>
+#include "byte_slice.h"
+
namespace net
{
namespace zmq
@@ -183,6 +185,22 @@ namespace zmq
{
return retry_op(zmq_send, socket, payload.data(), payload.size(), flags);
}
+
+ expect<void> send(epee::byte_slice&& payload, void* socket, int flags) noexcept
+ {
+ void* const data = const_cast<std::uint8_t*>(payload.data());
+ const std::size_t size = payload.size();
+ auto buffer = payload.take_buffer(); // clears `payload` from callee
+
+ zmq_msg_t msg{};
+ MONERO_ZMQ_CHECK(zmq_msg_init_data(std::addressof(msg), data, size, epee::release_byte_slice::call, buffer.get()));
+ buffer.release(); // zmq will now decrement byte_slice ref-count
+
+ expect<void> sent = retry_op(zmq_msg_send, std::addressof(msg), socket, flags);
+ if (!sent) // beware if removing `noexcept` from this function - possible leak here
+ zmq_msg_close(std::addressof(msg));
+ return sent;
+ }
} // zmq
} // net