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