mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-13 02:42:58 +01:00
Code cleanup
This commit is contained in:
parent
bf74c0cfb1
commit
816299f11c
@ -374,8 +374,8 @@ int zmq::ctx_t::register_endpoint (const char *addr_,
|
||||
{
|
||||
endpoints_sync.lock ();
|
||||
|
||||
bool inserted = endpoints.insert (endpoints_t::value_type (
|
||||
std::string (addr_), endpoint_)).second;
|
||||
const bool inserted = endpoints.insert (
|
||||
endpoints_t::value_type (std::string (addr_), endpoint_)).second;
|
||||
|
||||
endpoints_sync.unlock ();
|
||||
|
||||
@ -420,6 +420,7 @@ void zmq::ctx_t::unregister_endpoints (socket_base_t *socket_)
|
||||
}
|
||||
++it;
|
||||
}
|
||||
|
||||
endpoints_sync.unlock ();
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ zmq::mailbox_t::mailbox_t ()
|
||||
// Get the pipe into passive state. That way, if the users starts by
|
||||
// polling on the associated file descriptor it will get woken up when
|
||||
// new command is posted.
|
||||
bool ok = cpipe.read (NULL);
|
||||
const bool ok = cpipe.read (NULL);
|
||||
zmq_assert (!ok);
|
||||
active = false;
|
||||
}
|
||||
@ -40,7 +40,7 @@ zmq::mailbox_t::~mailbox_t ()
|
||||
sync.unlock ();
|
||||
}
|
||||
|
||||
zmq::fd_t zmq::mailbox_t::get_fd ()
|
||||
zmq::fd_t zmq::mailbox_t::get_fd () const
|
||||
{
|
||||
return signaler.get_fd ();
|
||||
}
|
||||
@ -49,7 +49,7 @@ void zmq::mailbox_t::send (const command_t &cmd_)
|
||||
{
|
||||
sync.lock ();
|
||||
cpipe.write (cmd_, false);
|
||||
bool ok = cpipe.flush ();
|
||||
const bool ok = cpipe.flush ();
|
||||
sync.unlock ();
|
||||
if (!ok)
|
||||
signaler.send ();
|
||||
|
@ -40,7 +40,7 @@ namespace zmq
|
||||
mailbox_t ();
|
||||
~mailbox_t ();
|
||||
|
||||
fd_t get_fd ();
|
||||
fd_t get_fd () const;
|
||||
void send (const command_t &cmd_);
|
||||
int recv (command_t *cmd_, int timeout_);
|
||||
|
||||
|
@ -80,9 +80,9 @@ zmq::signaler_t::~signaler_t ()
|
||||
int rc = close (r);
|
||||
errno_assert (rc == 0);
|
||||
#elif defined ZMQ_HAVE_WINDOWS
|
||||
struct linger so_linger = { 1, 0 };
|
||||
const struct linger so_linger = { 1, 0 };
|
||||
int rc = setsockopt (w, SOL_SOCKET, SO_LINGER,
|
||||
(char *)&so_linger, sizeof (so_linger));
|
||||
(const char *) &so_linger, sizeof so_linger);
|
||||
wsa_assert (rc != SOCKET_ERROR);
|
||||
rc = closesocket (w);
|
||||
wsa_assert (rc != SOCKET_ERROR);
|
||||
@ -96,14 +96,14 @@ zmq::signaler_t::~signaler_t ()
|
||||
#endif
|
||||
}
|
||||
|
||||
zmq::fd_t zmq::signaler_t::get_fd ()
|
||||
zmq::fd_t zmq::signaler_t::get_fd () const
|
||||
{
|
||||
return r;
|
||||
}
|
||||
|
||||
void zmq::signaler_t::send ()
|
||||
{
|
||||
#if defined(HAVE_FORK)
|
||||
#if defined HAVE_FORK
|
||||
if (unlikely (pid != getpid ())) {
|
||||
//printf("Child process %d signaler_t::send returning without sending #1\n", getpid());
|
||||
return; // do not send anything in forked child context
|
||||
@ -131,7 +131,7 @@ void zmq::signaler_t::send ()
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
zmq_assert (nbytes == sizeof (dummy));
|
||||
zmq_assert (nbytes == sizeof dummy);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
@ -140,8 +140,7 @@ void zmq::signaler_t::send ()
|
||||
int zmq::signaler_t::wait (int timeout_)
|
||||
{
|
||||
#ifdef HAVE_FORK
|
||||
if (unlikely(pid != getpid()))
|
||||
{
|
||||
if (unlikely (pid != getpid ())) {
|
||||
// we have forked and the file descriptor is closed. Emulate an interupt
|
||||
// response.
|
||||
//printf("Child process %d signaler_t::wait returning simulating interrupt #1\n", getpid());
|
||||
@ -151,7 +150,6 @@ int zmq::signaler_t::wait (int timeout_)
|
||||
#endif
|
||||
|
||||
#ifdef ZMQ_POLL_BASED_ON_POLL
|
||||
|
||||
struct pollfd pfd;
|
||||
pfd.fd = r;
|
||||
pfd.events = POLLIN;
|
||||
@ -166,6 +164,7 @@ int zmq::signaler_t::wait (int timeout_)
|
||||
return -1;
|
||||
}
|
||||
#ifdef HAVE_FORK
|
||||
else
|
||||
if (unlikely (pid != getpid ())) {
|
||||
// we have forked and the file descriptor is closed. Emulate an interupt
|
||||
// response.
|
||||
@ -274,8 +273,8 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
// Windows CE does not manage security attributes
|
||||
SECURITY_DESCRIPTOR sd;
|
||||
SECURITY_ATTRIBUTES sa;
|
||||
memset (&sd, 0, sizeof (sd));
|
||||
memset (&sa, 0, sizeof (sa));
|
||||
memset (&sd, 0, sizeof sd);
|
||||
memset (&sa, 0, sizeof sa);
|
||||
|
||||
InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
|
||||
SetSecurityDescriptorDacl (&sd, TRUE, 0, FALSE);
|
||||
@ -310,7 +309,8 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
|
||||
win_assert (sync != NULL);
|
||||
}
|
||||
else if (signaler_port != 0) {
|
||||
else
|
||||
if (signaler_port != 0) {
|
||||
wchar_t mutex_name [MAX_PATH];
|
||||
swprintf (mutex_name, MAX_PATH, L"Global\\zmq-signaler-port-%d", signaler_port);
|
||||
|
||||
@ -338,16 +338,16 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
// Set SO_REUSEADDR and TCP_NODELAY on listening socket.
|
||||
BOOL so_reuseaddr = 1;
|
||||
int rc = setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
|
||||
(char *)&so_reuseaddr, sizeof (so_reuseaddr));
|
||||
(char *)&so_reuseaddr, sizeof so_reuseaddr);
|
||||
wsa_assert (rc != SOCKET_ERROR);
|
||||
BOOL tcp_nodelay = 1;
|
||||
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY,
|
||||
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
|
||||
(char *)&tcp_nodelay, sizeof tcp_nodelay);
|
||||
wsa_assert (rc != SOCKET_ERROR);
|
||||
|
||||
// Init sockaddr to signaler port.
|
||||
struct sockaddr_in addr;
|
||||
memset (&addr, 0, sizeof (addr));
|
||||
memset (&addr, 0, sizeof addr);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
||||
addr.sin_port = htons (signaler_port);
|
||||
@ -358,7 +358,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
|
||||
// Set TCP_NODELAY on writer socket.
|
||||
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY,
|
||||
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
|
||||
(char *) &tcp_nodelay, sizeof tcp_nodelay);
|
||||
wsa_assert (rc != SOCKET_ERROR);
|
||||
|
||||
if (sync != NULL) {
|
||||
@ -368,11 +368,11 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
}
|
||||
|
||||
// Bind listening socket to signaler port.
|
||||
rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
|
||||
rc = bind (listener, (const struct sockaddr*) &addr, sizeof addr);
|
||||
|
||||
if (rc != SOCKET_ERROR && signaler_port == 0) {
|
||||
// Retrieve ephemeral port number
|
||||
int addrlen = sizeof (addr);
|
||||
int addrlen = sizeof addr;
|
||||
rc = getsockname (listener, (struct sockaddr*) &addr, &addrlen);
|
||||
}
|
||||
|
||||
@ -382,7 +382,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
|
||||
// Connect writer to the listener.
|
||||
if (rc != SOCKET_ERROR)
|
||||
rc = connect (*w_, (struct sockaddr*) &addr, sizeof (addr));
|
||||
rc = connect (*w_, (struct sockaddr*) &addr, sizeof addr);
|
||||
|
||||
// Accept connection from writer.
|
||||
if (rc != SOCKET_ERROR)
|
||||
@ -439,7 +439,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
// The bug will be fixed in V5.6 ECO4 and beyond. In the meantime, we'll
|
||||
// create the socket pair manually.
|
||||
struct sockaddr_in lcladdr;
|
||||
memset (&lcladdr, 0, sizeof (lcladdr));
|
||||
memset (&lcladdr, 0, sizeof lcladdr);
|
||||
lcladdr.sin_family = AF_INET;
|
||||
lcladdr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
||||
lcladdr.sin_port = 0;
|
||||
@ -448,16 +448,16 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
errno_assert (listener != -1);
|
||||
|
||||
int on = 1;
|
||||
int rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
|
||||
int rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
|
||||
errno_assert (rc != -1);
|
||||
|
||||
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
|
||||
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELACK, &on, sizeof on);
|
||||
errno_assert (rc != -1);
|
||||
|
||||
rc = bind (listener, (struct sockaddr*) &lcladdr, sizeof (lcladdr));
|
||||
rc = bind (listener, (struct sockaddr*) &lcladdr, sizeof lcladdr);
|
||||
errno_assert (rc != -1);
|
||||
|
||||
socklen_t lcladdr_len = sizeof (lcladdr);
|
||||
socklen_t lcladdr_len = sizeof lcladdr;
|
||||
|
||||
rc = getsockname (listener, (struct sockaddr*) &lcladdr, &lcladdr_len);
|
||||
errno_assert (rc != -1);
|
||||
@ -468,13 +468,13 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
||||
*w_ = open_socket (AF_INET, SOCK_STREAM, 0);
|
||||
errno_assert (*w_ != -1);
|
||||
|
||||
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
|
||||
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
|
||||
errno_assert (rc != -1);
|
||||
|
||||
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
|
||||
rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELACK, &on, sizeof on);
|
||||
errno_assert (rc != -1);
|
||||
|
||||
rc = connect (*w_, (struct sockaddr*) &lcladdr, sizeof (lcladdr));
|
||||
rc = connect (*w_, (struct sockaddr*) &lcladdr, sizeof lcladdr);
|
||||
errno_assert (rc != -1);
|
||||
|
||||
*r_ = accept (listener, NULL, NULL);
|
||||
|
@ -41,7 +41,7 @@ namespace zmq
|
||||
signaler_t ();
|
||||
~signaler_t ();
|
||||
|
||||
fd_t get_fd ();
|
||||
fd_t get_fd () const;
|
||||
void send ();
|
||||
int wait (int timeout_);
|
||||
void recv ();
|
||||
|
@ -365,7 +365,7 @@ int zmq::socket_base_t::bind (const char *addr_)
|
||||
|
||||
if (protocol == "inproc") {
|
||||
const endpoint_t endpoint = { this, options };
|
||||
int rc = register_endpoint (addr_, endpoint);
|
||||
const int rc = register_endpoint (addr_, endpoint);
|
||||
if (rc == 0) {
|
||||
connect_pending (addr_, this);
|
||||
last_endpoint.assign (addr_);
|
||||
@ -485,7 +485,8 @@ int zmq::socket_base_t::connect (const char *addr_)
|
||||
int rcvhwm = 0;
|
||||
if (peer.socket == NULL)
|
||||
rcvhwm = options.rcvhwm;
|
||||
else if (options.rcvhwm != 0 && peer.options.sndhwm != 0)
|
||||
else
|
||||
if (options.rcvhwm != 0 && peer.options.sndhwm != 0)
|
||||
rcvhwm = options.rcvhwm + peer.options.sndhwm;
|
||||
|
||||
// Create a bi-directional pipe to connect the peers.
|
||||
@ -524,11 +525,9 @@ int zmq::socket_base_t::connect (const char *addr_)
|
||||
const endpoint_t endpoint = {this, options};
|
||||
pend_connection (std::string (addr_), endpoint, new_pipes);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// If required, send the identity of the local socket to the peer.
|
||||
if (peer.options.recv_identity) {
|
||||
|
||||
msg_t id;
|
||||
rc = id.init_size (options.identity_size);
|
||||
errno_assert (rc == 0);
|
||||
@ -569,7 +568,7 @@ int zmq::socket_base_t::connect (const char *addr_)
|
||||
options.type == ZMQ_SUB ||
|
||||
options.type == ZMQ_REQ);
|
||||
if (unlikely (is_single_connect)) {
|
||||
endpoints_t::iterator it = endpoints.find (addr_);
|
||||
const endpoints_t::iterator it = endpoints.find (addr_);
|
||||
if (it != endpoints.end ()) {
|
||||
// There is no valid use for multiple connects for SUB-PUB nor
|
||||
// DEALER-ROUTER nor REQ-REP. Multiple connects produces
|
||||
@ -983,7 +982,7 @@ int zmq::socket_base_t::process_commands (int timeout_, bool throttle_)
|
||||
// commands recently, so that we can throttle the new commands.
|
||||
|
||||
// Get the CPU's tick counter. If 0, the counter is not available.
|
||||
uint64_t tsc = zmq::clock_t::rdtsc ();
|
||||
const uint64_t tsc = zmq::clock_t::rdtsc ();
|
||||
|
||||
// Optimised version of command processing - it doesn't have to check
|
||||
// for incoming commands each time. It does so only if certain time
|
||||
@ -1176,12 +1175,11 @@ void zmq::socket_base_t::pipe_terminated (pipe_t *pipe_)
|
||||
xpipe_terminated (pipe_);
|
||||
|
||||
// Remove pipe from inproc pipes
|
||||
for (inprocs_t::iterator it = inprocs.begin(); it != inprocs.end(); ++it) {
|
||||
for (inprocs_t::iterator it = inprocs.begin (); it != inprocs.end (); ++it)
|
||||
if (it->second == pipe_) {
|
||||
inprocs.erase (it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the pipe from the list of attached pipes and confirm its
|
||||
// termination if we are already shutting down.
|
||||
|
Loading…
Reference in New Issue
Block a user