diff --git a/src/client.rs b/src/client.rs index 93eb292..6c7a1df 100644 --- a/src/client.rs +++ b/src/client.rs @@ -25,6 +25,8 @@ use std::{ use crate::{debug, error, info, trace}; const MAX_THREADS: usize = 50; +const DEFALT_LISTEN_PORT: u32 = 2234; + pub enum ClientOperation { NewPeer(NewPeer), ConnectToPeer(Peer), @@ -69,6 +71,7 @@ impl ClientContext { } } pub struct Client { + listen_port: u32, address: PeerAddress, username: String, password: String, @@ -81,6 +84,7 @@ impl Client { address: PeerAddress, username: String, password: String, + listen_port: Option, ) -> Self { crate::utils::logger::init(); let context = Arc::new(RwLock::new(ClientContext::new())); @@ -89,6 +93,7 @@ impl Client { context.read().unwrap().thread_pool.thread_count() ); Self { + listen_port: listen_port.unwrap_or(DEFALT_LISTEN_PORT), address, username, password, @@ -96,6 +101,13 @@ impl Client { context, } } + pub fn with_defaults( + address: PeerAddress, + username: String, + password: String, + ) -> Self { + Self::new(address, username, password, None) + } pub fn connect(&mut self) { let (sender, message_reader): ( @@ -103,6 +115,7 @@ impl Client { Receiver, ) = mpsc::channel(); + let listen_port = self.listen_port; self.context.write().unwrap().sender = Some(sender.clone()); let client_sender = sender.clone(); @@ -116,7 +129,7 @@ impl Client { ); thread::spawn(move || { - Listen::start(2234, client_sender.clone()); + Listen::start(listen_port, client_sender.clone()); }); let mut unlocked_context = self.context.write().unwrap(); unlocked_context.server_sender = diff --git a/src/main.rs b/src/main.rs index b6b4a25..5c744df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ fn main() { use soulseek_rs::{Client, PeerAddress}; use std::time::Duration; - let mut client = Client::new( + let mut client = Client::with_defaults( // PeerAddress::new(String::from("server.slsknet.org"), 2242), PeerAddress::new(String::from("127.0.0.1"), 2242), String::from("insane_in_the_brain3"), diff --git a/src/utils/zlib.rs b/src/utils/zlib.rs index 599889b..8b52421 100644 --- a/src/utils/zlib.rs +++ b/src/utils/zlib.rs @@ -72,7 +72,7 @@ pub fn deflate(input: &[u8]) -> Result> { return Err(SoulseekRs::CompressionError("invalid CINFO".to_string())); } let flg = r.read_byte()?; - if !((cmf as u32) * 256 + (flg as u32)).is_multiple_of(31) { + if ((cmf as u32) * 256 + (flg as u32)) % 31 != 0 { return Err(SoulseekRs::CompressionError( "CMF+FLG checksum failed".to_string(), ));