0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-28 07:58:14 +08:00

allow specify binding address on radio with udp

This commit is contained in:
somdoron 2016-04-29 10:48:46 +03:00
parent 9798f74d17
commit 34d5028ea8
4 changed files with 20 additions and 9 deletions

View File

@ -882,7 +882,7 @@ int zmq::socket_base_t::connect (const char *addr_)
if (protocol == "udp") {
paddr->resolved.udp_addr = new (std::nothrow) udp_address_t ();
alloc_assert (paddr->resolved.udp_addr);
rc = paddr->resolved.udp_addr->resolve (address.c_str());
rc = paddr->resolved.udp_addr->resolve (address.c_str(), options.type == ZMQ_DISH);
if (rc != 0) {
LIBZMQ_DELETE(paddr);
EXIT_MUTEX ();

View File

@ -58,7 +58,7 @@ zmq::udp_address_t::~udp_address_t ()
{
}
int zmq::udp_address_t::resolve (const char *name_)
int zmq::udp_address_t::resolve (const char *name_, bool receiver_)
{
// Find the ':' at end that separates address from the port number.
const char *delimiter = strrchr (name_, ':');
@ -80,6 +80,11 @@ int zmq::udp_address_t::resolve (const char *name_)
dest_address.sin_family = AF_INET;
dest_address.sin_port = htons (port);
// Only when the udp is receiver we allow * as the address
if (addr_str == "*" && receiver_)
dest_address.sin_addr.s_addr = htons (INADDR_ANY);
else
dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ());
if (dest_address.sin_addr.s_addr == INADDR_NONE) {
@ -104,9 +109,15 @@ int zmq::udp_address_t::resolve (const char *name_)
return -1;
}
// If a receiver and not a multicast, the dest address
// is actually the bind address
if (receiver_ && !is_mutlicast)
bind_address = dest_address;
else {
bind_address.sin_family = AF_INET;
bind_address.sin_port = htons (port);
bind_address.sin_addr.s_addr = htons (INADDR_ANY);
}
address = name_;

View File

@ -48,7 +48,7 @@ namespace zmq
udp_address_t ();
virtual ~udp_address_t ();
int resolve (const char *name_);
int resolve (const char *name_, bool receiver_);
// The opposite to resolve()
virtual int to_string (std::string &addr_);

View File

@ -95,10 +95,10 @@ int main (void)
void *radio = zmq_socket (ctx, ZMQ_RADIO);
void *dish = zmq_socket (ctx, ZMQ_DISH);
int rc = zmq_connect (radio, "udp://127.0.0.1:5556");
int rc = zmq_bind (dish, "udp://*:5556");
assert (rc == 0);
rc = zmq_bind (dish, "udp://127.0.0.1:5556");
rc = zmq_connect (radio, "udp://127.0.0.1:5556");
assert (rc == 0);
msleep (SETTLE_TIME);