… | |
… | |
10 | use Event; |
10 | use Event; |
11 | use Opcode; |
11 | use Opcode; |
12 | use Safe; |
12 | use Safe; |
13 | use Safe::Hole; |
13 | use Safe::Hole; |
14 | |
14 | |
15 | use Coro 3.64 (); |
15 | use Coro 4.1 (); |
16 | use Coro::State; |
16 | use Coro::State; |
17 | use Coro::Handle; |
17 | use Coro::Handle; |
18 | use Coro::Event; |
18 | use Coro::Event; |
19 | use Coro::Timer; |
19 | use Coro::Timer; |
20 | use Coro::Signal; |
20 | use Coro::Signal; |
… | |
… | |
27 | use BDB (); |
27 | use BDB (); |
28 | use Data::Dumper; |
28 | use Data::Dumper; |
29 | use Digest::MD5; |
29 | use Digest::MD5; |
30 | use Fcntl; |
30 | use Fcntl; |
31 | use YAML::Syck (); |
31 | use YAML::Syck (); |
32 | use IO::AIO 2.32 (); |
32 | use IO::AIO 2.51 (); |
33 | use Time::HiRes; |
33 | use Time::HiRes; |
34 | use Compress::LZF; |
34 | use Compress::LZF; |
35 | use Digest::MD5 (); |
35 | use 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 | logBacktrace, 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; |
… | |
… | |
1044 | close $fh; |
1046 | close $fh; |
1045 | |
1047 | |
1046 | if (@$objs) { |
1048 | if (@$objs) { |
1047 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1049 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1048 | chmod SAVE_MODE, $fh; |
1050 | chmod SAVE_MODE, $fh; |
1049 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
1051 | my $data = Coro::Storable::blocking_nfreeze { version => 1, objs => $objs }; |
1050 | aio_write $fh, 0, (length $data), $data, 0; |
1052 | aio_write $fh, 0, (length $data), $data, 0; |
1051 | aio_fsync $fh if $cf::USE_FSYNC; |
1053 | aio_fsync $fh if $cf::USE_FSYNC; |
1052 | close $fh; |
1054 | close $fh; |
1053 | aio_rename "$filename.pst~", "$filename.pst"; |
1055 | aio_rename "$filename.pst~", "$filename.pst"; |
1054 | } |
1056 | } |
… | |
… | |
1085 | |
1087 | |
1086 | unless (aio_stat "$filename.pst") { |
1088 | unless (aio_stat "$filename.pst") { |
1087 | (aio_load "$filename.pst", $av) >= 0 |
1089 | (aio_load "$filename.pst", $av) >= 0 |
1088 | or return; |
1090 | or return; |
1089 | |
1091 | |
1090 | $av = eval { (Storable::thaw $av)->{objs} }; |
1092 | my $st = eval { Coro::Storable::thaw $av } |
|
|
1093 | || eval { my $guard = Coro::Storable::guard; Storable::thaw $av }; #d# compatibility, remove |
|
|
1094 | $av = $st->{objs}; |
1091 | } |
1095 | } |
1092 | |
1096 | |
1093 | utf8::decode (my $decname = $filename); |
1097 | utf8::decode (my $decname = $filename); |
1094 | warn sprintf "loading %s (%d,%d)\n", |
1098 | warn sprintf "loading %s (%d,%d)\n", |
1095 | $decname, length $data, scalar @{$av || []}; |
1099 | $decname, length $data, scalar @{$av || []}; |
… | |
… | |
2892 | =pod |
2896 | =pod |
2893 | |
2897 | |
2894 | The following functions and methods are available within a safe environment: |
2898 | The following functions and methods are available within a safe environment: |
2895 | |
2899 | |
2896 | cf::object |
2900 | cf::object |
2897 | contr pay_amount pay_player map x y force_find force_add |
2901 | contr pay_amount pay_player map x y force_find force_add destroy |
2898 | insert remove name archname title slaying race decrease_ob_nr |
2902 | insert remove name archname title slaying race decrease_ob_nr |
2899 | |
2903 | |
2900 | cf::object::player |
2904 | cf::object::player |
2901 | player |
2905 | player |
2902 | |
2906 | |
… | |
… | |
2909 | =cut |
2913 | =cut |
2910 | |
2914 | |
2911 | for ( |
2915 | for ( |
2912 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2916 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2913 | insert remove inv name archname title slaying race |
2917 | insert remove inv name archname title slaying race |
2914 | decrease_ob_nr)], |
2918 | decrease_ob_nr destroy)], |
2915 | ["cf::object::player" => qw(player)], |
2919 | ["cf::object::player" => qw(player)], |
2916 | ["cf::player" => qw(peaceful)], |
2920 | ["cf::player" => qw(peaceful)], |
2917 | ["cf::map" => qw(trigger)], |
2921 | ["cf::map" => qw(trigger)], |
2918 | ) { |
2922 | ) { |
2919 | no strict 'refs'; |
2923 | no strict 'refs'; |
… | |
… | |
3276 | # and maps saved/destroyed asynchronously. |
3280 | # and maps saved/destroyed asynchronously. |
3277 | warn "begin emergency player save\n"; |
3281 | warn "begin emergency player save\n"; |
3278 | for my $login (keys %cf::PLAYER) { |
3282 | for my $login (keys %cf::PLAYER) { |
3279 | my $pl = $cf::PLAYER{$login} or next; |
3283 | my $pl = $cf::PLAYER{$login} or next; |
3280 | $pl->valid or next; |
3284 | $pl->valid or next; |
|
|
3285 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3281 | $pl->save; |
3286 | $pl->save; |
3282 | } |
3287 | } |
3283 | warn "end emergency player save\n"; |
3288 | warn "end emergency player save\n"; |
3284 | |
3289 | |
3285 | warn "begin emergency map save\n"; |
3290 | warn "begin emergency map save\n"; |