~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to src/tcp.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-10-16 19:49:30 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20121016194930-98r0bi746eoaa4iv
Tags: 3.2.1~rc2+dfsg-1
* New upstream RC release (Closes: #690704)
* Bump Standards-Version to 3.9.4 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2010-2011 250bpm s.r.o.
 
3
    Copyright (c) 2007-2009 iMatix Corporation
 
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
 
5
 
 
6
    This file is part of 0MQ.
 
7
 
 
8
    0MQ is free software; you can redistribute it and/or modify it under
 
9
    the terms of the GNU Lesser General Public License as published by
 
10
    the Free Software Foundation; either version 3 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    0MQ is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU Lesser General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Lesser General Public License
 
19
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
#include "ip.hpp"
 
23
#include "tcp.hpp"
 
24
#include "err.hpp"
 
25
#include "platform.hpp"
 
26
 
 
27
#if defined ZMQ_HAVE_WINDOWS
 
28
#include "windows.hpp"
 
29
#else
 
30
#include <fcntl.h>
 
31
#include <sys/types.h>
 
32
#include <sys/socket.h>
 
33
#include <netinet/in.h>
 
34
#include <netinet/tcp.h>
 
35
#endif
 
36
 
 
37
#if defined ZMQ_HAVE_OPENVMS
 
38
#include <ioctl.h>
 
39
#endif
 
40
 
 
41
void zmq::tune_tcp_socket (fd_t s_)
 
42
{
 
43
    //  Disable Nagle's algorithm. We are doing data batching on 0MQ level,
 
44
    //  so using Nagle wouldn't improve throughput in anyway, but it would
 
45
    //  hurt latency.
 
46
    int nodelay = 1;
 
47
    int rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELAY, (char*) &nodelay,
 
48
        sizeof (int));
 
49
#ifdef ZMQ_HAVE_WINDOWS
 
50
    wsa_assert (rc != SOCKET_ERROR);
 
51
#else
 
52
    errno_assert (rc == 0);
 
53
#endif
 
54
 
 
55
#ifdef ZMQ_HAVE_OPENVMS
 
56
    //  Disable delayed acknowledgements as they hurt latency is serious manner.
 
57
    int nodelack = 1;
 
58
    rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELACK, (char*) &nodelack,
 
59
        sizeof (int));
 
60
    errno_assert (rc != SOCKET_ERROR);
 
61
#endif
 
62
}
 
63
 
 
64
void zmq::tune_tcp_keepalives (fd_t s_, int keepalive_, int keepalive_cnt_, int keepalive_idle_, int keepalive_intvl_)
 
65
{
 
66
    // These options are used only under certain #ifdefs below.
 
67
    (void)keepalive_;
 
68
    (void)keepalive_cnt_;
 
69
    (void)keepalive_idle_;
 
70
    (void)keepalive_intvl_;
 
71
 
 
72
    // If none of the #ifdefs apply, then s_ is unused.
 
73
    (void)s_;
 
74
 
 
75
    //  Tuning TCP keep-alives if platform allows it
 
76
    //  All values = -1 means skip and leave it for OS
 
77
#ifdef ZMQ_HAVE_SO_KEEPALIVE
 
78
    if (keepalive_ != -1) {
 
79
        int rc = setsockopt (s_, SOL_SOCKET, SO_KEEPALIVE, (char*) &keepalive_, sizeof (int));
 
80
#ifdef ZMQ_HAVE_WINDOWS
 
81
        wsa_assert (rc != SOCKET_ERROR);
 
82
#else
 
83
        errno_assert (rc == 0);
 
84
#endif
 
85
 
 
86
#ifdef ZMQ_HAVE_TCP_KEEPCNT
 
87
        if (keepalive_cnt_ != -1) {
 
88
            int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPCNT, &keepalive_cnt_, sizeof (int));
 
89
#ifdef ZMQ_HAVE_WINDOWS
 
90
            wsa_assert (rc != SOCKET_ERROR);
 
91
#else
 
92
            errno_assert (rc == 0);
 
93
#endif
 
94
        }
 
95
#endif // ZMQ_HAVE_TCP_KEEPCNT
 
96
 
 
97
#ifdef ZMQ_HAVE_TCP_KEEPIDLE
 
98
        if (keepalive_idle_ != -1) {
 
99
            int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPIDLE, &keepalive_idle_, sizeof (int));
 
100
#ifdef ZMQ_HAVE_WINDOWS
 
101
            wsa_assert (rc != SOCKET_ERROR);
 
102
#else
 
103
            errno_assert (rc == 0);
 
104
#endif
 
105
        }
 
106
#else // ZMQ_HAVE_TCP_KEEPIDLE
 
107
#ifdef ZMQ_HAVE_TCP_KEEPALIVE
 
108
        if (keepalive_idle_ != -1) {
 
109
            int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPALIVE, &keepalive_idle_, sizeof (int));
 
110
#ifdef ZMQ_HAVE_WINDOWS
 
111
            wsa_assert (rc != SOCKET_ERROR);
 
112
#else
 
113
            errno_assert (rc == 0);
 
114
#endif
 
115
        }
 
116
#endif // ZMQ_HAVE_TCP_KEEPALIVE
 
117
#endif // ZMQ_HAVE_TCP_KEEPIDLE
 
118
 
 
119
#ifdef ZMQ_HAVE_TCP_KEEPINTVL
 
120
        if (keepalive_intvl_ != -1) {
 
121
            int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPINTVL, &keepalive_intvl_, sizeof (int));
 
122
#ifdef ZMQ_HAVE_WINDOWS
 
123
            wsa_assert (rc != SOCKET_ERROR);
 
124
#else
 
125
            errno_assert (rc == 0);
 
126
#endif
 
127
        }
 
128
#endif // ZMQ_HAVE_TCP_KEEPINTVL
 
129
    }
 
130
#endif // ZMQ_HAVE_SO_KEEPALIVE
 
131
}