mirror of
https://github.com/zeromq/cppzmq.git
synced 2025-03-01 10:57:59 +01:00
Merge pull request #301 from gummif/gfa/proxy
Problem: proxy is not typesafe
This commit is contained in:
commit
e1fe5e5209
@ -1,5 +1,8 @@
|
||||
#include <catch.hpp>
|
||||
#include <zmq.hpp>
|
||||
#ifdef ZMQ_CPP11
|
||||
#include <future>
|
||||
#endif
|
||||
|
||||
TEST_CASE("socket create destroy", "[socket]")
|
||||
{
|
||||
@ -27,3 +30,59 @@ TEST_CASE("socket sends and receives const buffer", "[socket]")
|
||||
CHECK(2 == receiver.recv(buf, 2));
|
||||
CHECK(0 == memcmp(buf, "Hi", 2));
|
||||
}
|
||||
|
||||
#ifdef ZMQ_CPP11
|
||||
TEST_CASE("socket proxy", "[socket]")
|
||||
{
|
||||
zmq::context_t context;
|
||||
zmq::socket_t front(context, ZMQ_ROUTER);
|
||||
zmq::socket_t back(context, ZMQ_ROUTER);
|
||||
zmq::socket_t capture(context, ZMQ_DEALER);
|
||||
front.bind("inproc://test1");
|
||||
back.bind("inproc://test2");
|
||||
capture.bind("inproc://test3");
|
||||
auto f = std::async(std::launch::async, [&]() {
|
||||
auto s1 = std::move(front);
|
||||
auto s2 = std::move(back);
|
||||
auto s3 = std::move(capture);
|
||||
try
|
||||
{
|
||||
zmq::proxy(s1, s2, &s3);
|
||||
}
|
||||
catch (const zmq::error_t& e)
|
||||
{
|
||||
return e.num() == ETERM;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
context.close();
|
||||
CHECK(f.get());
|
||||
}
|
||||
|
||||
TEST_CASE("socket proxy steerable", "[socket]")
|
||||
{
|
||||
zmq::context_t context;
|
||||
zmq::socket_t front(context, ZMQ_ROUTER);
|
||||
zmq::socket_t back(context, ZMQ_ROUTER);
|
||||
zmq::socket_t control(context, ZMQ_SUB);
|
||||
front.bind("inproc://test1");
|
||||
back.bind("inproc://test2");
|
||||
control.connect("inproc://test3");
|
||||
auto f = std::async(std::launch::async, [&]() {
|
||||
auto s1 = std::move(front);
|
||||
auto s2 = std::move(back);
|
||||
auto s3 = std::move(control);
|
||||
try
|
||||
{
|
||||
zmq::proxy_steerable(s1, s2, ZMQ_NULLPTR, &s3);
|
||||
}
|
||||
catch (const zmq::error_t& e)
|
||||
{
|
||||
return e.num() == ETERM;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
context.close();
|
||||
CHECK(f.get());
|
||||
}
|
||||
#endif
|
||||
|
56
zmq.hpp
56
zmq.hpp
@ -201,23 +201,6 @@ inline int poll(std::vector<zmq_pollitem_t> &items, long timeout_ = -1)
|
||||
#endif
|
||||
|
||||
|
||||
inline void proxy(void *frontend, void *backend, void *capture)
|
||||
{
|
||||
int rc = zmq_proxy(frontend, backend, capture);
|
||||
if (rc != 0)
|
||||
throw error_t();
|
||||
}
|
||||
|
||||
#ifdef ZMQ_HAS_PROXY_STEERABLE
|
||||
inline void
|
||||
proxy_steerable(void *frontend, void *backend, void *capture, void *control)
|
||||
{
|
||||
int rc = zmq_proxy_steerable(frontend, backend, capture, control);
|
||||
if (rc != 0)
|
||||
throw error_t();
|
||||
}
|
||||
#endif
|
||||
|
||||
inline void version(int *major_, int *minor_, int *patch_)
|
||||
{
|
||||
zmq_version(major_, minor_, patch_);
|
||||
@ -792,6 +775,45 @@ class socket_t
|
||||
void operator=(const socket_t &) ZMQ_DELETED_FUNCTION;
|
||||
};
|
||||
|
||||
ZMQ_DEPRECATED("from 4.3.1, use proxy taking socket_t objects")
|
||||
inline void proxy(void *frontend, void *backend, void *capture)
|
||||
{
|
||||
int rc = zmq_proxy(frontend, backend, capture);
|
||||
if (rc != 0)
|
||||
throw error_t();
|
||||
}
|
||||
|
||||
inline void proxy(socket_t &frontend, socket_t &backend, socket_t *capture = ZMQ_NULLPTR)
|
||||
{
|
||||
int rc = zmq_proxy(static_cast<void *>(frontend),
|
||||
static_cast<void *>(backend),
|
||||
capture ? static_cast<void *>(*capture) : ZMQ_NULLPTR);
|
||||
if (rc != 0)
|
||||
throw error_t();
|
||||
}
|
||||
|
||||
#ifdef ZMQ_HAS_PROXY_STEERABLE
|
||||
ZMQ_DEPRECATED("from 4.3.1, use proxy_steerable taking socket_t objects")
|
||||
inline void
|
||||
proxy_steerable(void *frontend, void *backend, void *capture, void *control)
|
||||
{
|
||||
int rc = zmq_proxy_steerable(frontend, backend, capture, control);
|
||||
if (rc != 0)
|
||||
throw error_t();
|
||||
}
|
||||
|
||||
inline void
|
||||
proxy_steerable(socket_t &frontend, socket_t &backend, socket_t *capture, socket_t *control)
|
||||
{
|
||||
int rc = zmq_proxy_steerable(static_cast<void *>(frontend),
|
||||
static_cast<void *>(backend),
|
||||
capture ? static_cast<void *>(*capture) : ZMQ_NULLPTR,
|
||||
control ? static_cast<void *>(*control) : ZMQ_NULLPTR);
|
||||
if (rc != 0)
|
||||
throw error_t();
|
||||
}
|
||||
#endif
|
||||
|
||||
class monitor_t
|
||||
{
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user