ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.456 by root, Tue Sep 30 04:00:35 2008 UTC vs.
Revision 1.465 by root, Thu Jan 8 00:54:55 2009 UTC

32use Opcode; 32use Opcode;
33use Safe; 33use Safe;
34use Safe::Hole; 34use Safe::Hole;
35use Storable (); 35use Storable ();
36 36
37use Guard ();
37use Coro (); 38use Coro ();
38use Coro::State; 39use Coro::State;
39use Coro::Handle; 40use Coro::Handle;
40use Coro::EV; 41use Coro::EV;
41use Coro::AnyEvent; 42use Coro::AnyEvent;
42use Coro::Timer; 43use Coro::Timer;
43use Coro::Signal; 44use Coro::Signal;
44use Coro::Semaphore; 45use Coro::Semaphore;
46use Coro::SemaphoreSet;
45use Coro::AnyEvent; 47use Coro::AnyEvent;
46use Coro::AIO; 48use Coro::AIO;
47use Coro::BDB 1.6; 49use Coro::BDB 1.6;
48use Coro::Storable; 50use Coro::Storable;
49use Coro::Util (); 51use Coro::Util ();
130our $JITTER; # average jitter 132our $JITTER; # average jitter
131our $TICK_START; # for load detecting purposes 133our $TICK_START; # for load detecting purposes
132 134
133our @POST_INIT; 135our @POST_INIT;
134 136
137our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow)
138
135binmode STDOUT; 139binmode STDOUT;
136binmode STDERR; 140binmode STDERR;
137 141
138# read virtual server time, if available 142# read virtual server time, if available
139unless ($RUNTIME || !-e $RUNTIMEFILE) { 143unless ($RUNTIME || !-e $RUNTIMEFILE) {
328Wait until the given lock is available. See cf::lock_acquire. 332Wait until the given lock is available. See cf::lock_acquire.
329 333
330=item my $lock = cf::lock_acquire $string 334=item my $lock = cf::lock_acquire $string
331 335
332Wait until the given lock is available and then acquires it and returns 336Wait until the given lock is available and then acquires it and returns
333a Coro::guard object. If the guard object gets destroyed (goes out of scope, 337a L<Guard> object. If the guard object gets destroyed (goes out of scope,
334for example when the coroutine gets canceled), the lock is automatically 338for example when the coroutine gets canceled), the lock is automatically
335returned. 339returned.
336 340
337Locks are *not* recursive, locking from the same coro twice results in a 341Locks are *not* recursive, locking from the same coro twice results in a
338deadlocked coro. 342deadlocked coro.
344 348
345Return true if the lock is currently active, i.e. somebody has locked it. 349Return true if the lock is currently active, i.e. somebody has locked it.
346 350
347=cut 351=cut
348 352
349our %LOCK; 353our $LOCKS = new Coro::SemaphoreSet;
350our %LOCKER;#d#
351 354
352sub lock_wait($) { 355sub lock_wait($) {
353 my ($key) = @_; 356 $LOCKS->wait ($_[0]);
354
355 if ($LOCKER{$key} == $Coro::current) {#d#
356 Carp::cluck "lock_wait($key) for already-acquired lock";#d#
357 return;#d#
358 }#d#
359
360 # wait for lock, if any
361 while ($LOCK{$key}) {
362 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
363 push @{ $LOCK{$key} }, $Coro::current;
364 Coro::schedule;
365 }
366} 357}
367 358
368sub lock_acquire($) { 359sub lock_acquire($) {
369 my ($key) = @_; 360 $LOCKS->guard ($_[0])
370
371 # wait, to be sure we are not locked
372 lock_wait $key;
373
374 $LOCK{$key} = [];
375 $LOCKER{$key} = $Coro::current;#d#
376
377 Coro::guard {
378 delete $LOCKER{$key};#d#
379 # wake up all waiters, to be on the safe side
380 $_->ready for @{ delete $LOCK{$key} };
381 }
382} 361}
383 362
384sub lock_active($) { 363sub lock_active($) {
385 my ($key) = @_; 364 $LOCKS->count ($_[0]) < 1
386
387 ! ! $LOCK{$key}
388} 365}
389 366
390sub freeze_mainloop { 367sub freeze_mainloop {
391 tick_inhibit_inc; 368 tick_inhibit_inc;
392 369
393 Coro::guard \&tick_inhibit_dec; 370 &Guard::guard (\&tick_inhibit_dec);
394} 371}
395 372
396=item cf::periodic $interval, $cb 373=item cf::periodic $interval, $cb
397 374
398Like EV::periodic, but randomly selects a starting point so that the actions 375Like EV::periodic, but randomly selects a starting point so that the actions
1189 } 1166 }
1190 1167
1191 aio_rename "$filename~", $filename; 1168 aio_rename "$filename~", $filename;
1192 1169
1193 $filename =~ s%/[^/]+$%%; 1170 $filename =~ s%/[^/]+$%%;
1194 aio_pathsync $filename; 1171 aio_pathsync $filename if $cf::USE_FSYNC;
1195 } else { 1172 } else {
1196 warn "FATAL: $filename~: $!\n"; 1173 warn "unable to save objects: $filename~: $!\n";
1197 } 1174 }
1198 } else { 1175 } else {
1199 aio_unlink $filename; 1176 aio_unlink $filename;
1200 aio_unlink "$filename.pst"; 1177 aio_unlink "$filename.pst";
1201 } 1178 }
1292 my ($name, $cb) = @_; 1269 my ($name, $cb) = @_;
1293 1270
1294 $EXTICMD{$name} = $cb; 1271 $EXTICMD{$name} = $cb;
1295} 1272}
1296 1273
1274use File::Glob ();
1275
1297cf::player->attach ( 1276cf::player->attach (
1298 on_command => sub { 1277 on_command => sub {
1299 my ($pl, $name, $params) = @_; 1278 my ($pl, $name, $params) = @_;
1300 1279
1301 my $cb = $COMMAND{$name} 1280 my $cb = $COMMAND{$name}
1332 } 1311 }
1333 1312
1334 cf::override; 1313 cf::override;
1335 }, 1314 },
1336); 1315);
1316
1317# "readahead" all extensions
1318sub cache_extensions {
1319 my $grp = IO::AIO::aio_group;
1320
1321 add $grp IO::AIO::aio_readdir $LIBDIR, sub {
1322 for (grep /\.ext$/, @{$_[0]}) {
1323 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data;
1324 }
1325 };
1326
1327 $grp
1328}
1337 1329
1338sub load_extensions { 1330sub load_extensions {
1339 cf::sync_job { 1331 cf::sync_job {
1340 my %todo; 1332 my %todo;
1341 1333
1870 1862
1871 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; 1863 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go;
1872 "$UNIQUEDIR/$path" 1864 "$UNIQUEDIR/$path"
1873} 1865}
1874 1866
1875# and all this just because we cannot iterate over
1876# all maps in C++...
1877sub change_all_map_light {
1878 my ($change) = @_;
1879
1880 $_->change_map_light ($change)
1881 for grep $_->outdoor, values %cf::MAP;
1882}
1883
1884sub decay_objects { 1867sub decay_objects {
1885 my ($self) = @_; 1868 my ($self) = @_;
1886 1869
1887 return if $self->{deny_reset}; 1870 return if $self->{deny_reset};
1888 1871
1970sub find { 1953sub find {
1971 my ($path, $origin) = @_; 1954 my ($path, $origin) = @_;
1972 1955
1973 $path = normalise $path, $origin && $origin->path; 1956 $path = normalise $path, $origin && $origin->path;
1974 1957
1975 cf::lock_wait "map_data:$path";#d#remove 1958 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
1976 cf::lock_wait "map_find:$path"; 1959 my $guard2 = cf::lock_acquire "map_find:$path";
1977 1960
1978 $cf::MAP{$path} || do { 1961 $cf::MAP{$path} || do {
1979 my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it
1980 my $guard2 = cf::lock_acquire "map_find:$path";
1981
1982 my $map = new_from_path cf::map $path 1962 my $map = new_from_path cf::map $path
1983 or return; 1963 or return;
1984 1964
1985 $map->{last_save} = $cf::RUNTIME; 1965 $map->{last_save} = $cf::RUNTIME;
1986 1966
2053 2033
2054 unless ($self->{deny_activate}) { 2034 unless ($self->{deny_activate}) {
2055 $self->decay_objects; 2035 $self->decay_objects;
2056 $self->fix_auto_apply; 2036 $self->fix_auto_apply;
2057 $self->update_buttons; 2037 $self->update_buttons;
2058 cf::cede_to_tick;
2059 $self->set_darkness_map;
2060 cf::cede_to_tick; 2038 cf::cede_to_tick;
2061 $self->activate; 2039 $self->activate;
2062 } 2040 }
2063 2041
2064 $self->{last_save} = $cf::RUNTIME; 2042 $self->{last_save} = $cf::RUNTIME;
2791 id => "infobox", 2769 id => "infobox",
2792 title => "Body Parts", 2770 title => "Body Parts",
2793 reply => undef, 2771 reply => undef,
2794 tooltip => "Shows which body parts you posess and are available", 2772 tooltip => "Shows which body parts you posess and are available",
2795 }, 2773 },
2774 "c/statistics" => {
2775 id => "infobox",
2776 title => "Statistics",
2777 reply => undef,
2778 tooltip => "Shows your primary statistics",
2779 },
2796 "c/skills" => { 2780 "c/skills" => {
2797 id => "infobox", 2781 id => "infobox",
2798 title => "Skills", 2782 title => "Skills",
2799 reply => undef, 2783 reply => undef,
2800 tooltip => "Shows your experience per skill and item power", 2784 tooltip => "Shows your experience per skill and item power",
2785 },
2786 "c/resistances" => {
2787 id => "infobox",
2788 title => "Resistances",
2789 reply => undef,
2790 tooltip => "Shows your resistances",
2791 },
2792 "c/pets" => {
2793 id => "infobox",
2794 title => "Pets",
2795 reply => undef,
2796 tooltip => "Shows information abotu your pets/a specific pet",
2801 }, 2797 },
2802 "c/uptime" => { 2798 "c/uptime" => {
2803 id => "infobox", 2799 id => "infobox",
2804 title => "Uptime", 2800 title => "Uptime",
2805 reply => undef, 2801 reply => undef,
2815 id => "party", 2811 id => "party",
2816 title => "Party", 2812 title => "Party",
2817 reply => "gsay ", 2813 reply => "gsay ",
2818 tooltip => "Messages and chat related to your party", 2814 tooltip => "Messages and chat related to your party",
2819 }, 2815 },
2816 "c/death" => {
2817 id => "death",
2818 title => "Death",
2819 reply => undef,
2820 tooltip => "Reason for and more info about your most recent death",
2821 },
2822 "c/say" => $SAY_CHANNEL,
2823 "c/chat" => $CHAT_CHANNEL,
2820); 2824);
2821 2825
2822sub cf::client::send_msg { 2826sub cf::client::send_msg {
2823 my ($self, $channel, $msg, $color, @extra) = @_; 2827 my ($self, $channel, $msg, $color, @extra) = @_;
2824 2828
2829 2833
2830 # check predefined channels, for the benefit of C 2834 # check predefined channels, for the benefit of C
2831 if ($CHANNEL{$channel}) { 2835 if ($CHANNEL{$channel}) {
2832 $channel = $CHANNEL{$channel}; 2836 $channel = $CHANNEL{$channel};
2833 2837
2834 $self->ext_msg (channel_info => $channel) 2838 $self->ext_msg (channel_info => $channel);
2835 if $self->can_msg;
2836
2837 $channel = $channel->{id}; 2839 $channel = $channel->{id};
2838 2840
2839 } elsif (ref $channel) { 2841 } elsif (ref $channel) {
2840 # send meta info to client, if not yet sent 2842 # send meta info to client, if not yet sent
2841 unless (exists $self->{channel}{$channel->{id}}) { 2843 unless (exists $self->{channel}{$channel->{id}}) {
2842 $self->{channel}{$channel->{id}} = $channel; 2844 $self->{channel}{$channel->{id}} = $channel;
2843 $self->ext_msg (channel_info => $channel) 2845 $self->ext_msg (channel_info => $channel);
2844 if $self->can_msg;
2845 } 2846 }
2846 2847
2847 $channel = $channel->{id}; 2848 $channel = $channel->{id};
2848 } 2849 }
2849 2850
2850 return unless @extra || length $msg; 2851 return unless @extra || length $msg;
2851 2852
2852 if ($self->can_msg) {
2853 # default colour, mask it out 2853 # default colour, mask it out
2854 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) 2854 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF)
2855 if $color & cf::NDI_DEF; 2855 if $color & cf::NDI_DEF;
2856 2856
2857 my $pkt = "msg " 2857 my $pkt = "msg "
2858 . $self->{json_coder}->encode ( 2858 . $self->{json_coder}->encode (
2859 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2859 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2860 ); 2860 );
2861 2861
2862 # try lzf for large packets 2862 # try lzf for large packets
2863 $pkt = "lzf " . Compress::LZF::compress $pkt 2863 $pkt = "lzf " . Compress::LZF::compress $pkt
2864 if 1024 <= length $pkt and $self->{can_lzf}; 2864 if 1024 <= length $pkt and $self->{can_lzf};
2865 2865
2866 # split very large packets 2866 # split very large packets
2867 if (8192 < length $pkt and $self->{can_lzf}) { 2867 if (8192 < length $pkt and $self->{can_lzf}) {
2868 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; 2868 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2869 $pkt = "frag"; 2869 $pkt = "frag";
2870 } 2870 }
2871 2871
2872 $self->send_packet ($pkt); 2872 $self->send_packet ($pkt);
2873 } else {
2874 if ($color >= 0) {
2875 # replace some tags by gcfclient-compatible ones
2876 for ($msg) {
2877 1 while
2878 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2879 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/
2880 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/
2881 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/
2882 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/;
2883 }
2884
2885 $color &= cf::NDI_COLOR_MASK;
2886
2887 utf8::encode $msg;
2888
2889 if (0 && $msg =~ /\[/) {
2890 # COMMAND/INFO
2891 $self->send_packet ("drawextinfo $color 10 8 $msg")
2892 } else {
2893 $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g;
2894 $self->send_packet ("drawinfo $color $msg")
2895 }
2896 }
2897 }
2898} 2873}
2899 2874
2900=item $client->ext_msg ($type, @msg) 2875=item $client->ext_msg ($type, @msg)
2901 2876
2902Sends an ext event to the client. 2877Sends an ext event to the client.
3412 3387
3413 cf::init_experience; 3388 cf::init_experience;
3414 cf::init_anim; 3389 cf::init_anim;
3415 cf::init_attackmess; 3390 cf::init_attackmess;
3416 cf::init_dynamic; 3391 cf::init_dynamic;
3417 cf::init_block;
3418 3392
3419 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3393 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3420 3394
3421 # we must not ever block the main coroutine 3395 # we must not ever block the main coroutine
3422 local $Coro::idle = sub { 3396 local $Coro::idle = sub {
3558} 3532}
3559 3533
3560sub emergency_save() { 3534sub emergency_save() {
3561 my $freeze_guard = cf::freeze_mainloop; 3535 my $freeze_guard = cf::freeze_mainloop;
3562 3536
3563 warn "enter emergency perl save\n"; 3537 warn "emergency_perl_save: enter\n";
3564 3538
3565 cf::sync_job { 3539 cf::sync_job {
3540 # this is a trade-off: we want to be very quick here, so
3541 # save all maps without fsync, and later call a global sync
3542 # (which in turn might be very very slow)
3543 local $USE_FSYNC = 0;
3544
3566 # use a peculiar iteration method to avoid tripping on perl 3545 # use a peculiar iteration method to avoid tripping on perl
3567 # refcount bugs in for. also avoids problems with players 3546 # refcount bugs in for. also avoids problems with players
3568 # and maps saved/destroyed asynchronously. 3547 # and maps saved/destroyed asynchronously.
3569 warn "begin emergency player save\n"; 3548 warn "emergency_perl_save: begin player save\n";
3570 for my $login (keys %cf::PLAYER) { 3549 for my $login (keys %cf::PLAYER) {
3571 my $pl = $cf::PLAYER{$login} or next; 3550 my $pl = $cf::PLAYER{$login} or next;
3572 $pl->valid or next; 3551 $pl->valid or next;
3573 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3552 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3574 $pl->save; 3553 $pl->save;
3575 } 3554 }
3576 warn "end emergency player save\n"; 3555 warn "emergency_perl_save: end player save\n";
3577 3556
3578 warn "begin emergency map save\n"; 3557 warn "emergency_perl_save: begin map save\n";
3579 for my $path (keys %cf::MAP) { 3558 for my $path (keys %cf::MAP) {
3580 my $map = $cf::MAP{$path} or next; 3559 my $map = $cf::MAP{$path} or next;
3581 $map->valid or next; 3560 $map->valid or next;
3582 $map->save; 3561 $map->save;
3583 } 3562 }
3584 warn "end emergency map save\n"; 3563 warn "emergency_perl_save: end map save\n";
3585 3564
3586 warn "begin emergency database checkpoint\n"; 3565 warn "emergency_perl_save: begin database checkpoint\n";
3587 BDB::db_env_txn_checkpoint $DB_ENV; 3566 BDB::db_env_txn_checkpoint $DB_ENV;
3588 warn "end emergency database checkpoint\n"; 3567 warn "emergency_perl_save: end database checkpoint\n";
3589 3568
3590 warn "begin write uuid\n"; 3569 warn "emergency_perl_save: begin write uuid\n";
3591 write_uuid_sync 1; 3570 write_uuid_sync 1;
3592 warn "end write uuid\n"; 3571 warn "emergency_perl_save: end write uuid\n";
3593 }; 3572 };
3594 3573
3574 warn "emergency_perl_save: starting sync()\n";
3575 IO::AIO::aio_sync sub {
3576 warn "emergency_perl_save: finished sync()\n";
3577 };
3578
3595 warn "leave emergency perl save\n"; 3579 warn "emergency_perl_save: leave\n";
3596} 3580}
3597 3581
3598sub post_cleanup { 3582sub post_cleanup {
3599 my ($make_core) = @_; 3583 my ($make_core) = @_;
3600 3584
3637 warn "can only reload from main coroutine"; 3621 warn "can only reload from main coroutine";
3638 return; 3622 return;
3639 } 3623 }
3640 3624
3641 return if $RELOAD++; 3625 return if $RELOAD++;
3626
3627 my $t1 = EV::time;
3642 3628
3643 while ($RELOAD) { 3629 while ($RELOAD) {
3644 warn "reloading..."; 3630 warn "reloading...";
3645 3631
3646 warn "entering sync_job"; 3632 warn "entering sync_job";
3725 cf::reload_config; 3711 cf::reload_config;
3726 3712
3727 warn "loading extensions"; 3713 warn "loading extensions";
3728 cf::load_extensions; 3714 cf::load_extensions;
3729 3715
3716 if ($REATTACH_ON_RELOAD) {
3730 warn "reattaching attachments to objects/players"; 3717 warn "reattaching attachments to objects/players";
3731 _global_reattach; # objects, sockets 3718 _global_reattach; # objects, sockets
3732 warn "reattaching attachments to maps"; 3719 warn "reattaching attachments to maps";
3733 reattach $_ for values %MAP; 3720 reattach $_ for values %MAP;
3734 warn "reattaching attachments to players"; 3721 warn "reattaching attachments to players";
3735 reattach $_ for values %PLAYER; 3722 reattach $_ for values %PLAYER;
3723 }
3736 3724
3737 warn "running post_load"; 3725 warn "running post_init jobs";
3738 (pop @POST_INIT)->(1) while @POST_INIT; 3726 (pop @POST_INIT)->(1) while @POST_INIT;
3739 3727
3740 warn "leaving sync_job"; 3728 warn "leaving sync_job";
3741 3729
3742 1 3730 1
3746 }; 3734 };
3747 3735
3748 warn "reloaded"; 3736 warn "reloaded";
3749 --$RELOAD; 3737 --$RELOAD;
3750 } 3738 }
3739
3740 $t1 = EV::time - $t1;
3741 warn "reload completed in ${t1}s\n";
3751}; 3742};
3752 3743
3753our $RELOAD_WATCHER; # used only during reload 3744our $RELOAD_WATCHER; # used only during reload
3754 3745
3755sub reload_perl() { 3746sub reload_perl() {
3756 # doing reload synchronously and two reloads happen back-to-back, 3747 # doing reload synchronously and two reloads happen back-to-back,
3757 # coro crashes during coro_state_free->destroy here. 3748 # coro crashes during coro_state_free->destroy here.
3758 3749
3750 $RELOAD_WATCHER ||= cf::async {
3751 Coro::AIO::aio_wait cache_extensions;
3752
3759 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub { 3753 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub {
3760 do_reload_perl; 3754 do_reload_perl;
3761 undef $RELOAD_WATCHER; 3755 undef $RELOAD_WATCHER;
3756 };
3762 }; 3757 };
3763} 3758}
3764 3759
3765register_command "reload" => sub { 3760register_command "reload" => sub {
3766 my ($who, $arg) = @_; 3761 my ($who, $arg) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines