… | |
… | |
57 | use JSON::XS 2.01 (); |
57 | use JSON::XS 2.01 (); |
58 | use BDB (); |
58 | use BDB (); |
59 | use Data::Dumper; |
59 | use Data::Dumper; |
60 | use Fcntl; |
60 | use Fcntl; |
61 | use YAML::XS (); |
61 | use YAML::XS (); |
|
|
62 | use CBOR::XS (); |
62 | use IO::AIO (); |
63 | use IO::AIO (); |
63 | use Compress::LZF; |
64 | use Compress::LZF; |
64 | use Digest::MD5 (); |
65 | use Digest::MD5 (); |
65 | |
66 | |
66 | AnyEvent::detect; |
67 | AnyEvent::detect; |
… | |
… | |
225 | =item $cf::RUNTIME |
226 | =item $cf::RUNTIME |
226 | |
227 | |
227 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
228 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
228 | every server tick. |
229 | every server tick. |
229 | |
230 | |
230 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
231 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
231 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
232 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
232 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
233 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
233 | |
234 | |
234 | Various directories - "/etc", read-only install directory, perl-library |
235 | Various directories - "/etc", read-only install directory, perl-library |
235 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
236 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
… | |
… | |
464 | |
465 | |
465 | sub decode_yaml($) { |
466 | sub decode_yaml($) { |
466 | fork_call { YAML::XS::Load $_[0] } @_ |
467 | fork_call { YAML::XS::Load $_[0] } @_ |
467 | } |
468 | } |
468 | |
469 | |
|
|
470 | =item $scalar = cf::decode_cbor $scalar |
|
|
471 | |
|
|
472 | Same as CBOR::XS::decode_cbor, but takes server ticks into account, so |
|
|
473 | blocks. For small amounts of data, C<CBOR::XS::decode_cbor> is the better |
|
|
474 | alternative. |
|
|
475 | |
|
|
476 | =cut |
|
|
477 | |
|
|
478 | sub decode_cbor($) { |
|
|
479 | # we assume 10mb/s minimum decoding speed (on a ~2ghz machine) |
|
|
480 | cf::get_slot +(length $_[0]) / 10_000_000, 0, "decode_cbor"; |
|
|
481 | CBOR::XS::decode_cbor $_[0] |
|
|
482 | } |
|
|
483 | |
469 | =item $scalar = cf::unlzf $scalar |
484 | =item $scalar = cf::unlzf $scalar |
470 | |
485 | |
471 | Same as Compress::LZF::compress, but takes server ticks into account, so |
486 | Same as Compress::LZF::compress, but takes server ticks into account, so |
472 | blocks. |
487 | blocks. |
473 | |
488 | |
… | |
… | |
510 | } |
525 | } |
511 | } |
526 | } |
512 | |
527 | |
513 | =item cf::lock_wait $string |
528 | =item cf::lock_wait $string |
514 | |
529 | |
515 | Wait until the given lock is available. See cf::lock_acquire. |
530 | Wait until the given lock is available. See cf::lock_acquire. |
516 | |
531 | |
517 | =item my $lock = cf::lock_acquire $string |
532 | =item my $lock = cf::lock_acquire $string |
518 | |
533 | |
519 | Wait until the given lock is available and then acquires it and returns |
534 | Wait until the given lock is available and then acquires it and returns |
520 | a L<Guard> object. If the guard object gets destroyed (goes out of scope, |
535 | a L<Guard> object. If the guard object gets destroyed (goes out of scope, |
… | |
… | |
649 | BEGIN { *async = \&Coro::async_pool } |
664 | BEGIN { *async = \&Coro::async_pool } |
650 | |
665 | |
651 | =item cf::sync_job { BLOCK } |
666 | =item cf::sync_job { BLOCK } |
652 | |
667 | |
653 | The design of Deliantra requires that the main coroutine ($Coro::main) |
668 | The design of Deliantra requires that the main coroutine ($Coro::main) |
654 | is always able to handle events or runnable, as Deliantra is only |
669 | is always able to handle events or is runnable, as Deliantra is only |
655 | partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not |
670 | partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not |
656 | acceptable. |
671 | acceptable. |
657 | |
672 | |
658 | If it must be done, put the blocking parts into C<sync_job>. This will run |
673 | If it must be done, put the blocking parts into C<sync_job>. This will run |
659 | the given BLOCK in another coroutine while waiting for the result. The |
674 | the given BLOCK in another coroutine while waiting for the result. The |
… | |
… | |
687 | |
702 | |
688 | while ($busy) { |
703 | while ($busy) { |
689 | if (Coro::nready) { |
704 | if (Coro::nready) { |
690 | Coro::cede_notself; |
705 | Coro::cede_notself; |
691 | } else { |
706 | } else { |
692 | EV::loop EV::LOOP_ONESHOT; |
707 | EV::run EV::RUN_ONCE; |
693 | } |
708 | } |
694 | } |
709 | } |
695 | |
710 | |
696 | my $time = AE::time - $time; |
711 | my $time = AE::time - $time; |
697 | |
712 | |
… | |
… | |
2384 | |
2399 | |
2385 | $MAP_PREFETCHER ||= cf::async { |
2400 | $MAP_PREFETCHER ||= cf::async { |
2386 | $Coro::current->{desc} = "map prefetcher"; |
2401 | $Coro::current->{desc} = "map prefetcher"; |
2387 | |
2402 | |
2388 | while (%MAP_PREFETCH) { |
2403 | while (%MAP_PREFETCH) { |
2389 | while (my ($k, $v) = each %MAP_PREFETCH) { |
2404 | for my $k (keys %MAP_PREFETCH) { |
2390 | if (my $map = find $k) { |
2405 | if (my $map = find $k) { |
2391 | $map->load if $v; |
2406 | $map->load if $MAP_PREFETCH{$k}; |
2392 | } |
2407 | } |
2393 | |
2408 | |
2394 | delete $MAP_PREFETCH{$k}; |
2409 | delete $MAP_PREFETCH{$k}; |
2395 | } |
2410 | } |
2396 | } |
2411 | } |
… | |
… | |
3915 | seek $fh, 0, 0; |
3930 | seek $fh, 0, 0; |
3916 | print $fh $$; |
3931 | print $fh $$; |
3917 | } |
3932 | } |
3918 | |
3933 | |
3919 | sub main_loop { |
3934 | sub main_loop { |
3920 | trace "EV::loop starting\n"; |
3935 | trace "EV::run starting\n"; |
3921 | if (1) { |
3936 | if (1) { |
3922 | EV::loop; |
3937 | EV::run; |
3923 | } |
3938 | } |
3924 | trace "EV::loop returned\n"; |
3939 | trace "EV::run returned\n"; |
3925 | goto &main_loop unless $REALLY_UNLOOP; |
3940 | goto &main_loop unless $REALLY_UNLOOP; |
3926 | } |
3941 | } |
3927 | |
3942 | |
3928 | sub main { |
3943 | sub main { |
3929 | cf::init_globals; # initialise logging |
3944 | cf::init_globals; # initialise logging |
… | |
… | |
3938 | # we must not ever block the main coroutine |
3953 | # we must not ever block the main coroutine |
3939 | $Coro::idle = sub { |
3954 | $Coro::idle = sub { |
3940 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3955 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3941 | (async { |
3956 | (async { |
3942 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3957 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3943 | EV::loop EV::LOOP_ONESHOT; |
3958 | EV::run EV::RUN_ONCE; |
3944 | })->prio (Coro::PRIO_MAX); |
3959 | })->prio (Coro::PRIO_MAX); |
3945 | }; |
3960 | }; |
3946 | |
3961 | |
3947 | evthread_start IO::AIO::poll_fileno; |
3962 | evthread_start IO::AIO::poll_fileno; |
3948 | |
3963 | |