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_POSIX_SOCKET_HPP__
14 #define __STOUT_OS_POSIX_SOCKET_HPP__
15 
16 #include <errno.h>
17 #include <unistd.h>
18 
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 
22 namespace net {
23 
24 // Import `socket` functions into `net::` namespace.
30 
31 // The error indicates the last socket operation has been
32 // interupted, the operation can be restarted immediately.
33 inline bool is_restartable_error(int error)
34 {
35  return (error == EINTR);
36 }
37 
38 
39 // The error indicates the last socket function on a non-blocking socket
40 // cannot be completed. This is a temporary condition and the caller can
41 // retry the operation later.
42 inline bool is_retryable_error(int error)
43 {
44  return (error == EWOULDBLOCK || error == EAGAIN);
45 }
46 
47 
48 inline bool is_inprogress_error(int error)
49 {
50  return (error == EINPROGRESS);
51 }
52 
53 
54 inline bool is_socket(int fd)
55 {
56  struct stat statbuf;
57  if (::fstat(fd, &statbuf) < 0) {
58  return false;
59  }
60 
61  return S_ISSOCK(statbuf.st_mode) != 0;
62 }
63 
64 } // namespace net {
65 
66 #endif // __STOUT_OS_POSIX_SOCKET_HPP__
bool is_socket(int fd)
Definition: socket.hpp:54
bool is_inprogress_error(int error)
Definition: socket.hpp:48
bool S_ISSOCK(const int mode)
Definition: windows.hpp:236
ssize_t send(const os::WindowsFD &fd, const void *buf, size_t len, int flags)
Definition: socket.hpp:136
bool is_restartable_error(int error)
Definition: socket.hpp:33
ssize_t recv(const os::WindowsFD &fd, void *buf, size_t len, int flags)
Definition: socket.hpp:145
Future< Connection > connect(const network::Address &address, Scheme scheme)
Try< int_fd > accept(int_fd s)
Definition: network.hpp:31
Try< hashmap< std::string, uint64_t > > stat(const std::string &hierarchy, const std::string &cgroup, const std::string &file)
std::string error(const std::string &msg, uint32_t code)
Try< Nothing > bind(int_fd s, const Address &address)
Definition: network.hpp:46
bool is_retryable_error(int error)
Definition: socket.hpp:42