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
171
172
|
use std::{ffi::{c_int, c_void}, fmt::Display, error::Error};
use libc::{read, close, pipe2, O_NONBLOCK, EINTR, EAGAIN, EWOULDBLOCK, EPIPE };
use errno::{errno, set_errno, Errno}; //Why isn't this in libc?!?
/// Sends byte data to the corresponding receiver.
pub(crate) struct Sender {
handle : FileHandle,
}
/// Receives byte data from the corresponding sender.
pub(crate) struct Receiver {
handle : FileHandle,
}
impl Receiver {
pub(crate) fn read_byte(&self) -> Result<Option<u8>, ReceiveError> {
set_errno(Errno(0));
let mut buf : u8 = 0;
let status = unsafe {read(self.handle.get_raw(),&mut buf as *mut u8 as *mut c_void, 1)};
let e = errno();
if status > 0 {
Ok(Some(buf))
} else if status == 0 && e == Errno(0) {
Err(ReceiveError::SenderHasHungUp)
} else if e.0 == EINTR {
self.read_byte() //got interrupted by a signal, try again.
} else if e.0 == EAGAIN || e.0 == EWOULDBLOCK {
Ok(None) //nothing to receive
} else {
Err(ReceiveError::UnknownError)
}
}
pub(crate) fn file_handle(&self) -> &FileHandle {
&self.handle
}
}
impl Sender {
pub(crate) fn send_byte(&self, byte : u8) -> Result<(), SendError> {
set_errno(Errno(0));
let status = unsafe {libc::write(self.handle.get_raw(), &byte as *const u8 as *const c_void, 1)};
let e = errno();
if status > 0 {
Ok(())
} else if e.0 == EINTR {
self.send_byte(byte) //interrupted, retry
} else if e.0 == EPIPE {
Err(SendError::ReceiverHasHungUp)
} else if e.0 == EAGAIN {
Err(SendError::ChannelFullWouldBlock)
} else {
Err(SendError::UnknownError)
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SendError{
ReceiverHasHungUp,
ChannelFullWouldBlock,
UnknownError
}
impl Display for SendError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SendError::ReceiverHasHungUp => write!(f, "Write failed, Receiver has closed their end of the pipe."),
SendError::ChannelFullWouldBlock => write!(f, "Write failed, the pipe is clogged."),
SendError::UnknownError => write!(f, "Write failed for unknown reasons. Probably a bug."),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ReceiveError {
SenderHasHungUp,
UnknownError,
}
impl Display for ReceiveError{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReceiveError::SenderHasHungUp => write!(f, "Read failed, Sender has closed their end of the pipe."),
ReceiveError::UnknownError => write!(f, "Read failed for unknown reasons. Probably a bug."),
}
}
}
impl Error for ReceiveError {}
pub(crate) fn create_pipe_chan() -> Result<(Sender, Receiver),()> {
let mut handles : [c_int;2] = [0;2];
let result = unsafe { pipe2(handles.as_mut_ptr(), O_NONBLOCK) };
if result == -1 {
Err(())
} else {
Ok((
Sender{handle : FileHandle{raw : handles[1]}},
Receiver{handle : FileHandle{ raw : handles[0]}}
))
}
}
pub(crate) struct FileHandle {
raw : c_int,
}
impl FileHandle {
pub(crate) fn get_raw(&self) -> c_int {
self.raw
}
}
impl Drop for FileHandle {
fn drop(&mut self) {
unsafe {
close(self.raw);
}
}
}
#[cfg(test)]
mod pipe_chan_test {
use libc::fcntl;
use super::*;
#[test]
fn simple_send_read(){
let (send, recv) = create_pipe_chan().unwrap();
assert_eq!(recv.read_byte(), Ok(None));
send.send_byte(5).unwrap();
send.send_byte(27).unwrap();
assert_eq!(recv.read_byte(), Ok(Some(5)));
assert_eq!(recv.read_byte(), Ok(Some(27)));
assert_eq!(recv.read_byte(), Ok(None));
}
#[test]
fn simple_drop_sender(){
let (send, recv) = create_pipe_chan().unwrap();
assert_eq!(recv.read_byte(), Ok(None));
send.send_byte(5).unwrap();
send.send_byte(27).unwrap();
drop(send);
assert_eq!(recv.read_byte(), Ok(Some(5)));
assert_eq!(recv.read_byte(), Ok(Some(27)));
assert_eq!(recv.read_byte(), Err(ReceiveError::SenderHasHungUp));
}
#[test]
fn simple_drop_receiver(){
let (send, recv) = create_pipe_chan().unwrap();
assert_eq!(recv.read_byte(), Ok(None));
send.send_byte(5).unwrap();
drop(recv);
assert_eq!(send.send_byte(27), Err(SendError::ReceiverHasHungUp));
}
#[test]
fn overfill_sender(){
let (send, recv) = create_pipe_chan().unwrap();
assert_eq!(recv.read_byte(), Ok(None));
let capacity = unsafe {fcntl(send.handle.get_raw(), libc::F_GETPIPE_SZ)};
for _ in 0..capacity {
assert!(send.send_byte(3).is_ok());
}
assert_eq!(send.send_byte(3), Err(SendError::ChannelFullWouldBlock));
}
}
|