ironsbe_client/session.rs
1//! Client session management.
2//!
3//! Wraps a transport [`Connection`] to provide send/recv for the client.
4
5use bytes::BytesMut;
6use ironsbe_transport::traits::Connection;
7
8/// Client session wrapping a transport [`Connection`].
9///
10/// `C` is the concrete connection type supplied by the active
11/// [`Transport`](ironsbe_transport::Transport) backend.
12pub struct ClientSession<C: Connection> {
13 conn: C,
14}
15
16impl<C: Connection> ClientSession<C> {
17 /// Creates a new client session from a transport connection.
18 #[must_use]
19 pub fn new(conn: C) -> Self {
20 Self { conn }
21 }
22
23 /// Sends a message to the server.
24 ///
25 /// # Errors
26 /// Returns an error if send fails.
27 pub async fn send(&mut self, message: &[u8]) -> std::io::Result<()> {
28 self.conn.send(message).await.map_err(std::io::Error::other)
29 }
30
31 /// Receives a message from the server.
32 ///
33 /// # Returns
34 /// `Ok(Some(bytes))` if received, `Ok(None)` if connection closed.
35 ///
36 /// # Errors
37 /// Returns an error if receive fails.
38 pub async fn recv(&mut self) -> std::io::Result<Option<BytesMut>> {
39 self.conn.recv().await.map_err(std::io::Error::other)
40 }
41}