mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-14 02:57:47 +01:00
IPC address related functionality refactored into ipc_address_t class
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
This commit is contained in:
parent
3488af048f
commit
b01a8e1751
@ -24,6 +24,7 @@ libzmq_la_SOURCES = \
|
||||
io_object.hpp \
|
||||
io_thread.hpp \
|
||||
ip.hpp \
|
||||
ipc_address.hpp \
|
||||
ipc_connecter.hpp \
|
||||
ipc_listener.hpp \
|
||||
i_engine.hpp \
|
||||
@ -88,6 +89,7 @@ libzmq_la_SOURCES = \
|
||||
io_object.cpp \
|
||||
io_thread.cpp \
|
||||
ip.cpp \
|
||||
ipc_address.cpp \
|
||||
ipc_connecter.cpp \
|
||||
ipc_listener.cpp \
|
||||
kqueue.cpp \
|
||||
|
24
src/ip.cpp
24
src/ip.cpp
@ -28,10 +28,6 @@
|
||||
#include "platform.hpp"
|
||||
#include "stdint.hpp"
|
||||
|
||||
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
||||
#include <sys/un.h>
|
||||
#endif
|
||||
|
||||
#if !defined ZMQ_HAVE_WINDOWS
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
@ -396,26 +392,6 @@ int zmq::resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zmq::resolve_local_path (sockaddr_storage *addr_, socklen_t *addr_len_,
|
||||
const char *path_)
|
||||
{
|
||||
#if defined ZMQ_HAVE_WINDOWS || defined ZMQ_HAVE_OPENVMS
|
||||
errno = EPROTONOSUPPORT;
|
||||
return -1;
|
||||
#else
|
||||
sockaddr_un *un = (sockaddr_un*) addr_;
|
||||
if (strlen (path_) >= sizeof (un->sun_path))
|
||||
{
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
strcpy (un->sun_path, path_);
|
||||
un->sun_family = AF_UNIX;
|
||||
*addr_len_ = sizeof (sockaddr_un);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void zmq::tune_tcp_socket (fd_t s_)
|
||||
{
|
||||
// Disable Nagle's algorithm. We are doing data batching on 0MQ level,
|
||||
|
@ -58,10 +58,6 @@ namespace zmq
|
||||
int resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
|
||||
const char *hostname_, bool ipv4only_);
|
||||
|
||||
// This function sets up address for UNIX domain transport.
|
||||
int resolve_local_path (sockaddr_storage *addr_, socklen_t *addr_len_,
|
||||
const char* pathname_);
|
||||
|
||||
// Tunes the supplied TCP socket for the best latency.
|
||||
void tune_tcp_socket (fd_t s_);
|
||||
|
||||
|
60
src/ipc_address.cpp
Normal file
60
src/ipc_address.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
Copyright (c) 2007-2011 iMatix Corporation
|
||||
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
|
||||
|
||||
This file is part of 0MQ.
|
||||
|
||||
0MQ is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
0MQ is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ipc_address.hpp"
|
||||
|
||||
#if !defined ZMQ_HAVE_WINDOWS || !defined ZMQ_HAVE_OPENVMS
|
||||
|
||||
#include "err.hpp"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
zmq::ipc_address_t::ipc_address_t ()
|
||||
{
|
||||
memset (&address, 0, sizeof (address));
|
||||
}
|
||||
|
||||
zmq::ipc_address_t::~ipc_address_t ()
|
||||
{
|
||||
}
|
||||
|
||||
int zmq::ipc_address_t::resolve (const char *path_)
|
||||
{
|
||||
if (strlen (path_) >= sizeof (address.sun_path)) {
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
|
||||
address.sun_family = AF_UNIX;
|
||||
strcpy (address.sun_path, path_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
sockaddr *zmq::ipc_address_t::addr ()
|
||||
{
|
||||
return (sockaddr*) &address;
|
||||
}
|
||||
|
||||
socklen_t zmq::ipc_address_t::addrlen ()
|
||||
{
|
||||
return (socklen_t) sizeof (address);
|
||||
}
|
||||
|
||||
#endif
|
61
src/ipc_address.hpp
Normal file
61
src/ipc_address.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
Copyright (c) 2007-2011 iMatix Corporation
|
||||
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
|
||||
|
||||
This file is part of 0MQ.
|
||||
|
||||
0MQ is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
0MQ is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __ZMQ_IPC_ADDRESS_HPP_INCLUDED__
|
||||
#define __ZMQ_IPC_ADDRESS_HPP_INCLUDED__
|
||||
|
||||
#include "platform.hpp"
|
||||
|
||||
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
namespace zmq
|
||||
{
|
||||
|
||||
class ipc_address_t
|
||||
{
|
||||
public:
|
||||
|
||||
ipc_address_t ();
|
||||
~ipc_address_t ();
|
||||
|
||||
// This function sets up the address for UNIX domain transport.
|
||||
int resolve (const char* path_);
|
||||
|
||||
sockaddr *addr ();
|
||||
socklen_t addrlen ();
|
||||
|
||||
private:
|
||||
|
||||
struct sockaddr_un address;
|
||||
|
||||
ipc_address_t (const ipc_address_t&);
|
||||
const ipc_address_t &operator = (const ipc_address_t&);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -48,9 +48,6 @@ zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
|
||||
session (session_),
|
||||
current_reconnect_ivl(options.reconnect_ivl)
|
||||
{
|
||||
memset (&addr, 0, sizeof (addr));
|
||||
addr_len = 0;
|
||||
|
||||
// TODO: set_addess should be called separately, so that the error
|
||||
// can be propagated.
|
||||
int rc = set_address (address_);
|
||||
@ -169,16 +166,14 @@ int zmq::ipc_connecter_t::get_new_reconnect_ivl ()
|
||||
|
||||
int zmq::ipc_connecter_t::set_address (const char *addr_)
|
||||
{
|
||||
return resolve_local_path (&addr, &addr_len, addr_);
|
||||
return address.resolve (addr_);
|
||||
}
|
||||
|
||||
int zmq::ipc_connecter_t::open ()
|
||||
{
|
||||
zmq_assert (s == retired_fd);
|
||||
struct sockaddr *sa = (struct sockaddr*) &addr;
|
||||
|
||||
// Create the socket.
|
||||
zmq_assert (AF_UNIX == sa->sa_family);
|
||||
s = socket (AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s == -1)
|
||||
return -1;
|
||||
@ -187,7 +182,7 @@ int zmq::ipc_connecter_t::open ()
|
||||
unblock_socket (s);
|
||||
|
||||
// Connect to the remote peer.
|
||||
int rc = ::connect (s, (struct sockaddr*) &addr, sizeof (sockaddr_un));
|
||||
int rc = ::connect (s, address.addr (), address.addrlen ());
|
||||
|
||||
// Connect was successfull immediately.
|
||||
if (rc == 0)
|
||||
|
@ -26,10 +26,10 @@
|
||||
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
||||
|
||||
#include "fd.hpp"
|
||||
#include "ip.hpp"
|
||||
#include "own.hpp"
|
||||
#include "io_object.hpp"
|
||||
#include "stdint.hpp"
|
||||
#include "io_object.hpp"
|
||||
#include "ipc_address.hpp"
|
||||
|
||||
namespace zmq
|
||||
{
|
||||
@ -85,8 +85,7 @@ namespace zmq
|
||||
fd_t connect ();
|
||||
|
||||
// Address to connect to.
|
||||
sockaddr_storage addr;
|
||||
socklen_t addr_len;
|
||||
ipc_address_t address;
|
||||
|
||||
// Underlying socket.
|
||||
fd_t s;
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "stream_engine.hpp"
|
||||
#include "ipc_address.hpp"
|
||||
#include "io_thread.hpp"
|
||||
#include "session.hpp"
|
||||
#include "config.hpp"
|
||||
@ -45,8 +46,6 @@ zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_,
|
||||
s (retired_fd),
|
||||
socket (socket_)
|
||||
{
|
||||
memset (&addr, 0, sizeof (addr));
|
||||
addr_len = 0;
|
||||
}
|
||||
|
||||
zmq::ipc_listener_t::~ipc_listener_t ()
|
||||
@ -100,9 +99,11 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
|
||||
// Get rid of the file associated with the UNIX domain socket that
|
||||
// may have been left behind by the previous run of the application.
|
||||
::unlink (addr_);
|
||||
filename.clear ();
|
||||
|
||||
// Convert the address into sockaddr_un structure.
|
||||
int rc = resolve_local_path (&addr, &addr_len, addr_);
|
||||
// Initialise the address structure.
|
||||
ipc_address_t address;
|
||||
int rc = address.resolve (addr_);
|
||||
if (rc != 0)
|
||||
return -1;
|
||||
|
||||
@ -112,7 +113,7 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
|
||||
return -1;
|
||||
|
||||
// Bind the socket to the file path.
|
||||
rc = bind (s, (struct sockaddr*) &addr, addr_len);
|
||||
rc = bind (s, address.addr (), address.addrlen ());
|
||||
if (rc != 0)
|
||||
return -1;
|
||||
|
||||
@ -136,9 +137,8 @@ int zmq::ipc_listener_t::close ()
|
||||
|
||||
// If there's an underlying UNIX domain socket, get rid of the file it
|
||||
// is associated with.
|
||||
struct sockaddr_un *su = (struct sockaddr_un*) &addr;
|
||||
if (AF_UNIX == su->sun_family && has_file) {
|
||||
rc = ::unlink(su->sun_path);
|
||||
if (has_file && !filename.empty ()) {
|
||||
rc = ::unlink(filename.c_str ());
|
||||
if (rc != 0)
|
||||
return -1;
|
||||
}
|
||||
|
@ -25,11 +25,12 @@
|
||||
|
||||
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "fd.hpp"
|
||||
#include "ip.hpp"
|
||||
#include "own.hpp"
|
||||
#include "io_object.hpp"
|
||||
#include "stdint.hpp"
|
||||
#include "io_object.hpp"
|
||||
|
||||
namespace zmq
|
||||
{
|
||||
@ -62,13 +63,12 @@ namespace zmq
|
||||
// if the connection was dropped while waiting in the listen backlog.
|
||||
fd_t accept ();
|
||||
|
||||
// Address to listen on.
|
||||
sockaddr_storage addr;
|
||||
socklen_t addr_len;
|
||||
|
||||
// True, if the undelying file for UNIX domain socket exists.
|
||||
bool has_file;
|
||||
|
||||
// Name of the file associated with the UNIX domain address.
|
||||
std::string filename;
|
||||
|
||||
// Underlying socket.
|
||||
fd_t s;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user