1 |
use futures_util::{SinkExt, StreamExt};
|
2 |
use giterated_api::DaemonConnectionPool;
|
3 |
use giterated_models::{
|
4 |
messages::error::ConnectionError,
|
5 |
model::authenticated::{Authenticated, AuthenticatedPayload},
|
6 |
};
|
7 |
use serde::Serialize;
|
8 |
use tokio_tungstenite::tungstenite::Message;
|
9 |
|
10 |
pub async fn wrap_forwarded(pool: &DaemonConnectionPool, message: AuthenticatedPayload) -> Message {
|
11 |
let connection = pool.get().await;
|
12 |
|
13 |
let mut connection = match connection {
|
14 |
Ok(connection) => connection,
|
15 |
Err(e) => {
|
16 |
return Message::Binary(serde_json::to_vec(&ConnectionError(e.to_string())).unwrap())
|
17 |
}
|
18 |
};
|
19 |
|
20 |
let send_result = connection
|
21 |
.send(Message::Binary(serde_json::to_vec(&message).unwrap()))
|
22 |
.await;
|
23 |
|
24 |
if let Err(e) = send_result {
|
25 |
return Message::Binary(serde_json::to_vec(&ConnectionError(e.to_string())).unwrap());
|
26 |
}
|
27 |
|
28 |
loop {
|
29 |
let message = connection.next().await;
|
30 |
|
31 |
match message {
|
32 |
Some(Ok(message)) => {
|
33 |
match message {
|
34 |
Message::Binary(payload) => return Message::Binary(payload),
|
35 |
Message::Ping(_) => {
|
36 |
let _ = connection.send(Message::Pong(vec![])).await;
|
37 |
continue;
|
38 |
}
|
39 |
Message::Close(_) => {
|
40 |
return Message::Binary(
|
41 |
String::from("The instance you wanted to talk to hung up on me :(")
|
42 |
.into_bytes(),
|
43 |
)
|
44 |
}
|
45 |
_ => continue,
|
46 |
};
|
47 |
}
|
48 |
Some(Err(e)) => {
|
49 |
return Message::Binary(
|
50 |
serde_json::to_vec(&ConnectionError(e.to_string())).unwrap(),
|
51 |
)
|
52 |
}
|
53 |
_ => {
|
54 |
info!("Unhandled");
|
55 |
continue;
|
56 |
}
|
57 |
}
|
58 |
}
|
59 |
|
60 |
todo!()
|
61 |
}
|
62 |
|