blob: 4d081d5ecc1d7577fb45b69e62c7b91366d503ab (
plain)
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
|
#include "cru/common/platform/unix/UnixPipe.h"
#include "cru/common/Exception.h"
#include "cru/common/log/Logger.h"
#include <fcntl.h>
#include <sys/fcntl.h>
#include <unistd.h>
namespace cru::platform::unix {
UnixPipe::UnixPipe(Usage usage, UnixPipeFlag flags)
: usage_(usage), flags_(flags) {
int fds[2];
if (pipe(fds) != 0) {
throw ErrnoException(u"Failed to create unix pipe.");
}
if (flags & UnixPipeFlags::NonBlock) {
fcntl(fds[0], F_SETFL, O_NONBLOCK);
fcntl(fds[1], F_SETFL, O_NONBLOCK);
}
read_fd_ = fds[0];
write_fd_ = fds[1];
}
int UnixPipe::GetSelfFileDescriptor() {
if (usage_ == Usage::Send) {
return write_fd_;
} else {
return read_fd_;
}
}
int UnixPipe::GetOtherFileDescriptor() {
if (usage_ == Usage::Send) {
return read_fd_;
} else {
return write_fd_;
}
}
UnixPipe::~UnixPipe() {
if (close(GetSelfFileDescriptor()) != 0) {
CRU_LOG_ERROR(u"Failed to close unix pipe file descriptor.");
}
}
} // namespace cru::platform::unix
|