Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
socket.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/net/tcp/socket.cpp
3  *
4  * Lightweight wrapper around BSD socket API.
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2015 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
14 
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
17 #include <netinet/tcp.h>
18 
19 namespace thrill {
20 namespace net {
21 namespace tcp {
22 
23 void Socket::SetKeepAlive(bool activate) {
24  assert(IsValid());
25 
26  int sockoptflag = (activate ? 1 : 0);
27 
28  /* Enable sending of keep-alive messages on connection-oriented sockets. */
29  if (::setsockopt(fd_, SOL_SOCKET, SO_KEEPALIVE,
30  &sockoptflag, sizeof(sockoptflag)) != 0)
31  {
32  LOG << "Cannot set SO_KEEPALIVE on socket fd " << fd_
33  << ": " << strerror(errno);
34  }
35 }
36 
37 void Socket::SetReuseAddr(bool activate) {
38  assert(IsValid());
39 
40  int sockoptflag = (activate ? 1 : 0);
41 
42  /* set SO_REUSEPORT */
43 #ifdef SO_REUSEPORT
44  if (::setsockopt(fd_, SOL_SOCKET, SO_REUSEPORT,
45  &sockoptflag, sizeof(sockoptflag)) != 0)
46  {
47  LOG << "Cannot set SO_REUSEPORT on socket fd " << fd_
48  << ": " << strerror(errno);
49  }
50 #else
51  if (::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR,
52  &sockoptflag, sizeof(sockoptflag)) != 0)
53  {
54  LOG << "Cannot set SO_REUSEADDR on socket fd " << fd_
55  << ": " << strerror(errno);
56  }
57 #endif
58 }
59 
60 void Socket::SetNoDelay(bool activate) {
61  assert(IsValid());
62 
63 #if __linux__ || __FreeBSD__ || __APPLE__
64  int sockoptflag = (activate ? 1 : 0);
65 
66  /* TCP_NODELAY If set, disable the Nagle algorithm. This means that
67  segments are always sent as soon as possible, even if there is only a
68  small amount of data. When not set, data is buffered until there is a
69  sufficient amount to send out, thereby avoiding the frequent sending of
70  small packets, which results in poor utilization of the network. This
71  option cannot be used at the same time as the option TCP_CORK. */
72  if (::setsockopt(fd_, IPPROTO_TCP, TCP_NODELAY,
73  &sockoptflag, sizeof(sockoptflag)) != 0)
74  {
75  LOG << "Cannot set TCP_NODELAY on socket fd " << fd_
76  << ": " << strerror(errno);
77  }
78 #endif
79 }
80 
81 void Socket::SetSndBuf(size_t size) {
82  assert(IsValid());
83 
84 #if __linux__ || __FreeBSD__ || __APPLE__
85 
86  int sockoptflag = static_cast<int>(size);
87 
88  /*
89  * SO_SNDBUF Sets or gets the maximum socket send buffer in bytes. The
90  * kernel doubles this value (to allow space for bookkeeping overhead) when
91  * it is set using setsockopt(2), and this doubled value is returned by
92  * getsockopt(2). The default value is set by the
93  * /proc/sys/net/core/wmem_default file and the maximum allowed value is set
94  * by the /proc/sys/net/core/wmem_max file. The minimum (doubled) value for
95  * this option is 2048.
96  */
97  if (::setsockopt(fd_, SOL_SOCKET, SO_SNDBUF,
98  &sockoptflag, sizeof(sockoptflag)) != 0)
99  {
100  LOG << "Cannot set SO_SNDBUF on socket fd " << fd_
101  << ": " << strerror(errno);
102  }
103 #endif
104 }
105 
106 void Socket::SetRcvBuf(size_t size) {
107  assert(IsValid());
108 
109 #if __linux__ || __FreeBSD__ || __APPLE__
110 
111  int sockoptflag = static_cast<int>(size);
112 
113  /*
114  * SO_RCVBUF Sets or gets the maximum socket receive buffer in bytes. The
115  * kernel doubles this value (to allow space for bookkeeping overhead) when
116  * it is set using setsockopt(2), and this doubled value is returned by
117  * getsockopt(2). The default value is set by the
118  * /proc/sys/net/core/rmem_default file, and the maximum allowed value is
119  * set by the /proc/sys/net/core/rmem_max file. The minimum (doubled) value
120  * for this option is 256.
121  */
122  if (::setsockopt(fd_, SOL_SOCKET, SO_RCVBUF,
123  &sockoptflag, sizeof(sockoptflag)) != 0)
124  {
125  LOG << "Cannot set SO_RCVBUF on socket fd " << fd_
126  << ": " << strerror(errno);
127  }
128 #endif
129 }
130 
131 } // namespace tcp
132 } // namespace net
133 } // namespace thrill
134 
135 /******************************************************************************/
bool IsValid() const
Check whether the contained file descriptor is valid.
Definition: socket.hpp:150
void SetRcvBuf(size_t size)
Set SO_RCVBUF socket option.
Definition: socket.cpp:106
int fd_
the file descriptor of the socket.
Definition: socket.hpp:603
void SetKeepAlive(bool activate=true)
Enable sending of keep-alive messages on connection-oriented sockets.
Definition: socket.cpp:23
void SetReuseAddr(bool activate=true)
Definition: socket.cpp:37
void SetSndBuf(size_t size)
Set SO_SNDBUF socket option.
Definition: socket.cpp:81
void SetNoDelay(bool activate=true)
Definition: socket.cpp:60
#define LOG
Default logging method: output if the local debug variable is true.
Definition: logger.hpp:24
int setsockopt(int level, int optname, const void *optval, socklen_t optlen)
Perform raw setsockopt() operation on socket.
Definition: socket.hpp:564