11use crate :: * ;
22
3+ /// Implementation of InnerContext methods.
34impl InnerContext {
5+ /// Creates a new InnerContext with default values.
6+ ///
7+ /// # Returns
8+ ///
9+ /// - `InnerContext` - New context instance.
410 pub fn new ( ) -> Self {
511 InnerContext {
612 socket : None ,
@@ -12,39 +18,91 @@ impl InnerContext {
1218 }
1319}
1420
21+ /// Implementation of Context methods.
22+ ///
23+ /// Provides thread-safe operations on the UDP context.
1524impl Context {
25+ /// Creates a Context from an InnerContext.
26+ ///
27+ /// # Arguments
28+ ///
29+ /// - `InnerContext` - The inner context to wrap.
30+ ///
31+ /// # Returns
32+ ///
33+ /// - `Context` - New thread-safe context wrapper.
1634 pub ( crate ) fn from_inner_context ( ctx : InnerContext ) -> Self {
1735 Self ( Arc :: new ( RwLock :: new ( ctx) ) )
1836 }
1937
38+ /// Acquires a read lock on the inner context.
39+ ///
40+ /// # Returns
41+ ///
42+ /// - `RwLockReadContext` - Read guard for the inner context.
2043 pub async fn get_read_lock ( & self ) -> RwLockReadContext {
2144 self . 0 . read ( ) . await
2245 }
2346
47+ /// Acquires a write lock on the inner context.
48+ ///
49+ /// # Returns
50+ ///
51+ /// - `RwLockWriteContext` - Write guard for the inner context.
2452 pub async fn get_write_lock ( & self ) -> RwLockWriteContext {
2553 self . 0 . write ( ) . await
2654 }
2755
56+ /// Gets a clone of the inner context.
57+ ///
58+ /// # Returns
59+ ///
60+ /// - `InnerContext` - Clone of the inner context.
2861 pub async fn get ( & self ) -> InnerContext {
2962 self . get_read_lock ( ) . await . clone ( )
3063 }
3164
65+ /// Gets the request data from the context.
66+ ///
67+ /// # Returns
68+ ///
69+ /// - `Request` - Clone of the request data.
3270 pub async fn get_request ( & self ) -> Request {
3371 self . get ( ) . await . request . clone ( )
3472 }
3573
74+ /// Gets the response data from the context.
75+ ///
76+ /// # Returns
77+ ///
78+ /// - `Response` - Clone of the response data.
3679 pub async fn get_response ( & self ) -> Response {
3780 self . get ( ) . await . response . clone ( )
3881 }
3982
83+ /// Gets the UDP socket from the context.
84+ ///
85+ /// # Returns
86+ ///
87+ /// - `OptionArcRwLockUdpSocket` - Clone of the socket if present.
4088 pub async fn get_socket ( & self ) -> OptionArcRwLockUdpSocket {
4189 self . get ( ) . await . socket . clone ( )
4290 }
4391
92+ /// Gets the socket address from the context.
93+ ///
94+ /// # Returns
95+ ///
96+ /// - `OptionSocketAddr` - Clone of the socket address if present.
4497 pub async fn get_socket_addr ( & self ) -> OptionSocketAddr {
4598 self . get ( ) . await . socket_addr . clone ( )
4699 }
47100
101+ /// Gets the socket address or default if not present.
102+ ///
103+ /// # Returns
104+ ///
105+ /// - `SocketAddr` - Socket address or default (0.0.0.0:0).
48106 pub async fn get_socket_addr_or_default ( & self ) -> SocketAddr {
49107 let socket_result: OptionArcRwLockUdpSocket = self . get_socket ( ) . await ;
50108 if socket_result. is_none ( ) {
@@ -59,31 +117,69 @@ impl Context {
59117 socket_addr
60118 }
61119
120+ /// Gets the socket address as a string.
121+ ///
122+ /// # Returns
123+ ///
124+ /// - `Option<String>` - Socket address string if present.
62125 pub async fn get_socket_addr_string ( & self ) -> Option < String > {
63126 self . get_socket_addr ( ) . await . map ( |data| data. to_string ( ) )
64127 }
65128
129+ /// Gets the socket address or default as a string.
130+ ///
131+ /// # Returns
132+ ///
133+ /// - `String` - Socket address string or default.
66134 pub async fn get_socket_addr_or_default_string ( & self ) -> String {
67135 self . get_socket_addr_or_default ( ) . await . to_string ( )
68136 }
69137
138+ /// Gets the socket host IP address.
139+ ///
140+ /// # Returns
141+ ///
142+ /// - `OptionSocketHost` - Host IP address if present.
70143 pub async fn get_socket_host ( & self ) -> OptionSocketHost {
71144 self . get_socket_addr ( )
72145 . await
73146 . map ( |socket_addr : SocketAddr | socket_addr. ip ( ) )
74147 }
75148
149+ /// Gets the socket port number.
150+ ///
151+ /// # Returns
152+ ///
153+ /// - `OptionSocketPort` - Port number if present.
76154 pub async fn get_socket_port ( & self ) -> OptionSocketPort {
77155 self . get_socket_addr ( )
78156 . await
79157 . map ( |socket_addr : SocketAddr | socket_addr. port ( ) )
80158 }
81159
160+ /// Sets the response data in the context.
161+ ///
162+ /// # Arguments
163+ ///
164+ /// - `T` - Data convertible to ResponseData.
165+ ///
166+ /// # Returns
167+ ///
168+ /// - `&Self` - Reference to self for chaining.
82169 pub ( super ) async fn set_response < T : Into < ResponseData > > ( & self , data : T ) -> & Self {
83170 self . get_write_lock ( ) . await . response = Response :: from ( data) ;
84171 self
85172 }
86173
174+ /// Sends response data through the socket.
175+ ///
176+ /// # Arguments
177+ ///
178+ /// - `T` - Data convertible to ResponseData.
179+ ///
180+ /// # Returns
181+ ///
182+ /// - `ResponseResult` - Result of the send operation.
87183 pub async fn send < T : Into < ResponseData > > ( & self , data : T ) -> ResponseResult {
88184 let response_result: ResponseResult = self
89185 . set_response ( data)
@@ -95,6 +191,16 @@ impl Context {
95191 return response_result;
96192 }
97193
194+ /// Sets a value in the context data storage.
195+ ///
196+ /// # Arguments
197+ ///
198+ /// - `&str` - Key for the value.
199+ /// - `&T` - Value to store (must be Any + Send + Sync + Clone).
200+ ///
201+ /// # Returns
202+ ///
203+ /// - `&Self` - Reference to self for chaining.
98204 pub async fn set_data_value < T : Any + Send + Sync + Clone > (
99205 & self ,
100206 key : & str ,
@@ -107,6 +213,15 @@ impl Context {
107213 self
108214 }
109215
216+ /// Gets a value from the context data storage.
217+ ///
218+ /// # Arguments
219+ ///
220+ /// - `&str` - Key for the value.
221+ ///
222+ /// # Returns
223+ ///
224+ /// - `Option<T>` - Retrieved value if present and of correct type.
110225 pub async fn get_data_value < T : Any + Send + Sync + Clone > ( & self , key : & str ) -> Option < T > {
111226 self . get_read_lock ( )
112227 . await
@@ -116,11 +231,25 @@ impl Context {
116231 . cloned ( )
117232 }
118233
234+ /// Removes a value from the context data storage.
235+ ///
236+ /// # Arguments
237+ ///
238+ /// - `&str` - Key for the value to remove.
239+ ///
240+ /// # Returns
241+ ///
242+ /// - `&Self` - Reference to self for chaining.
119243 pub async fn remove_data_value ( & self , key : & str ) -> & Self {
120244 self . get_write_lock ( ) . await . data . remove ( key) ;
121245 self
122246 }
123247
248+ /// Clears all data from the context data storage.
249+ ///
250+ /// # Returns
251+ ///
252+ /// - `&Self` - Reference to self for chaining.
124253 pub async fn clear_data ( & self ) -> & Self {
125254 self . get_write_lock ( ) . await . data . clear ( ) ;
126255 self
0 commit comments