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

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.298 by root, Sun Jul 8 14:50:07 2007 UTC vs.
Revision 1.314 by root, Mon Jul 23 16:53:15 2007 UTC

29use Fcntl; 29use Fcntl;
30use YAML::Syck (); 30use YAML::Syck ();
31use IO::AIO 2.32 (); 31use IO::AIO 2.32 ();
32use Time::HiRes; 32use Time::HiRes;
33use Compress::LZF; 33use Compress::LZF;
34use Digest::MD5 ();
34 35
35# configure various modules to our taste 36# configure various modules to our taste
36# 37#
37$Storable::canonical = 1; # reduce rsync transfers 38$Storable::canonical = 1; # reduce rsync transfers
38Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator 39Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator
165=item cf::wait_for_tick, cf::wait_for_tick_begin 166=item cf::wait_for_tick, cf::wait_for_tick_begin
166 167
167These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only 168These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
168returns directly I<after> the tick processing (and consequently, can only wake one process 169returns directly I<after> the tick processing (and consequently, can only wake one process
169per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 170per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
171
172=item @cf::INVOKE_RESULTS
173
174This array contains the results of the last C<invoke ()> call. When
175C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
176that call.
170 177
171=back 178=back
172 179
173=cut 180=cut
174 181
315 }; 322 };
316 $TICK_WATCHER->stop; 323 $TICK_WATCHER->stop;
317 $guard 324 $guard
318} 325}
319 326
327=item cf::get_slot $time[, $priority]
328
329Allocate $time seconds of blocking CPU time at priority C<$priority>:
330This call blocks and returns only when you have at least C<$time> seconds
331of cpu time till the next tick. The slot is only valid till the next cede.
332
333Useful for short background jobs.
334
335=cut
336
337our @SLOT_QUEUE;
338our $SLOT_QUEUE;
339
340$SLOT_QUEUE->cancel if $SLOT_QUEUE;
341$SLOT_QUEUE = Coro::async {
342 my $signal = new Coro::Signal;
343
344 while () {
345 next_job:
346 my $avail = cf::till_tick;
347 if ($avail > 0.01) {
348 for (0 .. $#SLOT_QUEUE) {
349 if ($SLOT_QUEUE[$_][0] < $avail) {
350 my $job = splice @SLOT_QUEUE, $_, 1, ();
351 $job->[2]->send;
352 Coro::cede;
353 goto next_job;
354 }
355 }
356 }
357
358 if (@SLOT_QUEUE) {
359 # we do not use wait_For_tick() as it returns immediately when tick is inactive
360 push @cf::WAIT_FOR_TICK, $signal;
361 $signal->wait;
362 } else {
363 Coro::schedule;
364 }
365 }
366};
367
368sub get_slot($;$) {
369 my ($time, $pri) = @_;
370
371 push @SLOT_QUEUE, [$time, $pri, my $sig = new Coro::Signal];
372 @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE;
373 $SLOT_QUEUE->ready;
374 $sig->wait;
375}
376
320=item cf::async { BLOCK } 377=item cf::async { BLOCK }
321 378
322Currently the same as Coro::async_pool, meaning you cannot use 379Currently the same as Coro::async_pool, meaning you cannot use
323C<on_destroy>, C<join> or other gimmicks on these coroutines. The only 380C<on_destroy>, C<join> or other gimmicks on these coroutines. The only
324thing you are allowed to do is call C<prio> on it. 381thing you are allowed to do is call C<prio> on it.
409Coro::Storable. May, of course, block. Note that the executed sub may 466Coro::Storable. May, of course, block. Note that the executed sub may
410never block itself or use any form of Event handling. 467never block itself or use any form of Event handling.
411 468
412=cut 469=cut
413 470
471sub _store_scalar {
472 open my $fh, ">", \my $buf
473 or die "fork_call: cannot open fh-to-buf in child : $!";
474 Storable::store_fd $_[0], $fh;
475 close $fh;
476
477 $buf
478}
479
414sub fork_call(&@) { 480sub fork_call(&@) {
415 my ($cb, @args) = @_; 481 my ($cb, @args) = @_;
416 482
417# socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC 483# socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
418# or die "socketpair: $!"; 484# or die "socketpair: $!";
433 return wantarray ? @$res : $res->[-1]; 499 return wantarray ? @$res : $res->[-1];
434 } else { 500 } else {
435 reset_signals; 501 reset_signals;
436 local $SIG{__WARN__}; 502 local $SIG{__WARN__};
437 local $SIG{__DIE__}; 503 local $SIG{__DIE__};
504 # just in case, this hack effectively disables event
505 # in the child. cleaner and slower would be canceling all watchers,
506 # but this works for the time being.
438 local $Coro::idle; 507 local $Coro::idle;
439 $Coro::current->prio (Coro::PRIO_MAX); 508 $Coro::current->prio (Coro::PRIO_MAX);
509
440 eval { 510 eval {
441 close $fh1; 511 close $fh1;
442 512
443 my @res = eval { $cb->(@args) }; 513 my @res = eval { $cb->(@args) };
444 514
445 open my $fh, ">", \my $buf 515 syswrite $fh2, _store_scalar $@ ? \"$@" : \@res;
446 or die "fork_call: cannot open fh-to-buf in child : $!";
447 Storable::store_fd +($@ ? \"$@" : \@res), $fh;
448 close $fh;
449
450 warn "writing ", length $buf;
451 my $x;
452 (length $buf) == ($x = syswrite $fh2, $buf)
453 or warn "error writing ".(length $buf)." != $x\n";
454 close $fh2; 516 close $fh2;
455 }; 517 };
456 518
457 warn $@ if $@; 519 warn $@ if $@;
458 _exit 0; 520 _exit 0;
860 "; 922 ";
861 die if $@; 923 die if $@;
862} 924}
863 925
864our $override; 926our $override;
865our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? 927our @INVOKE_RESULTS = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
866 928
867sub override { 929sub override {
868 $override = 1; 930 $override = 1;
869 @invoke_results = (); 931 @INVOKE_RESULTS = (@_);
870} 932}
871 933
872sub do_invoke { 934sub do_invoke {
873 my $event = shift; 935 my $event = shift;
874 my $callbacks = shift; 936 my $callbacks = shift;
875 937
876 @invoke_results = (); 938 @INVOKE_RESULTS = ();
877 939
878 local $override; 940 local $override;
879 941
880 for (@$callbacks) { 942 for (@$callbacks) {
881 eval { &{$_->[1]} }; 943 eval { &{$_->[1]} };
898 960
899Generate an object-specific event with the given arguments. 961Generate an object-specific event with the given arguments.
900 962
901This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be 963This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be
902removed in future versions), and there is no public API to access override 964removed in future versions), and there is no public API to access override
903results (if you must, access C<@cf::invoke_results> directly). 965results (if you must, access C<@cf::INVOKE_RESULTS> directly).
904 966
905=back 967=back
906 968
907=cut 969=cut
908 970
909############################################################################# 971#############################################################################
910# object support 972# object support
973#
974
975sub _can_merge {
976 my ($ob1, $ob2) = @_;
977
978 local $Storable::canonical = 1;
979 my $fob1 = Storable::freeze $ob1;
980 my $fob2 = Storable::freeze $ob2;
981
982 $fob1 eq $fob2
983}
911 984
912sub reattach { 985sub reattach {
913 # basically do the same as instantiate, without calling instantiate 986 # basically do the same as instantiate, without calling instantiate
914 my ($obj) = @_; 987 my ($obj) = @_;
915 988
1483 my ($path) = @_; 1556 my ($path) = @_;
1484 1557
1485 my ($match, $specificity); 1558 my ($match, $specificity);
1486 1559
1487 for my $region (list) { 1560 for my $region (list) {
1488 if ($region->match && $path =~ $region->match) { 1561 if ($region->{match} && $path =~ $region->{match}) {
1489 ($match, $specificity) = ($region, $region->specificity) 1562 ($match, $specificity) = ($region, $region->specificity)
1490 if $region->specificity > $specificity; 1563 if $region->specificity > $specificity;
1491 } 1564 }
1492 } 1565 }
1493 1566
1613 $self->init; # pass $1 etc. 1686 $self->init; # pass $1 etc.
1614 return $self; 1687 return $self;
1615 } 1688 }
1616 } 1689 }
1617 1690
1618 Carp::carp "unable to resolve path '$path' (base '$base')."; 1691 Carp::cluck "unable to resolve path '$path' (base '$base').";
1619 () 1692 ()
1620} 1693}
1621 1694
1622sub init { 1695sub init {
1623 my ($self) = @_; 1696 my ($self) = @_;
1901 1974
1902 undef $MAP_PREFETCH{$path}; 1975 undef $MAP_PREFETCH{$path};
1903 $MAP_PREFETCHER ||= cf::async { 1976 $MAP_PREFETCHER ||= cf::async {
1904 while (%MAP_PREFETCH) { 1977 while (%MAP_PREFETCH) {
1905 for my $path (keys %MAP_PREFETCH) { 1978 for my $path (keys %MAP_PREFETCH) {
1906 my $map = find $path 1979 if (my $map = find $path) {
1907 or next;
1908 $map->load; 1980 $map->load;
1981 }
1909 1982
1910 delete $MAP_PREFETCH{$path}; 1983 delete $MAP_PREFETCH{$path};
1911 } 1984 }
1912 } 1985 }
1913 undef $MAP_PREFETCHER; 1986 undef $MAP_PREFETCHER;
2250 2323
2251 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2324 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2252 $self->enter_map ($map, $x, $y); 2325 $self->enter_map ($map, $x, $y);
2253} 2326}
2254 2327
2255=item $player_object->goto ($path, $x, $y[, $check->($map)]) 2328=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]])
2256 2329
2257Moves the player to the given map-path and coordinates by first freezing 2330Moves the player to the given map-path and coordinates by first freezing
2258her, loading and preparing them map, calling the provided $check callback 2331her, loading and preparing them map, calling the provided $check callback
2259that has to return the map if sucecssful, and then unfreezes the player on 2332that has to return the map if sucecssful, and then unfreezes the player on
2260the new (success) or old (failed) map position. 2333the new (success) or old (failed) map position. In either case, $done will
2334be called at the end of this process.
2261 2335
2262=cut 2336=cut
2263 2337
2264our $GOTOGEN; 2338our $GOTOGEN;
2265 2339
2266sub cf::object::player::goto { 2340sub cf::object::player::goto {
2267 my ($self, $path, $x, $y, $check) = @_; 2341 my ($self, $path, $x, $y, $check, $done) = @_;
2268 2342
2269 # do generation counting so two concurrent goto's will be executed in-order 2343 # do generation counting so two concurrent goto's will be executed in-order
2270 my $gen = $self->{_goto_generation} = ++$GOTOGEN; 2344 my $gen = $self->{_goto_generation} = ++$GOTOGEN;
2271 2345
2272 $self->enter_link; 2346 $self->enter_link;
2292 2366
2293 if ($gen == $self->{_goto_generation}) { 2367 if ($gen == $self->{_goto_generation}) {
2294 delete $self->{_goto_generation}; 2368 delete $self->{_goto_generation};
2295 $self->leave_link ($map, $x, $y); 2369 $self->leave_link ($map, $x, $y);
2296 } 2370 }
2371
2372 $done->() if $done;
2297 })->prio (1); 2373 })->prio (1);
2298} 2374}
2299 2375
2300=item $player_object->enter_exit ($exit_object) 2376=item $player_object->enter_exit ($exit_object)
2301 2377
2410 2486
2411 utf8::encode $text; 2487 utf8::encode $text;
2412 $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); 2488 $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text);
2413} 2489}
2414 2490
2415=item $client->send_msg ($color, $type, $msg, [extra...]) 2491=item $client->send_msg ($channel, $msg, $color, [extra...])
2416 2492
2417Send a drawinfo or msg packet to the client, formatting the msg for the 2493Send a drawinfo or msg packet to the client, formatting the msg for the
2418client if neccessary. C<$type> should be a string identifying the type of 2494client if neccessary. C<$type> should be a string identifying the type of
2419the message, with C<log> being the default. If C<$color> is negative, suppress 2495the message, with C<log> being the default. If C<$color> is negative, suppress
2420the message unless the client supports the msg packet. 2496the message unless the client supports the msg packet.
2421 2497
2422=cut 2498=cut
2423 2499
2424sub cf::client::send_msg { 2500sub cf::client::send_msg {
2425 my ($self, $color, $type, $msg, @extra) = @_; 2501 my ($self, $channel, $msg, $color, @extra) = @_;
2426 2502
2427 $msg = $self->pl->expand_cfpod ($msg); 2503 $msg = $self->pl->expand_cfpod ($msg);
2428 2504
2505 $color &= ~cf::NDI_UNIQUE; # just in case...
2506
2507 if (ref $channel) {
2508 # send meta info to client, if not yet sent
2509 unless (exists $self->{channel}{$channel->{id}}) {
2510 $self->{channel}{$channel->{id}} = $channel;
2511 $self->ext_event (channel_info => %$channel);
2512 }
2513
2514 $channel = $channel->{id};
2515 }
2516
2429 return unless @extra || length $msg; 2517 return unless @extra || length $msg;
2430 2518
2431 if ($self->can_msg) { 2519 if ($self->can_msg) {
2432 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra])); 2520 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra]));
2433 } else { 2521 } else {
2434 # replace some tags by gcfclient-compatible ones 2522 # replace some tags by gcfclient-compatible ones
2435 for ($msg) { 2523 for ($msg) {
2436 1 while 2524 1 while
2437 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2525 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2458 2546
2459=cut 2547=cut
2460 2548
2461sub cf::client::ext_event($$%) { 2549sub cf::client::ext_event($$%) {
2462 my ($self, $type, %msg) = @_; 2550 my ($self, $type, %msg) = @_;
2551
2552 return unless $self->extcmd;
2463 2553
2464 $msg{msgtype} = "event_$type"; 2554 $msg{msgtype} = "event_$type";
2465 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); 2555 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg));
2466} 2556}
2467 2557
2714 { 2804 {
2715 my $faces = $facedata->{faceinfo}; 2805 my $faces = $facedata->{faceinfo};
2716 2806
2717 while (my ($face, $info) = each %$faces) { 2807 while (my ($face, $info) = each %$faces) {
2718 my $idx = (cf::face::find $face) || cf::face::alloc $face; 2808 my $idx = (cf::face::find $face) || cf::face::alloc $face;
2719 cf::face::set $idx, $info->{visibility}, $info->{magicmap}; 2809 cf::face::set_visibility $idx, $info->{visibility};
2810 cf::face::set_magicmap $idx, $info->{magicmap};
2720 cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; 2811 cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32};
2721 cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; 2812 cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64};
2722 Coro::cede; 2813
2814 cf::cede_to_tick;
2723 } 2815 }
2724 2816
2725 while (my ($face, $info) = each %$faces) { 2817 while (my ($face, $info) = each %$faces) {
2726 next unless $info->{smooth}; 2818 next unless $info->{smooth};
2727 my $idx = cf::face::find $face 2819 my $idx = cf::face::find $face
2728 or next; 2820 or next;
2729 if (my $smooth = cf::face::find $info->{smooth}) { 2821 if (my $smooth = cf::face::find $info->{smooth}) {
2822 cf::face::set_smooth $idx, $smooth;
2730 cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; 2823 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
2731 } else { 2824 } else {
2732 warn "smooth face '$info->{smooth}' not found for face '$face'"; 2825 warn "smooth face '$info->{smooth}' not found for face '$face'";
2733 } 2826 }
2734 Coro::cede; 2827
2828 cf::cede_to_tick;
2735 } 2829 }
2736 } 2830 }
2737 2831
2738 { 2832 {
2739 my $anims = $facedata->{animinfo}; 2833 my $anims = $facedata->{animinfo};
2740 2834
2741 while (my ($anim, $info) = each %$anims) { 2835 while (my ($anim, $info) = each %$anims) {
2742 cf::anim::set $anim, $info->{frames}, $info->{facings}; 2836 cf::anim::set $anim, $info->{frames}, $info->{facings};
2743 Coro::cede; 2837 cf::cede_to_tick;
2744 } 2838 }
2745 2839
2746 cf::anim::invalidate_all; # d'oh 2840 cf::anim::invalidate_all; # d'oh
2841 }
2842
2843 {
2844 # TODO: for gcfclient pleasure, we should give resources
2845 # that gcfclient doesn't grok a >10000 face index.
2846 my $res = $facedata->{resource};
2847 my $enc = JSON::XS->new->utf8->canonical;
2848
2849 while (my ($name, $info) = each %$res) {
2850 my $meta = $enc->encode ({
2851 name => $name,
2852 type => $info->{type},
2853 copyright => $info->{copyright}, #TODO#
2854 });
2855
2856 my $idx = (cf::face::find $name) || cf::face::alloc $name;
2857
2858 if ($name =~ /\.jpg$/) {
2859 cf::face::set_data $idx, 0, $info->{data}, $info->{chksum};#d# temp hack
2860 cf::face::set_data $idx, 1, $info->{data}, $info->{chksum};#d# temp hack
2861 } else {
2862 my $data = pack "(w/a*)*", $meta, $info->{data};
2863 my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata
2864
2865 cf::face::set_type $idx, 1;
2866 cf::face::set_data $idx, 0, $data, $chk;
2867 }
2868
2869 cf::cede_to_tick;
2870 }
2747 } 2871 }
2748 2872
2749 1 2873 1
2750} 2874}
2751 2875
2752sub reload_regions { 2876sub reload_regions {
2753 load_resource_file "$MAPDIR/regions" 2877 load_resource_file "$MAPDIR/regions"
2754 or die "unable to load regions file\n"; 2878 or die "unable to load regions file\n";
2879
2880 for (cf::region::list) {
2881 $_->{match} = qr/$_->{match}/
2882 if exists $_->{match};
2883 }
2755} 2884}
2756 2885
2757sub reload_facedata { 2886sub reload_facedata {
2758 load_facedata "$DATADIR/facedata" 2887 load_facedata "$DATADIR/facedata"
2759 or die "unable to load facedata\n"; 2888 or die "unable to load facedata\n";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines