Skip to main content

ocpp_client/
transport.rs

1#[cfg(feature = "websocket")]
2pub(crate) mod websocket;
3
4use alloc::boxed::Box;
5use alloc::string::String;
6use core::future::Future;
7use core::pin::Pin;
8
9/// A transport-agnostic boxed error, so `TransportSink`/`TransportStream` stay dyn-safe
10/// regardless of what's underneath (WebSocket today, a framed serial link later).
11pub type TransportError = Box<dyn core::error::Error + Send + Sync>;
12
13/// One thing read off a transport: a complete OCPP-J text frame, or a protocol-level
14/// keepalive event. Carrying ping/pong through the abstraction (rather than hiding it
15/// entirely inside the WebSocket adapter) keeps `send_ping`/`on_ping` possible without the
16/// generic client knowing anything WebSocket-specific.
17#[derive(Debug)]
18pub enum TransportEvent {
19    Frame(String),
20    Ping,
21    Pong,
22}
23
24/// The write half of a transport: sends one complete OCPP-J text frame at a time.
25///
26/// Implementations own only framing (e.g. WebSocket masking) - `Client` never sees
27/// anything but whole frames and keepalive events.
28///
29/// Methods return a boxed future (the shape `#[async_trait]` expands to, written by hand)
30/// rather than using `async fn` in the trait, so `Box<dyn TransportSink>` stays usable - this
31/// crate has no dependency on the `async-trait` crate itself, only on `alloc`.
32pub trait TransportSink: Send {
33    fn send<'a>(
34        &'a mut self,
35        frame: String,
36    ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'a>>;
37    fn ping<'a>(
38        &'a mut self,
39    ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'a>>;
40    fn pong<'a>(
41        &'a mut self,
42    ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'a>>;
43    fn close<'a>(
44        &'a mut self,
45    ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'a>>;
46}
47
48/// The read half of a transport: yields one [`TransportEvent`] at a time, or `None` when
49/// the other side closed the connection.
50pub trait TransportStream: Send {
51    fn recv<'a>(
52        &'a mut self,
53    ) -> Pin<Box<dyn Future<Output = Result<Option<TransportEvent>, TransportError>> + Send + 'a>>;
54}