35 #include "platform.hpp" 37 #if defined ZMQ_HAVE_WINDOWS 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <netinet/in.h> 44 #include <netinet/tcp.h> 47 #if defined ZMQ_HAVE_OPENVMS 57 int rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELAY, (
char*) &nodelay,
59 #ifdef ZMQ_HAVE_WINDOWS 60 wsa_assert (rc != SOCKET_ERROR);
65 #ifdef ZMQ_HAVE_OPENVMS 68 rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELACK, (
char*) &nodelack,
76 const int rc = setsockopt (sockfd_, SOL_SOCKET, SO_SNDBUF,
77 (
char*) &bufsize_,
sizeof bufsize_);
78 #ifdef ZMQ_HAVE_WINDOWS 79 wsa_assert (rc != SOCKET_ERROR);
87 const int rc = setsockopt (sockfd_, SOL_SOCKET, SO_RCVBUF,
88 (
char *) &bufsize_,
sizeof bufsize_);
89 #ifdef ZMQ_HAVE_WINDOWS 90 wsa_assert (rc != SOCKET_ERROR);
97 int keepalive_idle_,
int keepalive_intvl_)
110 #ifdef ZMQ_HAVE_WINDOWS 111 if (keepalive_ != -1) {
112 tcp_keepalive keepalive_opts;
113 keepalive_opts.onoff = keepalive_;
114 keepalive_opts.keepalivetime = keepalive_idle_ != -1 ?
115 keepalive_idle_ * 1000 : 7200000;
116 keepalive_opts.keepaliveinterval = keepalive_intvl_ != -1 ?
117 keepalive_intvl_ * 1000 : 1000;
118 DWORD num_bytes_returned;
119 int rc = WSAIoctl(s_, SIO_KEEPALIVE_VALS, &keepalive_opts,
120 sizeof(keepalive_opts), NULL, 0, &num_bytes_returned, NULL, NULL);
121 wsa_assert (rc != SOCKET_ERROR);
124 #ifdef ZMQ_HAVE_SO_KEEPALIVE 125 if (keepalive_ != -1) {
126 int rc = setsockopt (s_, SOL_SOCKET, SO_KEEPALIVE,
127 (
char*) &keepalive_,
sizeof (
int));
130 #ifdef ZMQ_HAVE_TCP_KEEPCNT 131 if (keepalive_cnt_ != -1) {
132 int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPCNT,
133 &keepalive_cnt_,
sizeof (
int));
136 #endif // ZMQ_HAVE_TCP_KEEPCNT 138 #ifdef ZMQ_HAVE_TCP_KEEPIDLE 139 if (keepalive_idle_ != -1) {
140 int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPIDLE,
141 &keepalive_idle_,
sizeof (
int));
144 #else // ZMQ_HAVE_TCP_KEEPIDLE 145 #ifdef ZMQ_HAVE_TCP_KEEPALIVE 146 if (keepalive_idle_ != -1) {
147 int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPALIVE,
148 &keepalive_idle_,
sizeof (
int));
151 #endif // ZMQ_HAVE_TCP_KEEPALIVE 152 #endif // ZMQ_HAVE_TCP_KEEPIDLE 154 #ifdef ZMQ_HAVE_TCP_KEEPINTVL 155 if (keepalive_intvl_ != -1) {
156 int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPINTVL,
157 &keepalive_intvl_,
sizeof (
int));
160 #endif // ZMQ_HAVE_TCP_KEEPINTVL 162 #endif // ZMQ_HAVE_SO_KEEPALIVE 163 #endif // ZMQ_HAVE_WINDOWS 173 #if defined (ZMQ_HAVE_WINDOWS) && defined (TCP_MAXRT) 176 int rc = setsockopt (sockfd_, IPPROTO_TCP, TCP_MAXRT, (
char*) &timeout_,
178 wsa_assert (rc != SOCKET_ERROR);
180 #elif defined (TCP_USER_TIMEOUT) 181 int rc = setsockopt (sockfd_, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout_,
189 #ifdef ZMQ_HAVE_WINDOWS 191 int nbytes = send (s_, (
char*) data_, (
int) size_, 0);
195 const int last_error = WSAGetLastError();
196 if (nbytes == SOCKET_ERROR && last_error == WSAEWOULDBLOCK)
200 if (nbytes == SOCKET_ERROR && (
201 last_error == WSAENETDOWN ||
202 last_error == WSAENETRESET ||
203 last_error == WSAEHOSTUNREACH ||
204 last_error == WSAECONNABORTED ||
205 last_error == WSAETIMEDOUT ||
206 last_error == WSAECONNRESET
213 if (nbytes == SOCKET_ERROR && last_error == WSAENOBUFS)
216 wsa_assert (nbytes != SOCKET_ERROR);
220 ssize_t nbytes = send (s_, data_, size_, 0);
225 if (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK ||
233 && errno != EDESTADDRREQ
239 && errno != EOPNOTSUPP);
243 return static_cast <
int> (nbytes);
250 #ifdef ZMQ_HAVE_WINDOWS 252 const int rc = recv (s_, (
char*) data_, (
int) size_, 0);
256 if (rc == SOCKET_ERROR) {
257 const int last_error = WSAGetLastError();
258 if (last_error == WSAEWOULDBLOCK) {
262 wsa_assert (last_error == WSAENETDOWN ||
263 last_error == WSAENETRESET ||
264 last_error == WSAECONNABORTED ||
265 last_error == WSAETIMEDOUT ||
266 last_error == WSAECONNRESET ||
267 last_error == WSAECONNREFUSED ||
268 last_error == WSAENOTCONN);
269 errno = wsa_error_to_errno (last_error);
273 return rc == SOCKET_ERROR ? -1 : rc;
277 const ssize_t rc = recv (s_, data_, size_, 0);
287 if (errno == EWOULDBLOCK || errno == EINTR)
291 return static_cast <
int> (rc);
void tune_tcp_keepalives(fd_t s_, int keepalive_, int keepalive_cnt_, int keepalive_idle_, int keepalive_intvl_)
void set_tcp_receive_buffer(fd_t sockfd_, int bufsize_)
#define LIBZMQ_UNUSED(object)
void tune_tcp_maxrt(fd_t sockfd_, int timeout_)
void tune_tcp_socket(fd_t s_)
void set_tcp_send_buffer(fd_t sockfd_, int bufsize_)
int tcp_write(fd_t s_, const void *data_, size_t size_)
int tcp_read(fd_t s_, void *data_, size_t size_)