Guest User

Oleg Sevostyanov

a guest
Nov 26th, 2010
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.48 KB | None | 0 0
  1. /*
  2.     Copyright (c) 2007-2010 iMatix Corporation
  3.  
  4.     This file is part of 0MQ.
  5.  
  6.     0MQ is free software; you can redistribute it and/or modify it under
  7.     the terms of the GNU Lesser General Public License as published by
  8.     the Free Software Foundation; either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     0MQ is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU Lesser General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU Lesser General Public License
  17.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. */
  19.  
  20. #include "mailbox.hpp"
  21. #include "platform.hpp"
  22. #include "err.hpp"
  23. #include "fd.hpp"
  24. #include "ip.hpp"
  25.  
  26. #if defined ZMQ_HAVE_WINDOWS
  27. #include "windows.hpp"
  28. #else
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <limits.h>
  32. #include <netinet/tcp.h>
  33. #include <unistd.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #endif
  37.  
  38. zmq::fd_t zmq::mailbox_t::get_fd ()
  39. {
  40.     return r;
  41. }
  42.  
  43. #if defined ZMQ_HAVE_WINDOWS
  44.  
  45. zmq::mailbox_t::mailbox_t ()
  46. {
  47.     //  Create the socketpair for signalling.
  48.     int rc = make_socketpair (&r, &w);
  49.     errno_assert (rc == 0);
  50.  
  51.     //  Set the writer to non-blocking mode.
  52.     unsigned long argp = 1;
  53.     rc = ioctlsocket (w, FIONBIO, &argp);
  54.     wsa_assert (rc != SOCKET_ERROR);
  55.  
  56.     //  Set the reader to non-blocking mode.
  57.     argp = 1;
  58.     rc = ioctlsocket (r, FIONBIO, &argp);
  59.     wsa_assert (rc != SOCKET_ERROR);
  60. }
  61.  
  62. zmq::mailbox_t::~mailbox_t ()
  63. {
  64.     int rc = closesocket (w);
  65.     wsa_assert (rc != SOCKET_ERROR);
  66.  
  67.     rc = closesocket (r);
  68.     wsa_assert (rc != SOCKET_ERROR);
  69. }
  70.  
  71. void zmq::mailbox_t::send (const command_t &cmd_)
  72. {
  73.     //  TODO: Implement SNDBUF auto-resizing as for POSIX platforms.
  74.     //  In the mean time, the following code with assert if the send()
  75.     //  call would block.
  76.     //int nbytes = ::send (w, (char *)&cmd_, sizeof (command_t), 0);
  77.     //wsa_assert (nbytes != SOCKET_ERROR);
  78.     //zmq_assert (nbytes == sizeof (command_t));
  79.  
  80.  
  81.     //  Attempt to write an entire command without blocking.
  82.     int nbytes;
  83.  
  84.     do {
  85.       nbytes = ::send (w, (char *)&cmd_, sizeof (command_t), 0);
  86.     } while ( SOCKET_ERROR == nbytes );
  87.  
  88.     if (EINTR == errno)
  89.       return;
  90.  
  91.     //  Attempt to increase mailbox SNDBUF if the send failed.
  92.     if ( SOCKET_ERROR == nbytes && errno == EAGAIN) {
  93.       int old_sndbuf, new_sndbuf;
  94.       socklen_t sndbuf_size = sizeof old_sndbuf;
  95.  
  96.       //  Retrieve current send buffer size.
  97.       int rc = getsockopt (w, SOL_SOCKET, SO_SNDBUF, (char *)&old_sndbuf, &sndbuf_size);
  98.       errno_assert (rc == 0);
  99.       new_sndbuf = old_sndbuf * 2;
  100.  
  101.       //  Double the new send buffer size.
  102.       rc = setsockopt (w, SOL_SOCKET, SO_SNDBUF, (char *)&new_sndbuf, sndbuf_size);
  103.       errno_assert (rc == 0);
  104.  
  105.       //  Verify that the OS actually honored the request.
  106.       rc = getsockopt (w, SOL_SOCKET, SO_SNDBUF, (char *)&new_sndbuf, &sndbuf_size);
  107.       errno_assert (rc == 0);
  108.       zmq_assert (new_sndbuf > old_sndbuf);
  109.  
  110.       //  Retry the sending operation; at this point it must succeed.
  111.       do
  112.       {
  113.         nbytes = ::send (w, (char *)&cmd_, sizeof (command_t), 0);
  114.       } while (nbytes == SOCKET_ERROR && errno == EINTR);
  115.     }
  116.  
  117.     wsa_assert ( SOCKET_ERROR != nbytes );
  118.  
  119.     //  This should never happen as we've already checked that command size is
  120.     //  less than PIPE_BUF.
  121.     zmq_assert (nbytes == sizeof (command_t));
  122. }
  123.  
  124. int zmq::mailbox_t::recv (command_t *cmd_, bool block_)
  125. {
  126.     //  If required, set the reader to blocking mode.
  127.     if (block_) {
  128.         unsigned long argp = 0;
  129.         int rc = ioctlsocket (r, FIONBIO, &argp);
  130.         wsa_assert (SOCKET_ERROR != rc);
  131.     }
  132.  
  133.     //  Attempt to read an entire command. Returns EAGAIN if non-blocking
  134.     //  and a command is not available. Save value of errno if we wish to pass
  135.     //  it to caller.
  136.     int err = 0;
  137.     int nbytes = ::recv (r, (char *)cmd_, sizeof (command_t), 0);
  138.     if (SOCKET_ERROR != nbytes && WSAGetLastError () == WSAEWOULDBLOCK)
  139.         err = EAGAIN;
  140.  
  141.     //  Re-set the reader to non-blocking mode.
  142.     if (block_) {
  143.         unsigned long argp = 1;
  144.         int rc = ioctlsocket (r, FIONBIO, &argp);
  145.         wsa_assert (rc != SOCKET_ERROR);
  146.     }
  147.  
  148.     //  If the recv failed, return with the saved errno.
  149.     if (err != 0) {
  150.         errno = err;
  151.         return -1;
  152.     }
  153.  
  154.     //  Sanity check for success.
  155.     wsa_assert(SOCKET_ERROR != nbytes);
  156.  
  157.     //  TODO: This is not a generic way to handle this...
  158.     if (WSAEWOULDBLOCK == WSAGetLastError())
  159.       return 0;
  160.  
  161.     //  Check whether we haven't got half of command.
  162.     zmq_assert (nbytes == sizeof (command_t));
  163.  
  164.     return 0;
  165. }
  166.  
  167. #else
  168.  
  169. zmq::mailbox_t::mailbox_t ()
  170. {
  171. #ifdef PIPE_BUF
  172.     //  Make sure that command can be written to the socket in atomic fashion.
  173.     //  If this wasn't guaranteed, commands from different threads would be
  174.     //  interleaved.
  175.     zmq_assert (sizeof (command_t) <= PIPE_BUF);
  176. #endif
  177.  
  178.     //  Create the socketpair for signaling.
  179.     int rc = make_socketpair (&r, &w);
  180.     errno_assert (rc == 0);
  181.  
  182.     //  Set the writer to non-blocking mode.
  183.     int flags = fcntl (w, F_GETFL, 0);
  184.     errno_assert (flags >= 0);
  185.     rc = fcntl (w, F_SETFL, flags | O_NONBLOCK);
  186.     errno_assert (rc == 0);
  187.  
  188. #ifndef MSG_DONTWAIT
  189.     //  Set the reader to non-blocking mode.
  190.     flags = fcntl (r, F_GETFL, 0);
  191.     errno_assert (flags >= 0);
  192.     rc = fcntl (r, F_SETFL, flags | O_NONBLOCK);
  193.     errno_assert (rc == 0);
  194. #endif
  195. }
  196.  
  197. zmq::mailbox_t::~mailbox_t ()
  198. {
  199.     close (w);
  200.     close (r);
  201. }
  202.  
  203. void zmq::mailbox_t::send (const command_t &cmd_)
  204. {
  205.     //  Attempt to write an entire command without blocking.
  206.     ssize_t nbytes;
  207.     do {
  208.         nbytes = ::send (w, &cmd_, sizeof (command_t), 0);
  209.     } while (nbytes == -1 && errno == EINTR);
  210.  
  211.     //  Attempt to increase mailbox SNDBUF if the send failed.
  212.     if (nbytes == -1 && errno == EAGAIN) {
  213.         int old_sndbuf, new_sndbuf;
  214.         socklen_t sndbuf_size = sizeof old_sndbuf;
  215.  
  216.         //  Retrieve current send buffer size.
  217.         int rc = getsockopt (w, SOL_SOCKET, SO_SNDBUF, &old_sndbuf,
  218.             &sndbuf_size);
  219.         errno_assert (rc == 0);
  220.         new_sndbuf = old_sndbuf * 2;
  221.  
  222.         //  Double the new send buffer size.
  223.         rc = setsockopt (w, SOL_SOCKET, SO_SNDBUF, &new_sndbuf, sndbuf_size);
  224.         errno_assert (rc == 0);
  225.  
  226.         //  Verify that the OS actually honored the request.
  227.         rc = getsockopt (w, SOL_SOCKET, SO_SNDBUF, &new_sndbuf, &sndbuf_size);
  228.         errno_assert (rc == 0);
  229.         zmq_assert (new_sndbuf > old_sndbuf);
  230.  
  231.         //  Retry the sending operation; at this point it must succeed.
  232.         do {
  233.             nbytes = ::send (w, &cmd_, sizeof (command_t), 0);
  234.         } while (nbytes == -1 && errno == EINTR);
  235.     }
  236.     errno_assert (nbytes != -1);
  237.  
  238.     //  This should never happen as we've already checked that command size is
  239.     //  less than PIPE_BUF.
  240.     zmq_assert (nbytes == sizeof (command_t));
  241. }
  242.  
  243. int zmq::mailbox_t::recv (command_t *cmd_, bool block_)
  244. {
  245. #ifdef MSG_DONTWAIT
  246.  
  247.     //  Attempt to read an entire command. Returns EAGAIN if non-blocking
  248.     //  mode is requested and a command is not available.
  249.     ssize_t nbytes = ::recv (r, cmd_, sizeof (command_t),
  250.         block_ ? 0 : MSG_DONTWAIT);
  251.     if (nbytes == -1 && (errno == EAGAIN || errno == EINTR))
  252.         return -1;
  253. #else
  254.  
  255.     //  If required, set the reader to blocking mode.
  256.     if (block_) {
  257.         int flags = fcntl (r, F_GETFL, 0);
  258.         errno_assert (flags >= 0);
  259.         int rc = fcntl (r, F_SETFL, flags & ~O_NONBLOCK);
  260.         errno_assert (rc == 0);
  261.     }
  262.  
  263.     //  Attempt to read an entire command. Returns EAGAIN if non-blocking
  264.     //  and a command is not available. Save value of errno if we wish to pass
  265.     //  it to caller.
  266.     int err = 0;
  267.     ssize_t nbytes = ::recv (r, cmd_, sizeof (command_t), 0);
  268.     if (nbytes == -1 && (errno == EAGAIN || errno == EINTR))  
  269.         err = errno;
  270.  
  271.     //  Re-set the reader to non-blocking mode.
  272.     if (block_) {
  273.         int flags = fcntl (r, F_GETFL, 0);
  274.         errno_assert (flags >= 0);
  275.         int rc = fcntl (r, F_SETFL, flags | O_NONBLOCK);
  276.         errno_assert (rc == 0);
  277.     }
  278.  
  279.     //  If the recv failed, return with the saved errno if set.
  280.     if (err != 0) {
  281.         errno = err;
  282.         return -1;
  283.     }
  284.  
  285. #endif
  286.  
  287.     //  Sanity check for success.
  288.     errno_assert (nbytes != -1);
  289.  
  290.     //  Check whether we haven't got half of command.
  291.     zmq_assert (nbytes == sizeof (command_t));
  292.  
  293.     return 0;
  294. }
  295.  
  296. #endif
  297.  
  298. int zmq::mailbox_t::make_socketpair (fd_t *r_, fd_t *w_)
  299. {
  300. #if defined ZMQ_HAVE_WINDOWS
  301.  
  302.     //  Windows has no 'socketpair' function. CreatePipe is no good as pipe
  303.     //  handles cannot be polled on. Here we create the socketpair by hand.
  304.     *w_ = INVALID_SOCKET;
  305.     *r_ = INVALID_SOCKET;
  306.  
  307.     //  Create listening socket.
  308.     SOCKET listener;
  309.     listener = socket (AF_INET, SOCK_STREAM, 0);
  310.     wsa_assert (listener != INVALID_SOCKET);
  311.  
  312.     //  Set SO_REUSEADDR and TCP_NODELAY on listening socket.
  313.     BOOL so_reuseaddr = 1;
  314.     int rc = setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
  315.         (char *)&so_reuseaddr, sizeof (so_reuseaddr));
  316.     wsa_assert (rc != SOCKET_ERROR);
  317.     BOOL tcp_nodelay = 1;
  318.     rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY,
  319.         (char *)&tcp_nodelay, sizeof (tcp_nodelay));
  320.     wsa_assert (rc != SOCKET_ERROR);
  321.  
  322.     //  Bind listening socket to any free local port.
  323.     struct sockaddr_in addr;
  324.     memset (&addr, 0, sizeof (addr));
  325.     addr.sin_family = AF_INET;
  326.     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  327.     addr.sin_port = 0;
  328.     rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
  329.     wsa_assert (rc != SOCKET_ERROR);
  330.  
  331.     //  Retrieve local port listener is bound to (into addr).
  332.     int addrlen = sizeof (addr);
  333.     rc = getsockname (listener, (struct sockaddr*) &addr, &addrlen);
  334.     wsa_assert (rc != SOCKET_ERROR);
  335.  
  336.     //  Listen for incomming connections.
  337.     rc = listen (listener, 1);
  338.     wsa_assert (rc != SOCKET_ERROR);
  339.  
  340.     //  Create the writer socket.
  341.     *w_ = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0,  0);
  342.     wsa_assert (*w_ != INVALID_SOCKET);
  343.  
  344.     //  Set TCP_NODELAY on writer socket.
  345.     rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY,
  346.         (char *)&tcp_nodelay, sizeof (tcp_nodelay));
  347.     wsa_assert (rc != SOCKET_ERROR);
  348.  
  349.     //  Connect writer to the listener.
  350.     rc = connect (*w_, (sockaddr *) &addr, sizeof (addr));
  351.     wsa_assert (rc != SOCKET_ERROR);
  352.  
  353.     //  Accept connection from writer.
  354.     *r_ = accept (listener, NULL, NULL);
  355.     wsa_assert (*r_ != INVALID_SOCKET);
  356.  
  357.     //  We don't need the listening socket anymore. Close it.
  358.     rc = closesocket (listener);
  359.     wsa_assert (rc != SOCKET_ERROR);
  360.  
  361.     return 0;
  362.  
  363. #elif defined ZMQ_HAVE_OPENVMS
  364.  
  365.     //  Whilst OpenVMS supports socketpair - it maps to AF_INET only.  Further,
  366.     //  it does not set the socket options TCP_NODELAY and TCP_NODELACK which
  367.     //  can lead to performance problems.
  368.     //
  369.     //  The bug will be fixed in V5.6 ECO4 and beyond.  In the meantime, we'll
  370.     //  create the socket pair manually.
  371.     sockaddr_in lcladdr;
  372.     memset (&lcladdr, 0, sizeof (lcladdr));
  373.     lcladdr.sin_family = AF_INET;
  374.     lcladdr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  375.     lcladdr.sin_port = 0;
  376.  
  377.     int listener = socket (AF_INET, SOCK_STREAM, 0);
  378.     errno_assert (listener != -1);
  379.  
  380.     int on = 1;
  381.     int rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
  382.     errno_assert (rc != -1);
  383.  
  384.     rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
  385.     errno_assert (rc != -1);
  386.  
  387.     rc = bind(listener, (struct sockaddr*) &lcladdr, sizeof (lcladdr));
  388.     errno_assert (rc != -1);
  389.  
  390.     socklen_t lcladdr_len = sizeof (lcladdr);
  391.  
  392.     rc = getsockname (listener, (struct sockaddr*) &lcladdr, &lcladdr_len);
  393.     errno_assert (rc != -1);
  394.  
  395.     rc = listen (listener, 1);
  396.     errno_assert (rc != -1);
  397.  
  398.     *w_ = socket (AF_INET, SOCK_STREAM, 0);
  399.     errno_assert (*w_ != -1);
  400.  
  401.     rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
  402.     errno_assert (rc != -1);
  403.  
  404.     rc = setsockopt (*w_, IPPROTO_TCP, TCP_NODELACK, &on, sizeof (on));
  405.     errno_assert (rc != -1);
  406.  
  407.     rc = connect (*w_, (struct sockaddr*) &lcladdr, sizeof (lcladdr));
  408.     errno_assert (rc != -1);
  409.  
  410.     *r_ = accept (listener, NULL, NULL);
  411.     errno_assert (*r_ != -1);
  412.  
  413.     close (listener);
  414.  
  415.     return 0;
  416.  
  417. #else // All other implementations support socketpair()
  418.  
  419.     int sv [2];
  420.     int rc = socketpair (AF_UNIX, SOCK_STREAM, 0, sv);
  421.     errno_assert (rc == 0);
  422.     *w_ = sv [0];
  423.     *r_ = sv [1];
  424.     return 0;
  425.  
  426. #endif
  427. }
Advertisement
Add Comment
Please, Sign In to add comment