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

ambee/giterated

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

Add message forwarding

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨e4fa992

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