--- deliantra/server/ext/dm-support.ext 2007/01/12 01:29:23 1.3 +++ deliantra/server/ext/dm-support.ext 2007/04/18 14:24:09 1.5 @@ -8,34 +8,65 @@ sub tcp_serve($) { my ($fh) = @_; + select $fh; + binmode $fh, ":raw:perlio:utf8"; my $buf; my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z); my (%l, @l); # for use by shell users - print $fh "Welcome!\n", - "Remember that everything entered here will be in the main coro context within cf::!\n", - "You can freely use \$a .. \$z and \@l and \%l\n"; - print $fh "\n> "; + print < "; Event->io (fd => $fh, poll => 'r', data => 0, cb => sub { if (defined (my $cmd = <$fh>)) { - my $old_fh = select $fh; + $cmd =~ s/\s+$//; if ($cmd =~ /^\s*exit\b/i) { print "will not exit() server.\n"; } else { - package cf; - my @res = eval $cmd; - warn "tcpshell evaluation error: $@" if $@; - print $@ if $@; - - print cf::dumpval @res > 1 ? \@res : $res[0] if @res; + my $sub = sub { + package cf; + select $fh; + + # compile first, the execute, as Coro does not support switching in eval string + my $cb = eval "sub { $cmd }"; + + my $t1 = Time::HiRes::time; + my @res = $@ ? () : eval { $cb->() }; + my $t2 = Time::HiRes::time; + + print "\n", + "command: '$cmd'\n", + "execution time: ", $t2 - $t1, "\n"; + warn "evaluation error: $@" if $@; + print "evaluation error: $@\n" if $@; + print "result:\n", cf::dumpval @res > 1 ? \@res : $res[0] if @res; + print "\n> "; + }; + + if ($cmd =~ s/\s*&$//) { + Coro::async_pool { $sub->() }; + } else { + $sub->(); + } } print "\n> "; - select $old_fh; } else { $_[0]->w->cancel; }