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.374 by root, Wed Sep 19 22:07:16 2007 UTC vs.
Revision 1.388 by root, Thu Oct 18 02:56:13 2007 UTC

4use strict; 4use strict;
5 5
6use Symbol; 6use Symbol;
7use List::Util; 7use List::Util;
8use Socket; 8use Socket;
9use Storable;
10use Event; 9use Event;
11use Opcode; 10use Opcode;
12use Safe; 11use Safe;
13use Safe::Hole; 12use Safe::Hole;
13use Storable ();
14 14
15use Coro 3.64 (); 15use Coro 4.1 ();
16use Coro::State; 16use Coro::State;
17use Coro::Handle; 17use Coro::Handle;
18use Coro::Event; 18use Coro::Event;
19use Coro::Timer; 19use Coro::Timer;
20use Coro::Signal; 20use Coro::Signal;
27use BDB (); 27use BDB ();
28use Data::Dumper; 28use Data::Dumper;
29use Digest::MD5; 29use Digest::MD5;
30use Fcntl; 30use Fcntl;
31use YAML::Syck (); 31use YAML::Syck ();
32use IO::AIO 2.32 (); 32use IO::AIO 2.51 ();
33use Time::HiRes; 33use Time::HiRes;
34use Compress::LZF; 34use Compress::LZF;
35use Digest::MD5 (); 35use Digest::MD5 ();
36 36
37# configure various modules to our taste 37# configure various modules to our taste
373 } 373 }
374 } 374 }
375 } 375 }
376 376
377 if (@SLOT_QUEUE) { 377 if (@SLOT_QUEUE) {
378 # we do not use wait_For_tick() as it returns immediately when tick is inactive 378 # we do not use wait_for_tick() as it returns immediately when tick is inactive
379 push @cf::WAIT_FOR_TICK, $signal; 379 push @cf::WAIT_FOR_TICK, $signal;
380 $signal->wait; 380 $signal->wait;
381 } else { 381 } else {
382 Coro::schedule; 382 Coro::schedule;
383 } 383 }
427 my $time = Event::time; 427 my $time = Event::time;
428 428
429 # this is the main coro, too bad, we have to block 429 # this is the main coro, too bad, we have to block
430 # till the operation succeeds, freezing the server :/ 430 # till the operation succeeds, freezing the server :/
431 431
432 LOG llevError, Carp::longmess "sync job";#d#
433
432 # TODO: use suspend/resume instead 434 # TODO: use suspend/resume instead
433 # (but this is cancel-safe) 435 # (but this is cancel-safe)
434 my $freeze_guard = freeze_mainloop; 436 my $freeze_guard = freeze_mainloop;
435 437
436 my $busy = 1; 438 my $busy = 1;
442 warn $@ if $@; 444 warn $@ if $@;
443 undef $busy; 445 undef $busy;
444 })->prio (Coro::PRIO_MAX); 446 })->prio (Coro::PRIO_MAX);
445 447
446 while ($busy) { 448 while ($busy) {
449 if (Coro::nready) {
450 Coro::cede_notself;
451 } else {
447 Coro::cede or Event::one_event; 452 Event::one_event;
453 }
448 } 454 }
449 455
450 $time = Event::time - $time; 456 $time = Event::time - $time;
451 457
452 LOG llevError | logBacktrace, Carp::longmess "long sync job" 458 LOG llevError | logBacktrace, Carp::longmess "long sync job"
964 970
965=cut 971=cut
966 972
967############################################################################# 973#############################################################################
968# object support 974# object support
969#
970 975
976sub _object_equal($$);
977sub _object_equal($$) {
978 my ($a, $b) = @_;
979
980 return 0 unless (ref $a) eq (ref $b);
981
982 if ("HASH" eq ref $a) {
983 my @ka = keys %$a;
984 my @kb = keys %$b;
985
986 return 0 if @ka != @kb;
987
988 for (0 .. $#ka) {
989 return 0 unless $ka[$_] eq $kb[$_];
990 return 0 unless _object_equal $a->{$ka[$_]}, $b->{$kb[$_]};
991 }
992
993 } elsif ("ARRAY" eq ref $a) {
994
995 return 0 if @$a != @$b;
996
997 for (0 .. $#$a) {
998 return 0 unless _object_equal $a->[$_], $b->[$_];
999 }
1000
1001 } elsif ($a ne $b) {
1002 return 0;
1003 }
1004
1005 1
1006}
1007
1008our $SLOW_MERGES;#d#
971sub _can_merge { 1009sub _can_merge {
972 my ($ob1, $ob2) = @_; 1010 my ($ob1, $ob2) = @_;
973 1011
974 local $Storable::canonical = 1; 1012 ++$SLOW_MERGES;#d#
975 my $fob1 = Storable::freeze $ob1;
976 my $fob2 = Storable::freeze $ob2;
977 1013
978 $fob1 eq $fob2 1014 # we do the slow way here
1015 return _object_equal $ob1, $ob2
979} 1016}
980 1017
981sub reattach { 1018sub reattach {
982 # basically do the same as instantiate, without calling instantiate 1019 # basically do the same as instantiate, without calling instantiate
983 my ($obj) = @_; 1020 my ($obj) = @_;
1044 close $fh; 1081 close $fh;
1045 1082
1046 if (@$objs) { 1083 if (@$objs) {
1047 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { 1084 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) {
1048 chmod SAVE_MODE, $fh; 1085 chmod SAVE_MODE, $fh;
1049 my $data = Storable::nfreeze { version => 1, objs => $objs }; 1086 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs };
1050 aio_write $fh, 0, (length $data), $data, 0; 1087 aio_write $fh, 0, (length $data), $data, 0;
1051 aio_fsync $fh if $cf::USE_FSYNC; 1088 aio_fsync $fh if $cf::USE_FSYNC;
1052 close $fh; 1089 close $fh;
1053 aio_rename "$filename.pst~", "$filename.pst"; 1090 aio_rename "$filename.pst~", "$filename.pst";
1054 } 1091 }
1085 1122
1086 unless (aio_stat "$filename.pst") { 1123 unless (aio_stat "$filename.pst") {
1087 (aio_load "$filename.pst", $av) >= 0 1124 (aio_load "$filename.pst", $av) >= 0
1088 or return; 1125 or return;
1089 1126
1090 $av = eval { (Storable::thaw $av)->{objs} }; 1127 my $st = eval { Coro::Storable::thaw $av };
1128 $av = $st->{objs};
1091 } 1129 }
1092 1130
1093 utf8::decode (my $decname = $filename); 1131 utf8::decode (my $decname = $filename);
1094 warn sprintf "loading %s (%d,%d)\n", 1132 warn sprintf "loading %s (%d,%d)\n",
1095 $decname, length $data, scalar @{$av || []}; 1133 $decname, length $data, scalar @{$av || []};
2273=cut 2311=cut
2274 2312
2275sub deref { 2313sub deref {
2276 my ($ref) = @_; 2314 my ($ref) = @_;
2277 2315
2278 # temporary compatibility#TODO#remove
2279 $ref =~ s{^<}{player/<};
2280
2281 if ($ref =~ m{^player\/(<1\.\d+>)/(.*)$}) { 2316 if ($ref =~ m{^player\/(<1\.[0-9a-f]+>)/(.*)$}) {
2282 my ($uuid, $name) = ($1, $2); 2317 my ($uuid, $name) = ($1, $2);
2283 my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name 2318 my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name
2284 or return; 2319 or return;
2285 $pl->ob->uuid eq $uuid 2320 $pl->ob->uuid eq $uuid
2286 or return; 2321 or return;
2633 2668
2634=cut 2669=cut
2635 2670
2636our %CHANNEL = ( 2671our %CHANNEL = (
2637 "c/identify" => { 2672 "c/identify" => {
2638 id => "identify", 2673 id => "infobox",
2639 title => "Identify", 2674 title => "Identify",
2640 reply => undef, 2675 reply => undef,
2641 tooltip => "Items recently identified", 2676 tooltip => "Items recently identified",
2642 }, 2677 },
2643 "c/examine" => { 2678 "c/examine" => {
2644 id => "examine", 2679 id => "infobox",
2645 title => "Examine", 2680 title => "Examine",
2646 reply => undef, 2681 reply => undef,
2647 tooltip => "Signs and other items you examined", 2682 tooltip => "Signs and other items you examined",
2648 }, 2683 },
2684 "c/lookat" => {
2685 id => "infobox",
2686 title => "Look",
2687 reply => undef,
2688 tooltip => "What you saw there",
2689 },
2649); 2690);
2650 2691
2651sub cf::client::send_msg { 2692sub cf::client::send_msg {
2652 my ($self, $channel, $msg, $color, @extra) = @_; 2693 my ($self, $channel, $msg, $color, @extra) = @_;
2653 2694
2654 $msg = $self->pl->expand_cfpod ($msg); 2695 $msg = $self->pl->expand_cfpod ($msg);
2655 2696
2656 $color &= cf::NDI_CLIENT_MASK; # just in case... 2697 $color &= cf::NDI_CLIENT_MASK; # just in case...
2657 2698
2658 # check predefined channels, for the benefit of C 2699 # check predefined channels, for the benefit of C
2659 $channel = $CHANNEL{$channel} if $CHANNEL{$channel}; 2700 if ($CHANNEL{$channel}) {
2701 $channel = $CHANNEL{$channel};
2660 2702
2703 $self->ext_msg (channel_info => $channel)
2704 if $self->can_msg;
2705
2706 $channel = $channel->{id};
2707
2661 if (ref $channel) { 2708 } elsif (ref $channel) {
2662 # send meta info to client, if not yet sent 2709 # send meta info to client, if not yet sent
2663 unless (exists $self->{channel}{$channel->{id}}) { 2710 unless (exists $self->{channel}{$channel->{id}}) {
2664 $self->{channel}{$channel->{id}} = $channel; 2711 $self->{channel}{$channel->{id}} = $channel;
2665 $self->ext_msg (channel_info => $channel) 2712 $self->ext_msg (channel_info => $channel)
2666 if $self->can_msg; 2713 if $self->can_msg;
2883=pod 2930=pod
2884 2931
2885The following functions and methods are available within a safe environment: 2932The following functions and methods are available within a safe environment:
2886 2933
2887 cf::object 2934 cf::object
2888 contr pay_amount pay_player map x y force_find force_add 2935 contr pay_amount pay_player map x y force_find force_add destroy
2889 insert remove name archname title slaying race decrease_ob_nr 2936 insert remove name archname title slaying race decrease_ob_nr
2890 2937
2891 cf::object::player 2938 cf::object::player
2892 player 2939 player
2893 2940
2900=cut 2947=cut
2901 2948
2902for ( 2949for (
2903 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 2950 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
2904 insert remove inv name archname title slaying race 2951 insert remove inv name archname title slaying race
2905 decrease_ob_nr)], 2952 decrease_ob_nr destroy)],
2906 ["cf::object::player" => qw(player)], 2953 ["cf::object::player" => qw(player)],
2907 ["cf::player" => qw(peaceful)], 2954 ["cf::player" => qw(peaceful)],
2908 ["cf::map" => qw(trigger)], 2955 ["cf::map" => qw(trigger)],
2909) { 2956) {
2910 no strict 'refs'; 2957 no strict 'refs';
3267 # and maps saved/destroyed asynchronously. 3314 # and maps saved/destroyed asynchronously.
3268 warn "begin emergency player save\n"; 3315 warn "begin emergency player save\n";
3269 for my $login (keys %cf::PLAYER) { 3316 for my $login (keys %cf::PLAYER) {
3270 my $pl = $cf::PLAYER{$login} or next; 3317 my $pl = $cf::PLAYER{$login} or next;
3271 $pl->valid or next; 3318 $pl->valid or next;
3319 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3272 $pl->save; 3320 $pl->save;
3273 } 3321 }
3274 warn "end emergency player save\n"; 3322 warn "end emergency player save\n";
3275 3323
3276 warn "begin emergency map save\n"; 3324 warn "begin emergency map save\n";
3324 3372
3325 warn "flushing outstanding aio requests"; 3373 warn "flushing outstanding aio requests";
3326 for (;;) { 3374 for (;;) {
3327 BDB::flush; 3375 BDB::flush;
3328 IO::AIO::flush; 3376 IO::AIO::flush;
3329 Coro::cede; 3377 Coro::cede_notself;
3330 last unless IO::AIO::nreqs || BDB::nreqs; 3378 last unless IO::AIO::nreqs || BDB::nreqs;
3331 warn "iterate..."; 3379 warn "iterate...";
3332 } 3380 }
3333 3381
3334 ++$RELOAD; 3382 ++$RELOAD;
3614 $AIO_POLL_WATCHER = Event->io ( 3662 $AIO_POLL_WATCHER = Event->io (
3615 reentrant => 0, 3663 reentrant => 0,
3616 data => WF_AUTOCANCEL, 3664 data => WF_AUTOCANCEL,
3617 fd => IO::AIO::poll_fileno, 3665 fd => IO::AIO::poll_fileno,
3618 poll => 'r', 3666 poll => 'r',
3619 prio => 6, 3667 prio => 0,
3620 cb => \&IO::AIO::poll_cb, 3668 cb => \&IO::AIO::poll_cb,
3621 ); 3669 );
3622} 3670}
3623 3671
3624my $_log_backtrace; 3672my $_log_backtrace;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines