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