http_type/stream/impl.rs
1use crate::*;
2
3impl ArcRwLockStream {
4 /// Creates a new ArcRwLockStream from an Arc<RwLock<TcpStream>>.
5 ///
6 /// # Arguments
7 ///
8 /// - `Arc<RwLock<TcpStream>>` - The stream to wrap.
9 ///
10 /// # Returns
11 ///
12 /// - `ArcRwLockStream` - The new stream wrapper.
13 pub fn from(arc_rw_lock_stream: ArcRwLock<TcpStream>) -> Self {
14 Self(arc_rw_lock_stream)
15 }
16
17 /// Creates a new ArcRwLockStream from a TcpStream.
18 ///
19 /// Wraps the stream in an Arc<RwLock<_>>.
20 ///
21 /// # Arguments
22 ///
23 /// - `TcpStream` - The raw stream to wrap.
24 ///
25 /// # Returns
26 ///
27 /// - `ArcRwLockStream` - The new thread-safe stream wrapper.
28 pub fn from_stream(stream: TcpStream) -> Self {
29 Self(arc_rwlock(stream))
30 }
31
32 /// Gets a read lock on the inner TcpStream.
33 ///
34 /// Allows shared read access to the stream.
35 ///
36 /// # Returns
37 ///
38 /// - `RwLockReadGuardTcpStream` - The read guard for the stream.
39 pub async fn read(&self) -> RwLockReadGuardTcpStream {
40 self.get_0().read().await
41 }
42
43 /// Gets a write lock on the inner TcpStream.
44 ///
45 /// Allows exclusive write access to the stream.
46 ///
47 /// # Returns
48 ///
49 /// - `RwLockWriteGuardTcpStream` - The write guard for the stream.
50 pub(crate) async fn write(&self) -> RwLockWriteGuardTcpStream {
51 self.get_0().write().await
52 }
53
54 /// Sends HTTP response data over the stream.
55 ///
56 /// # Arguments
57 ///
58 /// - `&ResponseData` - The response data to send.
59 ///
60 /// # Returns
61 ///
62 /// - `ResponseResult` - Result indicating success or failure.
63 pub async fn send(&self, data: &ResponseData) -> ResponseResult {
64 self.write()
65 .await
66 .write_all(&data)
67 .await
68 .map_err(|err| ResponseError::Response(err.to_string()))?;
69 Ok(())
70 }
71
72 /// Sends response body with WebSocket framing condition.
73 ///
74 /// Handles both HTTP and WebSocket response formats.
75 ///
76 /// # Arguments
77 ///
78 /// - `&ResponseBody` - The response body data.
79 /// - `bool` - Whether to use WebSocket framing.
80 ///
81 /// # Returns
82 ///
83 /// - `ResponseResult` - Result indicating success or failure.
84 pub async fn send_body_conditional(
85 &self,
86 body: &ResponseBody,
87 is_websocket: bool,
88 ) -> ResponseResult {
89 let body_list: Vec<ResponseBody> = if is_websocket {
90 WebSocketFrame::create_response_frame_list(body)
91 } else {
92 vec![body.clone()]
93 };
94 let mut stream: RwLockWriteGuardTcpStream = self.write().await;
95 for tmp_body in body_list {
96 stream
97 .write_all(&tmp_body)
98 .await
99 .map_err(|err| ResponseError::Response(err.to_string()))?;
100 }
101 Ok(())
102 }
103
104 /// Sends HTTP response body (non-WebSocket).
105 ///
106 /// Convenience wrapper for send_body_conditional with WebSocket disabled.
107 ///
108 /// # Arguments
109 ///
110 /// - `&ResponseBody` - The response body data.
111 ///
112 /// # Returns
113 ///
114 /// - `ResponseResult` - Result indicating success or failure.
115 pub async fn send_body(&self, body: &ResponseBody) -> ResponseResult {
116 self.send_body_conditional(body, false).await
117 }
118
119 /// Sends WebSocket response body.
120 ///
121 /// Convenience wrapper for send_body_conditional with WebSocket enabled.
122 ///
123 /// # Arguments
124 ///
125 /// - `&ResponseBody` - The WebSocket frame data.
126 ///
127 /// # Returns
128 ///
129 /// - `ResponseResult` - Result indicating success or failure.
130 pub async fn send_ws_body(&self, body: &ResponseBody) -> ResponseResult {
131 self.send_body_conditional(body, true).await
132 }
133
134 /// Flushes all buffered data to the stream.
135 ///
136 /// # Returns
137 ///
138 /// - `&Self` - Reference to self for method chaining.
139 pub async fn flush(&self) -> &Self {
140 let _ = self.write().await.flush();
141 self
142 }
143}