Apache Mesos
socket.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_SOCKET_HPP__
14 #define __STOUT_OS_SOCKET_HPP__
15 
16 #include <stout/error.hpp>
17 #include <stout/try.hpp>
18 
19 #include <stout/os/int_fd.hpp>
20 
21 #ifdef __WINDOWS__
23 #else
25 #endif // __WINDOWS__
26 
27 namespace net {
28 
29 // Returns a socket file descriptor for the specified options.
30 // NOTE: on OS X, the returned socket will have the SO_NOSIGPIPE option set.
31 inline Try<int_fd> socket(int family, int type, int protocol)
32 {
33  int_fd s;
34  if ((s = ::socket(family, type, protocol)) < 0) {
35  return ErrnoError();
36  }
37 
38 #ifdef __APPLE__
39  // Disable SIGPIPE via setsockopt because OS X does not support
40  // the MSG_NOSIGNAL flag on send(2).
41  const int enable = 1;
42  if (setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int)) == -1) {
43  return ErrnoError();
44  }
45 #endif // __APPLE__
46 
47  return s;
48 }
49 
50 } // namespace net {
51 
52 #endif // __STOUT_OS_SOCKET_HPP__
Try< int_fd > socket(int family, int type, int protocol)
Definition: socket.hpp:31
Try< Nothing > enable(const std::string &hierarchy, const std::string &cgroup)
Definition: try.hpp:34
Definition: errorbase.hpp:49
Try< uint32_t > type(const std::string &path)
int int_fd
Definition: int_fd.hpp:35