| 1 |
// Copyright © 2021 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details |
| 5 |
|
| 6 |
use std::{collections::VecDeque, io::Result}; |
| 7 |
|
| 8 |
use bytes::BytesMut; |
| 9 |
use mio::{event::Source, Interest, Registry, Token}; |
| 10 |
|
| 11 |
#[cfg(unix)] |
| 12 |
mod unix; |
| 13 |
use crate::messages::RemoteHandle; |
| 14 |
|
| 15 |
#[cfg(unix)] |
| 16 |
pub use self::unix::*; |
| 17 |
|
| 18 |
#[cfg(windows)] |
| 19 |
mod windows; |
| 20 |
#[cfg(windows)] |
| 21 |
pub use self::windows::*; |
| 22 |
|
| 23 |
impl Source for Pipe { |
| 24 |
fn register(&mut self, registry: &Registry, token: Token, interests: Interest) -> Result<()> { |
| 25 |
self.io.register(registry, token, interests) |
| 26 |
} |
| 27 |
|
| 28 |
fn reregister(&mut self, registry: &Registry, token: Token, interests: Interest) -> Result<()> { |
| 29 |
self.io.reregister(registry, token, interests) |
| 30 |
} |
| 31 |
|
| 32 |
fn deregister(&mut self, registry: &Registry) -> Result<()> { |
| 33 |
self.io.deregister(registry) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
const HANDLE_QUEUE_LIMIT: usize = 16; |
| 38 |
|
| 39 |
#[derive(Debug)] |
| 40 |
pub struct ConnectionBuffer { |
| 41 |
pub buf: BytesMut, |
| 42 |
handles: VecDeque<RemoteHandle>, |
| 43 |
} |
| 44 |
|
| 45 |
impl ConnectionBuffer { |
| 46 |
pub fn with_capacity(cap: usize) -> Self { |
| 47 |
ConnectionBuffer { |
| 48 |
buf: BytesMut::with_capacity(cap), |
| 49 |
handles: VecDeque::with_capacity(HANDLE_QUEUE_LIMIT), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
pub fn is_empty(&self) -> bool { |
| 54 |
self.buf.is_empty() |
| 55 |
} |
| 56 |
|
| 57 |
pub fn push_handle(&mut self, handle: RemoteHandle) { |
| 58 |
assert!(self.handles.len() < self.handles.capacity()); |
| 59 |
self.handles.push_back(handle) |
| 60 |
} |
| 61 |
|
| 62 |
pub fn pop_handle(&mut self) -> Option<RemoteHandle> { |
| 63 |
self.handles.pop_front() |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub trait RecvMsg { |
| 68 |
// Receive data from the associated connection. `recv_msg` expects the capacity of |
| 69 |
// the `ConnectionBuffer` members have been adjusted appropriately by the caller. |
| 70 |
fn recv_msg(&mut self, buf: &mut ConnectionBuffer) -> Result<usize>; |
| 71 |
} |
| 72 |
|
| 73 |
pub trait SendMsg { |
| 74 |
// Send data on the associated connection. `send_msg` consumes and adjusts the length of the |
| 75 |
// `ConnectionBuffer` members based on the size of the successful send operation. |
| 76 |
fn send_msg(&mut self, buf: &mut ConnectionBuffer) -> Result<usize>; |
| 77 |
} |