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
use rusmpp::Command;
use tokio::sync::oneshot;
use crate::error::Error;
#[derive(Debug)]
pub enum Request {
/// Requests for which we are waiting for a response from the server.
///
/// These requests are stored in the connection's pending requests map.
Registered(RegisteredRequest),
/// Requests for which we are `not` waiting for a response from the server.
///
/// These requests are `not` stored in the connection's pending requests map.
Unregistered(UnregisteredRequest),
}
impl Request {
pub fn command(&self) -> &Command {
match self {
Request::Registered(request) => &request.command,
Request::Unregistered(request) => &request.command,
}
}
pub fn send_ack(self, ack: Result<(), Error>) -> Result<(), Result<(), Error>> {
match self {
Request::Registered(request) => request.ack.send(ack),
Request::Unregistered(request) => request.ack.send(ack),
}
}
}
#[derive(Debug)]
pub struct RegisteredRequest {
pub command: Command,
/// ack result means that the command was sent, or could not be sent.
pub ack: oneshot::Sender<Result<(), Error>>,
/// response is a command sent from the server with a sequence number matching this command's sequence number.
///
/// The background connection can only pass commands from the server with a matching sequence number without any validation.
/// It's the client's responsibility to handle error commands.
pub response: oneshot::Sender<Command>,
}
impl RegisteredRequest {
pub fn new(
command: Command,
) -> (
Self,
oneshot::Receiver<Result<(), Error>>,
oneshot::Receiver<Command>,
) {
let (ack, ack_rx) = oneshot::channel();
let (response, response_rx) = oneshot::channel();
(
Self {
command,
ack,
response,
},
ack_rx,
response_rx,
)
}
}
#[derive(Debug)]
pub struct UnregisteredRequest {
pub command: Command,
/// ack result means that the command was sent, or could not be sent.
pub ack: oneshot::Sender<Result<(), Error>>,
}
impl UnregisteredRequest {
pub fn new(command: Command) -> (Self, oneshot::Receiver<Result<(), Error>>) {
let (ack, rx) = oneshot::channel();
(Self { command, ack }, rx)
}
}
#[derive(Debug)]
pub struct CloseRequest {
/// ack result means that the connection started processing the close request.
pub ack: oneshot::Sender<()>,
}
impl CloseRequest {
pub fn new() -> (Self, oneshot::Receiver<()>) {
let (ack, rx) = oneshot::channel();
(Self { ack }, rx)
}
}