-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathselect.cpp
170 lines (143 loc) · 3.38 KB
/
select.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <marnav-io/device.hpp>
#include <marnav-io/selectable.hpp>
#include <memory>
#include <iostream>
#include <vector>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
namespace marnav_example
{
/// A named pipe, implementing device and selectable for demonstration purposes.
///
/// @note The created FIFO will not be removed.
class pipe : public marnav::io::device, virtual public marnav::io::selectable
{
friend class selector;
public:
pipe(std::string filename)
: filename_(filename)
{
}
~pipe() override { close(); }
void create() { ::mkfifo(filename_.c_str(), 0666); }
void open_read()
{
if (fd >= 0)
return;
fd = ::open(filename_.c_str(), O_RDONLY | O_NONBLOCK);
}
void open_write()
{
if (fd >= 0)
return;
fd = ::open(filename_.c_str(), O_WRONLY | O_NONBLOCK);
}
void open_read_write()
{
if (fd >= 0)
return;
fd = ::open(filename_.c_str(), O_RDWR | O_NONBLOCK);
}
void close() override
{
if (fd < 0)
return;
::close(fd);
fd = -1;
}
int read(char * buffer, uint32_t size) override
{
if (!buffer)
throw std::invalid_argument{"buffer"};
if (fd < 0)
throw std::runtime_error{"pipe not open"};
return ::read(fd, buffer, size);
}
int write(const char * buffer, uint32_t size) override
{
if (!buffer)
throw std::invalid_argument{"buffer"};
if (fd < 0)
throw std::runtime_error{"pipe not open"};
return ::write(fd, buffer, size);
}
protected:
void open() override {}
int get_fd() const override { return fd; }
private:
int fd{-1};
std::string filename_;
};
class selector
{
public:
std::vector<std::shared_ptr<marnav::io::selectable>> operator()(
const std::vector<std::shared_ptr<marnav::io::selectable>> & devices)
{
return select(devices);
}
static std::vector<std::shared_ptr<marnav::io::selectable>> select(
const std::vector<std::shared_ptr<marnav::io::selectable>> & devices)
{
std::vector<std::shared_ptr<marnav::io::selectable>> result;
if (devices.size() == 0)
return result;
// fill filedescriptor set for reading
fd_set rfds;
FD_ZERO(&rfds);
int max_fd = -1;
for (auto const & dev : devices) {
auto fd = dev->get_fd();
FD_SET(fd, &rfds);
if (fd > max_fd)
max_fd = fd;
}
// the actual select
auto rc = ::select(max_fd + 1, &rfds, nullptr, nullptr, nullptr);
if (rc < 0)
throw std::runtime_error{"select"};
// find out which device has caused the wakeup
result.reserve(rc);
for (auto const & dev : devices) {
if (FD_ISSET(dev->get_fd(), &rfds)) {
result.push_back(dev);
}
}
return result;
}
};
}
int main(int, char **)
{
// create a named pipe
auto dev0 = std::make_shared<marnav_example::pipe>("/tmp/foobar");
dev0->create();
dev0->open_read();
// open it a second time as different device
auto dev1 = std::make_shared<marnav_example::pipe>("/tmp/foobar");
dev1->open_read_write();
// write some data
dev1->write("foo\0", 4);
// wait on named pipe, receive and print data
auto devs = marnav_example::selector::select({dev0});
if (devs.size() == 0) {
std::cout << "no devices ready for read, probably timeout\n";
return -1;
}
for (auto & dev : devs) {
char buffer[128];
int rc = -1;
if (dev0 == dev) {
rc = dev0->read(buffer, sizeof(buffer));
}
if (dev1 == dev) {
rc = dev1->read(buffer, sizeof(buffer));
}
if (rc > 0) {
std::cout << "received: " << buffer << "\n";
}
}
return 0;
}