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
198
199
200
201
202
203
204
205
use crossbeam::sync::MsQueue;
use ether_dream::dac;
use ether_dream::protocol::{self, Command, SizeBytes, WriteBytes};
use std::{net, time};
use std::io::{self, Write};
use std::sync::Arc;
use stream::{self, Stream};
pub struct Listener {
state: Option<State>,
tcp_listener: net::TcpListener,
output_frame_rate: u32,
}
type DacQueue = MsQueue<dac::Addressed>;
enum State {
Waiting(dac::Addressed),
Connected(Arc<DacQueue>),
}
pub struct ActiveStream {
inner: Option<(stream::Handle, Arc<DacQueue>)>,
}
impl Listener {
pub fn new(dac: dac::Addressed, output_frame_rate: u32) -> io::Result<Self> {
let addr = net::SocketAddrV4::new([0, 0, 0, 0].into(), protocol::COMMUNICATION_PORT);
let tcp_listener = net::TcpListener::bind(addr)?;
let state = Some(State::Waiting(dac));
Ok(Listener { state, tcp_listener, output_frame_rate })
}
pub fn accept(&mut self) -> io::Result<(ActiveStream, net::SocketAddr)> {
loop {
match self.state.take().expect("listener state was `None`") {
State::Waiting(dac) => {
let (mut tcp_stream, source_addr) = self.tcp_listener.accept()?;
tcp_stream.set_read_timeout(Some(time::Duration::from_secs(1)))?;
tcp_stream.set_write_timeout(Some(time::Duration::from_secs(1)))?;
{
let response = protocol::DacResponse::ACK;
let command = protocol::command::Ping::START_BYTE;
let dac_status = dac.status.to_protocol();
let dac_response = protocol::DacResponse {
response,
command,
dac_status,
};
let mut bytes = [0u8; protocol::DacResponse::SIZE_BYTES];
(&mut bytes[..]).write_bytes(&dac_response)?;
tcp_stream.write(&bytes)?;
}
let stream = Stream::new(dac, tcp_stream, self.output_frame_rate)?.spawn()?;
let dac_queue = Arc::new(MsQueue::new());
let new_state = State::Connected(dac_queue.clone());
self.state = Some(new_state);
let active_stream = ActiveStream { inner: Some((stream, dac_queue)) };
return Ok((active_stream, source_addr));
},
State::Connected(dac_rx) => {
let mut dac = dac_rx.pop();
dac.status = dac::Status::from_protocol(&super::initial_status()).unwrap();
self.state = Some(State::Waiting(dac));
},
}
}
}
}
impl ActiveStream {
pub fn output(&self) -> stream::Output {
let (stream, _) = self.inner.as_ref()
.expect("`output` was called but stream has been closed");
stream.output()
}
pub fn wait(mut self) -> io::Error {
let (stream, dac_queue) = self.inner.take()
.expect("`wait` was called but stream has already been closed");
let (dac, io_err) = stream.wait();
dac_queue.push(dac);
io_err
}
fn close_inner(&mut self) -> io::Error {
let (stream, dac_queue) = self.inner.take()
.expect("`close` was called but stream has already been closed");
let (dac, io_err) = stream.close();
dac_queue.push(dac);
io_err
}
pub fn close(mut self) -> io::Error {
self.close_inner()
}
pub fn set_nodelay(&self, b: bool) -> io::Result<()> {
if let Some((stream_handle, _)) = self.inner.as_ref() {
stream_handle.set_nodelay(b)
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "called `set_nodelay` on a closed stream"))
}
}
pub fn nodelay(&self) -> io::Result<bool> {
if let Some((stream_handle, _)) = self.inner.as_ref() {
stream_handle.nodelay()
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "called `nodelay` on a closed stream"))
}
}
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
if let Some((stream_handle, _)) = self.inner.as_ref() {
stream_handle.set_ttl(ttl)
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "called `set_ttl` on a closed stream"))
}
}
pub fn ttl(&self) -> io::Result<u32> {
if let Some((stream_handle, _)) = self.inner.as_ref() {
stream_handle.ttl()
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "called `ttl` on a closed stream"))
}
}
}
impl Drop for ActiveStream {
fn drop(&mut self) {
self.close_inner();
}
}