| 1 | mod ring_buffer; |
| 2 | |
| 3 | use std::{ |
| 4 | io::{self, Read, Write}, |
| 5 | marker::PhantomData, |
| 6 | os::fd::AsFd, |
| 7 | }; |
| 8 | |
| 9 | use crate::exec::event::{EventHandle, EventRegistry, PollEvent, Process}; |
| 10 | |
| 11 | use self::ring_buffer::RingBuffer; |
| 12 | |
| 13 | // A pipe able to stream data bidirectionally between two read-write types. |
| 14 | pub(super) struct Pipe<L, R> { |
| 15 | left: L, |
| 16 | right: R, |
| 17 | buffer_lr: Buffer<L, R>, |
| 18 | buffer_rl: Buffer<R, L>, |
| 19 | background: bool, |
| 20 | } |
| 21 | |
| 22 | impl<L: Read + Write + AsFd, R: Read + Write + AsFd> Pipe<L, R> { |
| 23 | /// Create a new pipe between two read-write types and register them to be polled. |
| 24 | pub fn new<T: Process>( |
| 25 | left: L, |
| 26 | right: R, |
| 27 | registry: &mut EventRegistry<T>, |
| 28 | f_left: fn(PollEvent) -> T::Event, |
| 29 | f_right: fn(PollEvent) -> T::Event, |
| 30 | ) -> Self { |
| 31 | Self { |
| 32 | buffer_lr: Buffer::new( |
| 33 | registry.register_event(&left, PollEvent::Readable, f_left), |
| 34 | registry.register_event(&right, PollEvent::Writable, f_right), |
| 35 | registry, |
| 36 | ), |
| 37 | buffer_rl: Buffer::new( |
| 38 | registry.register_event(&right, PollEvent::Readable, f_right), |
| 39 | registry.register_event(&left, PollEvent::Writable, f_left), |
| 40 | registry, |
| 41 | ), |
| 42 | left, |
| 43 | right, |
| 44 | background: false, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Get a reference to the left side of the pipe. |
| 49 | pub(super) fn left(&self) -> &L { |
| 50 | &self.left |
| 51 | } |
| 52 | |
| 53 | /// Get a mutable reference to the left side of the pipe. |
| 54 | pub(super) fn left_mut(&mut self) -> &mut L { |
| 55 | &mut self.left |
| 56 | } |
| 57 | |
| 58 | /// Get a reference to the right side of the pipe. |
| 59 | pub(super) fn right(&self) -> &R { |
| 60 | &self.right |
| 61 | } |
| 62 | |
| 63 | /// Stop the poll events of this pipe. |
| 64 | pub(super) fn ignore_events<T: Process>(&mut self, registry: &mut EventRegistry<T>) { |
| 65 | self.buffer_lr.read_handle.ignore(registry); |
| 66 | self.buffer_lr.write_handle.ignore(registry); |
| 67 | self.buffer_rl.read_handle.ignore(registry); |
| 68 | self.buffer_rl.write_handle.ignore(registry); |
| 69 | } |
| 70 | |
| 71 | /// Stop the poll events of the left end of this pipe. |
| 72 | pub(super) fn disable_input<T: Process>(&mut self, registry: &mut EventRegistry<T>) { |
| 73 | self.buffer_lr.read_handle.ignore(registry); |
| 74 | self.background = true; |
| 75 | } |
| 76 | |
| 77 | /// Start/resume the poll events of the left end of this pipe. |
| 78 | pub(super) fn enable_input<T: Process>(&mut self, registry: &mut EventRegistry<T>) { |
| 79 | self.buffer_lr.read_handle.resume(registry); |
| 80 | self.background = false; |
| 81 | } |
| 82 | |
| 83 | /// Resume the poll events of this pipe |
| 84 | pub(super) fn resume_events<T: Process>(&mut self, registry: &mut EventRegistry<T>) { |
| 85 | if !self.background { |
| 86 | self.buffer_lr.read_handle.resume(registry); |
| 87 | } |
| 88 | self.buffer_lr.write_handle.resume(registry); |
| 89 | self.buffer_rl.read_handle.resume(registry); |
| 90 | self.buffer_rl.write_handle.resume(registry); |
| 91 | } |
| 92 | |
| 93 | /// Handle a poll event for the left side of the pipe. |
| 94 | pub(super) fn on_left_event<T: Process>( |
| 95 | &mut self, |
| 96 | poll_event: PollEvent, |
| 97 | registry: &mut EventRegistry<T>, |
| 98 | ) -> io::Result<()> { |
| 99 | match poll_event { |
| 100 | PollEvent::Readable => self.buffer_lr.read(&mut self.left, registry), |
| 101 | PollEvent::Writable => { |
| 102 | if self.buffer_rl.write(&mut self.left, registry)? { |
| 103 | self.buffer_rl.read_handle.resume(registry); |
| 104 | } |
| 105 | Ok(()) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// Handle a poll event for the right side of the pipe. |
| 111 | pub(super) fn on_right_event<T: Process>( |
| 112 | &mut self, |
| 113 | poll_event: PollEvent, |
| 114 | registry: &mut EventRegistry<T>, |
| 115 | ) -> io::Result<()> { |
| 116 | match poll_event { |
| 117 | PollEvent::Readable => self.buffer_rl.read(&mut self.right, registry), |
| 118 | PollEvent::Writable => { |
| 119 | if self.buffer_lr.write(&mut self.right, registry)? && !self.background { |
| 120 | self.buffer_lr.read_handle.resume(registry); |
| 121 | } |
| 122 | |
| 123 | Ok(()) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// Flush the pipe, ensuring that all the contents are written to the left side. |
| 129 | pub(super) fn flush_left(&mut self) -> io::Result<()> { |
| 130 | let buffer = &mut self.buffer_rl; |
| 131 | let source = &mut self.right; |
| 132 | let sink = &mut self.left; |
| 133 | |
| 134 | // Flush the ring buffer, then process any eventual bytes still in-flight. |
| 135 | buffer.internal.remove(sink)?; |
| 136 | |
| 137 | if buffer.write_handle.is_active() { |
| 138 | let mut buf = [0u8; RingBuffer::LEN]; |
| 139 | loop { |
| 140 | match source.read(&mut buf) { |
| 141 | Ok(read_bytes) => sink.write_all(&buf[..read_bytes])?, |
| 142 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => break, |
| 143 | Err(e) => return Err(e), |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | sink.flush() |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// A buffer that stores the bytes read from `R` before they are written to `W`. |
| 153 | struct Buffer<R, W> { |
| 154 | internal: RingBuffer, |
| 155 | /// The handle for the event of the reader. |
| 156 | read_handle: EventHandle, |
| 157 | /// The handle for the event of the writer. |
| 158 | write_handle: EventHandle, |
| 159 | marker: PhantomData<(R, W)>, |
| 160 | } |
| 161 | |
| 162 | impl<R: Read, W: Write> Buffer<R, W> { |
| 163 | /// Create a new, empty buffer |
| 164 | fn new<T: Process>( |
| 165 | read_handle: EventHandle, |
| 166 | mut write_handle: EventHandle, |
| 167 | registry: &mut EventRegistry<T>, |
| 168 | ) -> Self { |
| 169 | // The buffer is empty, don't write |
| 170 | write_handle.ignore(registry); |
| 171 | |
| 172 | Self { |
| 173 | internal: RingBuffer::new(), |
| 174 | read_handle, |
| 175 | write_handle, |
| 176 | marker: PhantomData, |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// Read bytes into the buffer. |
| 181 | /// |
| 182 | /// Calling this function will block until `read` is ready to be read. |
| 183 | fn read<T: Process>( |
| 184 | &mut self, |
| 185 | read: &mut R, |
| 186 | registry: &mut EventRegistry<T>, |
| 187 | ) -> io::Result<()> { |
| 188 | // If the buffer is full, there is nothing to be read. |
| 189 | if self.internal.is_full() { |
| 190 | self.read_handle.ignore(registry); |
| 191 | return Ok(()); |
| 192 | } |
| 193 | |
| 194 | // Read bytes and insert them into the buffer. |
| 195 | let inserted_len = self.internal.insert(read)?; |
| 196 | |
| 197 | // If we inserted something, the buffer is not empty anymore and we can resume writing. |
| 198 | if inserted_len > 0 { |
| 199 | self.write_handle.resume(registry); |
| 200 | } |
| 201 | |
| 202 | Ok(()) |
| 203 | } |
| 204 | |
| 205 | /// Write bytes from the buffer. |
| 206 | /// |
| 207 | /// Calling this function will block until `write` is ready to be written. |
| 208 | fn write<T: Process>( |
| 209 | &mut self, |
| 210 | write: &mut W, |
| 211 | registry: &mut EventRegistry<T>, |
| 212 | ) -> io::Result<bool> { |
| 213 | // If the buffer is empty, there is nothing to be written. |
| 214 | if self.internal.is_empty() { |
| 215 | self.write_handle.ignore(registry); |
| 216 | return Ok(false); |
| 217 | } |
| 218 | |
| 219 | // Remove bytes from the buffer and write them. |
| 220 | let removed_len = self.internal.remove(write)?; |
| 221 | |
| 222 | // Return whether we actually freed up some buffer space |
| 223 | Ok(removed_len > 0) |
| 224 | } |
| 225 | } |