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.376 by root, Mon Oct 1 00:44:44 2007 UTC vs.
Revision 1.386 by root, Mon Oct 15 17:50:27 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;
964 966
965=cut 967=cut
966 968
967############################################################################# 969#############################################################################
968# object support 970# object support
969#
970 971
972sub _object_equal($$);
973sub _object_equal($$) {
974 my ($a, $b) = @_;
975
976 return 0 unless (ref $a) eq (ref $b);
977
978 if ("HASH" eq ref $a) {
979 my @ka = keys %$a;
980 my @kb = keys %$b;
981
982 return 0 if @ka != @kb;
983
984 for (0 .. $#ka) {
985 return 0 unless $ka[$_] eq $kb[$_];
986 return 0 unless _object_equal $a->{$ka[$_]}, $b->{$kb[$_]};
987 }
988
989 } elsif ("ARRAY" eq ref $a) {
990
991 return 0 if @$a != @$b;
992
993 for (0 .. $#$a) {
994 return 0 unless _object_equal $a->[$_], $b->[$_];
995 }
996
997 } elsif ($a ne $b) {
998 return 0;
999 }
1000
1001 1
1002}
1003
1004our $SLOW_MERGES;#d#
971sub _can_merge { 1005sub _can_merge {
972 my ($ob1, $ob2) = @_; 1006 my ($ob1, $ob2) = @_;
973 1007
974 local $Storable::canonical = 1; 1008 ++$SLOW_MERGES;#d#
975 my $fob1 = Storable::freeze $ob1;
976 my $fob2 = Storable::freeze $ob2;
977 1009
978 $fob1 eq $fob2 1010 # we do the slow way here
1011 return _object_equal $ob1, $ob2
979} 1012}
980 1013
981sub reattach { 1014sub reattach {
982 # basically do the same as instantiate, without calling instantiate 1015 # basically do the same as instantiate, without calling instantiate
983 my ($obj) = @_; 1016 my ($obj) = @_;
1044 close $fh; 1077 close $fh;
1045 1078
1046 if (@$objs) { 1079 if (@$objs) {
1047 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { 1080 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) {
1048 chmod SAVE_MODE, $fh; 1081 chmod SAVE_MODE, $fh;
1049 my $data = Storable::nfreeze { version => 1, objs => $objs }; 1082 my $data = Coro::Storable::blocking_nfreeze { version => 1, objs => $objs };
1050 aio_write $fh, 0, (length $data), $data, 0; 1083 aio_write $fh, 0, (length $data), $data, 0;
1051 aio_fsync $fh if $cf::USE_FSYNC; 1084 aio_fsync $fh if $cf::USE_FSYNC;
1052 close $fh; 1085 close $fh;
1053 aio_rename "$filename.pst~", "$filename.pst"; 1086 aio_rename "$filename.pst~", "$filename.pst";
1054 } 1087 }
1085 1118
1086 unless (aio_stat "$filename.pst") { 1119 unless (aio_stat "$filename.pst") {
1087 (aio_load "$filename.pst", $av) >= 0 1120 (aio_load "$filename.pst", $av) >= 0
1088 or return; 1121 or return;
1089 1122
1090 $av = eval { (Storable::thaw $av)->{objs} }; 1123 my $st = eval { Coro::Storable::thaw $av }
1124 || eval { my $guard = Coro::Storable::guard; Storable::thaw $av }; #d# compatibility, remove
1125 $av = $st->{objs};
1091 } 1126 }
1092 1127
1093 utf8::decode (my $decname = $filename); 1128 utf8::decode (my $decname = $filename);
1094 warn sprintf "loading %s (%d,%d)\n", 1129 warn sprintf "loading %s (%d,%d)\n",
1095 $decname, length $data, scalar @{$av || []}; 1130 $decname, length $data, scalar @{$av || []};
2273=cut 2308=cut
2274 2309
2275sub deref { 2310sub deref {
2276 my ($ref) = @_; 2311 my ($ref) = @_;
2277 2312
2278 # temporary compatibility#TODO#remove
2279 $ref =~ s{^<}{player/<};
2280
2281 if ($ref =~ m{^player\/(<1\.\d+>)/(.*)$}) { 2313 if ($ref =~ m{^player\/(<1\.[0-9a-f]+>)/(.*)$}) {
2282 my ($uuid, $name) = ($1, $2); 2314 my ($uuid, $name) = ($1, $2);
2283 my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name 2315 my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name
2284 or return; 2316 or return;
2285 $pl->ob->uuid eq $uuid 2317 $pl->ob->uuid eq $uuid
2286 or return; 2318 or return;
2895=pod 2927=pod
2896 2928
2897The following functions and methods are available within a safe environment: 2929The following functions and methods are available within a safe environment:
2898 2930
2899 cf::object 2931 cf::object
2900 contr pay_amount pay_player map x y force_find force_add 2932 contr pay_amount pay_player map x y force_find force_add destroy
2901 insert remove name archname title slaying race decrease_ob_nr 2933 insert remove name archname title slaying race decrease_ob_nr
2902 2934
2903 cf::object::player 2935 cf::object::player
2904 player 2936 player
2905 2937
2912=cut 2944=cut
2913 2945
2914for ( 2946for (
2915 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 2947 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
2916 insert remove inv name archname title slaying race 2948 insert remove inv name archname title slaying race
2917 decrease_ob_nr)], 2949 decrease_ob_nr destroy)],
2918 ["cf::object::player" => qw(player)], 2950 ["cf::object::player" => qw(player)],
2919 ["cf::player" => qw(peaceful)], 2951 ["cf::player" => qw(peaceful)],
2920 ["cf::map" => qw(trigger)], 2952 ["cf::map" => qw(trigger)],
2921) { 2953) {
2922 no strict 'refs'; 2954 no strict 'refs';
3279 # and maps saved/destroyed asynchronously. 3311 # and maps saved/destroyed asynchronously.
3280 warn "begin emergency player save\n"; 3312 warn "begin emergency player save\n";
3281 for my $login (keys %cf::PLAYER) { 3313 for my $login (keys %cf::PLAYER) {
3282 my $pl = $cf::PLAYER{$login} or next; 3314 my $pl = $cf::PLAYER{$login} or next;
3283 $pl->valid or next; 3315 $pl->valid or next;
3316 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3284 $pl->save; 3317 $pl->save;
3285 } 3318 }
3286 warn "end emergency player save\n"; 3319 warn "end emergency player save\n";
3287 3320
3288 warn "begin emergency map save\n"; 3321 warn "begin emergency map save\n";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines