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 get_read_lock(&self) -> RwLockReadGuardTcpStream {
34        self.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 async fn get_write_lock(&self) -> RwLockWriteGuardTcpStream {
45        self.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.get_write_lock()
58            .await
59            .write_all(&data)
60            .await
61            .map_err(|err| ResponseError::ResponseError(err.to_string()))?;
62        Ok(())
63    }
64
65    /// Sends the HTTP 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(&self, body: &ResponseBody, is_websocket: bool) -> ResponseResult {
75        let body_list: Vec<ResponseBody> = if is_websocket {
76            WebSocketFrame::create_response_frame_list(body)
77        } else {
78            vec![body.clone()]
79        };
80        let mut stream: RwLockWriteGuardTcpStream = self.get_write_lock().await;
81        for tmp_body in body_list {
82            stream
83                .write_all(&tmp_body)
84                .await
85                .map_err(|err| ResponseError::ResponseError(err.to_string()))?;
86        }
87        Ok(())
88    }
89
90    /// Flush the TCP stream.
91    ///
92    /// - Returns: A `ResponseResult` indicating success or failure.
93    pub async fn flush(&self) -> ResponseResult {
94        self.get_write_lock()
95            .await
96            .flush()
97            .await
98            .map_err(|err| ResponseError::ResponseError(err.to_string()))?;
99        Ok(())
100    }
101
102    /// Closes the stream after sending the response.
103    ///
104    /// This function is responsible for:
105    /// - Building the response using the `build()` method.
106    /// - Setting the response using the `set_response()` method.
107    /// - Shutting down the write half of the TCP stream to indicate no more data will be sent.
108    ///
109    /// # Returns
110    /// - `ResponseResult`: The result of the operation, indicating whether the closure was successful or if an error occurred.
111    pub async fn close(&self) -> ResponseResult {
112        self.get_write_lock()
113            .await
114            .shutdown()
115            .await
116            .map_err(|err| ResponseError::CloseError(err.to_string()))
117    }
118}