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.453 by root, Tue Sep 23 04:29:11 2008 UTC vs.
Revision 1.463 by root, Sun Dec 28 06:59:27 2008 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
1187 } else { 1164 } else {
1188 aio_unlink "$filename.pst"; 1165 aio_unlink "$filename.pst";
1189 } 1166 }
1190 1167
1191 aio_rename "$filename~", $filename; 1168 aio_rename "$filename~", $filename;
1169
1170 $filename =~ s%/[^/]+$%%;
1171 aio_pathsync $filename if $cf::USE_FSYNC;
1192 } else { 1172 } else {
1193 warn "FATAL: $filename~: $!\n"; 1173 warn "unable to save objects: $filename~: $!\n";
1194 } 1174 }
1195 } else { 1175 } else {
1196 aio_unlink $filename; 1176 aio_unlink $filename;
1197 aio_unlink "$filename.pst"; 1177 aio_unlink "$filename.pst";
1198 } 1178 }
1289 my ($name, $cb) = @_; 1269 my ($name, $cb) = @_;
1290 1270
1291 $EXTICMD{$name} = $cb; 1271 $EXTICMD{$name} = $cb;
1292} 1272}
1293 1273
1274use File::Glob ();
1275
1294cf::player->attach ( 1276cf::player->attach (
1295 on_command => sub { 1277 on_command => sub {
1296 my ($pl, $name, $params) = @_; 1278 my ($pl, $name, $params) = @_;
1297 1279
1298 my $cb = $COMMAND{$name} 1280 my $cb = $COMMAND{$name}
1329 } 1311 }
1330 1312
1331 cf::override; 1313 cf::override;
1332 }, 1314 },
1333); 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}
1334 1329
1335sub load_extensions { 1330sub load_extensions {
1336 cf::sync_job { 1331 cf::sync_job {
1337 my %todo; 1332 my %todo;
1338 1333
1867 1862
1868 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; 1863 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go;
1869 "$UNIQUEDIR/$path" 1864 "$UNIQUEDIR/$path"
1870} 1865}
1871 1866
1872# and all this just because we cannot iterate over
1873# all maps in C++...
1874sub change_all_map_light {
1875 my ($change) = @_;
1876
1877 $_->change_map_light ($change)
1878 for grep $_->outdoor, values %cf::MAP;
1879}
1880
1881sub decay_objects { 1867sub decay_objects {
1882 my ($self) = @_; 1868 my ($self) = @_;
1883 1869
1884 return if $self->{deny_reset}; 1870 return if $self->{deny_reset};
1885 1871
1967sub find { 1953sub find {
1968 my ($path, $origin) = @_; 1954 my ($path, $origin) = @_;
1969 1955
1970 $path = normalise $path, $origin && $origin->path; 1956 $path = normalise $path, $origin && $origin->path;
1971 1957
1972 cf::lock_wait "map_data:$path";#d#remove 1958 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
1973 cf::lock_wait "map_find:$path"; 1959 my $guard2 = cf::lock_acquire "map_find:$path";
1974 1960
1975 $cf::MAP{$path} || do { 1961 $cf::MAP{$path} || do {
1976 my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it
1977 my $guard2 = cf::lock_acquire "map_find:$path";
1978
1979 my $map = new_from_path cf::map $path 1962 my $map = new_from_path cf::map $path
1980 or return; 1963 or return;
1981 1964
1982 $map->{last_save} = $cf::RUNTIME; 1965 $map->{last_save} = $cf::RUNTIME;
1983 1966
2050 2033
2051 unless ($self->{deny_activate}) { 2034 unless ($self->{deny_activate}) {
2052 $self->decay_objects; 2035 $self->decay_objects;
2053 $self->fix_auto_apply; 2036 $self->fix_auto_apply;
2054 $self->update_buttons; 2037 $self->update_buttons;
2055 cf::cede_to_tick;
2056 $self->set_darkness_map;
2057 cf::cede_to_tick; 2038 cf::cede_to_tick;
2058 $self->activate; 2039 $self->activate;
2059 } 2040 }
2060 2041
2061 $self->{last_save} = $cf::RUNTIME; 2042 $self->{last_save} = $cf::RUNTIME;
2812 id => "party", 2793 id => "party",
2813 title => "Party", 2794 title => "Party",
2814 reply => "gsay ", 2795 reply => "gsay ",
2815 tooltip => "Messages and chat related to your party", 2796 tooltip => "Messages and chat related to your party",
2816 }, 2797 },
2798 "c/say" => $SAY_CHANNEL,
2799 "c/chat" => $CHAT_CHANNEL,
2817); 2800);
2818 2801
2819sub cf::client::send_msg { 2802sub cf::client::send_msg {
2820 my ($self, $channel, $msg, $color, @extra) = @_; 2803 my ($self, $channel, $msg, $color, @extra) = @_;
2821 2804
2826 2809
2827 # check predefined channels, for the benefit of C 2810 # check predefined channels, for the benefit of C
2828 if ($CHANNEL{$channel}) { 2811 if ($CHANNEL{$channel}) {
2829 $channel = $CHANNEL{$channel}; 2812 $channel = $CHANNEL{$channel};
2830 2813
2831 $self->ext_msg (channel_info => $channel) 2814 $self->ext_msg (channel_info => $channel);
2832 if $self->can_msg;
2833
2834 $channel = $channel->{id}; 2815 $channel = $channel->{id};
2835 2816
2836 } elsif (ref $channel) { 2817 } elsif (ref $channel) {
2837 # send meta info to client, if not yet sent 2818 # send meta info to client, if not yet sent
2838 unless (exists $self->{channel}{$channel->{id}}) { 2819 unless (exists $self->{channel}{$channel->{id}}) {
2839 $self->{channel}{$channel->{id}} = $channel; 2820 $self->{channel}{$channel->{id}} = $channel;
2840 $self->ext_msg (channel_info => $channel) 2821 $self->ext_msg (channel_info => $channel);
2841 if $self->can_msg;
2842 } 2822 }
2843 2823
2844 $channel = $channel->{id}; 2824 $channel = $channel->{id};
2845 } 2825 }
2846 2826
2847 return unless @extra || length $msg; 2827 return unless @extra || length $msg;
2848 2828
2849 if ($self->can_msg) {
2850 # default colour, mask it out 2829 # default colour, mask it out
2851 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) 2830 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF)
2852 if $color & cf::NDI_DEF; 2831 if $color & cf::NDI_DEF;
2853 2832
2854 my $pkt = "msg " 2833 my $pkt = "msg "
2855 . $self->{json_coder}->encode ( 2834 . $self->{json_coder}->encode (
2856 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2835 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2857 ); 2836 );
2858 2837
2859 # try lzf for large packets 2838 # try lzf for large packets
2860 $pkt = "lzf " . Compress::LZF::compress $pkt 2839 $pkt = "lzf " . Compress::LZF::compress $pkt
2861 if 1024 <= length $pkt and $self->{can_lzf}; 2840 if 1024 <= length $pkt and $self->{can_lzf};
2862 2841
2863 # split very large packets 2842 # split very large packets
2864 if (8192 < length $pkt and $self->{can_lzf}) { 2843 if (8192 < length $pkt and $self->{can_lzf}) {
2865 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; 2844 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2866 $pkt = "frag"; 2845 $pkt = "frag";
2867 } 2846 }
2868 2847
2869 $self->send_packet ($pkt); 2848 $self->send_packet ($pkt);
2870 } else {
2871 if ($color >= 0) {
2872 # replace some tags by gcfclient-compatible ones
2873 for ($msg) {
2874 1 while
2875 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2876 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/
2877 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/
2878 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/
2879 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/;
2880 }
2881
2882 $color &= cf::NDI_COLOR_MASK;
2883
2884 utf8::encode $msg;
2885
2886 if (0 && $msg =~ /\[/) {
2887 # COMMAND/INFO
2888 $self->send_packet ("drawextinfo $color 10 8 $msg")
2889 } else {
2890 $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g;
2891 $self->send_packet ("drawinfo $color $msg")
2892 }
2893 }
2894 }
2895} 2849}
2896 2850
2897=item $client->ext_msg ($type, @msg) 2851=item $client->ext_msg ($type, @msg)
2898 2852
2899Sends an ext event to the client. 2853Sends an ext event to the client.
3409 3363
3410 cf::init_experience; 3364 cf::init_experience;
3411 cf::init_anim; 3365 cf::init_anim;
3412 cf::init_attackmess; 3366 cf::init_attackmess;
3413 cf::init_dynamic; 3367 cf::init_dynamic;
3414 cf::init_block;
3415 3368
3416 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3369 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3417 3370
3418 # we must not ever block the main coroutine 3371 # we must not ever block the main coroutine
3419 local $Coro::idle = sub { 3372 local $Coro::idle = sub {
3516 my $uuid = "$LOCALDIR/uuid"; 3469 my $uuid = "$LOCALDIR/uuid";
3517 3470
3518 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 3471 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3519 or return; 3472 or return;
3520 3473
3521 my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; 3474 my $value = uuid_seq uuid_cur;
3475
3476 unless ($value) {
3477 warn "cowardly refusing to write zero uuid value!\n";
3478 return;
3479 }
3480
3481 my $value = uuid_str $value + $uuid_skip;
3522 $uuid_skip = 0; 3482 $uuid_skip = 0;
3523 3483
3524 (aio_write $fh, 0, (length $value), $value, 0) <= 0 3484 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3525 and return; 3485 and return;
3526 3486
3548} 3508}
3549 3509
3550sub emergency_save() { 3510sub emergency_save() {
3551 my $freeze_guard = cf::freeze_mainloop; 3511 my $freeze_guard = cf::freeze_mainloop;
3552 3512
3553 warn "enter emergency perl save\n"; 3513 warn "emergency_perl_save: enter\n";
3554 3514
3555 cf::sync_job { 3515 cf::sync_job {
3516 # this is a trade-off: we want to be very quick here, so
3517 # save all maps without fsync, and later call a global sync
3518 # (which in turn might be very very slow)
3519 local $USE_FSYNC = 0;
3520
3556 # use a peculiar iteration method to avoid tripping on perl 3521 # use a peculiar iteration method to avoid tripping on perl
3557 # refcount bugs in for. also avoids problems with players 3522 # refcount bugs in for. also avoids problems with players
3558 # and maps saved/destroyed asynchronously. 3523 # and maps saved/destroyed asynchronously.
3559 warn "begin emergency player save\n"; 3524 warn "emergency_perl_save: begin player save\n";
3560 for my $login (keys %cf::PLAYER) { 3525 for my $login (keys %cf::PLAYER) {
3561 my $pl = $cf::PLAYER{$login} or next; 3526 my $pl = $cf::PLAYER{$login} or next;
3562 $pl->valid or next; 3527 $pl->valid or next;
3563 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3528 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3564 $pl->save; 3529 $pl->save;
3565 } 3530 }
3566 warn "end emergency player save\n"; 3531 warn "emergency_perl_save: end player save\n";
3567 3532
3568 warn "begin emergency map save\n"; 3533 warn "emergency_perl_save: begin map save\n";
3569 for my $path (keys %cf::MAP) { 3534 for my $path (keys %cf::MAP) {
3570 my $map = $cf::MAP{$path} or next; 3535 my $map = $cf::MAP{$path} or next;
3571 $map->valid or next; 3536 $map->valid or next;
3572 $map->save; 3537 $map->save;
3573 } 3538 }
3574 warn "end emergency map save\n"; 3539 warn "emergency_perl_save: end map save\n";
3575 3540
3576 warn "begin emergency database checkpoint\n"; 3541 warn "emergency_perl_save: begin database checkpoint\n";
3577 BDB::db_env_txn_checkpoint $DB_ENV; 3542 BDB::db_env_txn_checkpoint $DB_ENV;
3578 warn "end emergency database checkpoint\n"; 3543 warn "emergency_perl_save: end database checkpoint\n";
3579 3544
3580 warn "begin write uuid\n"; 3545 warn "emergency_perl_save: begin write uuid\n";
3581 write_uuid_sync 1; 3546 write_uuid_sync 1;
3582 warn "end write uuid\n"; 3547 warn "emergency_perl_save: end write uuid\n";
3583 }; 3548 };
3584 3549
3550 warn "emergency_perl_save: starting sync()\n";
3551 IO::AIO::aio_sync sub {
3552 warn "emergency_perl_save: finished sync()\n";
3553 };
3554
3585 warn "leave emergency perl save\n"; 3555 warn "emergency_perl_save: leave\n";
3586} 3556}
3587 3557
3588sub post_cleanup { 3558sub post_cleanup {
3589 my ($make_core) = @_; 3559 my ($make_core) = @_;
3590 3560
3627 warn "can only reload from main coroutine"; 3597 warn "can only reload from main coroutine";
3628 return; 3598 return;
3629 } 3599 }
3630 3600
3631 return if $RELOAD++; 3601 return if $RELOAD++;
3602
3603 my $t1 = EV::time;
3632 3604
3633 while ($RELOAD) { 3605 while ($RELOAD) {
3634 warn "reloading..."; 3606 warn "reloading...";
3635 3607
3636 warn "entering sync_job"; 3608 warn "entering sync_job";
3715 cf::reload_config; 3687 cf::reload_config;
3716 3688
3717 warn "loading extensions"; 3689 warn "loading extensions";
3718 cf::load_extensions; 3690 cf::load_extensions;
3719 3691
3692 if ($REATTACH_ON_RELOAD) {
3720 warn "reattaching attachments to objects/players"; 3693 warn "reattaching attachments to objects/players";
3721 _global_reattach; # objects, sockets 3694 _global_reattach; # objects, sockets
3722 warn "reattaching attachments to maps"; 3695 warn "reattaching attachments to maps";
3723 reattach $_ for values %MAP; 3696 reattach $_ for values %MAP;
3724 warn "reattaching attachments to players"; 3697 warn "reattaching attachments to players";
3725 reattach $_ for values %PLAYER; 3698 reattach $_ for values %PLAYER;
3699 }
3726 3700
3727 warn "running post_load"; 3701 warn "running post_init jobs";
3728 (pop @POST_INIT)->(1) while @POST_INIT; 3702 (pop @POST_INIT)->(1) while @POST_INIT;
3729 3703
3730 warn "leaving sync_job"; 3704 warn "leaving sync_job";
3731 3705
3732 1 3706 1
3736 }; 3710 };
3737 3711
3738 warn "reloaded"; 3712 warn "reloaded";
3739 --$RELOAD; 3713 --$RELOAD;
3740 } 3714 }
3715
3716 $t1 = EV::time - $t1;
3717 warn "reload completed in ${t1}s\n";
3741}; 3718};
3742 3719
3743our $RELOAD_WATCHER; # used only during reload 3720our $RELOAD_WATCHER; # used only during reload
3744 3721
3745sub reload_perl() { 3722sub reload_perl() {
3746 # doing reload synchronously and two reloads happen back-to-back, 3723 # doing reload synchronously and two reloads happen back-to-back,
3747 # coro crashes during coro_state_free->destroy here. 3724 # coro crashes during coro_state_free->destroy here.
3748 3725
3726 $RELOAD_WATCHER ||= cf::async {
3727 Coro::AIO::aio_wait cache_extensions;
3728
3749 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub { 3729 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub {
3750 do_reload_perl; 3730 do_reload_perl;
3751 undef $RELOAD_WATCHER; 3731 undef $RELOAD_WATCHER;
3732 };
3752 }; 3733 };
3753} 3734}
3754 3735
3755register_command "reload" => sub { 3736register_command "reload" => sub {
3756 my ($who, $arg) = @_; 3737 my ($who, $arg) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines