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:
parent
9798f74d17
commit
34d5028ea8
@ -882,7 +882,7 @@ int zmq::socket_base_t::connect (const char *addr_)
|
|||||||
if (protocol == "udp") {
|
if (protocol == "udp") {
|
||||||
paddr->resolved.udp_addr = new (std::nothrow) udp_address_t ();
|
paddr->resolved.udp_addr = new (std::nothrow) udp_address_t ();
|
||||||
alloc_assert (paddr->resolved.udp_addr);
|
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) {
|
if (rc != 0) {
|
||||||
LIBZMQ_DELETE(paddr);
|
LIBZMQ_DELETE(paddr);
|
||||||
EXIT_MUTEX ();
|
EXIT_MUTEX ();
|
||||||
|
@ -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.
|
// Find the ':' at end that separates address from the port number.
|
||||||
const char *delimiter = strrchr (name_, ':');
|
const char *delimiter = strrchr (name_, ':');
|
||||||
@ -80,7 +80,12 @@ int zmq::udp_address_t::resolve (const char *name_)
|
|||||||
|
|
||||||
dest_address.sin_family = AF_INET;
|
dest_address.sin_family = AF_INET;
|
||||||
dest_address.sin_port = htons (port);
|
dest_address.sin_port = htons (port);
|
||||||
dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ());
|
|
||||||
|
// 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) {
|
if (dest_address.sin_addr.s_addr == INADDR_NONE) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
@ -104,9 +109,15 @@ int zmq::udp_address_t::resolve (const char *name_)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bind_address.sin_family = AF_INET;
|
// If a receiver and not a multicast, the dest address
|
||||||
bind_address.sin_port = htons (port);
|
// is actually the bind address
|
||||||
bind_address.sin_addr.s_addr = htons (INADDR_ANY);
|
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_;
|
address = name_;
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ namespace zmq
|
|||||||
udp_address_t ();
|
udp_address_t ();
|
||||||
virtual ~udp_address_t ();
|
virtual ~udp_address_t ();
|
||||||
|
|
||||||
int resolve (const char *name_);
|
int resolve (const char *name_, bool receiver_);
|
||||||
|
|
||||||
// The opposite to resolve()
|
// The opposite to resolve()
|
||||||
virtual int to_string (std::string &addr_);
|
virtual int to_string (std::string &addr_);
|
||||||
|
@ -95,10 +95,10 @@ int main (void)
|
|||||||
void *radio = zmq_socket (ctx, ZMQ_RADIO);
|
void *radio = zmq_socket (ctx, ZMQ_RADIO);
|
||||||
void *dish = zmq_socket (ctx, ZMQ_DISH);
|
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);
|
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);
|
assert (rc == 0);
|
||||||
|
|
||||||
msleep (SETTLE_TIME);
|
msleep (SETTLE_TIME);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user