http_type/stream/
impl.rs

1use crate::*;
2
3impl ArcRwLockStream {
4    #[inline]
5    pub fn from(arc_rw_lock_stream: ArcRwLock<TcpStream>) -> Self {
6        Self(arc_rw_lock_stream)
7    }
8
9    #[inline]
10    pub fn from_stream(stream: TcpStream) -> Self {
11        Self(arc_rwlock(stream))
12    }
13
14    #[inline]
15    pub async fn get_read_lock(&self) -> RwLockReadGuardTcpStream {
16        self.0.read().await
17    }
18
19    #[inline]
20    pub async fn get_write_lock(&self) -> RwLockWriteGuardTcpStream {
21        self.0.write().await
22    }
23
24    /// Sends the HTTP response over a TCP stream.
25    ///
26    /// # Parameters
27    /// - `data`: Response data
28    ///
29    /// # Returns
30    /// - `Ok`: If the response is successfully sent.
31    /// - `Err`: If an error occurs during sending.
32    #[inline]
33    pub async fn send(&self, data: &ResponseData) -> ResponseResult {
34        let mut stream: RwLockWriteGuardTcpStream = self.get_write_lock().await;
35        stream
36            .write_all(&data)
37            .await
38            .map_err(|err| ResponseError::ResponseError(err.to_string()))?;
39        Ok(())
40    }
41
42    /// Sends the HTTP response body over a TCP stream.
43    ///
44    /// # Parameters
45    /// - `body`: Response body.
46    /// - `is_websocket`: Is websocket
47    ///
48    /// # Returns
49    /// - `Ok`: If the response body is successfully sent.
50    /// - `Err`: If an error occurs during sending.
51    #[inline]
52    pub async fn send_body(&self, body: &ResponseBody, is_websocket: bool) -> ResponseResult {
53        let mut stream: RwLockWriteGuardTcpStream = self.get_write_lock().await;
54        let body_list: Vec<ResponseBody> = if is_websocket {
55            WebSocketFrame::create_response_frame_list(body)
56        } else {
57            vec![body.clone()]
58        };
59        for tmp_body in body_list {
60            stream
61                .write_all(&tmp_body)
62                .await
63                .map_err(|err| ResponseError::ResponseError(err.to_string()))?;
64        }
65        Ok(())
66    }
67
68    /// Flush the TCP stream.    
69    ///
70    /// - Returns: A `ResponseResult` indicating success or failure.
71    #[inline]
72    pub async fn flush(&self) -> ResponseResult {
73        let mut stream: RwLockWriteGuardTcpStream = self.get_write_lock().await;
74        stream
75            .flush()
76            .await
77            .map_err(|err| ResponseError::ResponseError(err.to_string()))?;
78        Ok(())
79    }
80
81    /// Closes the stream after sending the response.
82    ///
83    /// This function is responsible for:
84    /// - Building the response using the `build()` method.
85    /// - Setting the response using the `set_response()` method.
86    /// - Shutting down the write half of the TCP stream to indicate no more data will be sent.
87    ///
88    /// # Returns
89    /// - `ResponseResult`: The result of the operation, indicating whether the closure was successful or if an error occurred.
90    #[inline]
91    pub async fn close(&self) -> ResponseResult {
92        let mut stream: RwLockWriteGuardTcpStream = self.get_write_lock().await;
93        stream
94            .shutdown()
95            .await
96            .map_err(|err| ResponseError::CloseError(err.to_string()))
97    }
98}