|
|
|
@ -3,6 +3,7 @@ |
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use futures::stream::{Stream, StreamExt};
|
|
|
|
use log::{debug, info};
|
|
|
|
use thiserror::Error;
|
|
|
|
use tokio::net;
|
|
|
|
|
|
|
|
@ -54,6 +55,9 @@ pub enum ClientLoginError { |
|
|
|
#[error("unexpected response: {0:?}")]
|
|
|
|
UnexpectedResponse(ServerResponse),
|
|
|
|
|
|
|
|
#[error("unexpected end of file")]
|
|
|
|
UnexpectedEof,
|
|
|
|
|
|
|
|
#[error("i/o error: {0}")]
|
|
|
|
IOError(#[from] io::Error),
|
|
|
|
}
|
|
|
|
@ -104,21 +108,22 @@ impl Client { |
|
|
|
|
|
|
|
let response = self.frame_stream.read().await?;
|
|
|
|
match response {
|
|
|
|
ServerResponse::LoginResponse(LoginResponse::LoginOk {
|
|
|
|
Some(ServerResponse::LoginResponse(LoginResponse::LoginOk {
|
|
|
|
motd,
|
|
|
|
ip,
|
|
|
|
password_md5_opt,
|
|
|
|
}) => {
|
|
|
|
println!("Logged in successfully!");
|
|
|
|
println!("Message Of The Day: {}", motd);
|
|
|
|
println!("Public IP address: {}", ip);
|
|
|
|
println!("Password MD5: {:?}", password_md5_opt);
|
|
|
|
})) => {
|
|
|
|
info!("Logged in successfully!");
|
|
|
|
info!("Message Of The Day: {}", motd);
|
|
|
|
info!("Public IP address: {}", ip);
|
|
|
|
info!("Password MD5: {:?}", password_md5_opt);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
ServerResponse::LoginResponse(LoginResponse::LoginFail { reason }) => {
|
|
|
|
Err(ClientLoginError::LoginFailed(reason))
|
|
|
|
}
|
|
|
|
response @ _ => Err(ClientLoginError::UnexpectedResponse(response)),
|
|
|
|
Some(ServerResponse::LoginResponse(LoginResponse::LoginFail {
|
|
|
|
reason,
|
|
|
|
})) => Err(ClientLoginError::LoginFailed(reason)),
|
|
|
|
Some(response) => Err(ClientLoginError::UnexpectedResponse(response)),
|
|
|
|
None => Err(ClientLoginError::UnexpectedEof),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ -135,14 +140,16 @@ impl Client { |
|
|
|
self.frame_stream.write(&request).await?;
|
|
|
|
Ok(RunOnceResult::Continue)
|
|
|
|
} else {
|
|
|
|
// Sender has been dropped. Shut down the write half of the stream.
|
|
|
|
self.frame_stream.shutdown().await?;
|
|
|
|
// Sender has been dropped.
|
|
|
|
Ok(RunOnceResult::Break)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
read_result = self.frame_stream.read() => {
|
|
|
|
let response = read_result?;
|
|
|
|
Ok(RunOnceResult::Response(response))
|
|
|
|
match read_result? {
|
|
|
|
Some(response) => Ok(RunOnceResult::Response(response)),
|
|
|
|
// TODO: Consider returning error here.
|
|
|
|
None => Ok(RunOnceResult::Break),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
@ -164,6 +171,14 @@ impl Client { |
|
|
|
RunOnceResult::Response(response) => yield response,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Client: shutting down outbound stream");
|
|
|
|
self.frame_stream.shutdown().await?;
|
|
|
|
|
|
|
|
// Drain the receiving end of the connection.
|
|
|
|
while let Some(response) = self.frame_stream.read().await? {
|
|
|
|
yield response;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -178,8 +193,15 @@ mod tests { |
|
|
|
|
|
|
|
use super::{Client, ClientOptions, Version};
|
|
|
|
|
|
|
|
// Enable capturing logs in tests.
|
|
|
|
fn init() {
|
|
|
|
env_logger::builder().is_test(true).try_init().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn client_like_grpc() {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let (server, handle) = fake_server().await.unwrap();
|
|
|
|
let server_task = tokio::spawn(server.serve());
|
|
|
|
|
|
|
|
@ -197,7 +219,6 @@ mod tests { |
|
|
|
yield ServerRequest::UserStatusRequest(UserStatusRequest {
|
|
|
|
user_name: "bob".to_string(),
|
|
|
|
});
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|