srt_protocol/options/
listener.rs1use std::{
2 convert::TryInto,
3 net::{Ipv4Addr, SocketAddr},
4};
5
6use super::*;
7
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub struct ListenerOptions {
10 pub socket: SocketOptions,
11}
12
13impl ListenerOptions {
14 pub fn new(local: impl TryInto<SocketAddress>) -> Result<Valid<Self>, OptionsError> {
15 Self::with(local, Default::default())
16 }
17
18 pub fn with(
19 local: impl TryInto<SocketAddress>,
20 socket: SocketOptions,
21 ) -> Result<Valid<ListenerOptions>, OptionsError> {
22 let local_address = local
23 .try_into()
24 .map_err(|_| OptionsError::InvalidLocalAddress)?;
25
26 use SocketHost::*;
27 let local = match local_address.host {
28 Ipv4(ipv4) => SocketAddr::new(ipv4.into(), local_address.port),
29 Ipv6(ipv6) => SocketAddr::new(ipv6.into(), local_address.port),
30 Domain(_) => return Err(OptionsError::InvalidLocalAddress),
31 };
32
33 let mut options = Self { socket };
34 options.socket.connect.local.set_port(local.port());
35 if local.ip() != Ipv4Addr::UNSPECIFIED {
36 options.socket.connect.local.set_ip(local.ip());
37 }
38
39 options.try_validate()
40 }
41}
42
43impl Validation for ListenerOptions {
44 type Error = OptionsError;
45
46 fn is_valid(&self) -> Result<(), Self::Error> {
47 self.socket.is_valid()?;
48 if self.socket.connect.local.port() == 0 {
49 Err(OptionsError::LocalPortRequiredToListen)
50 } else {
51 self.is_valid_composite()
52 }
53 }
54}
55
56impl CompositeValidation for ListenerOptions {
57 fn is_valid_composite(&self) -> Result<(), <Self as Validation>::Error> {
58 Ok(())
59 }
60}
61
62impl OptionsOf<SocketOptions> for ListenerOptions {
63 fn set_options(&mut self, value: SocketOptions) {
64 self.socket = value;
65 }
66}
67
68impl OptionsOf<Connect> for ListenerOptions {
69 fn set_options(&mut self, value: Connect) {
70 self.socket.connect = value;
71 }
72}
73
74impl OptionsOf<Session> for ListenerOptions {
75 fn set_options(&mut self, value: Session) {
76 self.socket.session = value;
77 }
78}
79
80impl OptionsOf<Encryption> for ListenerOptions {
81 fn set_options(&mut self, value: Encryption) {
82 self.socket.encryption = value;
83 }
84}
85
86impl OptionsOf<Sender> for ListenerOptions {
87 fn set_options(&mut self, value: Sender) {
88 self.socket.sender = value;
89 }
90}
91
92impl OptionsOf<Receiver> for ListenerOptions {
93 fn set_options(&mut self, value: Receiver) {
94 self.socket.receiver = value;
95 }
96}