… | |
… | |
21 | |
21 | |
22 | package cf; |
22 | package cf; |
23 | |
23 | |
24 | use 5.10.0; |
24 | use 5.10.0; |
25 | use utf8; |
25 | use utf8; |
26 | use strict "vars", "subs"; |
26 | use strict qw(vars subs); |
27 | |
27 | |
28 | use Symbol; |
28 | use Symbol; |
29 | use List::Util; |
29 | use List::Util; |
30 | use Socket; |
30 | use Socket; |
31 | use EV; |
31 | use EV; |
32 | use Opcode; |
32 | use Opcode; |
33 | use Safe; |
33 | use Safe; |
34 | use Safe::Hole; |
34 | use Safe::Hole; |
35 | use Storable (); |
35 | use Storable (); |
36 | |
36 | |
|
|
37 | use Guard (); |
37 | use Coro (); |
38 | use Coro (); |
38 | use Coro::State; |
39 | use Coro::State; |
39 | use Coro::Handle; |
40 | use Coro::Handle; |
40 | use Coro::EV; |
41 | use Coro::EV; |
41 | use Coro::AnyEvent; |
42 | use Coro::AnyEvent; |
42 | use Coro::Timer; |
43 | use Coro::Timer; |
43 | use Coro::Signal; |
44 | use Coro::Signal; |
44 | use Coro::Semaphore; |
45 | use Coro::Semaphore; |
|
|
46 | use Coro::SemaphoreSet; |
45 | use Coro::AnyEvent; |
47 | use Coro::AnyEvent; |
46 | use Coro::AIO; |
48 | use Coro::AIO; |
47 | use Coro::BDB 1.6; |
49 | use Coro::BDB 1.6; |
48 | use Coro::Storable; |
50 | use Coro::Storable; |
49 | use Coro::Util (); |
51 | use Coro::Util (); |
… | |
… | |
110 | our $BDB_DEADLOCK_WATCHER; |
112 | our $BDB_DEADLOCK_WATCHER; |
111 | our $BDB_CHECKPOINT_WATCHER; |
113 | our $BDB_CHECKPOINT_WATCHER; |
112 | our $BDB_TRICKLE_WATCHER; |
114 | our $BDB_TRICKLE_WATCHER; |
113 | our $DB_ENV; |
115 | our $DB_ENV; |
114 | |
116 | |
|
|
117 | our @EXTRA_MODULES = qw(pod mapscript); |
|
|
118 | |
115 | our %CFG; |
119 | our %CFG; |
116 | |
120 | |
117 | our $UPTIME; $UPTIME ||= time; |
121 | our $UPTIME; $UPTIME ||= time; |
118 | our $RUNTIME; |
122 | our $RUNTIME; |
119 | our $NOW; |
123 | our $NOW; |
… | |
… | |
129 | our $LOADAVG; # same thing, but with alpha-smoothing |
133 | our $LOADAVG; # same thing, but with alpha-smoothing |
130 | our $JITTER; # average jitter |
134 | our $JITTER; # average jitter |
131 | our $TICK_START; # for load detecting purposes |
135 | our $TICK_START; # for load detecting purposes |
132 | |
136 | |
133 | our @POST_INIT; |
137 | our @POST_INIT; |
|
|
138 | |
|
|
139 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
134 | |
140 | |
135 | binmode STDOUT; |
141 | binmode STDOUT; |
136 | binmode STDERR; |
142 | binmode STDERR; |
137 | |
143 | |
138 | # read virtual server time, if available |
144 | # read virtual server time, if available |
… | |
… | |
328 | Wait until the given lock is available. See cf::lock_acquire. |
334 | Wait until the given lock is available. See cf::lock_acquire. |
329 | |
335 | |
330 | =item my $lock = cf::lock_acquire $string |
336 | =item my $lock = cf::lock_acquire $string |
331 | |
337 | |
332 | Wait until the given lock is available and then acquires it and returns |
338 | Wait until the given lock is available and then acquires it and returns |
333 | a Coro::guard object. If the guard object gets destroyed (goes out of scope, |
339 | a L<Guard> object. If the guard object gets destroyed (goes out of scope, |
334 | for example when the coroutine gets canceled), the lock is automatically |
340 | for example when the coroutine gets canceled), the lock is automatically |
335 | returned. |
341 | returned. |
336 | |
342 | |
337 | Locks are *not* recursive, locking from the same coro twice results in a |
343 | Locks are *not* recursive, locking from the same coro twice results in a |
338 | deadlocked coro. |
344 | deadlocked coro. |
… | |
… | |
344 | |
350 | |
345 | Return true if the lock is currently active, i.e. somebody has locked it. |
351 | Return true if the lock is currently active, i.e. somebody has locked it. |
346 | |
352 | |
347 | =cut |
353 | =cut |
348 | |
354 | |
349 | our %LOCK; |
355 | our $LOCKS = new Coro::SemaphoreSet; |
350 | our %LOCKER;#d# |
|
|
351 | |
356 | |
352 | sub lock_wait($) { |
357 | sub lock_wait($) { |
353 | my ($key) = @_; |
358 | $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 | } |
359 | } |
367 | |
360 | |
368 | sub lock_acquire($) { |
361 | sub lock_acquire($) { |
369 | my ($key) = @_; |
362 | $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 | } |
363 | } |
383 | |
364 | |
384 | sub lock_active($) { |
365 | sub lock_active($) { |
385 | my ($key) = @_; |
366 | $LOCKS->count ($_[0]) < 1 |
386 | |
|
|
387 | ! ! $LOCK{$key} |
|
|
388 | } |
367 | } |
389 | |
368 | |
390 | sub freeze_mainloop { |
369 | sub freeze_mainloop { |
391 | tick_inhibit_inc; |
370 | tick_inhibit_inc; |
392 | |
371 | |
393 | Coro::guard \&tick_inhibit_dec; |
372 | &Guard::guard (\&tick_inhibit_dec); |
394 | } |
373 | } |
395 | |
374 | |
396 | =item cf::periodic $interval, $cb |
375 | =item cf::periodic $interval, $cb |
397 | |
376 | |
398 | Like EV::periodic, but randomly selects a starting point so that the actions |
377 | Like EV::periodic, but randomly selects a starting point so that the actions |
… | |
… | |
1187 | } else { |
1166 | } else { |
1188 | aio_unlink "$filename.pst"; |
1167 | aio_unlink "$filename.pst"; |
1189 | } |
1168 | } |
1190 | |
1169 | |
1191 | aio_rename "$filename~", $filename; |
1170 | aio_rename "$filename~", $filename; |
|
|
1171 | |
|
|
1172 | $filename =~ s%/[^/]+$%%; |
|
|
1173 | aio_pathsync $filename if $cf::USE_FSYNC; |
1192 | } else { |
1174 | } else { |
1193 | warn "FATAL: $filename~: $!\n"; |
1175 | warn "unable to save objects: $filename~: $!\n"; |
1194 | } |
1176 | } |
1195 | } else { |
1177 | } else { |
1196 | aio_unlink $filename; |
1178 | aio_unlink $filename; |
1197 | aio_unlink "$filename.pst"; |
1179 | aio_unlink "$filename.pst"; |
1198 | } |
1180 | } |
… | |
… | |
1289 | my ($name, $cb) = @_; |
1271 | my ($name, $cb) = @_; |
1290 | |
1272 | |
1291 | $EXTICMD{$name} = $cb; |
1273 | $EXTICMD{$name} = $cb; |
1292 | } |
1274 | } |
1293 | |
1275 | |
|
|
1276 | use File::Glob (); |
|
|
1277 | |
1294 | cf::player->attach ( |
1278 | cf::player->attach ( |
1295 | on_command => sub { |
1279 | on_command => sub { |
1296 | my ($pl, $name, $params) = @_; |
1280 | my ($pl, $name, $params) = @_; |
1297 | |
1281 | |
1298 | my $cb = $COMMAND{$name} |
1282 | my $cb = $COMMAND{$name} |
… | |
… | |
1329 | } |
1313 | } |
1330 | |
1314 | |
1331 | cf::override; |
1315 | cf::override; |
1332 | }, |
1316 | }, |
1333 | ); |
1317 | ); |
|
|
1318 | |
|
|
1319 | # "readahead" all extensions |
|
|
1320 | sub cache_extensions { |
|
|
1321 | my $grp = IO::AIO::aio_group; |
|
|
1322 | |
|
|
1323 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
|
|
1324 | for (grep /\.ext$/, @{$_[0]}) { |
|
|
1325 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
|
|
1326 | } |
|
|
1327 | }; |
|
|
1328 | |
|
|
1329 | $grp |
|
|
1330 | } |
1334 | |
1331 | |
1335 | sub load_extensions { |
1332 | sub load_extensions { |
1336 | cf::sync_job { |
1333 | cf::sync_job { |
1337 | my %todo; |
1334 | my %todo; |
1338 | |
1335 | |
… | |
… | |
1867 | |
1864 | |
1868 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1865 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1869 | "$UNIQUEDIR/$path" |
1866 | "$UNIQUEDIR/$path" |
1870 | } |
1867 | } |
1871 | |
1868 | |
1872 | # and all this just because we cannot iterate over |
|
|
1873 | # all maps in C++... |
|
|
1874 | sub change_all_map_light { |
|
|
1875 | my ($change) = @_; |
|
|
1876 | |
|
|
1877 | $_->change_map_light ($change) |
|
|
1878 | for grep $_->outdoor, values %cf::MAP; |
|
|
1879 | } |
|
|
1880 | |
|
|
1881 | sub decay_objects { |
1869 | sub decay_objects { |
1882 | my ($self) = @_; |
1870 | my ($self) = @_; |
1883 | |
1871 | |
1884 | return if $self->{deny_reset}; |
1872 | return if $self->{deny_reset}; |
1885 | |
1873 | |
… | |
… | |
1967 | sub find { |
1955 | sub find { |
1968 | my ($path, $origin) = @_; |
1956 | my ($path, $origin) = @_; |
1969 | |
1957 | |
1970 | $path = normalise $path, $origin && $origin->path; |
1958 | $path = normalise $path, $origin && $origin->path; |
1971 | |
1959 | |
1972 | cf::lock_wait "map_data:$path";#d#remove |
1960 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1973 | cf::lock_wait "map_find:$path"; |
1961 | my $guard2 = cf::lock_acquire "map_find:$path"; |
1974 | |
1962 | |
1975 | $cf::MAP{$path} || do { |
1963 | $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 |
1964 | my $map = new_from_path cf::map $path |
1980 | or return; |
1965 | or return; |
1981 | |
1966 | |
1982 | $map->{last_save} = $cf::RUNTIME; |
1967 | $map->{last_save} = $cf::RUNTIME; |
1983 | |
1968 | |
… | |
… | |
2050 | |
2035 | |
2051 | unless ($self->{deny_activate}) { |
2036 | unless ($self->{deny_activate}) { |
2052 | $self->decay_objects; |
2037 | $self->decay_objects; |
2053 | $self->fix_auto_apply; |
2038 | $self->fix_auto_apply; |
2054 | $self->update_buttons; |
2039 | $self->update_buttons; |
2055 | cf::cede_to_tick; |
|
|
2056 | $self->set_darkness_map; |
|
|
2057 | cf::cede_to_tick; |
2040 | cf::cede_to_tick; |
2058 | $self->activate; |
2041 | $self->activate; |
2059 | } |
2042 | } |
2060 | |
2043 | |
2061 | $self->{last_save} = $cf::RUNTIME; |
2044 | $self->{last_save} = $cf::RUNTIME; |
… | |
… | |
2788 | id => "infobox", |
2771 | id => "infobox", |
2789 | title => "Body Parts", |
2772 | title => "Body Parts", |
2790 | reply => undef, |
2773 | reply => undef, |
2791 | tooltip => "Shows which body parts you posess and are available", |
2774 | tooltip => "Shows which body parts you posess and are available", |
2792 | }, |
2775 | }, |
|
|
2776 | "c/statistics" => { |
|
|
2777 | id => "infobox", |
|
|
2778 | title => "Statistics", |
|
|
2779 | reply => undef, |
|
|
2780 | tooltip => "Shows your primary statistics", |
|
|
2781 | }, |
2793 | "c/skills" => { |
2782 | "c/skills" => { |
2794 | id => "infobox", |
2783 | id => "infobox", |
2795 | title => "Skills", |
2784 | title => "Skills", |
2796 | reply => undef, |
2785 | reply => undef, |
2797 | tooltip => "Shows your experience per skill and item power", |
2786 | tooltip => "Shows your experience per skill and item power", |
|
|
2787 | }, |
|
|
2788 | "c/resistances" => { |
|
|
2789 | id => "infobox", |
|
|
2790 | title => "Resistances", |
|
|
2791 | reply => undef, |
|
|
2792 | tooltip => "Shows your resistances", |
|
|
2793 | }, |
|
|
2794 | "c/pets" => { |
|
|
2795 | id => "infobox", |
|
|
2796 | title => "Pets", |
|
|
2797 | reply => undef, |
|
|
2798 | tooltip => "Shows information abotu your pets/a specific pet", |
2798 | }, |
2799 | }, |
2799 | "c/uptime" => { |
2800 | "c/uptime" => { |
2800 | id => "infobox", |
2801 | id => "infobox", |
2801 | title => "Uptime", |
2802 | title => "Uptime", |
2802 | reply => undef, |
2803 | reply => undef, |
… | |
… | |
2812 | id => "party", |
2813 | id => "party", |
2813 | title => "Party", |
2814 | title => "Party", |
2814 | reply => "gsay ", |
2815 | reply => "gsay ", |
2815 | tooltip => "Messages and chat related to your party", |
2816 | tooltip => "Messages and chat related to your party", |
2816 | }, |
2817 | }, |
|
|
2818 | "c/death" => { |
|
|
2819 | id => "death", |
|
|
2820 | title => "Death", |
|
|
2821 | reply => undef, |
|
|
2822 | tooltip => "Reason for and more info about your most recent death", |
|
|
2823 | }, |
|
|
2824 | "c/say" => $SAY_CHANNEL, |
|
|
2825 | "c/chat" => $CHAT_CHANNEL, |
2817 | ); |
2826 | ); |
2818 | |
2827 | |
2819 | sub cf::client::send_msg { |
2828 | sub cf::client::send_msg { |
2820 | my ($self, $channel, $msg, $color, @extra) = @_; |
2829 | my ($self, $channel, $msg, $color, @extra) = @_; |
2821 | |
2830 | |
… | |
… | |
2826 | |
2835 | |
2827 | # check predefined channels, for the benefit of C |
2836 | # check predefined channels, for the benefit of C |
2828 | if ($CHANNEL{$channel}) { |
2837 | if ($CHANNEL{$channel}) { |
2829 | $channel = $CHANNEL{$channel}; |
2838 | $channel = $CHANNEL{$channel}; |
2830 | |
2839 | |
2831 | $self->ext_msg (channel_info => $channel) |
2840 | $self->ext_msg (channel_info => $channel); |
2832 | if $self->can_msg; |
|
|
2833 | |
|
|
2834 | $channel = $channel->{id}; |
2841 | $channel = $channel->{id}; |
2835 | |
2842 | |
2836 | } elsif (ref $channel) { |
2843 | } elsif (ref $channel) { |
2837 | # send meta info to client, if not yet sent |
2844 | # send meta info to client, if not yet sent |
2838 | unless (exists $self->{channel}{$channel->{id}}) { |
2845 | unless (exists $self->{channel}{$channel->{id}}) { |
2839 | $self->{channel}{$channel->{id}} = $channel; |
2846 | $self->{channel}{$channel->{id}} = $channel; |
2840 | $self->ext_msg (channel_info => $channel) |
2847 | $self->ext_msg (channel_info => $channel); |
2841 | if $self->can_msg; |
|
|
2842 | } |
2848 | } |
2843 | |
2849 | |
2844 | $channel = $channel->{id}; |
2850 | $channel = $channel->{id}; |
2845 | } |
2851 | } |
2846 | |
2852 | |
2847 | return unless @extra || length $msg; |
2853 | return unless @extra || length $msg; |
2848 | |
2854 | |
2849 | if ($self->can_msg) { |
|
|
2850 | # default colour, mask it out |
2855 | # default colour, mask it out |
2851 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2856 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2852 | if $color & cf::NDI_DEF; |
2857 | if $color & cf::NDI_DEF; |
2853 | |
2858 | |
2854 | my $pkt = "msg " |
2859 | my $pkt = "msg " |
2855 | . $self->{json_coder}->encode ( |
2860 | . $self->{json_coder}->encode ( |
2856 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2861 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2857 | ); |
2862 | ); |
2858 | |
2863 | |
2859 | # try lzf for large packets |
2864 | # try lzf for large packets |
2860 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2865 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2861 | if 1024 <= length $pkt and $self->{can_lzf}; |
2866 | if 1024 <= length $pkt and $self->{can_lzf}; |
2862 | |
2867 | |
2863 | # split very large packets |
2868 | # split very large packets |
2864 | if (8192 < length $pkt and $self->{can_lzf}) { |
2869 | if (8192 < length $pkt and $self->{can_lzf}) { |
2865 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2870 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2866 | $pkt = "frag"; |
2871 | $pkt = "frag"; |
2867 | } |
2872 | } |
2868 | |
2873 | |
2869 | $self->send_packet ($pkt); |
2874 | $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 | } |
2875 | } |
2896 | |
2876 | |
2897 | =item $client->ext_msg ($type, @msg) |
2877 | =item $client->ext_msg ($type, @msg) |
2898 | |
2878 | |
2899 | Sends an ext event to the client. |
2879 | Sends an ext event to the client. |
… | |
… | |
3074 | The following functions and methods are available within a safe environment: |
3054 | The following functions and methods are available within a safe environment: |
3075 | |
3055 | |
3076 | cf::object |
3056 | cf::object |
3077 | contr pay_amount pay_player map x y force_find force_add destroy |
3057 | contr pay_amount pay_player map x y force_find force_add destroy |
3078 | insert remove name archname title slaying race decrease split |
3058 | insert remove name archname title slaying race decrease split |
|
|
3059 | value |
3079 | |
3060 | |
3080 | cf::object::player |
3061 | cf::object::player |
3081 | player |
3062 | player |
3082 | |
3063 | |
3083 | cf::player |
3064 | cf::player |
… | |
… | |
3089 | =cut |
3070 | =cut |
3090 | |
3071 | |
3091 | for ( |
3072 | for ( |
3092 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3073 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3093 | insert remove inv nrof name archname title slaying race |
3074 | insert remove inv nrof name archname title slaying race |
3094 | decrease split destroy change_exp)], |
3075 | decrease split destroy change_exp value msg lore send_msg)], |
3095 | ["cf::object::player" => qw(player)], |
3076 | ["cf::object::player" => qw(player)], |
3096 | ["cf::player" => qw(peaceful)], |
3077 | ["cf::player" => qw(peaceful send_msg)], |
3097 | ["cf::map" => qw(trigger)], |
3078 | ["cf::map" => qw(trigger)], |
3098 | ) { |
3079 | ) { |
3099 | no strict 'refs'; |
3080 | no strict 'refs'; |
3100 | my ($pkg, @funs) = @$_; |
3081 | my ($pkg, @funs) = @$_; |
3101 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3082 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
… | |
… | |
3118 | my ($code, %vars) = @_; |
3099 | my ($code, %vars) = @_; |
3119 | |
3100 | |
3120 | my $qcode = $code; |
3101 | my $qcode = $code; |
3121 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3102 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3122 | $qcode =~ s/\n/\\n/g; |
3103 | $qcode =~ s/\n/\\n/g; |
|
|
3104 | |
|
|
3105 | %vars = (_dummy => 0) unless %vars; |
3123 | |
3106 | |
3124 | local $_; |
3107 | local $_; |
3125 | local @safe::cf::_safe_eval_args = values %vars; |
3108 | local @safe::cf::_safe_eval_args = values %vars; |
3126 | |
3109 | |
3127 | my $eval = |
3110 | my $eval = |
… | |
… | |
3409 | |
3392 | |
3410 | cf::init_experience; |
3393 | cf::init_experience; |
3411 | cf::init_anim; |
3394 | cf::init_anim; |
3412 | cf::init_attackmess; |
3395 | cf::init_attackmess; |
3413 | cf::init_dynamic; |
3396 | cf::init_dynamic; |
3414 | cf::init_block; |
|
|
3415 | |
3397 | |
3416 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3398 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3417 | |
3399 | |
3418 | # we must not ever block the main coroutine |
3400 | # we must not ever block the main coroutine |
3419 | local $Coro::idle = sub { |
3401 | local $Coro::idle = sub { |
… | |
… | |
3555 | } |
3537 | } |
3556 | |
3538 | |
3557 | sub emergency_save() { |
3539 | sub emergency_save() { |
3558 | my $freeze_guard = cf::freeze_mainloop; |
3540 | my $freeze_guard = cf::freeze_mainloop; |
3559 | |
3541 | |
3560 | warn "enter emergency perl save\n"; |
3542 | warn "emergency_perl_save: enter\n"; |
3561 | |
3543 | |
3562 | cf::sync_job { |
3544 | cf::sync_job { |
|
|
3545 | # this is a trade-off: we want to be very quick here, so |
|
|
3546 | # save all maps without fsync, and later call a global sync |
|
|
3547 | # (which in turn might be very very slow) |
|
|
3548 | local $USE_FSYNC = 0; |
|
|
3549 | |
3563 | # use a peculiar iteration method to avoid tripping on perl |
3550 | # use a peculiar iteration method to avoid tripping on perl |
3564 | # refcount bugs in for. also avoids problems with players |
3551 | # refcount bugs in for. also avoids problems with players |
3565 | # and maps saved/destroyed asynchronously. |
3552 | # and maps saved/destroyed asynchronously. |
3566 | warn "begin emergency player save\n"; |
3553 | warn "emergency_perl_save: begin player save\n"; |
3567 | for my $login (keys %cf::PLAYER) { |
3554 | for my $login (keys %cf::PLAYER) { |
3568 | my $pl = $cf::PLAYER{$login} or next; |
3555 | my $pl = $cf::PLAYER{$login} or next; |
3569 | $pl->valid or next; |
3556 | $pl->valid or next; |
3570 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3557 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3571 | $pl->save; |
3558 | $pl->save; |
3572 | } |
3559 | } |
3573 | warn "end emergency player save\n"; |
3560 | warn "emergency_perl_save: end player save\n"; |
3574 | |
3561 | |
3575 | warn "begin emergency map save\n"; |
3562 | warn "emergency_perl_save: begin map save\n"; |
3576 | for my $path (keys %cf::MAP) { |
3563 | for my $path (keys %cf::MAP) { |
3577 | my $map = $cf::MAP{$path} or next; |
3564 | my $map = $cf::MAP{$path} or next; |
3578 | $map->valid or next; |
3565 | $map->valid or next; |
3579 | $map->save; |
3566 | $map->save; |
3580 | } |
3567 | } |
3581 | warn "end emergency map save\n"; |
3568 | warn "emergency_perl_save: end map save\n"; |
3582 | |
3569 | |
3583 | warn "begin emergency database checkpoint\n"; |
3570 | warn "emergency_perl_save: begin database checkpoint\n"; |
3584 | BDB::db_env_txn_checkpoint $DB_ENV; |
3571 | BDB::db_env_txn_checkpoint $DB_ENV; |
3585 | warn "end emergency database checkpoint\n"; |
3572 | warn "emergency_perl_save: end database checkpoint\n"; |
3586 | |
3573 | |
3587 | warn "begin write uuid\n"; |
3574 | warn "emergency_perl_save: begin write uuid\n"; |
3588 | write_uuid_sync 1; |
3575 | write_uuid_sync 1; |
3589 | warn "end write uuid\n"; |
3576 | warn "emergency_perl_save: end write uuid\n"; |
3590 | }; |
3577 | }; |
3591 | |
3578 | |
|
|
3579 | warn "emergency_perl_save: starting sync()\n"; |
|
|
3580 | IO::AIO::aio_sync sub { |
|
|
3581 | warn "emergency_perl_save: finished sync()\n"; |
|
|
3582 | }; |
|
|
3583 | |
3592 | warn "leave emergency perl save\n"; |
3584 | warn "emergency_perl_save: leave\n"; |
3593 | } |
3585 | } |
3594 | |
3586 | |
3595 | sub post_cleanup { |
3587 | sub post_cleanup { |
3596 | my ($make_core) = @_; |
3588 | my ($make_core) = @_; |
3597 | |
3589 | |
… | |
… | |
3634 | warn "can only reload from main coroutine"; |
3626 | warn "can only reload from main coroutine"; |
3635 | return; |
3627 | return; |
3636 | } |
3628 | } |
3637 | |
3629 | |
3638 | return if $RELOAD++; |
3630 | return if $RELOAD++; |
|
|
3631 | |
|
|
3632 | my $t1 = EV::time; |
3639 | |
3633 | |
3640 | while ($RELOAD) { |
3634 | while ($RELOAD) { |
3641 | warn "reloading..."; |
3635 | warn "reloading..."; |
3642 | |
3636 | |
3643 | warn "entering sync_job"; |
3637 | warn "entering sync_job"; |
… | |
… | |
3704 | clear_package "safe::$_" |
3698 | clear_package "safe::$_" |
3705 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3699 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3706 | |
3700 | |
3707 | warn "unloading cf.pm \"a bit\""; |
3701 | warn "unloading cf.pm \"a bit\""; |
3708 | delete $INC{"cf.pm"}; |
3702 | delete $INC{"cf.pm"}; |
3709 | delete $INC{"cf/pod.pm"}; |
3703 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3710 | |
3704 | |
3711 | # don't, removes xs symbols, too, |
3705 | # don't, removes xs symbols, too, |
3712 | # and global variables created in xs |
3706 | # and global variables created in xs |
3713 | #clear_package __PACKAGE__; |
3707 | #clear_package __PACKAGE__; |
3714 | |
3708 | |
… | |
… | |
3722 | cf::reload_config; |
3716 | cf::reload_config; |
3723 | |
3717 | |
3724 | warn "loading extensions"; |
3718 | warn "loading extensions"; |
3725 | cf::load_extensions; |
3719 | cf::load_extensions; |
3726 | |
3720 | |
|
|
3721 | if ($REATTACH_ON_RELOAD) { |
3727 | warn "reattaching attachments to objects/players"; |
3722 | warn "reattaching attachments to objects/players"; |
3728 | _global_reattach; # objects, sockets |
3723 | _global_reattach; # objects, sockets |
3729 | warn "reattaching attachments to maps"; |
3724 | warn "reattaching attachments to maps"; |
3730 | reattach $_ for values %MAP; |
3725 | reattach $_ for values %MAP; |
3731 | warn "reattaching attachments to players"; |
3726 | warn "reattaching attachments to players"; |
3732 | reattach $_ for values %PLAYER; |
3727 | reattach $_ for values %PLAYER; |
|
|
3728 | } |
3733 | |
3729 | |
3734 | warn "running post_load"; |
3730 | warn "running post_init jobs"; |
3735 | (pop @POST_INIT)->(1) while @POST_INIT; |
3731 | (pop @POST_INIT)->(1) while @POST_INIT; |
3736 | |
3732 | |
3737 | warn "leaving sync_job"; |
3733 | warn "leaving sync_job"; |
3738 | |
3734 | |
3739 | 1 |
3735 | 1 |
… | |
… | |
3743 | }; |
3739 | }; |
3744 | |
3740 | |
3745 | warn "reloaded"; |
3741 | warn "reloaded"; |
3746 | --$RELOAD; |
3742 | --$RELOAD; |
3747 | } |
3743 | } |
|
|
3744 | |
|
|
3745 | $t1 = EV::time - $t1; |
|
|
3746 | warn "reload completed in ${t1}s\n"; |
3748 | }; |
3747 | }; |
3749 | |
3748 | |
3750 | our $RELOAD_WATCHER; # used only during reload |
3749 | our $RELOAD_WATCHER; # used only during reload |
3751 | |
3750 | |
3752 | sub reload_perl() { |
3751 | sub reload_perl() { |
3753 | # doing reload synchronously and two reloads happen back-to-back, |
3752 | # doing reload synchronously and two reloads happen back-to-back, |
3754 | # coro crashes during coro_state_free->destroy here. |
3753 | # coro crashes during coro_state_free->destroy here. |
3755 | |
3754 | |
|
|
3755 | $RELOAD_WATCHER ||= cf::async { |
|
|
3756 | Coro::AIO::aio_wait cache_extensions; |
|
|
3757 | |
3756 | $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub { |
3758 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3757 | do_reload_perl; |
3759 | do_reload_perl; |
3758 | undef $RELOAD_WATCHER; |
3760 | undef $RELOAD_WATCHER; |
|
|
3761 | }; |
3759 | }; |
3762 | }; |
3760 | } |
3763 | } |
3761 | |
3764 | |
3762 | register_command "reload" => sub { |
3765 | register_command "reload" => sub { |
3763 | my ($who, $arg) = @_; |
3766 | my ($who, $arg) = @_; |
… | |
… | |
3922 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3925 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3923 | } |
3926 | } |
3924 | } |
3927 | } |
3925 | |
3928 | |
3926 | # load additional modules |
3929 | # load additional modules |
3927 | use cf::pod; |
3930 | eval "use cf::$_" for @EXTRA_MODULES; |
3928 | |
3931 | |
3929 | END { cf::emergency_save } |
3932 | END { cf::emergency_save } |
3930 | |
3933 | |
3931 | 1 |
3934 | 1 |
3932 | |
3935 | |