aboutsummaryrefslogtreecommitdiff
path: root/src/common/platform/unix/UnixPipe.cpp
blob: 8393f022d90e330499d157b92757e45080a670e5 (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
#include "cru/common/platform/unix/UnixPipe.h"
#include "cru/common/Exception.h"
#include "cru/common/log/Logger.h"

#include <unistd.h>

namespace cru::platform::unix {
UnixPipe::UnixPipe(Usage usage) : usage_(usage) {
  int fds[2];
  if (pipe(fds) != 0) {
    throw ErrnoException(u"Failed to create unix pipe.");
  }
  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