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
pub use rosc;
pub use self::recv::Receiver;
#[doc(inline)]
pub use self::rosc::{
decoder, encoder, OscBundle as Bundle, OscColor as Color, OscError as Error,
OscMessage as Message, OscMidiMessage as MidiMessage, OscType as Type,
};
pub use self::send::Sender;
use std;
use std::net::{Ipv4Addr, SocketAddr};
pub mod recv;
pub mod send;
pub struct Unconnected;
pub struct Connected {
addr: SocketAddr,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Packet {
Message(Message),
Bundle(Bundle),
}
impl From<Message> for Packet {
fn from(msg: Message) -> Self {
Packet::Message(msg)
}
}
impl From<Bundle> for Packet {
fn from(bundle: Bundle) -> Self {
Packet::Bundle(bundle)
}
}
impl From<rosc::OscPacket> for Packet {
fn from(packet: rosc::OscPacket) -> Self {
match packet {
rosc::OscPacket::Message(msg) => msg.into(),
rosc::OscPacket::Bundle(bundle) => bundle.into(),
}
}
}
impl<A> From<(A, Vec<Type>)> for Packet
where
A: Into<String>,
{
fn from((addr, args): (A, Vec<Type>)) -> Self {
msg(addr, args).into()
}
}
impl Into<rosc::OscPacket> for Packet {
fn into(self) -> rosc::OscPacket {
match self {
Packet::Message(msg) => rosc::OscPacket::Message(msg),
Packet::Bundle(bundle) => rosc::OscPacket::Bundle(bundle),
}
}
}
impl Packet {
pub fn unfold(self, msgs: &mut Vec<Message>) {
match self {
Packet::Message(msg) => msgs.push(msg),
Packet::Bundle(bundle) => {
for packet in bundle.content {
Packet::unfold(packet.into(), msgs);
}
}
}
}
pub fn into_msgs(self) -> Vec<Message> {
let mut msgs = vec![];
self.unfold(&mut msgs);
msgs
}
}
pub fn default_ipv4_addr() -> Ipv4Addr {
Ipv4Addr::new(0, 0, 0, 0)
}
pub fn receiver(port: u16) -> Result<Receiver, std::io::Error> {
Receiver::bind(port)
}
pub fn sender() -> Result<Sender, std::io::Error> {
Sender::bind()
}
pub fn msg<A>(addr: A, args: Vec<Type>) -> Message
where
A: Into<String>,
{
let addr = addr.into();
let args = Some(args);
Message { addr, args }
}
pub fn decode(bytes: &[u8]) -> Result<Packet, Error> {
rosc::decoder::decode(bytes).map(|p| p.into())
}
pub fn encode(packet: Packet) -> Result<Vec<u8>, Error> {
let rosc_packet = packet.into();
rosc::encoder::encode(&rosc_packet)
}
#[derive(Debug)]
pub enum CommunicationError {
Io(std::io::Error),
Osc(Error),
Poisoned,
}
impl From<std::io::Error> for CommunicationError {
fn from(err: std::io::Error) -> Self {
CommunicationError::Io(err)
}
}
impl From<Error> for CommunicationError {
fn from(err: Error) -> Self {
CommunicationError::Osc(err)
}
}
impl<T> From<std::sync::PoisonError<T>> for CommunicationError {
fn from(_: std::sync::PoisonError<T>) -> Self {
CommunicationError::Poisoned
}
}
impl std::error::Error for CommunicationError {
fn description(&self) -> &str {
match *self {
CommunicationError::Io(ref err) => std::error::Error::description(err),
CommunicationError::Osc(ref _err) => "Failed to decode the OSC packet",
CommunicationError::Poisoned => "The inner buffer's mutex was poisoned",
}
}
fn cause(&self) -> Option<&std::error::Error> {
match *self {
CommunicationError::Io(ref err) => Some(err),
CommunicationError::Osc(ref _err) => None,
_ => None,
}
}
}
impl std::fmt::Display for CommunicationError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", std::error::Error::description(self))
}
}