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
#![forbid(unsafe_code)]
use super::{awaitable_responses::AwaitableResponses, *};
use std::{fmt::Debug, sync::Arc};
use crate::openssh_sftp_protocol::constants::SSH2_FILEXFER_VERSION;
#[derive(Debug)]
struct SharedDataInner<Buffer, Q, Auxiliary> {
queue: Q,
responses: AwaitableResponses<Buffer>,
auxiliary: Auxiliary,
}
#[derive(Debug)]
pub struct SharedData<Buffer, Q, Auxiliary = ()>(Arc<SharedDataInner<Buffer, Q, Auxiliary>>);
impl<Buffer, Q, Auxiliary> Clone for SharedData<Buffer, Q, Auxiliary> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<Buffer: Send + Sync, Q, Auxiliary> SharedData<Buffer, Q, Auxiliary> {
fn new(queue: Q, auxiliary: Auxiliary) -> Self {
SharedData(Arc::new(SharedDataInner {
responses: AwaitableResponses::new(),
queue,
auxiliary,
}))
}
}
impl<Buffer, Q, Auxiliary> SharedData<Buffer, Q, Auxiliary> {
pub fn queue(&self) -> &Q {
&self.0.queue
}
pub(crate) fn responses(&self) -> &AwaitableResponses<Buffer> {
&self.0.responses
}
pub fn get_auxiliary(&self) -> &Auxiliary {
&self.0.auxiliary
}
}
impl<Buffer: Send + Sync, Q, Auxiliary> SharedData<Buffer, Q, Auxiliary> {
#[inline(always)]
pub fn create_response_id(&self) -> Id<Buffer> {
self.responses().insert()
}
#[inline(always)]
pub fn try_reserve_id(&self, new_id_cnt: u32) -> bool {
self.responses().try_reserve(new_id_cnt)
}
#[inline(always)]
pub fn reserve_id(&self, new_id_cnt: u32) {
self.responses().reserve(new_id_cnt);
}
}
pub async fn connect<Buffer, Q, Auxiliary>(
queue: Q,
auxiliary: Auxiliary,
) -> Result<WriteEnd<Buffer, Q, Auxiliary>, Error>
where
Buffer: ToBuffer + Send + Sync + 'static,
Q: Queue,
{
let shared_data = SharedData::new(queue, auxiliary);
let mut write_end = WriteEnd::new(shared_data);
write_end.send_hello(SSH2_FILEXFER_VERSION).await?;
Ok(write_end)
}