http_type/stream/
impl.rs

1use crate::*;
2
3impl ArcRwLockStream {
4    /// Creates a new `ArcRwLockStream` from an `Arc<RwLock<TcpStream>>`.
5    ///
6    /// # Parameters
7    /// - `arc_rw_lock_stream`: An `Arc<RwLock<TcpStream>>` that will be wrapped in the new `ArcRwLockStream`
8    ///
9    /// # Returns
10    /// Returns a new `ArcRwLockStream` instance containing the provided stream
11    pub fn from(arc_rw_lock_stream: ArcRwLock<TcpStream>) -> Self {
12        Self(arc_rw_lock_stream)
13    }
14
15    /// Creates a new `ArcRwLockStream` from a `TcpStream`.
16    ///
17    /// # Parameters
18    /// - `stream`: A `TcpStream` that will be wrapped in the new `ArcRwLockStream`
19    ///
20    /// # Returns
21    /// Returns a new `ArcRwLockStream` instance containing the provided stream wrapped in an `Arc<RwLock<_>>`
22    pub fn from_stream(stream: TcpStream) -> Self {
23        Self(arc_rwlock(stream))
24    }
25
26    /// Returns a reference to the inner `TcpStream`.
27    ///
28    /// This method acquires a read lock on the underlying stream, allowing shared access
29    /// to the TCP stream while preventing concurrent writes.
30    ///
31    /// # Returns
32    /// Returns a read guard that provides shared access to the TCP stream
33    pub async fn read(&self) -> RwLockReadGuardTcpStream {
34        self.get_0().read().await
35    }
36
37    /// Returns a mutable reference to the inner `TcpStream`.
38    ///
39    /// This method acquires a write lock on the underlying stream, allowing exclusive access
40    /// for writing operations while preventing any concurrent access.
41    ///
42    /// # Returns
43    /// Returns a write guard that provides exclusive access to the TCP stream
44    pub(crate) async fn write(&self) -> RwLockWriteGuardTcpStream {
45        self.get_0().write().await
46    }
47
48    /// Sends the HTTP response over a TCP stream.
49    ///
50    /// # Parameters
51    /// - `data`: Response data
52    ///
53    /// # Returns
54    /// - `Ok`: If the response is successfully sent.
55    /// - `Err`: If an error occurs during sending.
56    pub async fn send(&self, data: &ResponseData) -> ResponseResult {
57        self.write()
58            .await
59            .write_all(&data)
60            .await
61            .map_err(|err| ResponseError::Response(err.to_string()))?;
62        Ok(())
63    }
64
65    /// Sends the HTTP or HTTP websocket response body over a TCP stream.
66    ///
67    /// # Parameters
68    /// - `body`: Response body.
69    /// - `is_websocket`: Is websocket
70    ///
71    /// # Returns
72    /// - `Ok`: If the response body is successfully sent.
73    /// - `Err`: If an error occurs during sending.
74    pub async fn send_body_conditional(
75        &self,
76        body: &ResponseBody,
77        is_websocket: bool,
78    ) -> ResponseResult {
79        let body_list: Vec<ResponseBody> = if is_websocket {
80            WebSocketFrame::create_response_frame_list(body)
81        } else {
82            vec![body.clone()]
83        };
84        let mut stream: RwLockWriteGuardTcpStream = self.write().await;
85        for tmp_body in body_list {
86            stream
87                .write_all(&tmp_body)
88                .await
89                .map_err(|err| ResponseError::Response(err.to_string()))?;
90        }
91        Ok(())
92    }
93
94    /// Sends the HTTP response body over a TCP stream.
95    ///
96    /// # Parameters
97    /// - `body`: Response body.
98    ///
99    /// # Returns
100    /// - `Ok`: If the response body is successfully sent.
101    /// - `Err`: If an error occurs during sending.
102    pub async fn send_body(&self, body: &ResponseBody) -> ResponseResult {
103        self.send_body_conditional(body, false).await
104    }
105
106    /// Sends the HTTP ws response body over a TCP stream.
107    ///
108    /// # Parameters
109    /// - `body`: Response body.
110    ///
111    /// # Returns
112    /// - `Ok`: If the response body is successfully sent.
113    /// - `Err`: If an error occurs during sending.
114    pub async fn send_ws_body(&self, body: &ResponseBody) -> ResponseResult {
115        self.send_body_conditional(body, true).await
116    }
117
118    /// Flush the TCP stream.
119    ///
120    /// - Returns: A `ResponseResult` indicating success or failure.
121    pub async fn flush(&self) -> &Self {
122        let _ = self.write().await.flush();
123        self
124    }
125}