Browse Source

Introduce Config struct.

wip
Titouan Rigoudy 4 years ago
parent
commit
ead5936138
4 changed files with 74 additions and 47 deletions
  1. +18
    -11
      client/src/client.rs
  2. +36
    -12
      client/src/config.rs
  3. +19
    -23
      client/src/main.rs
  4. +1
    -1
      proto/src/server/credentials.rs

+ 18
- 11
client/src/client.rs View File

@ -6,7 +6,7 @@ use solstice_proto;
use solstice_proto::peer;
use solstice_proto::server;
use crate::config;
use crate::config::Config;
use crate::control;
use crate::login::LoginStatus;
use crate::room;
@ -42,6 +42,8 @@ pub struct Client {
proto_tx: crossbeam_channel::Sender<server::ServerRequest>,
proto_rx: crossbeam_channel::Receiver<server::ServerResponse>,
config: Config,
login_status: LoginStatus,
rooms: room::RoomMap,
@ -58,17 +60,21 @@ impl Client {
pub fn new(
proto_tx: crossbeam_channel::Sender<server::ServerRequest>,
proto_rx: crossbeam_channel::Receiver<server::ServerResponse>,
config: Config,
) -> Self {
let max_peers = config.max_peers;
Client {
proto_tx: proto_tx,
proto_rx: proto_rx,
config,
login_status: LoginStatus::Todo,
rooms: room::RoomMap::new(),
users: user::UserMap::new(),
peers: slab::Slab::new(config::MAX_PEERS),
peers: slab::Slab::new(max_peers),
}
}
@ -76,21 +82,22 @@ impl Client {
pub fn run(&mut self) {
info!("Logging in...");
let credentials = server::Credentials::new(
config::USERNAME.to_string(),
config::PASSWORD.to_string(),
)
.unwrap();
let version = server::Version::default();
self.send_to_server(credentials.into_login_request(version).into());
self.send_to_server(
self
.config
.credentials
.clone()
.into_login_request(version)
.into(),
);
self.login_status = LoginStatus::AwaitingResponse;
self.send_to_server(
server::SetListenPortRequest {
port: config::LISTEN_PORT,
port: self.config.peer_listen_port,
}
.into(),
);
@ -367,7 +374,7 @@ impl Client {
peer.state = PeerState::Open;
// Send a PeerInit.
peer::Message::PeerInit(peer::PeerInit {
user_name: config::USERNAME.to_string(),
user_name: self.config.credentials.user_name().to_string(),
connection_type: peer.connection_type.clone(),
token: peer.token,
})


+ 36
- 12
client/src/config.rs View File

@ -1,17 +1,41 @@
pub const USERNAME: &'static str = "solstice";
// The password is not used for much, and sent unencrypted over the wire, so
// why not even check it in to git
pub const PASSWORD: &'static str = "topsekrit";
use solstice_proto::server::Credentials;
pub const SERVER_HOST: &'static str = "server.slsknet.org";
pub const SERVER_PORT: u16 = 2242;
// TODO: Implement reading this from .toml files.
pub struct Config {
/// Credentials to present to the server.
pub credentials: Credentials,
#[allow(dead_code)]
pub const LISTEN_HOST: &'static str = "0.0.0.0";
/// The address of the server to connect to.
pub server_address: String,
pub const LISTEN_PORT: u16 = 2243;
/// The address on which to listen for incoming peer connections.
#[allow(dead_code)]
pub peer_listen_address: String,
pub const CONTROL_HOST: &'static str = "localhost";
pub const CONTROL_PORT: u16 = 2244;
// TODO: Remove this in favor of using `peer_listen_address` and the port of
// the bound listener.
/// The port on which to listen for incoming peer connections.
pub peer_listen_port: u16,
pub const MAX_PEERS: usize = 1000;
/// The address on which to listen for incoming control connections.
pub control_listen_address: String,
/// The maximum number of peer connections allowed at once.
pub max_peers: usize,
}
impl Default for Config {
fn default() -> Self {
let credentials =
Credentials::new("solstice".to_string(), "topsekrit".to_string())
.expect("building default credentials");
Self {
credentials,
server_address: "server.slsknet.org:2242".to_string(),
peer_listen_address: "0.0.0.0:2243".to_string(),
peer_listen_port: 2243,
control_listen_address: "localhost:2244".to_string(),
max_peers: 1000,
}
}
}

+ 19
- 23
client/src/main.rs View File

@ -22,6 +22,7 @@ mod message_handler;
mod room;
mod user;
use config::Config;
use context::ContextBundle;
use dispatcher::Dispatcher;
use executor::Executor;
@ -30,35 +31,26 @@ fn old_main() {
let (request_tx, _request_rx) = crossbeam_channel::bounded(100);
let (_response_tx, response_rx) = crossbeam_channel::bounded(100);
let mut client = client::Client::new(request_tx, response_rx);
let mut client =
client::Client::new(request_tx, response_rx, Config::default());
client.run();
}
async fn run_client(
config: Config,
request_rx: mpsc::Receiver<solstice_proto::ServerRequest>,
dispatcher_tx: mpsc::Sender<dispatcher::Message>,
) -> anyhow::Result<()> {
let address = format!(
"{}:{}",
config::SERVER_HOST,
config::SERVER_PORT
);
info!("Connecting to server at {}.", address);
let stream = TcpStream::connect(address)
info!("Connecting to server at {}.", config.server_address);
let stream = TcpStream::connect(&config.server_address)
.await
.context("connecting to server")?;
info!("Connection successful.");
let credentials = solstice_proto::server::Credentials::new(
config::USERNAME.to_string(),
config::PASSWORD.to_string(),
)
.context("validating credentials")?;
info!("Logging in to server.");
let (_info, mut worker) = solstice_proto::server::Client::new(stream)
.login(credentials)
.login(config.credentials)
.await
.context("logging in")?;
info!("Login successful.");
@ -94,17 +86,21 @@ async fn async_main() -> anyhow::Result<()> {
// TODO: Define a constant for this, or something.
let (dispatcher_tx, mut dispatcher_rx) = mpsc::channel(100);
let client_task =
tokio::spawn(run_client(bundle.server_request_rx, dispatcher_tx.clone()));
let config = Config::default();
let mut control_listener =
control::Listener::bind(&config.control_listen_address)
.await
.context("binding control listener")?;
let client_task = tokio::spawn(run_client(
config,
bundle.server_request_rx,
dispatcher_tx.clone(),
));
let dispatcher = Dispatcher::new();
let executor = Executor::new(bundle.context);
let control_address =
format!("{}:{}", config::CONTROL_HOST, config::CONTROL_PORT);
let mut control_listener = control::Listener::bind(&control_address)
.await
.context("binding control listener")?;
let control_task =
control_listener.run(dispatcher_tx, bundle.control_response_rx);


+ 1
- 1
proto/src/server/credentials.rs View File

@ -6,7 +6,7 @@ use crypto::md5::Md5;
use crate::server::{LoginRequest, Version};
/// Credentials for logging in a client to a server.
#[derive(Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Credentials {
user_name: String,
password: String,


Loading…
Cancel
Save