tcplane/context/
impl.rs

1use crate::*;
2
3impl InnerContext {
4    pub fn new() -> Self {
5        InnerContext {
6            stream: None,
7            request: Request::new(),
8            response: Response::default(),
9            log: Log::default(),
10            data: HashMap::default(),
11        }
12    }
13}
14
15impl Context {
16    pub(crate) fn from_inner_context(ctx: InnerContext) -> Self {
17        Self(Arc::new(RwLock::new(ctx)))
18    }
19
20    pub async fn get_read_lock(&self) -> RwLockReadContext {
21        self.0.read().await
22    }
23
24    pub async fn get_write_lock(&self) -> RwLockWriteContext {
25        self.0.write().await
26    }
27
28    pub async fn get(&self) -> InnerContext {
29        self.get_read_lock().await.clone()
30    }
31
32    pub async fn get_stream(&self) -> OptionArcRwLockStream {
33        self.get().await.get_stream().clone()
34    }
35
36    pub async fn get_request(&self) -> Request {
37        self.get().await.get_request().clone()
38    }
39
40    pub async fn get_response(&self) -> Response {
41        self.get().await.get_response().clone()
42    }
43
44    pub async fn get_log(&self) -> Log {
45        self.get().await.get_log().clone()
46    }
47
48    pub(super) async fn set_response_data<T: Into<ResponseData>>(&self, data: T) -> &Self {
49        self.get_write_lock()
50            .await
51            .get_mut_response()
52            .set_response_data(data);
53        self
54    }
55
56    pub async fn get_socket_addr(&self) -> OptionSocketAddr {
57        let stream_result: OptionArcRwLockStream = self.get_stream().await;
58        if stream_result.is_none() {
59            return None;
60        }
61        let socket_addr_opt: OptionSocketAddr = stream_result
62            .unwrap()
63            .get_read_lock()
64            .await
65            .peer_addr()
66            .ok();
67        socket_addr_opt
68    }
69
70    pub async fn get_socket_addr_or_default(&self) -> SocketAddr {
71        let stream_result: OptionArcRwLockStream = self.get_stream().await;
72        if stream_result.is_none() {
73            return DEFAULT_SOCKET_ADDR;
74        }
75        let socket_addr: SocketAddr = stream_result
76            .unwrap()
77            .get_read_lock()
78            .await
79            .peer_addr()
80            .unwrap_or(DEFAULT_SOCKET_ADDR);
81        socket_addr
82    }
83
84    pub async fn get_socket_addr_string(&self) -> Option<String> {
85        self.get_socket_addr().await.map(|data| data.to_string())
86    }
87
88    pub async fn get_socket_addr_or_default_string(&self) -> String {
89        self.get_socket_addr_or_default().await.to_string()
90    }
91
92    pub async fn get_socket_host(&self) -> OptionSocketHost {
93        self.get_socket_addr()
94            .await
95            .map(|socket_addr: SocketAddr| socket_addr.ip())
96    }
97
98    pub async fn get_socket_port(&self) -> OptionSocketPort {
99        self.get_socket_addr()
100            .await
101            .map(|socket_addr: SocketAddr| socket_addr.port())
102    }
103
104    pub async fn log_info<T, L>(&self, data: T, func: L) -> &Self
105    where
106        T: ToString,
107        L: LogFuncTrait<T>,
108    {
109        self.get_read_lock().await.get_log().info(data, func);
110        self
111    }
112
113    pub async fn log_debug<T, L>(&self, data: T, func: L) -> &Self
114    where
115        T: ToString,
116        L: LogFuncTrait<T>,
117    {
118        self.get_read_lock().await.get_log().debug(data, func);
119        self
120    }
121
122    pub async fn log_error<T, L>(&self, data: T, func: L) -> &Self
123    where
124        T: ToString,
125        L: LogFuncTrait<T>,
126    {
127        self.get_read_lock().await.get_log().error(data, func);
128        self
129    }
130
131    pub async fn async_log_info<T, L>(&self, data: T, func: L) -> &Self
132    where
133        T: ToString,
134        L: LogFuncTrait<T>,
135    {
136        self.get_read_lock()
137            .await
138            .get_log()
139            .async_info(data, func)
140            .await;
141        self
142    }
143
144    pub async fn async_log_debug<T, L>(&self, data: T, func: L) -> &Self
145    where
146        T: ToString,
147        L: LogFuncTrait<T>,
148    {
149        self.get_read_lock()
150            .await
151            .get_log()
152            .async_debug(data, func)
153            .await;
154        self
155    }
156
157    pub async fn async_log_error<T, L>(&self, data: T, func: L) -> &Self
158    where
159        T: ToString,
160        L: LogFuncTrait<T>,
161    {
162        self.get_read_lock()
163            .await
164            .get_log()
165            .async_error(data, func)
166            .await;
167        self
168    }
169
170    pub async fn send<T: Into<ResponseData>>(&self, data: T) -> ResponseResult {
171        if let Some(stream) = self.get_stream().await {
172            self.set_response_data(data)
173                .await
174                .get_response()
175                .await
176                .send(&stream)
177                .await?;
178            return Ok(());
179        }
180        Err(ResponseError::NotFoundStream)
181    }
182
183    pub async fn close(&self) -> ResponseResult {
184        if let Some(stream) = self.get_stream().await {
185            self.get_response().await.close(&stream).await?;
186            return Ok(());
187        }
188        Err(ResponseError::NotFoundStream)
189    }
190
191    pub async fn flush(&self) -> ResponseResult {
192        if let Some(stream) = self.get_stream().await {
193            self.get_response().await.flush(&stream).await?;
194            return Ok(());
195        }
196        Err(ResponseError::NotFoundStream)
197    }
198
199    pub async fn set_data_value<T: Any + Send + Sync + Clone>(
200        &self,
201        key: &str,
202        value: &T,
203    ) -> &Self {
204        self.get_write_lock()
205            .await
206            .get_mut_data()
207            .insert(key.to_owned(), Arc::new(value.clone()));
208        self
209    }
210
211    pub async fn get_data_value<T: Any + Send + Sync + Clone>(&self, key: &str) -> Option<T> {
212        self.get_read_lock()
213            .await
214            .get_data()
215            .get(key)
216            .and_then(|arc| arc.downcast_ref::<T>())
217            .cloned()
218    }
219
220    pub async fn remove_data_value(&self, key: &str) -> &Self {
221        self.get_write_lock().await.get_mut_data().remove(key);
222        self
223    }
224
225    pub async fn clear_data(&self) -> &Self {
226        self.get_write_lock().await.get_mut_data().clear();
227        self
228    }
229}