use std::hash::{Hash, Hasher};
use std::net;
use std::sync::{Arc, Mutex};
use crate::simplehttp::options;
use crate::simplehttp::server;
pub struct Conn {
conn: net::TcpStream,
}
impl Conn {
pub fn new(conn: net::TcpStream) -> Self {
return Conn { conn };
}
pub fn handle(mut conn: Arc<Conn>) {
println!("{:?}", conn.conn.peer_addr());
}
}
impl PartialEq<Self> for Conn {
fn eq(&self, other: &Self) -> bool {
return self as (*const Self) as u64 == other as (*const Self) as u64;
}
}
impl Eq for Conn {}
impl Hash for Conn {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self as (*const Self) as u64);
}
fn hash_slice<H: Hasher>(data: &[Self], state: &mut H) where Self: Sized {
for datum in data {
state.write_u64(datum as (*const Self) as u64);
}
}
}
use std::{net, thread};
use std::borrow::BorrowMut;
use std::collections::HashSet;
use std::net::TcpListener;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex};
use crate::simplehttp::conn;
use crate::simplehttp::options;
pub struct Server {
host: String,
port: u16,
running: Mutex<bool>,
listener: Option<Box<TcpListener>>,
alive: Mutex<HashSet<Arc<conn::Conn>>>,
}
impl Server {
pub fn new(host: String, port: u16) -> Self {
return Server {
host,
port,
running: Mutex::new(false),
listener: None,
alive: Mutex::new(HashSet::new()),
};
}
pub fn run(&mut self) {
let mut running_guard = self.running.lock().unwrap();
if *running_guard {
panic!("server, {}:{} is already running", self.host, self.port);
}
*running_guard = true;
std::mem::drop(running_guard);
self.listener = Some(Box::new(TcpListener::bind(format!("{}:{}", self.host, self.port)).unwrap()));
println!("simplehttpserver: running @ {}:{}", self.host, self.port);
for stream in self.listener.as_ref().unwrap().incoming() {
match stream {
Ok(stream) => {
let mut conn = Arc::new(conn::Conn::new(stream));
let mut alive_guard = self.alive.lock().unwrap();
// (*alive_guard).insert(conn.clone());
std::mem::drop(alive_guard);
let mut new_conn = conn.clone();
thread::spawn(move || {
conn::Conn::handle(new_conn);
});
}
Err(e) => {
println!("connection error: {:?}", e);
}
}
}
}
}
怎么才能做到 Conn 持有 Server 的 Arc 呀? 我想要 Conn::handle 结束之后,删除在 Server.alive 中的记录。
1
fgwmlhdkkkw OP 😭😭😭😭😭
|
2
fgwmlhdkkkw OP 咋没人呀,,再顶最后一次🤕
|
3
fgwmlhdkkkw OP |