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