本文整理汇总了C++中wsa_assert函数的典型用法代码示例。如果您正苦于以下问题:C++ wsa_assert函数的具体用法?C++ wsa_assert怎么用?C++ wsa_assert使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wsa_assert函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: zmq_assert
zmq::stream_engine_t::~stream_engine_t ()
{
zmq_assert (!plugged);
if (s != retired_fd) {
#ifdef ZMQ_HAVE_WINDOWS
int rc = closesocket (s);
wsa_assert (rc != SOCKET_ERROR);
#else
int rc = close (s);
#ifdef __FreeBSD_kernel__
// FreeBSD may return ECONNRESET on close() under load but this is not
// an error.
if (rc == -1 && errno == ECONNRESET)
rc = 0;
#endif
errno_assert (rc == 0);
#endif
s = retired_fd;
}
int rc = tx_msg.close ();
errno_assert (rc == 0);
// Drop reference to metadata and destroy it if we are
// the only user.
if (metadata != NULL) {
if (metadata->drop_ref ()) {
LIBZMQ_DELETE(metadata);
}
}
LIBZMQ_DELETE(encoder);
LIBZMQ_DELETE(decoder);
LIBZMQ_DELETE(mechanism);
}
示例2: getevents
int getevents (int s, int events, int timeout)
{
int rc;
fd_set pollset;
#if defined NN_HAVE_WINDOWS
SOCKET rcvfd;
SOCKET sndfd;
#else
int rcvfd;
int sndfd;
int maxfd;
#endif
size_t fdsz;
struct timeval tv;
int revents;
#if !defined NN_HAVE_WINDOWS
maxfd = 0;
#endif
FD_ZERO (&pollset);
if (events & NN_IN) {
fdsz = sizeof (rcvfd);
rc = nn_getsockopt (s, NN_SOL_SOCKET, NN_RCVFD, (char*) &rcvfd, &fdsz);
errno_assert (rc == 0);
nn_assert (fdsz == sizeof (rcvfd));
FD_SET (rcvfd, &pollset);
#if !defined NN_HAVE_WINDOWS
if (rcvfd + 1 > maxfd)
maxfd = rcvfd + 1;
#endif
}
if (events & NN_OUT) {
fdsz = sizeof (sndfd);
rc = nn_getsockopt (s, NN_SOL_SOCKET, NN_SNDFD, (char*) &sndfd, &fdsz);
errno_assert (rc == 0);
nn_assert (fdsz == sizeof (sndfd));
FD_SET (sndfd, &pollset);
#if !defined NN_HAVE_WINDOWS
if (sndfd + 1 > maxfd)
maxfd = sndfd + 1;
#endif
}
if (timeout >= 0) {
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
}
#if defined NN_HAVE_WINDOWS
rc = select (0, &pollset, NULL, NULL, timeout < 0 ? NULL : &tv);
wsa_assert (rc != SOCKET_ERROR);
#else
rc = select (maxfd, &pollset, NULL, NULL, timeout < 0 ? NULL : &tv);
errno_assert (rc >= 0);
#endif
revents = 0;
if ((events & NN_IN) && FD_ISSET (rcvfd, &pollset))
revents |= NN_IN;
if ((events & NN_OUT) && FD_ISSET (sndfd, &pollset))
revents |= NN_OUT;
return revents;
}
示例3: switch
int zmq::wsa_error_to_errno (int errcode)
{
switch (errcode) {
// 10009 - File handle is not valid.
case WSAEBADF:
return EBADF;
// 10013 - Permission denied.
case WSAEACCES:
return EACCES;
// 10014 - Bad address.
case WSAEFAULT:
return EFAULT;
// 10022 - Invalid argument.
case WSAEINVAL:
return EINVAL;
// 10024 - Too many open files.
case WSAEMFILE:
return EMFILE;
// 10036 - Operation now in progress.
case WSAEINPROGRESS:
return EAGAIN;
// 10040 - Message too long.
case WSAEMSGSIZE:
return EMSGSIZE;
// 10043 - Protocol not supported.
case WSAEPROTONOSUPPORT:
return EPROTONOSUPPORT;
// 10047 - Address family not supported by protocol family.
case WSAEAFNOSUPPORT:
return EAFNOSUPPORT;
// 10048 - Address already in use.
case WSAEADDRINUSE:
return EADDRINUSE;
// 10049 - Cannot assign requested address.
case WSAEADDRNOTAVAIL:
return EADDRNOTAVAIL;
// 10050 - Network is down.
case WSAENETDOWN:
return ENETDOWN;
// 10051 - Network is unreachable.
case WSAENETUNREACH:
return ENETUNREACH;
// 10052 - Network dropped connection on reset.
case WSAENETRESET:
return ENETRESET;
// 10053 - Software caused connection abort.
case WSAECONNABORTED:
return ECONNABORTED;
// 10054 - Connection reset by peer.
case WSAECONNRESET:
return ECONNRESET;
// 10055 - No buffer space available.
case WSAENOBUFS:
return ENOBUFS;
// 10057 - Socket is not connected.
case WSAENOTCONN:
return ENOTCONN;
// 10060 - Connection timed out.
case WSAETIMEDOUT:
return ETIMEDOUT;
// 10061 - Connection refused.
case WSAECONNREFUSED:
return ECONNREFUSED;
// 10065 - No route to host.
case WSAEHOSTUNREACH:
return EHOSTUNREACH;
default:
wsa_assert (false);
}
// Not reachable
return 0;
}
示例4: poll
int zmq::signaler_t::wait (int timeout_)
{
#ifdef HAVE_FORK
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());
errno = EINTR;
return -1;
}
#endif
#ifdef ZMQ_POLL_BASED_ON_POLL
struct pollfd pfd;
pfd.fd = r;
pfd.events = POLLIN;
int rc = poll (&pfd, 1, timeout_);
if (unlikely (rc < 0)) {
errno_assert (errno == EINTR);
return -1;
}
else
if (unlikely (rc == 0)) {
errno = EAGAIN;
return -1;
}
#ifdef HAVE_FORK
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 #2\n", getpid());
errno = EINTR;
return -1;
}
#endif
zmq_assert (rc == 1);
zmq_assert (pfd.revents & POLLIN);
return 0;
#elif defined ZMQ_POLL_BASED_ON_SELECT
fd_set fds;
FD_ZERO (&fds);
FD_SET (r, &fds);
struct timeval timeout;
if (timeout_ >= 0) {
timeout.tv_sec = timeout_ / 1000;
timeout.tv_usec = timeout_ % 1000 * 1000;
}
#ifdef ZMQ_HAVE_WINDOWS
int rc = select (0, &fds, NULL, NULL,
timeout_ >= 0 ? &timeout : NULL);
wsa_assert (rc != SOCKET_ERROR);
#else
int rc = select (r + 1, &fds, NULL, NULL,
timeout_ >= 0 ? &timeout : NULL);
if (unlikely (rc < 0)) {
errno_assert (errno == EINTR);
return -1;
}
#endif
if (unlikely (rc == 0)) {
errno = EAGAIN;
return -1;
}
zmq_assert (rc == 1);
return 0;
#else
#error
#endif
}
示例5: switch
//.........这里部分代码省略.........
return EAFNOSUPPORT;
// 10048 - Address already in use.
case WSAEADDRINUSE:
return EADDRINUSE;
// 10049 - Cannot assign requested address.
case WSAEADDRNOTAVAIL:
return EADDRNOTAVAIL;
// 10050 - Network is down.
case WSAENETDOWN:
return ENETDOWN;
// 10051 - Network is unreachable.
case WSAENETUNREACH:
return ENETUNREACH;
// 10052 - Network dropped connection on reset.
case WSAENETRESET:
return ENETRESET;
// 10053 - Software caused connection abort.
case WSAECONNABORTED:
return ECONNABORTED;
// 10054 - Connection reset by peer.
case WSAECONNRESET:
return ECONNRESET;
// 10055 - No buffer space available.
case WSAENOBUFS:
return ENOBUFS;
// 10056 - Socket is already connected.
case WSAEISCONN:
return EFAULT;
// 10057 - Socket is not connected.
case WSAENOTCONN:
return ENOTCONN;
// 10058 - Can't send after socket shutdown.
case WSAESHUTDOWN:
return EFAULT;
// 10059 - Too many references can't splice.
case WSAETOOMANYREFS:
return EFAULT;
// 10060 - Connection timed out.
case WSAETIMEDOUT:
return ETIMEDOUT;
// 10061 - Connection refused.
case WSAECONNREFUSED:
return ECONNREFUSED;
// 10062 - Too many levels of symbolic links.
case WSAELOOP:
return EFAULT;
// 10063 - File name too long.
case WSAENAMETOOLONG:
return EFAULT;
// 10064 - Host is down.
case WSAEHOSTDOWN:
return EAGAIN;
// 10065 - No route to host.
case WSAEHOSTUNREACH:
return EHOSTUNREACH;
// 10066 - Directory not empty.
case WSAENOTEMPTY:
return EFAULT;
// 10067 - Too many processes.
case WSAEPROCLIM:
return EFAULT;
// 10068 - Too many users.
case WSAEUSERS:
return EFAULT;
// 10069 - Disc Quota Exceeded.
case WSAEDQUOT:
return EFAULT;
// 10070 - Stale NFS file handle.
case WSAESTALE:
return EFAULT;
// 10071 - Too many levels of remote in path.
case WSAEREMOTE:
return EFAULT;
// 10091 - Network SubSystem is unavailable.
case WSASYSNOTREADY:
return EFAULT;
// 10092 - WINSOCK DLL Version out of range.
case WSAVERNOTSUPPORTED:
return EFAULT;
// 10093 - Successful WSASTARTUP not yet performed.
case WSANOTINITIALISED:
return EFAULT;
// 11001 - Host not found.
case WSAHOST_NOT_FOUND:
return EFAULT;
// 11002 - Non-Authoritative Host not found.
case WSATRY_AGAIN:
return EFAULT;
// 11003 - Non-Recoverable errors: FORMERR REFUSED NOTIMP.
case WSANO_RECOVERY:
return EFAULT;
// 11004 - Valid name no data record of requested.
case WSANO_DATA:
return EFAULT;
default:
wsa_assert (false);
}
// Not reachable
return 0;
}
示例6: setsockopt
void zmq::tune_tcp_keepalives (fd_t s_, int keepalive_, int keepalive_cnt_, int keepalive_idle_, int keepalive_intvl_)
{
// These options are used only under certain #ifdefs below.
(void)keepalive_;
(void)keepalive_cnt_;
(void)keepalive_idle_;
(void)keepalive_intvl_;
// If none of the #ifdefs apply, then s_ is unused.
(void)s_;
// Tuning TCP keep-alives if platform allows it
// All values = -1 means skip and leave it for OS
#ifdef ZMQ_HAVE_SO_KEEPALIVE
if (keepalive_ != -1) {
int rc = setsockopt (s_, SOL_SOCKET, SO_KEEPALIVE, (char*) &keepalive_, sizeof (int));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
#ifdef ZMQ_HAVE_TCP_KEEPCNT
if (keepalive_cnt_ != -1) {
int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPCNT, &keepalive_cnt_, sizeof (int));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
}
#endif // ZMQ_HAVE_TCP_KEEPCNT
#ifdef ZMQ_HAVE_TCP_KEEPIDLE
if (keepalive_idle_ != -1) {
int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPIDLE, &keepalive_idle_, sizeof (int));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
}
#else // ZMQ_HAVE_TCP_KEEPIDLE
#ifdef ZMQ_HAVE_TCP_KEEPALIVE
if (keepalive_idle_ != -1) {
int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPALIVE, &keepalive_idle_, sizeof (int));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
}
#endif // ZMQ_HAVE_TCP_KEEPALIVE
#endif // ZMQ_HAVE_TCP_KEEPIDLE
#ifdef ZMQ_HAVE_TCP_KEEPINTVL
if (keepalive_intvl_ != -1) {
int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPINTVL, &keepalive_intvl_, sizeof (int));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
}
#endif // ZMQ_HAVE_TCP_KEEPINTVL
}
#endif // ZMQ_HAVE_SO_KEEPALIVE
}
示例7: open_socket
int zmq::tcp_listener_t::set_address (const char *addr_)
{
// Convert the textual address into address structure.
int rc = address.resolve (addr_, true, options.ipv6);
if (rc != 0)
return -1;
address.to_string (endpoint);
if (options.use_fd != -1) {
s = options.use_fd;
socket->event_listening (endpoint, (int) s);
return 0;
}
// Create a listening socket.
s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
// IPv6 address family not supported, try automatic downgrade to IPv4.
if (s == zmq::retired_fd && address.family () == AF_INET6
&& errno == EAFNOSUPPORT && options.ipv6) {
rc = address.resolve (addr_, true, false);
if (rc != 0)
return rc;
s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
#ifdef ZMQ_HAVE_WINDOWS
if (s == INVALID_SOCKET) {
errno = wsa_error_to_errno (WSAGetLastError ());
return -1;
}
#if !defined _WIN32_WCE && !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
BOOL brc = SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0);
win_assert (brc);
#endif
#else
if (s == -1)
return -1;
#endif
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
if (address.family () == AF_INET6)
enable_ipv4_mapping (s);
// Set the IP Type-Of-Service for the underlying socket
if (options.tos != 0)
set_ip_type_of_service (s, options.tos);
// Set the socket to loopback fastpath if configured.
if (options.loopback_fastpath)
tcp_tune_loopback_fast_path (s);
// Bind the socket to a device if applicable
if (!options.bound_device.empty ())
bind_to_device (s, options.bound_device);
// Set the socket buffer limits for the underlying socket.
if (options.sndbuf >= 0)
set_tcp_send_buffer (s, options.sndbuf);
if (options.rcvbuf >= 0)
set_tcp_receive_buffer (s, options.rcvbuf);
// Allow reusing of the address.
int flag = 1;
#ifdef ZMQ_HAVE_WINDOWS
rc = setsockopt (s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char *) &flag,
sizeof (int));
wsa_assert (rc != SOCKET_ERROR);
#else
rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
errno_assert (rc == 0);
#endif
// Bind the socket to the network interface and port.
rc = bind (s, address.addr (), address.addrlen ());
#ifdef ZMQ_HAVE_WINDOWS
if (rc == SOCKET_ERROR) {
errno = wsa_error_to_errno (WSAGetLastError ());
goto error;
}
#else
if (rc != 0)
goto error;
#endif
// Listen for incoming connections.
rc = listen (s, options.backlog);
#ifdef ZMQ_HAVE_WINDOWS
if (rc == SOCKET_ERROR) {
errno = wsa_error_to_errno (WSAGetLastError ());
goto error;
}
#else
if (rc != 0)
goto error;
#endif
//.........这里部分代码省略.........
示例8: while
void zmq::select_t::loop ()
{
while (!stopping) {
// Execute any due timers.
int timeout = (int) execute_timers ();
#if defined ZMQ_HAVE_OSX
struct timeval tv = { (long) (timeout / 1000), timeout % 1000 * 1000 };
#else
struct timeval tv = { (long) (timeout / 1000), (long) (timeout % 1000 * 1000) };
#endif
int rc = 0;
#if defined ZMQ_HAVE_WINDOWS
/*
On Windows select does not allow to mix descriptors from different
service providers. It seems to work for AF_INET and AF_INET6,
but fails for AF_INET and VMCI. The workaround is to use
WSAEventSelect and WSAWaitForMultipleEvents to wait, then use
select to find out what actually changed. WSAWaitForMultipleEvents
cannot be used alone, because it does not support more than 64 events
which is not enough.
To reduce unncessary overhead, WSA is only used when there are more
than one family. Moreover, AF_INET and AF_INET6 are considered the same
family because Windows seems to handle them properly.
See get_fd_family for details.
*/
wsa_events_t wsa_events;
// If there is just one family, there is no reason to use WSA events.
if (family_entries.size () > 1) {
for (family_entries_t::iterator family_entry_it = family_entries.begin ();
family_entry_it != family_entries.end (); ++family_entry_it) {
family_entry_t& family_entry = family_entry_it->second;
for (fd_entries_t::iterator fd_entry_it = family_entry.fd_entries.begin ();
fd_entry_it != family_entry.fd_entries.end (); ++fd_entry_it) {
fd_t fd = fd_entry_it->fd;
// http://stackoverflow.com/q/35043420/188530
if (FD_ISSET (fd, &family_entry.fds_set.read) &&
FD_ISSET (fd, &family_entry.fds_set.write))
rc = WSAEventSelect (fd, wsa_events.events [3],
FD_READ | FD_ACCEPT | FD_CLOSE | FD_WRITE | FD_CONNECT | FD_OOB);
else if (FD_ISSET (fd, &family_entry.fds_set.read))
rc = WSAEventSelect (fd, wsa_events.events [0],
FD_READ | FD_ACCEPT | FD_CLOSE | FD_OOB);
else if (FD_ISSET (fd, &family_entry.fds_set.write))
rc = WSAEventSelect (fd, wsa_events.events [1],
FD_WRITE | FD_CONNECT | FD_OOB);
else if (FD_ISSET (fd, &family_entry.fds_set.error))
rc = WSAEventSelect (fd, wsa_events.events [2],
FD_OOB);
else
rc = 0;
wsa_assert (rc != SOCKET_ERROR);
}
}
}
#endif
#if defined ZMQ_HAVE_WINDOWS
if (family_entries.size () > 1) {
rc = WSAWaitForMultipleEvents (4, wsa_events.events, FALSE,
timeout ? timeout : INFINITE, FALSE);
wsa_assert (rc != (int)WSA_WAIT_FAILED);
zmq_assert (rc != WSA_WAIT_IO_COMPLETION);
if (rc == WSA_WAIT_TIMEOUT)
continue;
}
for (current_family_entry_it = family_entries.begin ();
current_family_entry_it != family_entries.end (); ++current_family_entry_it) {
family_entry_t& family_entry = current_family_entry_it->second;
// select will fail when run with empty sets.
if (family_entry.fd_entries.empty ())
continue;
fds_set_t local_fds_set = family_entry.fds_set;
if (family_entries.size () > 1) {
// There is no reason to wait again after WSAWaitForMultipleEvents.
// Simply collect what is ready.
struct timeval tv_nodelay = { 0, 0 };
rc = select (0, &local_fds_set.read, &local_fds_set.write, &local_fds_set.error,
&tv_nodelay);
}
else
rc = select (0, &local_fds_set.read, &local_fds_set.write,
&local_fds_set.error, timeout > 0 ? &tv : NULL);
wsa_assert (rc != SOCKET_ERROR);
// Size is cached to avoid iteration through recently added descriptors.
for (fd_entries_t::size_type i = 0, size = family_entry.fd_entries.size (); i < size && rc > 0; ++i) {
//.........这里部分代码省略.........
示例9: while
void zmq::select_t::loop ()
{
while (!stopping) {
// Execute any due timers.
int timeout = (int) execute_timers ();
// Intialise the pollsets.
memcpy (&readfds, &source_set_in, sizeof source_set_in);
memcpy (&writefds, &source_set_out, sizeof source_set_out);
memcpy (&exceptfds, &source_set_err, sizeof source_set_err);
// Wait for events.
#ifdef ZMQ_HAVE_OSX
struct timeval tv = {static_cast<int>(timeout / 1000),
static_cast<int>(timeout % 1000 * 1000)};
#else
struct timeval tv = {(long) (timeout / 1000),
(long) (timeout % 1000 * 1000)};
#endif
#ifdef ZMQ_HAVE_WINDOWS
int rc = select (0, &readfds, &writefds, &exceptfds,
timeout ? &tv : NULL);
wsa_assert (rc != SOCKET_ERROR);
#else
int rc = select (maxfd + 1, &readfds, &writefds, &exceptfds,
timeout ? &tv : NULL);
if (rc == -1) {
errno_assert (errno == EINTR);
continue;
}
#endif
// If there are no events (i.e. it's a timeout) there's no point
// in checking the pollset.
if (rc == 0)
continue;
for (fd_set_t::size_type i = 0; i < fds.size (); i ++) {
if (fds [i].fd == retired_fd)
continue;
if (FD_ISSET (fds [i].fd, &exceptfds))
fds [i].events->in_event ();
if (fds [i].fd == retired_fd)
continue;
if (FD_ISSET (fds [i].fd, &writefds))
fds [i].events->out_event ();
if (fds [i].fd == retired_fd)
continue;
if (FD_ISSET (fds [i].fd, &readfds))
fds [i].events->in_event ();
}
// Destroy retired event sources.
if (retired) {
fds.erase (std::remove_if (fds.begin (), fds.end (),
zmq::select_t::is_retired_fd), fds.end ());
retired = false;
}
}
}
示例10: nn_efd_wait
int nn_efd_wait (struct nn_efd *self, int timeout)
{
int rc;
struct timeval tv;
SOCKET fd = self->r;
uint64_t expire;
if (timeout > 0) {
expire = nn_clock_ms() + timeout;
tv.tv_sec = timeout / 1000;
tv.tv_usec = timeout % 1000 * 1000;
} else {
expire = timeout;
}
for (;;) {
if (nn_slow (fd == INVALID_SOCKET)) {
return -EBADF;
}
FD_SET (fd, &self->fds);
switch (expire) {
case 0:
tv.tv_sec = 0;
tv.tv_usec = 0;
break;
case (uint64_t)-1:
tv.tv_sec = 0;
tv.tv_usec = 100000;
break;
default:
timeout = (int)(expire - nn_clock_ms());
if (timeout < 0) {
return -ETIMEDOUT;
}
if (timeout > 100) {
tv.tv_sec = 0;
tv.tv_usec = 100000;
} else {
tv.tv_sec = timeout / 1000;
tv.tv_usec = timeout % 1000 * 1000;
}
}
rc = select (0, &self->fds, NULL, NULL, &tv);
if (nn_slow (rc == SOCKET_ERROR)) {
rc = nn_err_wsa_to_posix (WSAGetLastError ());
errno = rc;
/* Treat these as a non-fatal errors, typically occuring when the
socket is being closed from a separate thread during a blocking
I/O operation. */
if (rc == EINTR || rc == ENOTSOCK)
return -EINTR;
} else if (rc == 0) {
if (expire == 0)
return -ETIMEDOUT;
if ((expire != (uint64_t)-1) && (expire < nn_clock_ms())) {
return -ETIMEDOUT;
}
continue;
}
wsa_assert (rc >= 0);
return 0;
}
}
示例11: zmq_assert
void zmq::udp_engine_t::plug (io_thread_t *io_thread_, session_base_t *session_)
{
zmq_assert (!plugged);
plugged = true;
zmq_assert (!session);
zmq_assert (session_);
session = session_;
// Connect to I/O threads poller object.
io_object_t::plug (io_thread_);
handle = add_fd (fd);
// Bind the socket to a device if applicable
if (!options.bound_device.empty ())
bind_to_device (fd, options.bound_device);
if (send_enabled) {
if (!options.raw_socket) {
out_address = address->resolved.udp_addr->dest_addr ();
out_addrlen = address->resolved.udp_addr->dest_addrlen ();
} else {
out_address = (sockaddr *) &raw_address;
out_addrlen = sizeof (sockaddr_in);
}
set_pollout (handle);
}
if (recv_enabled) {
int on = 1;
int rc =
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
#ifdef ZMQ_HAVE_VXWORKS
rc = bind (fd, (sockaddr *) address->resolved.udp_addr->bind_addr (),
address->resolved.udp_addr->bind_addrlen ());
#else
rc = bind (fd, address->resolved.udp_addr->bind_addr (),
address->resolved.udp_addr->bind_addrlen ());
#endif
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
if (address->resolved.udp_addr->is_mcast ()) {
struct ip_mreq mreq;
mreq.imr_multiaddr = address->resolved.udp_addr->multicast_ip ();
mreq.imr_interface = address->resolved.udp_addr->interface_ip ();
rc = setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq,
sizeof (mreq));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
errno_assert (rc == 0);
#endif
}
set_pollin (handle);
// Call restart output to drop all join/leave commands
restart_output ();
}
}
示例12: sizeof
void zmq::udp_engine_t::in_event ()
{
struct sockaddr_in in_address;
socklen_t in_addrlen = sizeof (sockaddr_in);
#ifdef ZMQ_HAVE_WINDOWS
int nbytes = recvfrom (fd, (char *) in_buffer, MAX_UDP_MSG, 0,
(sockaddr *) &in_address, &in_addrlen);
const int last_error = WSAGetLastError ();
if (nbytes == SOCKET_ERROR) {
wsa_assert (last_error == WSAENETDOWN || last_error == WSAENETRESET
|| last_error == WSAEWOULDBLOCK);
return;
}
#elif defined ZMQ_HAVE_VXWORKS
int nbytes = recvfrom (fd, (char *) in_buffer, MAX_UDP_MSG, 0,
(sockaddr *) &in_address, (int *) &in_addrlen);
if (nbytes == -1) {
errno_assert (errno != EBADF && errno != EFAULT && errno != ENOMEM
&& errno != ENOTSOCK);
return;
}
#else
int nbytes = recvfrom (fd, in_buffer, MAX_UDP_MSG, 0,
(sockaddr *) &in_address, &in_addrlen);
if (nbytes == -1) {
errno_assert (errno != EBADF && errno != EFAULT && errno != ENOMEM
&& errno != ENOTSOCK);
return;
}
#endif
int rc;
int body_size;
int body_offset;
msg_t msg;
if (options.raw_socket) {
sockaddr_to_msg (&msg, &in_address);
body_size = nbytes;
body_offset = 0;
} else {
char *group_buffer = (char *) in_buffer + 1;
int group_size = in_buffer[0];
rc = msg.init_size (group_size);
errno_assert (rc == 0);
msg.set_flags (msg_t::more);
memcpy (msg.data (), group_buffer, group_size);
// This doesn't fit, just ingore
if (nbytes - 1 < group_size)
return;
body_size = nbytes - 1 - group_size;
body_offset = 1 + group_size;
}
// Push group description to session
rc = session->push_msg (&msg);
errno_assert (rc == 0 || (rc == -1 && errno == EAGAIN));
// Group description message doesn't fit in the pipe, drop
if (rc != 0) {
rc = msg.close ();
errno_assert (rc == 0);
reset_pollin (handle);
return;
}
rc = msg.close ();
errno_assert (rc == 0);
rc = msg.init_size (body_size);
errno_assert (rc == 0);
memcpy (msg.data (), in_buffer + body_offset, body_size);
// Push message body to session
rc = session->push_msg (&msg);
// Message body doesn't fit in the pipe, drop and reset session state
if (rc != 0) {
rc = msg.close ();
errno_assert (rc == 0);
session->reset ();
reset_pollin (handle);
return;
}
rc = msg.close ();
errno_assert (rc == 0);
session->flush ();
}
示例13: errno_assert
void zmq::udp_engine_t::out_event ()
{
msg_t group_msg;
int rc = session->pull_msg (&group_msg);
errno_assert (rc == 0 || (rc == -1 && errno == EAGAIN));
if (rc == 0) {
msg_t body_msg;
rc = session->pull_msg (&body_msg);
size_t group_size = group_msg.size ();
size_t body_size = body_msg.size ();
size_t size;
if (options.raw_socket) {
rc = resolve_raw_address ((char *) group_msg.data (), group_size);
// We discard the message if address is not valid
if (rc != 0) {
rc = group_msg.close ();
errno_assert (rc == 0);
body_msg.close ();
errno_assert (rc == 0);
return;
}
size = body_size;
memcpy (out_buffer, body_msg.data (), body_size);
} else {
size = group_size + body_size + 1;
// TODO: check if larger than maximum size
out_buffer[0] = (unsigned char) group_size;
memcpy (out_buffer + 1, group_msg.data (), group_size);
memcpy (out_buffer + 1 + group_size, body_msg.data (), body_size);
}
rc = group_msg.close ();
errno_assert (rc == 0);
body_msg.close ();
errno_assert (rc == 0);
#ifdef ZMQ_HAVE_WINDOWS
rc = sendto (fd, (const char *) out_buffer, (int) size, 0, out_address,
(int) out_addrlen);
wsa_assert (rc != SOCKET_ERROR);
#elif defined ZMQ_HAVE_VXWORKS
rc = sendto (fd, (caddr_t) out_buffer, size, 0,
(sockaddr *) out_address, (int) out_addrlen);
errno_assert (rc != -1);
#else
rc = sendto (fd, out_buffer, size, 0, out_address, out_addrlen);
errno_assert (rc != -1);
#endif
} else
reset_pollout (handle);
}
示例14: make_fdpair
static int make_fdpair (xs::fd_t *r_, xs::fd_t *w_)
{
#if defined XS_HAVE_EVENTFD
// Create eventfd object.
#if defined EFD_CLOEXEC
xs::fd_t fd = eventfd (0, EFD_CLOEXEC);
if (fd == -1)
return -1;
#else
xs::fd_t fd = eventfd (0, 0);
if (fd == -1)
return -1;
#if defined FD_CLOEXEC
int rc = fcntl (fd, F_SETFD, FD_CLOEXEC);
errno_assert (rc != -1);
#endif
#endif
*w_ = fd;
*r_ = fd;
return 0;
#elif defined XS_HAVE_WINDOWS
// On Windows we are using TCP sockets for in-process communication.
// That is a security hole -- other processes on the same box may connect
// to the bound TCP port and hook into internal signal processing of
// the library. To solve this problem we should use a proper in-process
// signaling mechanism such as private semaphore. However, on Windows,
// these cannot be polled on using select(). Other functions that allow
// polling on these objects (e.g. WaitForMulitpleObjects) don't allow
// to poll on sockets. Thus, the only way to fix the problem is to
// implement IOCP polling mechanism that allows to poll on both sockets
// and in-process synchronisation objects.
// Make the following critical section accessible to everyone.
SECURITY_ATTRIBUTES sa = {0};
sa.nLength = sizeof (sa);
sa.bInheritHandle = FALSE;
SECURITY_DESCRIPTOR sd;
BOOL ok = InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
win_assert (ok);
ok = SetSecurityDescriptorDacl(&sd, TRUE, (PACL) NULL, FALSE);
win_assert (ok);
sa.lpSecurityDescriptor = &sd;
// This function has to be in a system-wide critical section so that
// two instances of the library don't accidentally create signaler
// crossing the process boundary.
// We'll use named event object to implement the critical section.
HANDLE sync = CreateEvent (&sa, FALSE, TRUE, "xs-signaler-port-sync");
win_assert (sync != NULL);
// Enter the critical section.
DWORD dwrc = WaitForSingleObject (sync, INFINITE);
xs_assert (dwrc == WAIT_OBJECT_0);
// Windows has no 'socketpair' function. CreatePipe is no good as pipe
// handles cannot be polled on. Here we create the socketpair by hand.
*w_ = INVALID_SOCKET;
*r_ = INVALID_SOCKET;
// Create listening socket.
SOCKET listener;
listener = xs::open_socket (AF_INET, SOCK_STREAM, 0);
if (listener == xs::retired_fd)
return -1;
// 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));
wsa_assert (rc != SOCKET_ERROR);
BOOL tcp_nodelay = 1;
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY,
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
wsa_assert (rc != SOCKET_ERROR);
// Bind listening socket to the local port.
struct sockaddr_in addr;
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
addr.sin_port = htons (xs::signaler_port);
rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
wsa_assert (rc != SOCKET_ERROR);
// Listen for incomming connections.
rc = listen (listener, 1);
wsa_assert (rc != SOCKET_ERROR);
// Create the writer socket.
*w_ = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
if (*w_ == xs::retired_fd) {
rc = closesocket (listener);
wsa_assert (rc != SOCKET_ERROR);
return -1;
}
// Set TCP_NODELAY on writer socket.
//.........这里部分代码省略.........
示例15: nsocketpair
void nsocketpair(fd_t* pair)
{
#ifdef _WIN32
// Create listening socket.
SOCKET listener;
BOOL so_reuseaddr = 1;
int rc;
BOOL tcp_nodelay = 1;
struct sockaddr_in addr;
int addrlen = sizeof (addr);
// Windows has no 'socketpair' function. CreatePipe is no good as pipe
// handles cannot be polled on. Here we create the socketpair by hand.
pair[0] = INVALID_SOCKET;
pair[1] = INVALID_SOCKET;
socket_init();
listener = socket (AF_INET, SOCK_STREAM, 0);
wsa_assert (listener != INVALID_SOCKET);
// Set SO_REUSEADDR and TCP_NODELAY on listening socket.
rc = setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
(char *)&so_reuseaddr, sizeof (so_reuseaddr));
wsa_assert (rc != SOCKET_ERROR);
rc = setsockopt (listener, IPPROTO_TCP, TCP_NODELAY,
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
wsa_assert (rc != SOCKET_ERROR);
// Bind listening socket to any free local port.
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
addr.sin_port = 0;
rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
wsa_assert (rc != SOCKET_ERROR);
// Retrieve local port listener is bound to (into addr).
rc = getsockname (listener, (struct sockaddr*) &addr, &addrlen);
wsa_assert (rc != SOCKET_ERROR);
// Listen for incomming connections.
rc = listen (listener, 1);
wsa_assert (rc != SOCKET_ERROR);
// Create the writer socket.
pair[0] = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
wsa_assert (pair[0] != INVALID_SOCKET);
// Set TCP_NODELAY on writer socket.
rc = setsockopt (pair[0], IPPROTO_TCP, TCP_NODELAY,
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
wsa_assert (rc != SOCKET_ERROR);
// Connect writer to the listener.
rc = connect (pair[0], (struct sockaddr *) &addr, sizeof (addr));
wsa_assert (rc != SOCKET_ERROR);
// Accept connection from writer.
pair[1] = accept (listener, NULL, NULL);
wsa_assert (pair[1] != INVALID_SOCKET);
// We don't need the listening socket anymore. Close it.
rc = closesocket (listener);
wsa_assert (rc != SOCKET_ERROR);
#else
int rc = socketpair (AF_UNIX, SOCK_STREAM, 0, pair);
errno_assert (rc == 0);
#endif
}