… | |
… | |
427 | } |
427 | } |
428 | } |
428 | } |
429 | }; |
429 | }; |
430 | |
430 | |
431 | sub get_slot($;$$) { |
431 | sub get_slot($;$$) { |
|
|
432 | return if tick_inhibit || $Coro::current == $Coro::main; |
|
|
433 | |
432 | my ($time, $pri, $name) = @_; |
434 | my ($time, $pri, $name) = @_; |
433 | |
435 | |
434 | $time = $TICK * .6 if $time > $TICK * .6; |
436 | $time = $TICK * .6 if $time > $TICK * .6; |
435 | my $sig = new Coro::Signal; |
437 | my $sig = new Coro::Signal; |
436 | |
438 | |
… | |
… | |
1139 | utf8::decode (my $decname = $filename); |
1141 | utf8::decode (my $decname = $filename); |
1140 | warn sprintf "saving %s (%d,%d)\n", |
1142 | warn sprintf "saving %s (%d,%d)\n", |
1141 | $decname, length $$rdata, scalar @$objs; |
1143 | $decname, length $$rdata, scalar @$objs; |
1142 | |
1144 | |
1143 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1145 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1144 | chmod SAVE_MODE, $fh; |
1146 | aio_chmod $fh, SAVE_MODE; |
1145 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1147 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1146 | aio_fsync $fh if $cf::USE_FSYNC; |
1148 | aio_fsync $fh if $cf::USE_FSYNC; |
1147 | close $fh; |
1149 | aio_close $fh; |
1148 | |
1150 | |
1149 | if (@$objs) { |
1151 | if (@$objs) { |
1150 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1152 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1151 | chmod SAVE_MODE, $fh; |
1153 | aio_chmod $fh, SAVE_MODE; |
1152 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1154 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1153 | aio_write $fh, 0, (length $data), $data, 0; |
1155 | aio_write $fh, 0, (length $data), $data, 0; |
1154 | aio_fsync $fh if $cf::USE_FSYNC; |
1156 | aio_fsync $fh if $cf::USE_FSYNC; |
1155 | close $fh; |
1157 | aio_close $fh; |
1156 | aio_rename "$filename.pst~", "$filename.pst"; |
1158 | aio_rename "$filename.pst~", "$filename.pst"; |
1157 | } |
1159 | } |
1158 | } else { |
1160 | } else { |
1159 | aio_unlink "$filename.pst"; |
1161 | aio_unlink "$filename.pst"; |
1160 | } |
1162 | } |
… | |
… | |
1454 | my $f = new_from_file cf::object::thawer path $login |
1456 | my $f = new_from_file cf::object::thawer path $login |
1455 | or return; |
1457 | or return; |
1456 | |
1458 | |
1457 | my $pl = cf::player::load_pl $f |
1459 | my $pl = cf::player::load_pl $f |
1458 | or return; |
1460 | or return; |
|
|
1461 | |
1459 | local $cf::PLAYER_LOADING{$login} = $pl; |
1462 | local $cf::PLAYER_LOADING{$login} = $pl; |
1460 | $f->resolve_delayed_derefs; |
1463 | $f->resolve_delayed_derefs; |
1461 | $cf::PLAYER{$login} = $pl |
1464 | $cf::PLAYER{$login} = $pl |
1462 | } |
1465 | } |
1463 | } |
1466 | } |
… | |
… | |
1473 | |
1476 | |
1474 | return if $pl->{deny_save}; |
1477 | return if $pl->{deny_save}; |
1475 | |
1478 | |
1476 | aio_mkdir playerdir $pl, 0770; |
1479 | aio_mkdir playerdir $pl, 0770; |
1477 | $pl->{last_save} = $cf::RUNTIME; |
1480 | $pl->{last_save} = $cf::RUNTIME; |
|
|
1481 | |
|
|
1482 | cf::get_slot 0.01; |
1478 | |
1483 | |
1479 | $pl->save_pl ($path); |
1484 | $pl->save_pl ($path); |
1480 | cf::cede_to_tick; |
1485 | cf::cede_to_tick; |
1481 | } |
1486 | } |
1482 | |
1487 | |
… | |
… | |
1566 | |
1571 | |
1567 | for my $login (@$dirs) { |
1572 | for my $login (@$dirs) { |
1568 | my $path = path $login; |
1573 | my $path = path $login; |
1569 | |
1574 | |
1570 | # a .pst is a dead give-away for a valid player |
1575 | # a .pst is a dead give-away for a valid player |
1571 | unless (-e "$path.pst") { |
1576 | # if no pst file found, open and chekc for blocked users |
|
|
1577 | if (aio_stat "$path.pst") { |
1572 | my $fh = aio_open $path, Fcntl::O_RDONLY, 0 or next; |
1578 | my $fh = aio_open $path, Fcntl::O_RDONLY, 0 or next; |
1573 | aio_read $fh, 0, 512, my $buf, 0 or next; |
1579 | aio_read $fh, 0, 512, my $buf, 0 or next; |
1574 | $buf !~ /^password -------------$/m or next; # official not-valid tag |
1580 | $buf !~ /^password -------------$/m or next; # official not-valid tag |
1575 | } |
1581 | } |
1576 | |
1582 | |
… | |
… | |
1775 | our $MAX_RESET = 3600; |
1781 | our $MAX_RESET = 3600; |
1776 | our $DEFAULT_RESET = 3000; |
1782 | our $DEFAULT_RESET = 3000; |
1777 | |
1783 | |
1778 | sub generate_random_map { |
1784 | sub generate_random_map { |
1779 | my ($self, $rmp) = @_; |
1785 | my ($self, $rmp) = @_; |
|
|
1786 | |
|
|
1787 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
|
|
1788 | |
1780 | # mit "rum" bekleckern, nicht |
1789 | # mit "rum" bekleckern, nicht |
1781 | $self->_create_random_map ( |
1790 | $self->_create_random_map ( |
1782 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1791 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1783 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1792 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1784 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1793 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
… | |
… | |
1906 | |
1915 | |
1907 | # the temporary/swap location |
1916 | # the temporary/swap location |
1908 | sub save_path { |
1917 | sub save_path { |
1909 | my ($self) = @_; |
1918 | my ($self) = @_; |
1910 | |
1919 | |
1911 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1920 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1912 | "$TMPDIR/$path.map" |
1921 | "$TMPDIR/$path.map" |
1913 | } |
1922 | } |
1914 | |
1923 | |
1915 | # the unique path, undef == no special unique path |
1924 | # the unique path, undef == no special unique path |
1916 | sub uniq_path { |
1925 | sub uniq_path { |
1917 | my ($self) = @_; |
1926 | my ($self) = @_; |
1918 | |
1927 | |
1919 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1928 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1920 | "$UNIQUEDIR/$path" |
1929 | "$UNIQUEDIR/$path" |
1921 | } |
1930 | } |
1922 | |
1931 | |
1923 | # and all this just because we cannot iterate over |
1932 | # and all this just because we cannot iterate over |
1924 | # all maps in C++... |
1933 | # all maps in C++... |
… | |
… | |
2110 | } |
2119 | } |
2111 | |
2120 | |
2112 | $self->{last_save} = $cf::RUNTIME; |
2121 | $self->{last_save} = $cf::RUNTIME; |
2113 | $self->last_access ($cf::RUNTIME); |
2122 | $self->last_access ($cf::RUNTIME); |
2114 | |
2123 | |
2115 | $self->in_memory (cf::MAP_IN_MEMORY); |
2124 | $self->in_memory (cf::MAP_ACTIVE); |
2116 | } |
2125 | } |
2117 | |
2126 | |
2118 | $self->post_load; |
2127 | $self->post_load; |
2119 | } |
2128 | } |
2120 | |
2129 | |
… | |
… | |
2178 | my ($path, $origin, $load) = @_; |
2187 | my ($path, $origin, $load) = @_; |
2179 | |
2188 | |
2180 | $path = normalise $path, $origin && $origin->{path}; |
2189 | $path = normalise $path, $origin && $origin->{path}; |
2181 | |
2190 | |
2182 | if (my $map = $cf::MAP{$path}) { |
2191 | if (my $map = $cf::MAP{$path}) { |
2183 | return $map if !$load || $map->in_memory == cf::MAP_IN_MEMORY; |
2192 | return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; |
2184 | } |
2193 | } |
2185 | |
2194 | |
2186 | $MAP_PREFETCH{$path} |= $load; |
2195 | $MAP_PREFETCH{$path} |= $load; |
2187 | |
2196 | |
2188 | $MAP_PREFETCHER ||= cf::async { |
2197 | $MAP_PREFETCHER ||= cf::async { |
… | |
… | |
2225 | cf::async { |
2234 | cf::async { |
2226 | $Coro::current->{desc} = "map player save"; |
2235 | $Coro::current->{desc} = "map player save"; |
2227 | $_->contr->save for $self->players; |
2236 | $_->contr->save for $self->players; |
2228 | }; |
2237 | }; |
2229 | |
2238 | |
|
|
2239 | cf::get_slot 0.02; |
|
|
2240 | |
2230 | if ($uniq) { |
2241 | if ($uniq) { |
2231 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
2242 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
2232 | $self->_save_objects ($uniq, cf::IO_UNIQUES); |
2243 | $self->_save_objects ($uniq, cf::IO_UNIQUES); |
2233 | } else { |
2244 | } else { |
2234 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2245 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
… | |
… | |
2242 | $self->save; |
2253 | $self->save; |
2243 | |
2254 | |
2244 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2255 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2245 | |
2256 | |
2246 | return if $self->players; |
2257 | return if $self->players; |
2247 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
2258 | return if $self->in_memory != cf::MAP_ACTIVE; |
2248 | return if $self->{deny_save}; |
2259 | return if $self->{deny_save}; |
2249 | |
2260 | |
2250 | $self->in_memory (cf::MAP_SWAPPED); |
2261 | $self->in_memory (cf::MAP_SWAPPED); |
2251 | |
2262 | |
2252 | $self->deactivate; |
2263 | $self->deactivate; |
… | |
… | |
2370 | |
2381 | |
2371 | sub unique_maps() { |
2382 | sub unique_maps() { |
2372 | [ |
2383 | [ |
2373 | map { |
2384 | map { |
2374 | utf8::decode $_; |
2385 | utf8::decode $_; |
2375 | /\.map$/ |
2386 | s/\.map$//; # TODO future compatibility hack |
|
|
2387 | /\.pst$/ || !/^$PATH_SEP/o # TODO unique maps apparebntly lack the .map suffix :/ |
|
|
2388 | ? () |
2376 | ? normalise $_ |
2389 | : normalise $_ |
2377 | : () |
|
|
2378 | } @{ aio_readdir $UNIQUEDIR or [] } |
2390 | } @{ aio_readdir $UNIQUEDIR or [] } |
2379 | ] |
2391 | ] |
2380 | } |
2392 | } |
2381 | |
2393 | |
2382 | =back |
2394 | =back |
… | |
… | |
2389 | |
2401 | |
2390 | =over 4 |
2402 | =over 4 |
2391 | |
2403 | |
2392 | =item $ob->inv_recursive |
2404 | =item $ob->inv_recursive |
2393 | |
2405 | |
2394 | Returns the inventory of the object _and_ their inventories, recursively. |
2406 | Returns the inventory of the object I<and> their inventories, recursively, |
|
|
2407 | but I<not> the object itself. |
2395 | |
2408 | |
2396 | =cut |
2409 | =cut |
2397 | |
2410 | |
2398 | sub inv_recursive_; |
2411 | sub inv_recursive_; |
2399 | sub inv_recursive_ { |
2412 | sub inv_recursive_ { |
… | |
… | |
2404 | inv_recursive_ inv $_[0] |
2417 | inv_recursive_ inv $_[0] |
2405 | } |
2418 | } |
2406 | |
2419 | |
2407 | =item $ref = $ob->ref |
2420 | =item $ref = $ob->ref |
2408 | |
2421 | |
2409 | creates and returns a persistent reference to an objetc that can be stored as a string. |
2422 | Creates and returns a persistent reference to an object that can be stored as a string. |
2410 | |
2423 | |
2411 | =item $ob = cf::object::deref ($refstring) |
2424 | =item $ob = cf::object::deref ($refstring) |
2412 | |
2425 | |
2413 | returns the objetc referenced by refstring. may return undef when it cnanot find the object, |
2426 | returns the objetc referenced by refstring. may return undef when it cnanot find the object, |
2414 | even if the object actually exists. May block. |
2427 | even if the object actually exists. May block. |
… | |
… | |
2446 | can be C<undef>. Does the right thing when the player is currently in a |
2459 | can be C<undef>. Does the right thing when the player is currently in a |
2447 | dialogue with the given NPC character. |
2460 | dialogue with the given NPC character. |
2448 | |
2461 | |
2449 | =cut |
2462 | =cut |
2450 | |
2463 | |
|
|
2464 | our $SAY_CHANNEL = { |
|
|
2465 | id => "say", |
|
|
2466 | title => "Map", |
|
|
2467 | reply => "say ", |
|
|
2468 | tooltip => "Things said to and replied from npcs near you and other players on the same map only.", |
|
|
2469 | }; |
|
|
2470 | |
|
|
2471 | our $CHAT_CHANNEL = { |
|
|
2472 | id => "chat", |
|
|
2473 | title => "Chat", |
|
|
2474 | reply => "chat ", |
|
|
2475 | tooltip => "Player chat and shouts, global to the server.", |
|
|
2476 | }; |
|
|
2477 | |
2451 | # rough implementation of a future "reply" method that works |
2478 | # rough implementation of a future "reply" method that works |
2452 | # with dialog boxes. |
2479 | # with dialog boxes. |
2453 | #TODO: the first argument must go, split into a $npc->reply_to ( method |
2480 | #TODO: the first argument must go, split into a $npc->reply_to ( method |
2454 | sub cf::object::player::reply($$$;$) { |
2481 | sub cf::object::player::reply($$$;$) { |
2455 | my ($self, $npc, $msg, $flags) = @_; |
2482 | my ($self, $npc, $msg, $flags) = @_; |
… | |
… | |
2466 | my $dialog = $pl->{npc_dialog}; |
2493 | my $dialog = $pl->{npc_dialog}; |
2467 | $dialog->{pl}->ext_msg ($dialog->{id}, update => msg => $dialog->{pl}->expand_cfpod ($msg)); |
2494 | $dialog->{pl}->ext_msg ($dialog->{id}, update => msg => $dialog->{pl}->expand_cfpod ($msg)); |
2468 | |
2495 | |
2469 | } else { |
2496 | } else { |
2470 | $msg = $npc->name . " says: $msg" if $npc; |
2497 | $msg = $npc->name . " says: $msg" if $npc; |
2471 | $self->message ($msg, $flags); |
2498 | $self->send_msg ($SAY_CHANNEL => $msg, $flags); |
2472 | } |
2499 | } |
2473 | } |
2500 | } |
2474 | } |
2501 | } |
2475 | |
2502 | |
2476 | =item $object->send_msg ($channel, $msg, $color, [extra...]) |
2503 | =item $object->send_msg ($channel, $msg, $color, [extra...]) |
… | |
… | |
2688 | $rmp->{origin_y} = $exit->y; |
2715 | $rmp->{origin_y} = $exit->y; |
2689 | } |
2716 | } |
2690 | |
2717 | |
2691 | $rmp->{random_seed} ||= $exit->random_seed; |
2718 | $rmp->{random_seed} ||= $exit->random_seed; |
2692 | |
2719 | |
2693 | my $data = cf::encode_json $rmp; |
2720 | my $data = JSON::XS->new->utf8->pretty->canonical->encode ($rmp); |
2694 | my $md5 = Digest::MD5::md5_hex $data; |
2721 | my $md5 = Digest::MD5::md5_hex $data; |
2695 | my $meta = "$RANDOMDIR/$md5.meta"; |
2722 | my $meta = "$RANDOMDIR/$md5.meta"; |
2696 | |
2723 | |
2697 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2724 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2698 | aio_write $fh, 0, (length $data), $data, 0; |
2725 | aio_write $fh, 0, (length $data), $data, 0; |
… | |
… | |
2821 | id => "infobox", |
2848 | id => "infobox", |
2822 | title => "Map Info", |
2849 | title => "Map Info", |
2823 | reply => undef, |
2850 | reply => undef, |
2824 | tooltip => "Information related to the maps", |
2851 | tooltip => "Information related to the maps", |
2825 | }, |
2852 | }, |
|
|
2853 | "c/party" => { |
|
|
2854 | id => "party", |
|
|
2855 | title => "Party", |
|
|
2856 | reply => "gsay ", |
|
|
2857 | tooltip => "Messages and chat related to your party", |
|
|
2858 | }, |
2826 | ); |
2859 | ); |
2827 | |
2860 | |
2828 | sub cf::client::send_msg { |
2861 | sub cf::client::send_msg { |
2829 | my ($self, $channel, $msg, $color, @extra) = @_; |
2862 | my ($self, $channel, $msg, $color, @extra) = @_; |
2830 | |
2863 | |
… | |
… | |
3067 | |
3100 | |
3068 | The following functions and methods are available within a safe environment: |
3101 | The following functions and methods are available within a safe environment: |
3069 | |
3102 | |
3070 | cf::object |
3103 | cf::object |
3071 | contr pay_amount pay_player map x y force_find force_add destroy |
3104 | contr pay_amount pay_player map x y force_find force_add destroy |
3072 | insert remove name archname title slaying race decrease_ob_nr |
3105 | insert remove name archname title slaying race decrease split |
3073 | |
3106 | |
3074 | cf::object::player |
3107 | cf::object::player |
3075 | player |
3108 | player |
3076 | |
3109 | |
3077 | cf::player |
3110 | cf::player |
… | |
… | |
3083 | =cut |
3116 | =cut |
3084 | |
3117 | |
3085 | for ( |
3118 | for ( |
3086 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3119 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3087 | insert remove inv name archname title slaying race |
3120 | insert remove inv name archname title slaying race |
3088 | decrease_ob_nr destroy)], |
3121 | decrease split destroy)], |
3089 | ["cf::object::player" => qw(player)], |
3122 | ["cf::object::player" => qw(player)], |
3090 | ["cf::player" => qw(peaceful)], |
3123 | ["cf::player" => qw(peaceful)], |
3091 | ["cf::map" => qw(trigger)], |
3124 | ["cf::map" => qw(trigger)], |
3092 | ) { |
3125 | ) { |
3093 | no strict 'refs'; |
3126 | no strict 'refs'; |
… | |
… | |
3301 | while (my ($k, $v) = each %$want) { |
3334 | while (my ($k, $v) = each %$want) { |
3302 | $ns->fx_want ($k, $v); |
3335 | $ns->fx_want ($k, $v); |
3303 | } |
3336 | } |
3304 | }; |
3337 | }; |
3305 | |
3338 | |
|
|
3339 | sub load_resource_file($) { |
|
|
3340 | my $guard = lock_acquire "load_resource_file"; |
|
|
3341 | |
|
|
3342 | my $status = load_resource_file_ $_[0]; |
|
|
3343 | get_slot 0.1, 100; |
|
|
3344 | cf::arch::commit_load; |
|
|
3345 | |
|
|
3346 | $status |
|
|
3347 | } |
|
|
3348 | |
3306 | sub reload_regions { |
3349 | sub reload_regions { |
3307 | # HACK to clear player env face cache, we need some signal framework |
3350 | # HACK to clear player env face cache, we need some signal framework |
3308 | # for this (global event?) |
3351 | # for this (global event?) |
3309 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3352 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3310 | |
3353 | |
… | |
… | |
3323 | } |
3366 | } |
3324 | |
3367 | |
3325 | sub reload_archetypes { |
3368 | sub reload_archetypes { |
3326 | load_resource_file "$DATADIR/archetypes" |
3369 | load_resource_file "$DATADIR/archetypes" |
3327 | or die "unable to load archetypes\n"; |
3370 | or die "unable to load archetypes\n"; |
3328 | #d# NEED to laod twice to resolve forward references |
|
|
3329 | # this really needs to be done in an extra post-pass |
|
|
3330 | # (which needs to be synchronous, so solve it differently) |
|
|
3331 | load_resource_file "$DATADIR/archetypes" |
|
|
3332 | or die "unable to load archetypes\n"; |
|
|
3333 | } |
3371 | } |
3334 | |
3372 | |
3335 | sub reload_treasures { |
3373 | sub reload_treasures { |
3336 | load_resource_file "$DATADIR/treasures" |
3374 | load_resource_file "$DATADIR/treasures" |
3337 | or die "unable to load treasurelists\n"; |
3375 | or die "unable to load treasurelists\n"; |
3338 | } |
3376 | } |
3339 | |
3377 | |
3340 | sub reload_resources { |
3378 | sub reload_resources { |
3341 | warn "reloading resource files...\n"; |
3379 | warn "reloading resource files...\n"; |
3342 | |
3380 | |
|
|
3381 | reload_facedata; |
|
|
3382 | reload_archetypes; |
3343 | reload_regions; |
3383 | reload_regions; |
3344 | reload_facedata; |
|
|
3345 | #reload_archetypes;#d# |
|
|
3346 | reload_archetypes; |
|
|
3347 | reload_treasures; |
3384 | reload_treasures; |
3348 | |
3385 | |
3349 | warn "finished reloading resource files\n"; |
3386 | warn "finished reloading resource files\n"; |
3350 | } |
3387 | } |
3351 | |
3388 | |
3352 | sub init { |
3389 | sub init { |
|
|
3390 | my $guard = freeze_mainloop; |
|
|
3391 | |
3353 | reload_resources; |
3392 | reload_resources; |
3354 | } |
3393 | } |
3355 | |
3394 | |
3356 | sub reload_config { |
3395 | sub reload_config { |
3357 | open my $fh, "<:utf8", "$CONFDIR/config" |
3396 | open my $fh, "<:utf8", "$CONFDIR/config" |
… | |
… | |
3382 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3421 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3383 | EV::loop EV::LOOP_ONESHOT; |
3422 | EV::loop EV::LOOP_ONESHOT; |
3384 | })->prio (Coro::PRIO_MAX); |
3423 | })->prio (Coro::PRIO_MAX); |
3385 | }; |
3424 | }; |
3386 | |
3425 | |
|
|
3426 | { |
|
|
3427 | my $guard = freeze_mainloop; |
3387 | reload_config; |
3428 | reload_config; |
3388 | db_init; |
3429 | db_init; |
3389 | load_extensions; |
3430 | load_extensions; |
3390 | |
3431 | |
3391 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3432 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3392 | evthread_start IO::AIO::poll_fileno; |
3433 | evthread_start IO::AIO::poll_fileno; |
|
|
3434 | } |
|
|
3435 | |
3393 | EV::loop; |
3436 | EV::loop; |
3394 | } |
3437 | } |
3395 | |
3438 | |
3396 | ############################################################################# |
3439 | ############################################################################# |
3397 | # initialisation and cleanup |
3440 | # initialisation and cleanup |
… | |
… | |
3404 | cf::cleanup "SIG$signal"; |
3447 | cf::cleanup "SIG$signal"; |
3405 | }; |
3448 | }; |
3406 | } |
3449 | } |
3407 | } |
3450 | } |
3408 | |
3451 | |
3409 | sub write_runtime { |
3452 | sub write_runtime_sync { |
3410 | my $runtime = "$LOCALDIR/runtime"; |
3453 | my $runtime = "$LOCALDIR/runtime"; |
3411 | |
3454 | |
3412 | # first touch the runtime file to show we are still running: |
3455 | # first touch the runtime file to show we are still running: |
3413 | # the fsync below can take a very very long time. |
3456 | # the fsync below can take a very very long time. |
3414 | |
3457 | |
… | |
… | |
3440 | and return; |
3483 | and return; |
3441 | |
3484 | |
3442 | warn "runtime file written.\n"; |
3485 | warn "runtime file written.\n"; |
3443 | |
3486 | |
3444 | 1 |
3487 | 1 |
|
|
3488 | } |
|
|
3489 | |
|
|
3490 | our $uuid_lock; |
|
|
3491 | our $uuid_skip; |
|
|
3492 | |
|
|
3493 | sub write_uuid_sync($) { |
|
|
3494 | $uuid_skip ||= $_[0]; |
|
|
3495 | |
|
|
3496 | return if $uuid_lock; |
|
|
3497 | local $uuid_lock = 1; |
|
|
3498 | |
|
|
3499 | my $uuid = "$LOCALDIR/uuid"; |
|
|
3500 | |
|
|
3501 | my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 |
|
|
3502 | or return; |
|
|
3503 | |
|
|
3504 | my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; |
|
|
3505 | $uuid_skip = 0; |
|
|
3506 | |
|
|
3507 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
|
|
3508 | and return; |
|
|
3509 | |
|
|
3510 | # always fsync - this file is important |
|
|
3511 | aio_fsync $fh |
|
|
3512 | and return; |
|
|
3513 | |
|
|
3514 | close $fh |
|
|
3515 | or return; |
|
|
3516 | |
|
|
3517 | aio_rename "$uuid~", $uuid |
|
|
3518 | and return; |
|
|
3519 | |
|
|
3520 | warn "uuid file written ($value).\n"; |
|
|
3521 | |
|
|
3522 | 1 |
|
|
3523 | |
|
|
3524 | } |
|
|
3525 | |
|
|
3526 | sub write_uuid($$) { |
|
|
3527 | my ($skip, $sync) = @_; |
|
|
3528 | |
|
|
3529 | $sync ? write_uuid_sync $skip |
|
|
3530 | : async { write_uuid_sync $skip }; |
3445 | } |
3531 | } |
3446 | |
3532 | |
3447 | sub emergency_save() { |
3533 | sub emergency_save() { |
3448 | my $freeze_guard = cf::freeze_mainloop; |
3534 | my $freeze_guard = cf::freeze_mainloop; |
3449 | |
3535 | |
… | |
… | |
3471 | warn "end emergency map save\n"; |
3557 | warn "end emergency map save\n"; |
3472 | |
3558 | |
3473 | warn "begin emergency database checkpoint\n"; |
3559 | warn "begin emergency database checkpoint\n"; |
3474 | BDB::db_env_txn_checkpoint $DB_ENV; |
3560 | BDB::db_env_txn_checkpoint $DB_ENV; |
3475 | warn "end emergency database checkpoint\n"; |
3561 | warn "end emergency database checkpoint\n"; |
|
|
3562 | |
|
|
3563 | warn "begin write uuid\n"; |
|
|
3564 | write_uuid_sync 1; |
|
|
3565 | warn "end write uuid\n"; |
3476 | }; |
3566 | }; |
3477 | |
3567 | |
3478 | warn "leave emergency perl save\n"; |
3568 | warn "leave emergency perl save\n"; |
3479 | } |
3569 | } |
3480 | |
3570 | |
… | |
… | |
3495 | warn "reloading..."; |
3585 | warn "reloading..."; |
3496 | |
3586 | |
3497 | warn "entering sync_job"; |
3587 | warn "entering sync_job"; |
3498 | |
3588 | |
3499 | cf::sync_job { |
3589 | cf::sync_job { |
3500 | cf::write_runtime; # external watchdog should not bark |
3590 | cf::write_runtime_sync; # external watchdog should not bark |
3501 | cf::emergency_save; |
3591 | cf::emergency_save; |
3502 | cf::write_runtime; # external watchdog should not bark |
3592 | cf::write_runtime_sync; # external watchdog should not bark |
3503 | |
3593 | |
3504 | warn "syncing database to disk"; |
3594 | warn "syncing database to disk"; |
3505 | BDB::db_env_txn_checkpoint $DB_ENV; |
3595 | BDB::db_env_txn_checkpoint $DB_ENV; |
3506 | |
3596 | |
3507 | # if anything goes wrong in here, we should simply crash as we already saved |
3597 | # if anything goes wrong in here, we should simply crash as we already saved |
… | |
… | |
3632 | |
3722 | |
3633 | our @WAIT_FOR_TICK; |
3723 | our @WAIT_FOR_TICK; |
3634 | our @WAIT_FOR_TICK_BEGIN; |
3724 | our @WAIT_FOR_TICK_BEGIN; |
3635 | |
3725 | |
3636 | sub wait_for_tick { |
3726 | sub wait_for_tick { |
3637 | return if tick_inhibit; |
|
|
3638 | return if $Coro::current == $Coro::main; |
3727 | return if tick_inhibit || $Coro::current == $Coro::main; |
3639 | |
3728 | |
3640 | my $signal = new Coro::Signal; |
3729 | my $signal = new Coro::Signal; |
3641 | push @WAIT_FOR_TICK, $signal; |
3730 | push @WAIT_FOR_TICK, $signal; |
3642 | $signal->wait; |
3731 | $signal->wait; |
3643 | } |
3732 | } |
3644 | |
3733 | |
3645 | sub wait_for_tick_begin { |
3734 | sub wait_for_tick_begin { |
3646 | return if tick_inhibit; |
|
|
3647 | return if $Coro::current == $Coro::main; |
3735 | return if tick_inhibit || $Coro::current == $Coro::main; |
3648 | |
3736 | |
3649 | my $signal = new Coro::Signal; |
3737 | my $signal = new Coro::Signal; |
3650 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3738 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3651 | $signal->wait; |
3739 | $signal->wait; |
3652 | } |
3740 | } |
… | |
… | |
3662 | |
3750 | |
3663 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3751 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3664 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3752 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3665 | Coro::async_pool { |
3753 | Coro::async_pool { |
3666 | $Coro::current->{desc} = "runtime saver"; |
3754 | $Coro::current->{desc} = "runtime saver"; |
3667 | write_runtime |
3755 | write_runtime_sync |
3668 | or warn "ERROR: unable to write runtime file: $!"; |
3756 | or warn "ERROR: unable to write runtime file: $!"; |
3669 | }; |
3757 | }; |
3670 | } |
3758 | } |
3671 | |
3759 | |
3672 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3760 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |