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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::net::SocketAddr;
use futures::sync::mpsc;
use futures::sync::oneshot;
use futures::{self, Future, Stream};
use error::{SctpError, SctpResult};
use packet::{SSN, TSN};
use stack::association::{AcceptQueueReceiver, AssociationCommand, AssociationCommandSender};
use stack::SctpCommand;
use stack::Timeout;
use UserMessage;
#[derive(Clone, Debug)]
pub struct SctpHandle {
command_tx: mpsc::Sender<SctpCommand>,
}
impl SctpHandle {
pub fn new(tx: mpsc::Sender<SctpCommand>) -> SctpHandle {
SctpHandle { command_tx: tx }
}
fn send_cmd<T>(&mut self, cmd: SctpCommand, return_rx: oneshot::Receiver<T>) -> SctpResult<T> {
match self.command_tx.try_send(cmd) {
Ok(_) => {}
Err(ref e) if e.is_full() => return Err(SctpError::CommandQueueFull),
Err(ref e) if e.is_disconnected() => return Err(SctpError::Closed),
Err(_) => return Err(SctpError::BadState),
};
match return_rx.wait() {
Ok(item) => Ok(item),
Err(_) => Err(SctpError::Closed),
}
}
pub fn connect(
&mut self,
destination: SocketAddr,
timeout: Timeout,
) -> SctpResult<AssociationHandle> {
let (return_tx, return_rx) =
oneshot::channel::<SctpResult<mpsc::Sender<AssociationCommand>>>();
let association_command_tx = self.send_cmd(
SctpCommand::Connect(destination, timeout, return_tx),
return_rx,
)??;
Ok(AssociationHandle::new(association_command_tx, None))
}
pub fn listen(&mut self, port: u16) -> SctpResult<AssociationHandle> {
let (return_tx, return_rx) =
oneshot::channel::<(AssociationCommandSender, AcceptQueueReceiver)>();
let (association_command_tx, accept_queue_rx) =
self.send_cmd(SctpCommand::Listen(port, return_tx), return_rx)?;
Ok(AssociationHandle::new(
association_command_tx,
Some(accept_queue_rx),
))
}
pub fn exit(&mut self) -> SctpResult<()> {
let (return_tx, return_rx) = oneshot::channel::<()>();
self.send_cmd(SctpCommand::Exit(return_tx), return_rx)
}
}
#[derive(Debug)]
pub struct AssociationHandle {
command_tx: mpsc::Sender<AssociationCommand>,
accept_queue: Option<futures::stream::Wait<AcceptQueueReceiver>>,
closed: bool,
}
impl AssociationHandle {
fn new(
command_tx: mpsc::Sender<AssociationCommand>,
accept_queue_rx: Option<AcceptQueueReceiver>,
) -> AssociationHandle {
AssociationHandle {
command_tx,
accept_queue: accept_queue_rx.map(|rx| rx.wait()),
closed: false,
}
}
fn send_cmd<T>(
&mut self,
cmd: AssociationCommand,
return_rx: oneshot::Receiver<T>,
) -> SctpResult<T> {
match self.command_tx.try_send(cmd) {
Ok(_) => {}
Err(ref e) if e.is_full() => return Err(SctpError::CommandQueueFull),
Err(ref e) if e.is_disconnected() => return Err(SctpError::Closed),
Err(_) => return Err(SctpError::BadState),
};
match return_rx.wait() {
Ok(item) => Ok(item),
Err(_) => Err(SctpError::Closed),
}
}
pub fn command(&self) -> mpsc::Sender<AssociationCommand> {
self.command_tx.clone()
}
pub fn accept(&mut self) -> AssociationHandle {
match self.accept_queue {
Some(ref mut q) => {
match q.next() {
Some(Ok(new_command_tx)) => AssociationHandle::new(new_command_tx, None),
Some(Err(_)) => unreachable!(),
None => unreachable!(),
}
}
None => unreachable!(),
}
}
pub fn send(&mut self, message: UserMessage) -> SctpResult<()> {
let (return_tx, return_rx) = oneshot::channel::<SctpResult<()>>();
let result = self.send_cmd(AssociationCommand::Send(message, return_tx), return_rx)?;
result
}
pub fn send_bytes(&mut self, buffer: Vec<u8>) -> SctpResult<()> {
let message = UserMessage {
tsn: TSN::new(0),
unordered: false,
stream_id: 0,
ssn: SSN::new(0),
payload_protocol_id: 0,
buffer: buffer,
};
self.send(message)
}
pub fn recv(&mut self) -> SctpResult<Option<UserMessage>> {
let (return_tx, return_rx) = oneshot::channel::<SctpResult<Option<UserMessage>>>();
match self.send_cmd(AssociationCommand::Recv(return_tx), return_rx) {
Ok(Ok(m)) => Ok(m),
Ok(Err(e)) => Err(e),
Err(SctpError::Closed) => Ok(None),
Err(e) => Err(e),
}
}
pub fn recv_wait(&mut self) -> SctpResult<()> {
loop {
match self.recv() {
Ok(Some(_)) => {}
Ok(None) => break,
Err(e) => return Err(e),
}
}
Ok(())
}
pub fn abort(&mut self) -> SctpResult<()> {
let (return_tx, return_rx) = oneshot::channel::<()>();
self.send_cmd(AssociationCommand::Abort(return_tx), return_rx)
}
pub fn shutdown(&mut self) -> SctpResult<()> {
let (return_tx, return_rx) = oneshot::channel::<SctpResult<()>>();
let result = self.send_cmd(AssociationCommand::Shutdown(return_tx), return_rx)?;
result
}
pub fn set_recv_timeout(&mut self, timeout: Timeout) -> SctpResult<()> {
let (return_tx, return_rx) = oneshot::channel::<()>();
self.send_cmd(
AssociationCommand::SetRecvTimeout(timeout, return_tx),
return_rx,
)
}
pub fn set_send_timeout(&mut self, timeout: Timeout) -> SctpResult<()> {
let (return_tx, return_rx) = oneshot::channel::<()>();
self.send_cmd(
AssociationCommand::SetSendTimeout(timeout, return_tx),
return_rx,
)
}
}
impl Drop for AssociationHandle {
fn drop(&mut self) {
self.abort().unwrap_or(());
}
}