First barebones for problem 5
This commit is contained in:
parent
1b4d5f0eef
commit
aeebaff9f7
3 changed files with 86 additions and 0 deletions
12
problem_05/.gitignore
vendored
Normal file
12
problem_05/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
/target/
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
.idea
|
||||||
13
problem_05/Cargo.toml
Normal file
13
problem_05/Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
[package]
|
||||||
|
name = "problem_05"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "server"
|
||||||
|
path = "bin/server.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tokio = { version = "1.14.0", features = ["full"] }
|
||||||
|
tracing = "0.1.37"
|
||||||
|
tracing-subscriber = "0.3.17"
|
||||||
61
problem_05/bin/server.rs
Normal file
61
problem_05/bin/server.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
|
use tracing::{info, error};
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use tokio_util::codec::{Framed, LinesCodec};
|
||||||
|
use tokio::sync::broadcast;
|
||||||
|
|
||||||
|
const DEFAULT_IP: &str = "0.0.0.0";
|
||||||
|
const DEFAULT_PORT: &str = "1222";
|
||||||
|
|
||||||
|
const UPSTREAM_IP: &str = "206.189.113.124";
|
||||||
|
const UPSTREAM_PORT: &str = "16963";
|
||||||
|
|
||||||
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
enum Events {
|
||||||
|
ClientRequest(String),
|
||||||
|
ClientResponse(String),
|
||||||
|
UpstreamRequest(String),
|
||||||
|
UpstreamResponse(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
pub async fn main() -> Result<()> {
|
||||||
|
tracing_subscriber::fmt::try_init()?;
|
||||||
|
|
||||||
|
let listener = TcpListener::bind(&format!("{DEFAULT_IP}:{DEFAULT_PORT}")).await?;
|
||||||
|
let stream = TcpStream::connect(&format!("{UPSTREAM_IP}:{UPSTREAM_PORT}")).await?;
|
||||||
|
|
||||||
|
let (sender, receiver) = broadcast::channel(2);
|
||||||
|
|
||||||
|
info!("Start TCP server on {DEFAULT_IP}:{DEFAULT_PORT}");
|
||||||
|
info!("Connect to upstream on {UPSTREAM_IP}:{UPSTREAM_PORT}");
|
||||||
|
|
||||||
|
let listener_handle = tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let (socket, address) = listener.accept().await?;
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
info!("New request from: {address}");
|
||||||
|
let _ = handle_request(socket).await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let upstream_handle = tokio::spawn({
|
||||||
|
loop {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = listener_handle.await;
|
||||||
|
let _ = upstream_handle.await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_request(mut socket: TcpStream) -> Result<()> {
|
||||||
|
let framed = Framed::new(socket, LinesCodec::new());
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue