Skip to content

Commit 9866e33

Browse files
authored
Add socket_t::send_static (#678)
1 parent 041f755 commit 9866e33

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

tests/socket.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,47 @@ TEST_CASE("socket sends and receives const buffer", "[socket]")
507507
CHECK(0 == memcmp(buf, str, 2));
508508
}
509509

510+
TEST_CASE("socket send_static", "[socket]")
511+
{
512+
zmq::context_t context;
513+
zmq::socket_t sender(context, ZMQ_PAIR);
514+
zmq::socket_t receiver(context, ZMQ_PAIR);
515+
receiver.bind("inproc://test");
516+
sender.connect("inproc://test");
517+
SECTION("send_static with const_buffer")
518+
{
519+
CHECK(6 == *sender.send_static(zmq::buffer("hello")));
520+
char buf[6];
521+
const auto res = receiver.recv(zmq::buffer(buf));
522+
CHECK(res);
523+
CHECK(!res->truncated());
524+
CHECK(6 == res->size);
525+
CHECK(0 == memcmp(buf, "hello", 6));
526+
}
527+
#if CPPZMQ_HAS_STRING_VIEW
528+
SECTION("send_static with std::string_view")
529+
{
530+
CHECK(5 == *sender.send_static(std::string_view{"hello"}));
531+
char buf[5];
532+
const auto res = receiver.recv(zmq::buffer(buf));
533+
CHECK(res);
534+
CHECK(!res->truncated());
535+
CHECK(5 == res->size);
536+
CHECK(0 == memcmp(buf, "hello", 5));
537+
}
538+
SECTION("send_static with char literal")
539+
{
540+
CHECK(5 == *sender.send_static("hello"));
541+
char buf[5];
542+
const auto res = receiver.recv(zmq::buffer(buf));
543+
CHECK(res);
544+
CHECK(!res->truncated());
545+
CHECK(5 == res->size);
546+
CHECK(0 == memcmp(buf, "hello", 5));
547+
}
548+
#endif
549+
}
550+
510551
#ifdef ZMQ_CPP11
511552

512553
TEST_CASE("socket send none sndmore", "[socket]")

zmq.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,25 @@ class socket_base
20292029
}
20302030
#endif
20312031

2032+
send_result_t send_static(const_buffer buf, send_flags flags = send_flags::none)
2033+
{
2034+
int nbytes =
2035+
zmq_send_const(_handle, buf.data(), buf.size(), static_cast<int>(flags));
2036+
if (nbytes >= 0)
2037+
return static_cast<size_t>(nbytes);
2038+
if (zmq_errno() == EAGAIN)
2039+
return {};
2040+
throw error_t();
2041+
}
2042+
2043+
#if CPPZMQ_HAS_STRING_VIEW
2044+
send_result_t send_static(std::string_view str,
2045+
send_flags flags = send_flags::none)
2046+
{
2047+
return send_static(zmq::buffer(str), flags);
2048+
}
2049+
#endif
2050+
20322051
ZMQ_CPP11_DEPRECATED(
20332052
"from 4.3.1, use recv taking a mutable_buffer and recv_flags")
20342053
size_t recv(void *buf_, size_t len_, int flags_ = 0)

0 commit comments

Comments
 (0)