mirror of
https://github.com/zeromq/libzmq.git
synced 2025-09-16 19:27:17 +02:00
Problem: code duplication in get_address of ipc/tcp/tipc listener classes
Solution: pull up to base class
This commit is contained in:
parent
5c81bbe82e
commit
9a376fbe24
@ -150,17 +150,9 @@ void zmq::ipc_listener_t::in_event ()
|
||||
create_engine (fd);
|
||||
}
|
||||
|
||||
int zmq::ipc_listener_t::get_address (std::string &addr_)
|
||||
std::string zmq::ipc_listener_t::get_socket_name (zmq::fd_t fd_) const
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
const zmq_socklen_t sl = get_socket_address (&ss);
|
||||
if (sl == 0) {
|
||||
addr_.clear ();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ipc_address_t addr (reinterpret_cast<struct sockaddr *> (&ss), sl);
|
||||
return addr.to_string (addr_);
|
||||
return stream_listener_base_t::get_socket_name<ipc_address_t> (fd_);
|
||||
}
|
||||
|
||||
int zmq::ipc_listener_t::set_address (const char *addr_)
|
||||
|
@ -50,8 +50,8 @@ class ipc_listener_t : public stream_listener_base_t
|
||||
// Set address to listen on.
|
||||
int set_address (const char *addr_);
|
||||
|
||||
// Get the bound address for use with wildcards
|
||||
int get_address (std::string &addr_);
|
||||
protected:
|
||||
std::string get_socket_name (fd_t fd_) const;
|
||||
|
||||
private:
|
||||
// Handlers for I/O events.
|
||||
|
@ -51,13 +51,20 @@ zmq::stream_listener_base_t::~stream_listener_base_t ()
|
||||
zmq_assert (!_handle);
|
||||
}
|
||||
|
||||
int zmq::stream_listener_base_t::get_address (std::string &addr_) const
|
||||
{
|
||||
addr_ = get_socket_name (_s);
|
||||
return addr_.empty () ? -1 : 0;
|
||||
}
|
||||
|
||||
zmq::zmq_socklen_t
|
||||
zmq::stream_listener_base_t::get_socket_address (sockaddr_storage *ss_) const
|
||||
zmq::stream_listener_base_t::get_socket_address (fd_t fd_,
|
||||
sockaddr_storage *ss_)
|
||||
{
|
||||
zmq_socklen_t sl = static_cast<zmq_socklen_t> (sizeof (*ss_));
|
||||
|
||||
const int rc =
|
||||
getsockname (_s, reinterpret_cast<struct sockaddr *> (ss_), &sl);
|
||||
getsockname (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl);
|
||||
|
||||
return rc != 0 ? 0 : sl;
|
||||
}
|
||||
|
@ -57,8 +57,26 @@ class stream_listener_base_t : public own_t, public io_object_t
|
||||
const options_t &options_);
|
||||
~stream_listener_base_t ();
|
||||
|
||||
// Get the bound address for use with wildcards
|
||||
int get_address (std::string &addr_) const;
|
||||
|
||||
protected:
|
||||
zmq_socklen_t get_socket_address (sockaddr_storage *ss_) const;
|
||||
static zmq_socklen_t get_socket_address (fd_t fd_, sockaddr_storage *ss_);
|
||||
virtual std::string get_socket_name (fd_t fd_) const = 0;
|
||||
|
||||
template <typename T> static std::string get_socket_name (fd_t fd_)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
const zmq_socklen_t sl = get_socket_address (fd_, &ss);
|
||||
if (sl == 0) {
|
||||
return std::string ();
|
||||
}
|
||||
|
||||
const T addr (reinterpret_cast<struct sockaddr *> (&ss), sl);
|
||||
std::string address_string;
|
||||
addr.to_string (address_string);
|
||||
return address_string;
|
||||
}
|
||||
|
||||
private:
|
||||
// Handlers for incoming commands.
|
||||
|
@ -91,18 +91,9 @@ void zmq::tcp_listener_t::in_event ()
|
||||
create_engine (fd);
|
||||
}
|
||||
|
||||
int zmq::tcp_listener_t::get_address (std::string &addr_)
|
||||
std::string zmq::tcp_listener_t::get_socket_name (zmq::fd_t fd_) const
|
||||
{
|
||||
// Get the details of the TCP socket
|
||||
struct sockaddr_storage ss;
|
||||
const zmq_socklen_t sl = get_socket_address (&ss);
|
||||
if (!sl) {
|
||||
addr_.clear ();
|
||||
return -1;
|
||||
}
|
||||
|
||||
tcp_address_t addr (reinterpret_cast<struct sockaddr *> (&ss), sl);
|
||||
return addr.to_string (addr_);
|
||||
return stream_listener_base_t::get_socket_name<tcp_address_t> (fd_);
|
||||
}
|
||||
|
||||
int zmq::tcp_listener_t::set_address (const char *addr_)
|
||||
|
@ -46,8 +46,8 @@ class tcp_listener_t : public stream_listener_base_t
|
||||
// Set address to listen on.
|
||||
int set_address (const char *addr_);
|
||||
|
||||
// Get the bound address for use with wildcard
|
||||
int get_address (std::string &addr_);
|
||||
protected:
|
||||
std::string get_socket_name (fd_t fd_) const;
|
||||
|
||||
private:
|
||||
// Handlers for I/O events.
|
||||
|
@ -130,7 +130,7 @@ int zmq::tipc_address_t::resolve (const char *name)
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
int zmq::tipc_address_t::to_string (std::string &addr_)
|
||||
int zmq::tipc_address_t::to_string (std::string &addr_) const
|
||||
{
|
||||
if (address.family != AF_TIPC) {
|
||||
addr_.clear ();
|
||||
|
@ -55,7 +55,7 @@ class tipc_address_t
|
||||
int resolve (const char *name);
|
||||
|
||||
// The opposite to resolve()
|
||||
int to_string (std::string &addr_);
|
||||
int to_string (std::string &addr_) const;
|
||||
|
||||
// Handling different TIPC address types
|
||||
bool is_service () const;
|
||||
|
@ -76,17 +76,9 @@ void zmq::tipc_listener_t::in_event ()
|
||||
create_engine (fd);
|
||||
}
|
||||
|
||||
int zmq::tipc_listener_t::get_address (std::string &addr_)
|
||||
std::string zmq::tipc_listener_t::get_socket_name (zmq::fd_t fd_) const
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
const zmq_socklen_t sl = get_socket_address (&ss);
|
||||
if (!sl) {
|
||||
addr_.clear ();
|
||||
return -1;
|
||||
}
|
||||
|
||||
tipc_address_t addr ((struct sockaddr *) &ss, sl);
|
||||
return addr.to_string (addr_);
|
||||
return stream_listener_base_t::get_socket_name<tipc_address_t> (fd_);
|
||||
}
|
||||
|
||||
int zmq::tipc_listener_t::set_address (const char *addr_)
|
||||
@ -111,7 +103,7 @@ int zmq::tipc_listener_t::set_address (const char *addr_)
|
||||
// If random Port Identity, update address object to reflect the assigned address
|
||||
if (_address.is_random ()) {
|
||||
struct sockaddr_storage ss;
|
||||
const zmq_socklen_t sl = get_socket_address (&ss);
|
||||
const zmq_socklen_t sl = get_socket_address (_s, &ss);
|
||||
if (sl == 0)
|
||||
goto error;
|
||||
|
||||
|
@ -52,8 +52,8 @@ class tipc_listener_t : public stream_listener_base_t
|
||||
// Set address to listen on.
|
||||
int set_address (const char *addr_);
|
||||
|
||||
// Get the bound address for use with wildcards
|
||||
int get_address (std::string &addr_);
|
||||
protected:
|
||||
std::string get_socket_name (fd_t fd_) const;
|
||||
|
||||
private:
|
||||
// Handlers for I/O events.
|
||||
|
Loading…
x
Reference in New Issue
Block a user