make AtemSocket{,Inner} take a socketaddr at init time
This commit is contained in:
parent
a419a78a0a
commit
2ec4c231c2
|
@ -1,3 +1,4 @@
|
||||||
|
use core::net::SocketAddr;
|
||||||
use std::{io, sync::Arc, thread::yield_now};
|
use std::{io, sync::Arc, thread::yield_now};
|
||||||
|
|
||||||
use tokio::{sync::RwLock, task::JoinHandle};
|
use tokio::{sync::RwLock, task::JoinHandle};
|
||||||
|
@ -17,8 +18,8 @@ pub enum AtemSocketConnectionError {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AtemSocket {
|
impl AtemSocket {
|
||||||
pub fn new() -> Self {
|
pub fn new<A: Into<SocketAddr>>(addr: A) -> Self {
|
||||||
let socket = AtemSocketInner::new();
|
let socket = AtemSocketInner::new(addr.into());
|
||||||
let socket = Arc::new(RwLock::new(socket));
|
let socket = Arc::new(RwLock::new(socket));
|
||||||
|
|
||||||
let socket_clone = Arc::clone(&socket);
|
let socket_clone = Arc::clone(&socket);
|
||||||
|
@ -37,12 +38,8 @@ impl AtemSocket {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect(
|
pub async fn connect(&mut self) -> Result<(), AtemSocketConnectionError> {
|
||||||
&mut self,
|
self.socket.write().await.connect().await?;
|
||||||
address: String,
|
|
||||||
port: u16,
|
|
||||||
) -> Result<(), AtemSocketConnectionError> {
|
|
||||||
self.socket.write().await.connect(address, port).await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -60,9 +57,3 @@ impl AtemSocket {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AtemSocket {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
use core::net::SocketAddr;
|
||||||
use std::{
|
use std::{
|
||||||
io,
|
io,
|
||||||
net::SocketAddr,
|
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -68,8 +68,7 @@ pub struct AtemSocketInner {
|
||||||
session_id: u16,
|
session_id: u16,
|
||||||
|
|
||||||
socket: Option<UdpSocket>,
|
socket: Option<UdpSocket>,
|
||||||
address: String,
|
address: SocketAddr,
|
||||||
port: u16,
|
|
||||||
|
|
||||||
last_received_at: SystemTime,
|
last_received_at: SystemTime,
|
||||||
last_received_packed_id: u16,
|
last_received_packed_id: u16,
|
||||||
|
@ -92,7 +91,7 @@ enum AtemSocketWriteError {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AtemSocketInner {
|
impl AtemSocketInner {
|
||||||
pub fn new() -> Self {
|
pub fn new(addr: SocketAddr) -> Self {
|
||||||
AtemSocketInner {
|
AtemSocketInner {
|
||||||
connection_state: ConnectionState::Closed,
|
connection_state: ConnectionState::Closed,
|
||||||
reconnect_timer: None,
|
reconnect_timer: None,
|
||||||
|
@ -102,8 +101,7 @@ impl AtemSocketInner {
|
||||||
session_id: 0,
|
session_id: 0,
|
||||||
|
|
||||||
socket: None,
|
socket: None,
|
||||||
address: "0.0.0.0".to_string(),
|
address: addr,
|
||||||
port: 0,
|
|
||||||
|
|
||||||
last_received_at: SystemTime::now(),
|
last_received_at: SystemTime::now(),
|
||||||
last_received_packed_id: 0,
|
last_received_packed_id: 0,
|
||||||
|
@ -113,15 +111,9 @@ impl AtemSocketInner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect(&mut self, address: String, port: u16) -> Result<(), io::Error> {
|
pub async fn connect(&mut self) -> Result<(), io::Error> {
|
||||||
self.address = address.clone();
|
|
||||||
self.port = port;
|
|
||||||
|
|
||||||
let socket = UdpSocket::bind("0.0.0.0:0").await?;
|
let socket = UdpSocket::bind("0.0.0.0:0").await?;
|
||||||
let remote_addr = format!("{}:{}", address, port)
|
socket.connect(self.address).await?;
|
||||||
.parse::<SocketAddr>()
|
|
||||||
.unwrap();
|
|
||||||
socket.connect(remote_addr).await?;
|
|
||||||
self.socket = Some(socket);
|
self.socket = Some(socket);
|
||||||
|
|
||||||
self.start_timers();
|
self.start_timers();
|
||||||
|
@ -197,7 +189,7 @@ impl AtemSocketInner {
|
||||||
|
|
||||||
async fn restart_connection(&mut self) {
|
async fn restart_connection(&mut self) {
|
||||||
self.disconnect();
|
self.disconnect();
|
||||||
self.connect(self.address.clone(), self.port).await.ok();
|
self.connect().await.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn tick(&mut self) {
|
pub async fn tick(&mut self) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::time::Duration;
|
use std::{env::args, net::Ipv4Addr, time::Duration};
|
||||||
|
|
||||||
use atem_connection_rs::{
|
use atem_connection_rs::{
|
||||||
atem_lib::atem_socket::AtemSocket,
|
atem_lib::atem_socket::AtemSocket,
|
||||||
|
@ -18,8 +18,11 @@ async fn main() {
|
||||||
let switch_to_source_1 = ProgramInput::new(0, 1);
|
let switch_to_source_1 = ProgramInput::new(0, 1);
|
||||||
let switch_to_source_2 = ProgramInput::new(0, 2);
|
let switch_to_source_2 = ProgramInput::new(0, 2);
|
||||||
|
|
||||||
let mut atem = AtemSocket::new();
|
let mut atem = AtemSocket::new((
|
||||||
atem.connect("127.0.0.1".to_string(), 9910).await.ok();
|
args().skip(1).next().unwrap().parse::<Ipv4Addr>().unwrap(),
|
||||||
|
9910,
|
||||||
|
));
|
||||||
|
atem.connect().await.ok();
|
||||||
|
|
||||||
let mut tracking_id = 0;
|
let mut tracking_id = 0;
|
||||||
loop {
|
loop {
|
||||||
|
|
Loading…
Reference in New Issue