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

Comparing cf.schmorp.de/server/lib/cf.pm (file contents):
Revision 1.380 by root, Mon Oct 8 13:58:07 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 4.1 (); 15use Coro 4.1 ();
16use Coro::State; 16use Coro::State;
17use Coro::Handle; 17use Coro::Handle;
18use Coro::Event; 18use Coro::Event;
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 | logBacktrace, Carp::longmess "sync job";#d# 432 LOG llevError, Carp::longmess "sync job";#d#
433 433
434 # TODO: use suspend/resume instead 434 # TODO: use suspend/resume instead
435 # (but this is cancel-safe) 435 # (but this is cancel-safe)
436 my $freeze_guard = freeze_mainloop; 436 my $freeze_guard = freeze_mainloop;
437 437
966 966
967=cut 967=cut
968 968
969############################################################################# 969#############################################################################
970# object support 970# object support
971#
972 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#
973sub _can_merge { 1005sub _can_merge {
974 my ($ob1, $ob2) = @_; 1006 my ($ob1, $ob2) = @_;
975 1007
976 local $Storable::canonical = 1; 1008 ++$SLOW_MERGES;#d#
977 my $fob1 = Storable::freeze $ob1;
978 my $fob2 = Storable::freeze $ob2;
979 1009
980 $fob1 eq $fob2 1010 # we do the slow way here
1011 return _object_equal $ob1, $ob2
981} 1012}
982 1013
983sub reattach { 1014sub reattach {
984 # basically do the same as instantiate, without calling instantiate 1015 # basically do the same as instantiate, without calling instantiate
985 my ($obj) = @_; 1016 my ($obj) = @_;
1046 close $fh; 1077 close $fh;
1047 1078
1048 if (@$objs) { 1079 if (@$objs) {
1049 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) {
1050 chmod SAVE_MODE, $fh; 1081 chmod SAVE_MODE, $fh;
1051 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; 1082 my $data = Coro::Storable::blocking_nfreeze { version => 1, objs => $objs };
1052 aio_write $fh, 0, (length $data), $data, 0; 1083 aio_write $fh, 0, (length $data), $data, 0;
1053 aio_fsync $fh if $cf::USE_FSYNC; 1084 aio_fsync $fh if $cf::USE_FSYNC;
1054 close $fh; 1085 close $fh;
1055 aio_rename "$filename.pst~", "$filename.pst"; 1086 aio_rename "$filename.pst~", "$filename.pst";
1056 } 1087 }
2896=pod 2927=pod
2897 2928
2898The following functions and methods are available within a safe environment: 2929The following functions and methods are available within a safe environment:
2899 2930
2900 cf::object 2931 cf::object
2901 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
2902 insert remove name archname title slaying race decrease_ob_nr 2933 insert remove name archname title slaying race decrease_ob_nr
2903 2934
2904 cf::object::player 2935 cf::object::player
2905 player 2936 player
2906 2937
2913=cut 2944=cut
2914 2945
2915for ( 2946for (
2916 ["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
2917 insert remove inv name archname title slaying race 2948 insert remove inv name archname title slaying race
2918 decrease_ob_nr)], 2949 decrease_ob_nr destroy)],
2919 ["cf::object::player" => qw(player)], 2950 ["cf::object::player" => qw(player)],
2920 ["cf::player" => qw(peaceful)], 2951 ["cf::player" => qw(peaceful)],
2921 ["cf::map" => qw(trigger)], 2952 ["cf::map" => qw(trigger)],
2922) { 2953) {
2923 no strict 'refs'; 2954 no strict 'refs';
3280 # and maps saved/destroyed asynchronously. 3311 # and maps saved/destroyed asynchronously.
3281 warn "begin emergency player save\n"; 3312 warn "begin emergency player save\n";
3282 for my $login (keys %cf::PLAYER) { 3313 for my $login (keys %cf::PLAYER) {
3283 my $pl = $cf::PLAYER{$login} or next; 3314 my $pl = $cf::PLAYER{$login} or next;
3284 $pl->valid or next; 3315 $pl->valid or next;
3316 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3285 $pl->save; 3317 $pl->save;
3286 } 3318 }
3287 warn "end emergency player save\n"; 3319 warn "end emergency player save\n";
3288 3320
3289 warn "begin emergency map save\n"; 3321 warn "begin emergency map save\n";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines