|
|
|
@ -9,7 +9,7 @@ use tokio::net; |
|
|
|
|
|
|
|
use crate::proto::core::frame::FrameStream;
|
|
|
|
use crate::proto::server::{
|
|
|
|
LoginRequest, LoginResponse, ServerRequest, ServerResponse,
|
|
|
|
Credentials, LoginResponse, ServerRequest, ServerResponse,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Specifies a protocol version.
|
|
|
|
@ -32,8 +32,7 @@ impl Default for Version { |
|
|
|
|
|
|
|
/// Specifies options for a new `Client`.
|
|
|
|
pub struct ClientOptions {
|
|
|
|
pub user_name: String,
|
|
|
|
pub password: String,
|
|
|
|
pub credentials: Credentials,
|
|
|
|
pub version: Version,
|
|
|
|
}
|
|
|
|
|
|
|
|
@ -45,9 +44,6 @@ pub struct Client { |
|
|
|
/// An error that arose while logging in to a remote server.
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum ClientLoginError {
|
|
|
|
#[error("invalid credentials: {0}")]
|
|
|
|
InvalidCredentials(String),
|
|
|
|
|
|
|
|
#[error("login failed: {0}")]
|
|
|
|
LoginFailed(String),
|
|
|
|
|
|
|
|
@ -83,7 +79,7 @@ impl Client { |
|
|
|
frame_stream: FrameStream::new(tcp_stream),
|
|
|
|
};
|
|
|
|
|
|
|
|
client.handshake(&options).await?;
|
|
|
|
client.handshake(options).await?;
|
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
@ -92,17 +88,11 @@ impl Client { |
|
|
|
// Called this way because `login` is already taken.
|
|
|
|
async fn handshake(
|
|
|
|
&mut self,
|
|
|
|
options: &ClientOptions,
|
|
|
|
options: ClientOptions,
|
|
|
|
) -> Result<(), ClientLoginError> {
|
|
|
|
let login_request = LoginRequest::new(
|
|
|
|
&options.user_name,
|
|
|
|
&options.password,
|
|
|
|
options.version.major,
|
|
|
|
options.version.minor,
|
|
|
|
)
|
|
|
|
.map_err(|err| ClientLoginError::InvalidCredentials(err.to_string()))?;
|
|
|
|
|
|
|
|
let request = ServerRequest::LoginRequest(login_request);
|
|
|
|
let request = ServerRequest::LoginRequest(
|
|
|
|
options.credentials.into_login_request(options.version),
|
|
|
|
);
|
|
|
|
self.frame_stream.write(&request).await?;
|
|
|
|
|
|
|
|
let response = self.frame_stream.read().await?;
|
|
|
|
@ -188,7 +178,7 @@ mod tests { |
|
|
|
use tokio::net;
|
|
|
|
|
|
|
|
use crate::proto::server::testing::fake_server;
|
|
|
|
use crate::proto::server::*;
|
|
|
|
use crate::proto::server::{Credentials, ServerRequest, UserStatusRequest};
|
|
|
|
|
|
|
|
use super::{Client, ClientOptions, Version};
|
|
|
|
|
|
|
|
@ -206,9 +196,12 @@ mod tests { |
|
|
|
|
|
|
|
let stream = net::TcpStream::connect(handle.address()).await.unwrap();
|
|
|
|
|
|
|
|
let user_name = "alice".to_string();
|
|
|
|
let password = "sekrit".to_string();
|
|
|
|
let credentials = Credentials::new(user_name, password).unwrap();
|
|
|
|
|
|
|
|
let options = ClientOptions {
|
|
|
|
user_name: "alice".to_string(),
|
|
|
|
password: "sekrit".to_string(),
|
|
|
|
credentials,
|
|
|
|
version: Version::default(),
|
|
|
|
};
|
|
|
|
let client = Client::login(stream, options).await.unwrap();
|
|
|
|
|