ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/player-scheduler.ext
Revision: 1.2
Committed: Tue Sep 30 10:09:51 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-2_80, rel-2_72, rel-2_73, rel-2_79, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_82, rel-2_81, rel-2_78
Changes since 1.1: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #! perl # mandatory
2    
3     # player scheduler, evoking players from ram
4    
5     our $SCHEDULE_INTERVAL = $cf::CFG{player_schedule_interval} || 10; # time the player scheduler sleeps between runs
6     our $SAVE_TIMEOUT = $cf::CFG{player_save_interval} || 20; # save players every n seconds
7    
8     our $SCHEDULER = cf::async_ext {
9     $Coro::current->{desc} = "player scheduler";
10    
11     while () {
12     Coro::EV::timer_once $SCHEDULE_INTERVAL;
13    
14     # this weird form of iteration over values is used because
15     # the hash changes underneath us frequently, and for
16     # keeps a direct reference to the value without (in 5.8 perls)
17     # keeping a reference, so this is prone to crashes or worse.
18     my @players = keys %cf::PLAYER;
19     for (@players) {
20     my $pl = $cf::PLAYER{$_}
21     or next;
22     $pl->valid or next;
23    
24     eval {
25     if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
26     $pl->save;
27    
28     unless ($pl->active || $pl->ns) {
29     # check refcounts, this is tricky and needs to be adjusted to fit server internals
30     my $ob = $pl->ob;
31    
32     my $pl_ref = $pl->refcnt_cnt;
33     my $ob_ref = $ob->refcnt_cnt;
34    
35 root 1.2 ## pl_ref == (P)$pl + (P)%cf::PLAYER + (C)ob->contr
36     ## ob_ref == (P)$ob + (C)pl->observe + (C)simply being an object
37 root 1.1 if ($pl_ref == 3 && $ob_ref == 3) {
38     warn "player-scheduler destroy ", $ob->name;#d#
39    
40     # remove from sight and get fresh "copies"
41     $pl = delete $cf::PLAYER{$ob->name};
42     $ob = $pl->ob;
43    
44     $pl->destroy; # destroys $ob
45     } else {
46     my $a_ = $pl->refcnt;#d#
47     my $b_ = $ob->refcnt;#d#
48    
49     warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref/3 ob $ob_ref/3 (C pl $a_/1 ob $b_/2)\n";#d#
50     }
51     }
52     }
53     };
54     warn $@ if $@;
55     cf::cede_to_tick;
56     };
57     }
58     };
59    
60     $SCHEDULER->prio (1);
61