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

ambee/giterated

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

Begin new protocol refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨26651b1

⁨giterated-daemon/src/connection.rs⁩ - ⁨2087⁩ bytes
Raw
1 // pub mod authentication;
2 // pub mod forwarded;
3 // pub mod handshake;
4 // pub mod repository;
5 // pub mod user;
6 pub mod wrapper;
7
8 use std::{any::type_name, collections::HashMap};
9
10 use anyhow::Error;
11 use giterated_models::model::instance::{Instance, InstanceMeta};
12 use serde::{de::DeserializeOwned, Serialize};
13 use tokio::{net::TcpStream, task::JoinHandle};
14 use tokio_tungstenite::WebSocketStream;
15
16 #[derive(Debug, thiserror::Error)]
17 pub enum ConnectionError {
18 #[error("connection should close")]
19 Shutdown,
20 #[error("internal error {0}")]
21 InternalError(#[from] Error),
22 }
23
24 pub struct RawConnection {
25 pub task: JoinHandle<()>,
26 }
27
28 pub struct InstanceConnection {
29 pub instance: InstanceMeta,
30 pub task: JoinHandle<()>,
31 }
32
33 /// Represents a connection which hasn't finished the handshake.
34 pub struct UnestablishedConnection {
35 pub socket: WebSocketStream<TcpStream>,
36 }
37
38 #[derive(Default)]
39 pub struct Connections {
40 pub connections: Vec<RawConnection>,
41 pub instance_connections: HashMap<Instance, InstanceConnection>,
42 }
43
44 #[derive(Debug, thiserror::Error)]
45 #[error("handler did not handle")]
46 pub struct HandlerUnhandled;
47
48 pub trait MessageHandling<A, M, R> {
49 fn message_type() -> &'static str;
50 }
51
52 impl<T1, F, M, R> MessageHandling<(T1,), M, R> for F
53 where
54 F: FnOnce(T1) -> R,
55 T1: Serialize + DeserializeOwned,
56 {
57 fn message_type() -> &'static str {
58 type_name::<T1>()
59 }
60 }
61
62 impl<T1, T2, F, M, R> MessageHandling<(T1, T2), M, R> for F
63 where
64 F: FnOnce(T1, T2) -> R,
65 T1: Serialize + DeserializeOwned,
66 {
67 fn message_type() -> &'static str {
68 type_name::<T1>()
69 }
70 }
71
72 impl<T1, T2, T3, F, M, R> MessageHandling<(T1, T2, T3), M, R> for F
73 where
74 F: FnOnce(T1, T2, T3) -> R,
75 T1: Serialize + DeserializeOwned,
76 {
77 fn message_type() -> &'static str {
78 type_name::<T1>()
79 }
80 }
81
82 impl<T1, T2, T3, T4, F, M, R> MessageHandling<(T1, T2, T3, T4), M, R> for F
83 where
84 F: FnOnce(T1, T2, T3, T4) -> R,
85 T1: Serialize + DeserializeOwned,
86 {
87 fn message_type() -> &'static str {
88 type_name::<T1>()
89 }
90 }
91