1 |
pub mod authentication;
|
2 |
pub mod handshake;
|
3 |
pub mod repository;
|
4 |
pub mod user;
|
5 |
pub mod wrapper;
|
6 |
|
7 |
use std::{any::type_name, collections::HashMap};
|
8 |
|
9 |
use anyhow::Error;
|
10 |
use serde::{de::DeserializeOwned, Serialize};
|
11 |
use tokio::{net::TcpStream, task::JoinHandle};
|
12 |
use tokio_tungstenite::WebSocketStream;
|
13 |
|
14 |
use crate::{
|
15 |
messages::ErrorMessage,
|
16 |
model::instance::{Instance, InstanceMeta},
|
17 |
};
|
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 |
|
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 |
|