This commit is contained in:
aozhiwei 2023-10-12 08:51:11 +08:00
parent 805594a5fb
commit 98a3cd3fe7

View File

@ -9,11 +9,14 @@ const TVR_SIZE: usize = 1 << TVR_BITS;
const TVN_MASK: usize = TVN_SIZE - 1; const TVN_MASK: usize = TVN_SIZE - 1;
const TVR_MASK: usize = TVR_SIZE - 1; const TVR_MASK: usize = TVR_SIZE - 1;
pub type TimerAttacherRp = Rc::<RefCell::<TimerAttacher>>;
pub type TimerWp = Weak::<Rc::<RefCell::<TimerList>>>; pub type TimerWp = Weak::<Rc::<RefCell::<TimerList>>>;
pub type TimerCb = Box::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>; pub type TimerCb = Box::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>;
type TimerListListHeadRp = Rc::<RefCell::<crate::ListHead<TimerList>>>;
type TimerListRp = Rc::<RefCell::<TimerList>>;
pub struct TimerAttacher { pub struct TimerAttacher {
timers: Rc::<RefCell::<crate::ListHead<TimerList>>>, timers: TimerListListHeadRp,
} }
enum TimerType { enum TimerType {
@ -32,10 +35,10 @@ impl Default for TimerType {
#[derive(Default)] #[derive(Default)]
pub struct TimerList { pub struct TimerList {
holder: Rc::<RefCell::<TimerList>>, holder: TimerListRp,
wp: Rc::<Rc::<RefCell::<TimerList>>>, wp: Rc::<TimerListRp>,
timer_entry: Rc::<RefCell::<crate::ListHead<TimerList>>>, timer_entry: TimerListListHeadRp,
attach_entry: Rc::<RefCell::<crate::ListHead<TimerList>>>, attach_entry: TimerListListHeadRp,
timer_type: TimerType, timer_type: TimerType,
expire_time: i32, expire_time: i32,
expires: i64, expires: i64,
@ -84,7 +87,7 @@ impl Timer {
self.tv5[i] = crate::ListHead::<TimerList>::new_head(); self.tv5[i] = crate::ListHead::<TimerList>::new_head();
} }
self.timer_tick = (self.get_tick_count)(); self.timer_tick = (self.get_tick_count)();
let self_wp = self.get_rc_refcell(); let self_wp = self.shared_from_self();
let cb = Box::new( let cb = Box::new(
move |_event: i32, _args: Option<Vec<Rc::<dyn Any>>>| { move |_event: i32, _args: Option<Vec<Rc::<dyn Any>>>| {
self_wp.borrow_mut().gc_timer(_event, _args); self_wp.borrow_mut().gc_timer(_event, _args);
@ -97,7 +100,7 @@ impl Timer {
); );
} }
fn get_rc_refcell(&self) -> Rc::<RefCell::<Timer>> { fn shared_from_self(&self) -> Rc::<RefCell::<Timer>> {
//let cell_rc: &Rc<RefCell<Timer> = &self ; //let cell_rc: &Rc<RefCell<Timer> = &self ;
//let head: &ListHead<T> = &*cell_rc.borrow(); //let head: &ListHead<T> = &*cell_rc.borrow();
let cell_start = 0 as *const u8; let cell_start = 0 as *const u8;
@ -176,7 +179,7 @@ impl Timer {
timer_type: TimerType, timer_type: TimerType,
expire_time: i32, expire_time: i32,
cb: TimerCb, cb: TimerCb,
attacher: Option<Rc::<RefCell::<TimerAttacher>>>) -> TimerWp { attacher: Option<TimerAttacherRp>) -> TimerWp {
let t = self.new_timer_list(); let t = self.new_timer_list();
t.borrow_mut().cb = Some(cb); t.borrow_mut().cb = Some(cb);
t.borrow_mut().timer_type = timer_type; t.borrow_mut().timer_type = timer_type;
@ -220,32 +223,22 @@ impl Timer {
return Rc::downgrade(&t.borrow().wp); return Rc::downgrade(&t.borrow().wp);
} }
pub fn set_timeout(&mut self, pub fn set_timeout(&mut self, time: i32, cb: TimerCb) -> TimerWp {
expire_time: i32, return self.internal_add(TimerType::Timeout, time, cb, None);
cb: TimerCb
) -> TimerWp {
return self.internal_add(TimerType::Timeout, expire_time, cb, None);
} }
pub fn set_timeout_ex(&mut self, pub fn set_timeout_ex
expire_time: i32, (&mut self, time: i32, cb: TimerCb, attacher: TimerAttacherRp) -> TimerWp {
cb: TimerCb, return self.internal_add(TimerType::Timeout, time, cb, Some(attacher));
attacher: Rc::<RefCell::<TimerAttacher>>) -> TimerWp {
return self.internal_add(TimerType::Timeout, expire_time, cb, Some(attacher));
} }
pub fn set_interval(&mut self, pub fn set_interval(&mut self, time: i32, cb: TimerCb) -> TimerWp {
expire_time: i32, return self.internal_add(TimerType::Interval, time, cb, None);
cb: TimerCb
) -> TimerWp {
return self.internal_add(TimerType::Interval, expire_time, cb, None);
} }
pub fn set_interval_ex(&mut self, pub fn set_interval_ex
expire_time: i32, (&mut self, time: i32, cb: TimerCb, attacher: TimerAttacherRp) -> TimerWp {
cb: TimerCb, return self.internal_add(TimerType::Interval, time, cb, Some(attacher));
attacher: Rc::<RefCell::<TimerAttacher>>) -> TimerWp {
return self.internal_add(TimerType::Interval, expire_time, cb, Some(attacher));
} }
pub fn fire_event(&mut self, pub fn fire_event(&mut self,