feat: support ToSocketAddrs, not just 1 SocketAddr

This commit is contained in:
Mark 2025-03-11 22:11:36 +01:00
parent b8e729d81c
commit bf100f38e3
3 changed files with 13 additions and 12 deletions

View File

@ -2,7 +2,7 @@
use std::{ use std::{
io::{BufReader, Write}, io::{BufReader, Write},
net::{SocketAddr, TcpStream}, net::TcpStream,
path::PathBuf, path::PathBuf,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
thread, thread,
@ -65,7 +65,7 @@ mod textcfg;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct Args { struct Args {
/// the address to be used for the tcp connection to the server /// the address to be used for the tcp connection to the server
addr: SocketAddr, addr: String,
/// what to do /// what to do
#[command(subcommand)] #[command(subcommand)]
mode: Mode, mode: Mode,
@ -109,8 +109,8 @@ fn main() {
// parse args // parse args
let args = Args::parse(); let args = Args::parse();
// start // start
let addr = args.addr; let addr = args.addr.clone();
let mut con = TcpStream::connect(addr).unwrap(); let mut con = TcpStream::connect(&addr).unwrap();
let mode = args.mode; let mode = args.mode;
writeln!(con, "main").unwrap(); writeln!(con, "main").unwrap();
let database = Arc::new(Mutex::new(Database::new_clientside())); let database = Arc::new(Mutex::new(Database::new_clientside()));
@ -130,6 +130,7 @@ fn main() {
let database = Arc::clone(&database); let database = Arc::clone(&database);
let mut con = con.try_clone().unwrap(); let mut con = con.try_clone().unwrap();
// this is all you need to keep the db in sync // this is all you need to keep the db in sync
let addr = addr.clone();
thread::spawn(move || { thread::spawn(move || {
#[cfg(feature = "playback")] #[cfg(feature = "playback")]
#[cfg(not(feature = "speedy2d"))] #[cfg(not(feature = "speedy2d"))]
@ -182,7 +183,7 @@ fn main() {
break 'ifstatementworkaround; break 'ifstatementworkaround;
} }
let mut db = database.lock().unwrap(); let mut db = database.lock().unwrap();
let client_con: Box<dyn ClientIo> = Box::new(TcpStream::connect(addr).unwrap()); let client_con: Box<dyn ClientIo> = Box::new(TcpStream::connect(&addr).unwrap());
db.remote_server_as_song_file_source = Some(Arc::new(Mutex::new( db.remote_server_as_song_file_source = Some(Arc::new(Mutex::new(
musicdb_lib::server::get::Client::new(BufReader::new(client_con)).unwrap(), musicdb_lib::server::get::Client::new(BufReader::new(client_con)).unwrap(),
))); )));
@ -222,7 +223,7 @@ fn main() {
let get_con: Arc<Mutex<musicdb_lib::server::get::Client<Box<dyn ClientIo + 'static>>>> = let get_con: Arc<Mutex<musicdb_lib::server::get::Client<Box<dyn ClientIo + 'static>>>> =
Arc::new(Mutex::new( Arc::new(Mutex::new(
musicdb_lib::server::get::Client::new(BufReader::new(Box::new( musicdb_lib::server::get::Client::new(BufReader::new(Box::new(
TcpStream::connect(addr).expect("opening get client connection"), TcpStream::connect(&addr).expect("opening get client connection"),
) as _)) ) as _))
.expect("initializing get client connection"), .expect("initializing get client connection"),
)); ));

View File

@ -2,7 +2,7 @@ pub mod get;
use std::{ use std::{
io::{BufRead as _, BufReader, Read, Write}, io::{BufRead as _, BufReader, Read, Write},
net::{SocketAddr, TcpListener}, net::TcpListener,
sync::{mpsc, Arc, Mutex}, sync::{mpsc, Arc, Mutex},
thread, thread,
time::Duration, time::Duration,
@ -269,7 +269,7 @@ impl Command {
/// c) re-encode all received messages using Command::to_bytes_vec(), send them to the db, and send them to all your clients. /// c) re-encode all received messages using Command::to_bytes_vec(), send them to the db, and send them to all your clients.
pub fn run_server( pub fn run_server(
database: Arc<Mutex<Database>>, database: Arc<Mutex<Database>>,
addr_tcp: Option<SocketAddr>, addr_tcp: Option<String>,
sender_sender: Option<Box<dyn FnOnce(mpsc::Sender<(Command, Option<u64>)>)>>, sender_sender: Option<Box<dyn FnOnce(mpsc::Sender<(Command, Option<u64>)>)>>,
play_audio: bool, play_audio: bool,
) { ) {
@ -277,7 +277,7 @@ pub fn run_server(
} }
pub fn run_server_caching_thread_opt( pub fn run_server_caching_thread_opt(
database: Arc<Mutex<Database>>, database: Arc<Mutex<Database>>,
addr_tcp: Option<SocketAddr>, addr_tcp: Option<String>,
sender_sender: Option<Box<dyn FnOnce(mpsc::Sender<(Command, Option<u64>)>)>>, sender_sender: Option<Box<dyn FnOnce(mpsc::Sender<(Command, Option<u64>)>)>>,
caching_thread: Option<Box<dyn FnOnce(&mut crate::data::cache_manager::CacheManager)>>, caching_thread: Option<Box<dyn FnOnce(&mut crate::data::cache_manager::CacheManager)>>,
play_audio: bool, play_audio: bool,

View File

@ -18,7 +18,7 @@ use musicdb_lib::data::database::Database;
struct Args { struct Args {
/// optional address for tcp connections to the server /// optional address for tcp connections to the server
#[arg(long)] #[arg(long)]
tcp: Option<SocketAddr>, tcp: Option<String>,
/// optional address on which to start a website which can be used on devices without `musicdb-client` to control playback. /// optional address on which to start a website which can be used on devices without `musicdb-client` to control playback.
/// requires the `assets/` folder to be present! /// requires the `assets/` folder to be present!
#[arg(long)] #[arg(long)]
@ -65,7 +65,7 @@ enum Source {
Remote { Remote {
/// The address of another musicdb-server from where to load the songs /// The address of another musicdb-server from where to load the songs
#[arg()] #[arg()]
addr: SocketAddr, addr: String,
}, },
} }
// struct Args { // struct Args {
@ -80,7 +80,7 @@ enum Source {
// init: bool, // init: bool,
// /// optional address for tcp connections to the server // /// optional address for tcp connections to the server
// #[arg(long)] // #[arg(long)]
// tcp: Option<SocketAddr>, // tcp: Option<String>,
// /// optional address on which to start a website which can be used on devices without `musicdb-client` to control playback. // /// optional address on which to start a website which can be used on devices without `musicdb-client` to control playback.
// /// requires the `assets/` folder to be present! // /// requires the `assets/` folder to be present!
// #[arg(long)] // #[arg(long)]