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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::Result;
use crate::plain;
use crate::handler::{
TaskHandle, client::Sender, Stream, StreamSender, Configurator
};
use crate::packet::{Packet, PlainBytes};
use crate::traits::ByteStream;
use crate::pinned_future::PinnedFuture;
#[cfg(feature = "encrypted")]
use crate::{encrypted, packet::EncryptedBytes};
#[cfg(feature = "encrypted")]
use crypto::signature as sign;
use std::time::Duration;
use std::io;
#[derive(Debug, Clone)]
pub struct Config {
pub timeout: Duration,
pub body_limit: usize
}
pub struct ReconStrat<S> {
pub(crate) inner: Box<
dyn FnMut(usize) -> PinnedFuture<'static, io::Result<S>> + Send
>
}
impl<S> ReconStrat<S> {
pub fn new<F: 'static>(f: F) -> Self
where F: FnMut(usize) -> PinnedFuture<'static, io::Result<S>> + Send {
Self {
inner: Box::new(f)
}
}
}
pub struct Connection<P> {
sender: Sender<P>,
task: TaskHandle
}
impl<P> Connection<P> {
pub fn new<S>(
byte_stream: S,
cfg: Config,
recon_strat: Option<ReconStrat<S>>
) -> Self
where
S: ByteStream,
P: Packet<PlainBytes> + Send + 'static,
P::Header: Send
{
plain::client(byte_stream, cfg, recon_strat)
}
#[cfg(feature = "encrypted")]
pub fn new_encrypted<S>(
byte_stream: S,
cfg: Config,
recon_strat: Option<ReconStrat<S>>,
sign: sign::PublicKey
) -> Self
where
S: ByteStream,
P: Packet<EncryptedBytes> + Send + 'static,
P::Header: Send
{
encrypted::client(byte_stream, cfg, recon_strat, sign)
}
pub(crate) fn new_raw(sender: Sender<P>, task: TaskHandle) -> Self {
Self { sender, task }
}
pub fn update_config(&self, cfg: Config) {
self.sender.update_config(cfg);
}
pub fn configurator(&self) -> Configurator<Config> {
self.sender.configurator()
}
pub async fn request(&self, packet: P) -> Result<P> {
self.sender.request(packet).await
}
pub async fn open_stream(&self, packet: P) -> Result<Stream<P>> {
self.sender.open_stream(packet).await
}
pub async fn create_stream(&self, packet: P) -> Result<StreamSender<P>> {
self.sender.create_stream(packet).await
}
pub async fn wait(self) -> Result<()> {
self.task.wait().await
}
pub async fn close(self) -> Result<()> {
self.task.close().await
}
}