Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions tests/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,47 @@ TEST_CASE("socket sends and receives const buffer", "[socket]")
CHECK(0 == memcmp(buf, str, 2));
}

TEST_CASE("socket send_static", "[socket]")
{
zmq::context_t context;
zmq::socket_t sender(context, ZMQ_PAIR);
zmq::socket_t receiver(context, ZMQ_PAIR);
receiver.bind("inproc://test");
sender.connect("inproc://test");
SECTION("send_static with const_buffer")
{
CHECK(6 == *sender.send_static(zmq::buffer("hello")));
char buf[6];
const auto res = receiver.recv(zmq::buffer(buf));
CHECK(res);
CHECK(!res->truncated());
CHECK(6 == res->size);
CHECK(0 == memcmp(buf, "hello", 6));
}
#if CPPZMQ_HAS_STRING_VIEW
SECTION("send_static with std::string_view")
{
CHECK(5 == *sender.send_static(std::string_view{"hello"}));
char buf[5];
const auto res = receiver.recv(zmq::buffer(buf));
CHECK(res);
CHECK(!res->truncated());
CHECK(5 == res->size);
CHECK(0 == memcmp(buf, "hello", 5));
}
SECTION("send_static with char literal")
{
CHECK(5 == *sender.send_static("hello"));
char buf[5];
const auto res = receiver.recv(zmq::buffer(buf));
CHECK(res);
CHECK(!res->truncated());
CHECK(5 == res->size);
CHECK(0 == memcmp(buf, "hello", 5));
}
#endif
}

#ifdef ZMQ_CPP11

TEST_CASE("socket send none sndmore", "[socket]")
Expand Down
19 changes: 19 additions & 0 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,25 @@ class socket_base
}
#endif

send_result_t send_static(const_buffer buf, send_flags flags = send_flags::none)
{
int nbytes =
zmq_send_const(_handle, buf.data(), buf.size(), static_cast<int>(flags));
if (nbytes >= 0)
return static_cast<size_t>(nbytes);
if (zmq_errno() == EAGAIN)
return {};
throw error_t();
}

#if CPPZMQ_HAS_STRING_VIEW
send_result_t send_static(std::string_view str,
send_flags flags = send_flags::none)
{
return send_static(zmq::buffer(str), flags);
}
#endif

ZMQ_CPP11_DEPRECATED(
"from 4.3.1, use recv taking a mutable_buffer and recv_flags")
size_t recv(void *buf_, size_t len_, int flags_ = 0)
Expand Down