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
use async_std::net::{ToSocketAddrs, UdpSocket};
use async_std::stream::Stream;
use futures_lite::ready;
use rosc::OscPacket;
use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use crate::error::Error;
use crate::prelude::IntoOscPacket;
use crate::udp::UdpSocketStream;
#[derive(Debug)]
pub struct OscSocket {
socket: UdpSocketStream,
}
impl OscSocket {
pub fn new(socket: UdpSocket) -> Self {
let socket = UdpSocketStream::new(socket);
Self { socket }
}
pub async fn bind<A: ToSocketAddrs>(addr: A) -> Result<Self, Error> {
let socket = UdpSocket::bind(addr).await?;
Ok(Self::new(socket))
}
pub async fn connect<A: ToSocketAddrs>(&self, addrs: A) -> Result<(), Error> {
self.socket().connect(addrs).await?;
Ok(())
}
pub async fn send_to<A: ToSocketAddrs, P: IntoOscPacket>(
&self,
packet: P,
addrs: A,
) -> Result<(), Error> {
let buf = rosc::encoder::encode(&packet.into_osc_packet())?;
let n = self.socket().send_to(&buf[..], addrs).await?;
check_len(&buf[..], n)
}
pub async fn send<P: IntoOscPacket>(&self, packet: P) -> Result<(), Error> {
let buf = rosc::encoder::encode(&packet.into_osc_packet())?;
let n = self.socket().send(&buf[..]).await?;
check_len(&buf[..], n)
}
pub fn sender(&self) -> OscSender {
OscSender::new(self.socket.clone_inner())
}
pub fn socket(&self) -> &UdpSocket {
self.socket.get_ref()
}
pub fn local_addr(&self) -> Result<SocketAddr, Error> {
let addr = self.socket().local_addr()?;
Ok(addr)
}
}
impl Stream for OscSocket {
type Item = Result<(OscPacket, SocketAddr), Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let packet = ready!(Pin::new(&mut self.socket).poll_next(cx));
let message = match packet {
None => None,
Some(packet) => Some(match packet {
Err(err) => Err(err.into()),
Ok((buf, peer_addr)) => rosc::decoder::decode(&buf[..])
.map_err(|e| e.into())
.map(|p| (p, peer_addr)),
}),
};
Poll::Ready(message)
}
}
#[derive(Clone, Debug)]
pub struct OscSender {
socket: Arc<UdpSocket>,
}
impl OscSender {
fn new(socket: Arc<UdpSocket>) -> Self {
Self { socket }
}
pub async fn send_to<A: ToSocketAddrs, P: IntoOscPacket>(
&self,
packet: P,
addrs: A,
) -> Result<(), Error> {
let buf = rosc::encoder::encode(&packet.into_osc_packet())?;
let n = self.socket().send_to(&buf[..], addrs).await?;
check_len(&buf[..], n)
}
pub async fn send<P: IntoOscPacket>(&self, packet: P) -> Result<(), Error> {
let buf = rosc::encoder::encode(&packet.into_osc_packet())?;
let n = self.socket().send(&buf[..]).await?;
check_len(&buf[..], n)
}
pub fn socket(&self) -> &UdpSocket {
&*self.socket
}
}
fn check_len(buf: &[u8], len: usize) -> Result<(), Error> {
if len != buf.len() {
Err(io::Error::new(io::ErrorKind::Interrupted, "UDP packet not fully sent").into())
} else {
Ok(())
}
}