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