Thrill  0.1
select.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/net/tcp/select.hpp
3  *
4  * Lightweight wrapper around select()
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 
13 #pragma once
14 #ifndef THRILL_NET_TCP_SELECT_HEADER
15 #define THRILL_NET_TCP_SELECT_HEADER
16 
17 #include <sys/select.h>
18 
19 #include <algorithm>
20 #include <cassert>
21 #include <limits>
22 
23 namespace thrill {
24 namespace net {
25 namespace tcp {
26 
27 //! \addtogroup net_tcp TCP Socket API
28 //! \{
29 
30 /*!
31  * Select is an object-oriented wrapper for select(). It takes care of the
32  * bit-fields, etc.
33  */
34 class Select
35 {
36 public:
37  //! constructor
38  Select() {
39  FD_ZERO(&read_set_);
40  FD_ZERO(&write_set_);
41  FD_ZERO(&except_set_);
42  }
43 
44  //! Add a socket to the read and exception selection set
45  Select& SetRead(int fd) {
46  assert(fd >= 0);
47  FD_SET(fd, &read_set_);
48  max_fd_ = std::max(max_fd_, fd);
49  return *this;
50  }
51 
52  //! Add a socket to the write and exception selection set
53  Select& SetWrite(int fd) {
54  assert(fd >= 0);
55  FD_SET(fd, &write_set_);
56  max_fd_ = std::max(max_fd_, fd);
57  return *this;
58  }
59 
60  //! Add a socket to the exception selection set
61  Select& SetException(int fd) {
62  assert(fd >= 0);
63  FD_SET(fd, &except_set_);
64  max_fd_ = std::max(max_fd_, fd);
65  return *this;
66  }
67 
68  //! Check if a file descriptor is in the resulting read set.
69  bool InRead(int fd) const
70  { return FD_ISSET(fd, &read_set_) != 0; }
71 
72  //! Check if a file descriptor is in the resulting Write set.
73  bool InWrite(int fd) const
74  { return FD_ISSET(fd, &write_set_) != 0; }
75 
76  //! Check if a file descriptor is in the resulting exception set.
77  bool InException(int fd) const
78  { return FD_ISSET(fd, &except_set_) != 0; }
79 
80  //! Clear a file descriptor from the read set
81  Select& ClearRead(int fd)
82  { assert(fd >= 0); FD_CLR(fd, &read_set_); return *this; }
83 
84  //! Clear a file descriptor from the write set
85  Select& ClearWrite(int fd)
86  { assert(fd >= 0); FD_CLR(fd, &write_set_); return *this; }
87 
88  //! Clear a file descriptor from the exception set
90  { assert(fd >= 0); FD_CLR(fd, &except_set_); return *this; }
91 
92  //! Clear a file descriptor from all sets
93  Select& Clear(int fd)
94  { return ClearRead(fd).ClearWrite(fd).ClearException(fd); }
95 
96  //! Do a select(), which modifies the enclosed file descriptor objects.
97  int select(struct timeval* timeout = nullptr) {
98  return ::select(max_fd_ + 1,
99  &read_set_, &write_set_, &except_set_, timeout);
100  }
101 
102  //! Do a select() with timeout (in ms)
103  int select_timeout(double timeout) {
104  if (timeout == std::numeric_limits<double>::infinity())
105  return select(nullptr);
106  else {
107  struct timeval tv;
108  tv.tv_sec = static_cast<time_t>(timeout / 1000);
109  tv.tv_usec = static_cast<time_t>(
110  (timeout / 1000.0 - static_cast<double>(tv.tv_sec)) * 1000000);
111  return select(&tv);
112  }
113  }
114 
115 private:
116  //! read bit-field
117  fd_set read_set_;
118 
119  //! write bit-field
120  fd_set write_set_;
121 
122  //! exception bit-field
123  fd_set except_set_;
124 
125  //! maximum file descriptor value in bitsets
126  int max_fd_ = 0;
127 };
128 
129 //! \}
130 
131 } // namespace tcp
132 } // namespace net
133 } // namespace thrill
134 
135 #endif // !THRILL_NET_TCP_SELECT_HEADER
136 
137 /******************************************************************************/
Select & ClearException(int fd)
Clear a file descriptor from the exception set.
Definition: select.hpp:89
bool InException(int fd) const
Check if a file descriptor is in the resulting exception set.
Definition: select.hpp:77
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
bool InWrite(int fd) const
Check if a file descriptor is in the resulting Write set.
Definition: select.hpp:73
Select & ClearWrite(int fd)
Clear a file descriptor from the write set.
Definition: select.hpp:85
Select is an object-oriented wrapper for select().
Definition: select.hpp:34
int max_fd_
maximum file descriptor value in bitsets
Definition: select.hpp:126
int select_timeout(double timeout)
Do a select() with timeout (in ms)
Definition: select.hpp:103
int select(struct timeval *timeout=nullptr)
Do a select(), which modifies the enclosed file descriptor objects.
Definition: select.hpp:97
fd_set except_set_
exception bit-field
Definition: select.hpp:123
fd_set write_set_
write bit-field
Definition: select.hpp:120
Select & SetRead(int fd)
Add a socket to the read and exception selection set.
Definition: select.hpp:45
fd_set read_set_
read bit-field
Definition: select.hpp:117
Select & Clear(int fd)
Clear a file descriptor from all sets.
Definition: select.hpp:93
Select & SetWrite(int fd)
Add a socket to the write and exception selection set.
Definition: select.hpp:53
Select()
constructor
Definition: select.hpp:38
Select & SetException(int fd)
Add a socket to the exception selection set.
Definition: select.hpp:61
Select & ClearRead(int fd)
Clear a file descriptor from the read set.
Definition: select.hpp:81
bool InRead(int fd) const
Check if a file descriptor is in the resulting read set.
Definition: select.hpp:69