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 #[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 #[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 #[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 #[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}