blob: 63bcdadbb09b6f04bfea8d5c48bbf80cd48d03ea (
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#pragma once
#ifndef CRU_PLATFORM_UNIX
#error "This file can only be included on unix."
#endif
#include "../../Base.h"
#include "../../Bitmask.h"
namespace cru::platform::unix {
namespace details {
struct UnixPipeFlagTag;
}
using UnixPipeFlag = Bitmask<details::UnixPipeFlagTag>;
struct UnixPipeFlags {
constexpr static auto NonBlock = UnixPipeFlag::FromOffset(1);
};
/**
* @brief an unix pipe, commonly for communication of parent process and child
* process.
*
* There are two types of pipes sorted by its usage. For stdin, parent process
* SEND data to child process. For stdout and stderr, parent process RECEIVE
* data from child process. Each pipe has two ends, one for read and the other
* for write. But for send and receive, they are reversed. It is a little
* confused to consider which end should be used by parent and which end should
* be used by child. So this class help you make it clear. You specify SEND or
* RECEIVE, and this class give you a parent used end and a child used end.
*
* This class will only close the end used by parent when it is destructed. It
* is the user's duty to close the one used by child.
*/
class UnixPipe : public Object {
private:
constexpr static auto kLogTag = u"cru::platform::unix::UnixPipe";
public:
enum class Usage {
Send,
Receive,
};
explicit UnixPipe(Usage usage, bool auto_close, UnixPipeFlag flags = {});
CRU_DELETE_COPY(UnixPipe)
CRU_DELETE_MOVE(UnixPipe)
~UnixPipe();
/**
* @brief aka, the one used by parent process.
*/
int GetSelfFileDescriptor();
/**
* @brief aka, the one used by child process.
*/
int GetOtherFileDescriptor();
private:
Usage usage_;
bool auto_close_;
UnixPipeFlag flags_;
int read_fd_;
int write_fd_;
};
} // namespace cru::platform::unix
|