Fix handling stack
parent: tbd commit: c53b026
1 | use |
2 | , | SocketAddr
3 | , |
4 | ; |
5 | |
6 | use Error; |
7 | use ; |
8 | |
9 | use ; |
10 | |
11 | use ObjectBackend; |
12 | |
13 | use |
14 | , GiteratedMessage, AnyObject, | AuthenticatedPayload
15 | , | AnyOperation
16 | ; |
17 | use Serialize; |
18 | |
19 | use ; |
20 | use ; |
21 | use Table; |
22 | |
23 | use crate:: |
24 | , | AuthenticationTokenGranter
25 | , |
26 | , | DatabaseBackend
27 | , | InstanceConnections
28 | , | PublicKeyCache
29 | ; |
30 | |
31 | use Connections; |
32 | |
33 | pub async |
34 | socket: , |
35 | connections: , |
36 | repository_backend: , |
37 | user_backend: , |
38 | auth_granter: , |
39 | settings_backend: , |
40 | addr: SocketAddr, |
41 | instance: impl , |
42 | instance_connections: , |
43 | config: Table, |
44 | backend: DatabaseBackend, |
45 | |
46 | let connection_state = ConnectionState |
47 | socket: new, |
48 | connections, |
49 | repository_backend, |
50 | user_backend, |
51 | auth_granter, |
52 | settings_backend, |
53 | addr, |
54 | instance: instance.to_owned, |
55 | handshaked: new, |
56 | key_cache: default, |
57 | instance_connections: instance_connections.clone, |
58 | config, |
59 | ; |
60 | |
61 | let _handshaked = false; |
62 | |
63 | loop |
64 | let mut socket = connection_state.socket.lock .await; |
65 | let message = socket.next .await; |
66 | drop; |
67 | |
68 | match message |
69 | Some => |
70 | let payload = match message |
71 | => payload, | Binary
72 | => | Ping
73 | let mut socket = connection_state.socket.lock .await; |
74 | let _ = socket.send .await; |
75 | drop; |
76 | continue; |
77 | |
78 | => return, | Close
79 | _ => continue, |
80 | ; |
81 | |
82 | let message: AuthenticatedPayload = deserialize .unwrap; |
83 | |
84 | let message: = message.into_message; |
85 | |
86 | let result = backend |
87 | .object_operation |
88 | .await; |
89 | |
90 | // Map result to Vec<u8> on both |
91 | let result = match result |
92 | Ok => Ok, |
93 | Err => Err |
94 | => | Operation
95 | Operation |
96 | |
97 | => Internal, | Internal
98 | => Unhandled, | Unhandled
99 | , |
100 | ; |
101 | |
102 | let mut socket = connection_state.socket.lock .await; |
103 | let _ = socket |
104 | .send |
105 | .await; |
106 | |
107 | drop; |
108 | |
109 | _ => |
110 | return; |
111 | |
112 | |
113 | |
114 | |
115 | // loop { |
116 | // let mut socket = connection_state.socket.lock().await; |
117 | // let message = socket.next().await; |
118 | // drop(socket); |
119 | |
120 | // match message { |
121 | // Some(Ok(message)) => { |
122 | // let payload = match message { |
123 | // Message::Binary(payload) => payload, |
124 | // Message::Ping(_) => { |
125 | // let mut socket = connection_state.socket.lock().await; |
126 | // let _ = socket.send(Message::Pong(vec![])).await; |
127 | // drop(socket); |
128 | // continue; |
129 | // } |
130 | // Message::Close(_) => return, |
131 | // _ => continue, |
132 | // }; |
133 | // info!("one payload"); |
134 | |
135 | // let message = NetworkMessage(payload.clone()); |
136 | |
137 | // if !handshaked { |
138 | // info!("im foo baring"); |
139 | // if handshake_handle(&message, &connection_state).await.is_ok() { |
140 | // if connection_state.handshaked.load(Ordering::SeqCst) { |
141 | // handshaked = true; |
142 | // } |
143 | // } |
144 | // } else { |
145 | // let raw = serde_json::from_slice::<AuthenticatedPayload>(&payload).unwrap(); |
146 | |
147 | // if let Some(target_instance) = &raw.target_instance { |
148 | // if connection_state.instance != *target_instance { |
149 | // // Forward request |
150 | // info!("Forwarding message to {}", target_instance.url); |
151 | // let mut instance_connections = instance_connections.lock().await; |
152 | // let pool = instance_connections.get_or_open(&target_instance).unwrap(); |
153 | // let pool_clone = pool.clone(); |
154 | // drop(pool); |
155 | |
156 | // let result = wrap_forwarded(&pool_clone, raw).await; |
157 | |
158 | // let mut socket = connection_state.socket.lock().await; |
159 | // let _ = socket.send(result).await; |
160 | |
161 | // continue; |
162 | // } |
163 | // } |
164 | |
165 | // let message_type = &raw.message_type; |
166 | |
167 | // info!("Handling message with type: {}", message_type); |
168 | |
169 | // match authentication_handle(message_type, &message, &connection_state).await { |
170 | // Err(e) => { |
171 | // let _ = connection_state |
172 | // .send_raw(ConnectionError(e.to_string())) |
173 | // .await; |
174 | // } |
175 | // Ok(true) => continue, |
176 | // Ok(false) => {} |
177 | // } |
178 | |
179 | // match repository_handle(message_type, &message, &connection_state).await { |
180 | // Err(e) => { |
181 | // let _ = connection_state |
182 | // .send_raw(ConnectionError(e.to_string())) |
183 | // .await; |
184 | // } |
185 | // Ok(true) => continue, |
186 | // Ok(false) => {} |
187 | // } |
188 | |
189 | // match user_handle(message_type, &message, &connection_state).await { |
190 | // Err(e) => { |
191 | // let _ = connection_state |
192 | // .send_raw(ConnectionError(e.to_string())) |
193 | // .await; |
194 | // } |
195 | // Ok(true) => continue, |
196 | // Ok(false) => {} |
197 | // } |
198 | |
199 | // match authentication_handle(message_type, &message, &connection_state).await { |
200 | // Err(e) => { |
201 | // let _ = connection_state |
202 | // .send_raw(ConnectionError(e.to_string())) |
203 | // .await; |
204 | // } |
205 | // Ok(true) => continue, |
206 | // Ok(false) => {} |
207 | // } |
208 | |
209 | // error!( |
210 | // "Message completely unhandled: {}", |
211 | // std::str::from_utf8(&payload).unwrap() |
212 | // ); |
213 | // } |
214 | // } |
215 | // Some(Err(e)) => { |
216 | // error!("Closing connection for {:?} for {}", e, addr); |
217 | // return; |
218 | // } |
219 | // _ => { |
220 | // info!("Unhandled"); |
221 | // continue; |
222 | // } |
223 | // } |
224 | // } |
225 | |
226 | |
227 | |
228 | |
229 | socket: , |
230 | pub connections: , |
231 | pub repository_backend: , |
232 | pub user_backend: , |
233 | pub auth_granter: , |
234 | pub settings_backend: , |
235 | pub addr: SocketAddr, |
236 | pub instance: Instance, |
237 | pub handshaked: , |
238 | pub key_cache: , |
239 | pub instance_connections: , |
240 | pub config: Table, |
241 | |
242 | |
243 | |
244 | pub async |
245 | let payload = to_string?; |
246 | info!; |
247 | self.socket |
248 | .lock |
249 | .await |
250 | .send |
251 | .await?; |
252 | |
253 | Ok |
254 | |
255 | |
256 | pub async |
257 | let payload = to_string?; |
258 | info!; |
259 | self.socket |
260 | .lock |
261 | .await |
262 | .send |
263 | .await?; |
264 | |
265 | Ok |
266 | |
267 | |
268 | pub async |
269 | let mut keys = self.key_cache.lock .await; |
270 | keys.get .await |
271 | |
272 | |
273 |