feat: add configurable listen port
This commit is contained in:
+14
-1
@@ -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<u32>,
|
||||
) -> 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<ClientOperation>,
|
||||
) = 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 =
|
||||
|
||||
+1
-1
@@ -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"),
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ pub fn deflate(input: &[u8]) -> Result<Vec<u8>> {
|
||||
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(),
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user