JavaScript is disabled, refresh for a better experience. ambee/giterated-api

ambee/giterated-api

Git repository hosting, collaboration, and discovery for the Fediverse.

Fixes schmixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨92ff417

⁨src/handshake.rs⁩ - ⁨3826⁩ bytes
Raw
1 use futures_util::SinkExt;
2
3 use serde::Serialize;
4 use tokio_tungstenite::tungstenite::Message;
5
6 use crate::Socket;
7
8 pub struct GiteratedConnectionHandshaker(Socket);
9
10 impl GiteratedConnectionHandshaker {
11 pub fn new(socket: Socket) -> Self {
12 Self(socket)
13 }
14
15 pub async fn handshake(self) -> Result<Socket, anyhow::Error> {
16 // let mut socket = self.0;
17
18 // // Send handshake initiation
19 // Self::send_raw_message(
20 // &mut socket,
21 // &InitiateHandshake {
22 // version: Version::from_str("0.0.0").unwrap(),
23 // },
24 // )
25 // .await?;
26
27 // while let Some(message) = socket.next().await {
28 // let message = match message {
29 // Ok(message) => message,
30 // Err(err) => {
31 // error!("Error reading message: {:?}", err);
32 // continue;
33 // }
34 // };
35
36 // let payload = match message {
37 // Message::Text(text) => text.into_bytes(),
38 // Message::Binary(bytes) => bytes,
39 // Message::Ping(_) => continue,
40 // Message::Pong(_) => continue,
41 // Message::Close(_) => {
42 // socket.close(None).await?;
43
44 // return Err(SocketClosedError.into());
45 // }
46 // _ => unreachable!(),
47 // };
48
49 // info!("Read payload: {}", std::str::from_utf8(&payload).unwrap());
50
51 // // We try deserializing the finalize response first as it is smaller and just as common
52 // if let Ok(finalize) = serde_json::from_slice::<HandshakeFinalize>(&payload) {
53 // if finalize.success {
54 // info!("Handshake success!");
55
56 // return Ok(socket);
57 // } else {
58 // socket.close(None).await?;
59 // return Err(SocketClosedError.into());
60 // }
61 // } else {
62 // match serde_json::from_slice::<HandshakeResponse>(&payload) {
63 // Ok(_response) => {
64 // // let message = if !validate_version(&response.version) {
65 // // error!(
66 // // "Version compatibility failure! Our Version: {}, Their Version: {}",
67 // // version(),
68 // // response.version
69 // // );
70
71 // // HandshakeFinalize { success: false }
72 // // } else {
73 // // info!("Connected with a compatible version");
74
75 // // HandshakeFinalize { success: true }
76 // // };
77
78 // warn!("Version compatibility has been no-op'd");
79
80 // let message = HandshakeFinalize { success: true };
81 // // Send [`HandshakeFinalize`] to indicate if we're compatible or not
82 // Self::send_raw_message(&mut socket, &message).await?;
83 // }
84 // Err(err) => {
85 // error!("Error deserializing message: {:?}", err);
86 // continue;
87 // }
88 // }
89 // }
90 // }
91
92 Ok(self.0)
93 }
94
95 async fn send_raw_message<T: Serialize>(
96 socket: &mut Socket,
97 message: &T,
98 ) -> Result<(), tokio_tungstenite::tungstenite::Error> {
99 socket
100 .send(Message::Binary(bincode::serialize(message).unwrap()))
101 .await?;
102
103 Ok(())
104 }
105 }
106
107 #[derive(Debug, thiserror::Error)]
108 #[error("closed socket")]
109 struct SocketClosedError;
110