1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::Result;
use crate::plain;
use crate::traits::ByteStream;
use crate::handler::{TaskHandle, server::Receiver, Message, Configurator};
use crate::packet::{Packet, PlainBytes};
#[cfg(feature = "encrypted")]
use crate::{encrypted, packet::EncryptedBytes};
#[cfg(feature = "encrypted")]
use crypto::signature as sign;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct Config {
pub timeout: Duration,
pub body_limit: usize
}
pub struct Connection<P> {
receiver: Receiver<P>,
task: TaskHandle
}
impl<P> Connection<P> {
pub fn new<S>(byte_stream: S, cfg: Config) -> Self
where
S: ByteStream,
P: Packet<PlainBytes> + Send + 'static,
P::Header: Send
{
plain::server(byte_stream, cfg)
}
#[cfg(feature = "encrypted")]
pub fn new_encrypted<S>(
byte_stream: S,
cfg: Config,
sign: sign::Keypair
) -> Self
where
S: ByteStream,
P: Packet<EncryptedBytes> + Send + 'static,
P::Header: Send
{
encrypted::server(byte_stream, cfg, sign)
}
pub fn update_config(&self, cfg: Config) {
self.receiver.update_config(cfg);
}
pub fn configurator(&self) -> Configurator<Config> {
self.receiver.configurator()
}
pub(crate) fn new_raw(receiver: Receiver<P>, task: TaskHandle) -> Self {
Self { receiver, task }
}
pub async fn receive(&mut self) -> Option<Message<P>> {
self.receiver.receive().await
}
pub async fn close(self) -> Result<()> {
self.task.close().await
}
pub async fn wait(self) -> Result<()> {
self.task.wait().await
}
#[cfg(test)]
pub(crate) fn abort(self) {
self.task.abort()
}
}