| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
use std::path::{Path, PathBuf}; |
| 12 |
|
| 13 |
use crate::event::ServerEvent; |
| 14 |
use crate::request::ClientRequest; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#[must_use] |
| 19 |
pub fn default_socket_path() -> PathBuf { |
| 20 |
std::env::var_os("EVERYCYCLE_SOCKET").map_or_else( |
| 21 |
|| Path::new("/run/everycycle/api.sock").to_path_buf(), |
| 22 |
PathBuf::from, |
| 23 |
) |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
pub const MAX_FRAME_BYTES: u32 = 16 * 1024 * 1024; |
| 31 |
|
| 32 |
|
| 33 |
pub const FRAME_LENGTH_BYTES: usize = 4; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
pub trait ClientTransport: Send + Sync { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
fn submit(&self, request: ClientRequest) -> Result<EventStream, TransportError>; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
pub struct EventStream(pub Box<dyn Iterator<Item = Result<ServerEvent, TransportError>> + Send>); |
| 58 |
|
| 59 |
impl Iterator for EventStream { |
| 60 |
type Item = Result<ServerEvent, TransportError>; |
| 61 |
|
| 62 |
fn next(&mut self) -> Option<Self::Item> { |
| 63 |
self.0.next() |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[derive(Debug)] |
| 70 |
pub enum TransportError { |
| 71 |
|
| 72 |
Connect(std::io::Error), |
| 73 |
|
| 74 |
Io(std::io::Error), |
| 75 |
|
| 76 |
FrameTooLarge { size: u32 }, |
| 77 |
|
| 78 |
|
| 79 |
Codec(String), |
| 80 |
} |
| 81 |
|
| 82 |
impl core::fmt::Display for TransportError { |
| 83 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 84 |
match self { |
| 85 |
Self::Connect(e) => write!(f, "connect failed: {e}"), |
| 86 |
Self::Io(e) => write!(f, "transport io: {e}"), |
| 87 |
Self::FrameTooLarge { size } => { |
| 88 |
write!(f, "frame size {size} exceeds {MAX_FRAME_BYTES}") |
| 89 |
} |
| 90 |
Self::Codec(s) => write!(f, "codec error: {s}"), |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl std::error::Error for TransportError {} |
| 96 |
|