move tcp client into separate module

This commit is contained in:
Nikita Vilunov 2023-01-19 18:58:56 +01:00
parent 734ca6c423
commit d94d03466a
4 changed files with 84 additions and 68 deletions

View File

@ -1,7 +1,9 @@
mod http;
mod prelude;
mod tcp;
use crate::prelude::*;
use tcp::ClientSocketActor;
use std::collections::HashMap;
use std::future::Future;
@ -117,71 +119,3 @@ async fn handle_connection(
let writer = BufWriter::new(connect);
Ok(ClientSocketActor::launch(writer, updater, id)?)
}
struct ClientSocketActor {
sender: Sender<ClientSocketActorMessage>,
fiber: JoinHandle<Result<ClientSocketActorTermination>>,
}
#[derive(Debug, Clone)]
enum ClientSocketActorMessage {
Terminate,
}
#[derive(Debug, Clone)]
enum ClientSocketActorTermination {
ServerClosed,
}
impl ClientSocketActor {
pub async fn terminate(self) -> Result<ClientSocketActorTermination> {
self.sender
.send(ClientSocketActorMessage::Terminate)
.await?;
Ok(self.fiber.await??)
}
pub fn launch(
writer: BufWriter<TcpStream>,
updater: Sender<u32>,
id: u32,
) -> Result<ClientSocketActor> {
let (sender, chan) = tokio::sync::mpsc::channel(32);
let fiber: JoinHandle<Result<ClientSocketActorTermination>> =
tokio::spawn(ClientSocketActor::handle_connect(writer, chan, updater, id));
Ok(ClientSocketActor { sender, fiber })
}
async fn handle_connect(
mut writer: BufWriter<TcpStream>,
mut messagebox: Receiver<ClientSocketActorMessage>,
updater: Sender<u32>,
id: u32,
) -> Result<ClientSocketActorTermination> {
async fn handle(
messagebox: &mut Receiver<ClientSocketActorMessage>,
writer: &mut BufWriter<TcpStream>,
) -> Result<Option<ClientSocketActorTermination>> {
writer.write_all("privet\n".as_bytes()).await?;
writer.flush().await?;
tracing::info!("Wrote");
tokio::select! {
_ = messagebox.recv() => return Ok(Some(ClientSocketActorTermination::ServerClosed)),
_ = tokio::time::sleep(Duration::from_millis(200)) => (),
}
Ok(None)
}
loop {
match handle(&mut messagebox, &mut writer).await {
Ok(None) => {}
Ok(Some(termination)) => return Ok(termination),
Err(err) => {
tracing::error!("{}", err);
updater.send(id).await?;
return Err(err);
}
}
}
}
}

View File

@ -1,5 +1,6 @@
pub use tokio::pin;
pub use tokio::select;
pub use tokio::task::JoinHandle;
pub mod log {
pub use tracing::{debug, error, info, warn};

78
src/tcp/client.rs Normal file
View File

@ -0,0 +1,78 @@
use std::time::Duration;
use crate::prelude::*;
use tokio::{
io::{AsyncWriteExt, BufWriter},
net::TcpStream,
sync::mpsc::{Receiver, Sender},
};
pub struct ClientSocketActor {
sender: Sender<ClientSocketActorMessage>,
fiber: JoinHandle<Result<ClientSocketActorTermination>>,
}
#[derive(Debug, Clone)]
enum ClientSocketActorMessage {
Terminate,
}
#[derive(Debug, Clone)]
enum ClientSocketActorTermination {
ServerClosed,
}
impl ClientSocketActor {
pub async fn terminate(self) -> Result<()> {
self.sender
.send(ClientSocketActorMessage::Terminate)
.await?;
self.fiber.await??;
Ok(())
}
pub fn launch(
writer: BufWriter<TcpStream>,
updater: Sender<u32>,
id: u32,
) -> Result<ClientSocketActor> {
let (sender, chan) = tokio::sync::mpsc::channel(32);
let fiber: JoinHandle<Result<ClientSocketActorTermination>> =
tokio::spawn(ClientSocketActor::handle_connect(writer, chan, updater, id));
Ok(ClientSocketActor { sender, fiber })
}
async fn handle_connect(
mut writer: BufWriter<TcpStream>,
mut messagebox: Receiver<ClientSocketActorMessage>,
updater: Sender<u32>,
id: u32,
) -> Result<ClientSocketActorTermination> {
async fn handle(
messagebox: &mut Receiver<ClientSocketActorMessage>,
writer: &mut BufWriter<TcpStream>,
) -> Result<Option<ClientSocketActorTermination>> {
writer.write_all("privet\n".as_bytes()).await?;
writer.flush().await?;
tracing::info!("Wrote");
tokio::select! {
_ = messagebox.recv() => return Ok(Some(ClientSocketActorTermination::ServerClosed)),
_ = tokio::time::sleep(Duration::from_millis(200)) => (),
}
Ok(None)
}
loop {
match handle(&mut messagebox, &mut writer).await {
Ok(None) => {}
Ok(Some(termination)) => return Ok(termination),
Err(err) => {
tracing::error!("{}", err);
updater.send(id).await?;
return Err(err);
}
}
}
}
}

3
src/tcp/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod client;
pub use client::ClientSocketActor;