新手问个问题…… - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fgwmlhdkkkw
V2EX    Rust

新手问个问题……

  •  
  •   fgwmlhdkkkw 2022 年 3 月 23 日 1913 次点击
    这是一个创建于 1447 天前的主题,其中的信息可能已经有所发展或是发生改变。

    conn.rs

    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); } } } 

    server.rs

    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 中的记录。

    3 条回复
    fgwmlhdkkkw
        1
    fgwmlhdkkkw  
    OP
       2022 年 3 月 23 日
    fgwmlhdkkkw
        2
    fgwmlhdkkkw  
    OP
       2022 年 3 月 23 日 via Android
    咋没人呀,,再顶最后一次
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1250 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 17:32 PVG 01:32 LAX 10:32 JFK 13:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86