ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/dm-support.ext
Revision: 1.9
Committed: Thu Sep 13 08:35:24 2007 UTC (16 years, 8 months ago) by root
Branch: MAIN
Changes since 1.8: +1 -0 lines
Log Message:
rent was calling blocking functions in the main coro. doh.; also, do some cleanups

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     use IO::Socket;
4     use Storable qw/nfreeze thaw/;
5    
6     my %global; # for use by eval'ed commands
7    
8     sub tcp_serve($) {
9     my ($fh) = @_;
10    
11 root 1.4 select $fh;
12    
13 root 1.1 binmode $fh, ":raw:perlio:utf8";
14    
15     my $buf;
16 root 1.2 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);
17     my (%l, @l); # for use by shell users
18 root 1.1
19 root 1.4 print <<EOF;
20     Welcome!
21     Remember that everything entered here will be in the main coro context within cf::!
22     You can freely use \$a .. \$z and \@l and \%l
23    
24     Useful commands (note the '&'):
25    
26     reload_perl &
27 root 1.8 reload_config &
28 root 1.4 reload_regions &
29     reload_facedata &
30     reload_treasures &
31     reload_archetypes &
32 root 1.6 ext::help::reload &
33     ext::books::reload &
34 root 1.9 ext::map_tags::reload &
35 root 1.5 ext::map_world::reload &
36 root 1.4 EOF
37     print "\n> ";
38 root 1.1
39 root 1.3 Event->io (fd => $fh, poll => 'r', data => 0, cb => sub {
40 root 1.1 if (defined (my $cmd = <$fh>)) {
41 root 1.4 $cmd =~ s/\s+$//;
42 root 1.1
43     if ($cmd =~ /^\s*exit\b/i) {
44     print "will not exit() server.\n";
45     } else {
46 root 1.4 my $sub = sub {
47     package cf;
48     select $fh;
49    
50     # compile first, the execute, as Coro does not support switching in eval string
51     my $cb = eval "sub { $cmd }";
52    
53     my $t1 = Time::HiRes::time;
54     my @res = $@ ? () : eval { $cb->() };
55     my $t2 = Time::HiRes::time;
56    
57     print "\n",
58     "command: '$cmd'\n",
59     "execution time: ", $t2 - $t1, "\n";
60     warn "evaluation error: $@" if $@;
61     print "evaluation error: $@\n" if $@;
62     print "result:\n", cf::dumpval @res > 1 ? \@res : $res[0] if @res;
63     print "\n> ";
64     };
65    
66     if ($cmd =~ s/\s*&$//) {
67 root 1.7 Coro::async { $sub->() };
68 root 1.4 } else {
69     $sub->();
70     }
71 root 1.1 }
72    
73 root 1.2 print "\n> ";
74 root 1.1 } else {
75     $_[0]->w->cancel;
76     }
77     });
78     }
79    
80     # now a shell listening on a tcp-port - let the firewall decide access rights
81     if ($cf::CFG{perl_shell}) {
82     if (my $listen = new IO::Socket::INET LocalAddr => $cf::CFG{perl_shell}, Listen => 1, ReuseAddr => 1) {
83     Event->io (fd => $listen, poll => 'r', data => cf::WF_AUTOCANCEL, cb => sub { tcp_serve $listen->accept });
84     }
85     }