http_type/stream/
impl.rs

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