ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp.ext
(Generate patch)

Comparing deliantra/server/ext/tcp.ext (file contents):
Revision 1.24 by root, Mon Nov 5 23:24:02 2012 UTC vs.
Revision 1.27 by root, Tue Nov 6 16:17:23 2012 UTC

7use Socket; 7use Socket;
8use AnyEvent::Socket; 8use AnyEvent::Socket;
9 9
10CONF BIND_ADDRESSES = [[undef, 13327]]; 10CONF BIND_ADDRESSES = [[undef, 13327]];
11 11
12our $MAX_DETECT = 32; # how many bytes to read to identify the protocol 12our $MAX_DETECT; # how many bytes to read to identify the protocol
13 13
14our @LISTENERS; 14our @LISTENERS;
15our @DETECTORS;
16our %DETECTORS;
15 17
16sub flash_policy_server { 18sub _update_detectors {
17 my ($fh) = @_; 19 $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS;
20}
18 21
19 # socket policy file, just write answer and hope the kernel accepts it in one go 22sub register($$$$) {
20 syswrite $fh, <<EOF . "\x00" 23 my ($name, $max_detect, $detect, $serve) = @_;
21<?xml version="1.0"?> 24
22<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"> 25 $DETECTORS{$name} = [$max_detect, $detect, $serve, $name];
23<cross-domain-policy> 26 _update_detectors;
24 <allow-access-from domain="*" to-ports="*"/> 27
25</cross-domain-policy> 28 Guard::guard {
26EOF 29 delete $DETECTORS{$name};
30 _update_detectors;
31 }
27} 32}
33
34our $deliantra_detector = ext::tcp::register deliantra => 10, sub {
35 /^..version /s
36}, sub {
37 my $ns = cf::client::create fileno $_[1], $_[0];
38 $ns->run;
39 $ns->inbuf_append ($_[2]);
40};
28 41
29for (@$BIND_ADDRESSES) { 42for (@$BIND_ADDRESSES) {
30 my ($host, $port) = @$_; 43 my ($host, $port) = @$_;
31 cf::info "listening on ", (format_hostport $host, $port), "\n"; 44 cf::info "listening on ", (format_hostport $host, $port), "\n";
32 45
37 my $lhost = AnyEvent::Socket::format_address 50 my $lhost = AnyEvent::Socket::format_address
38 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; 51 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1];
39 52
40 my $id = format_hostport $host, $port; 53 my $id = format_hostport $host, $port;
41 54
42 cf::info "$id: new connection\n" 55 cf::info "$id: accepted connection.\n"
43 if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs 56 if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs
44 57
45 my $buf; 58 my $buf;
46 my $w; $w = AE::io $fh, 0, sub { 59 my $w; $w = AE::io $fh, 0, sub {
47 my $len = sysread $fh, $buf, 512, length $buf; 60 my $len = sysread $fh, $buf, 512, length $buf;
48 61
49 if ($len) { 62 if ($len) {
50 if ($buf =~ /^..version /s) { # deliantra protocol 63 for ($buf) {
64 for my $v (values %DETECTORS) {
65 if (my $cb = $v->[1]()) {
51 undef $w; 66 undef $w;
67 cf::debug "$id: detected protocol $v->[3].\n";
68 $v->[2]($id, $fh, $buf);
69 return;
70 }
71 }
52 72
53 my $ns = cf::client::create fileno $fh, $host; 73 if (length >= $MAX_DETECT) { # unable to detect protocol
54 $ns->inbuf_append ($buf);
55
56 } elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket
57 undef $w; 74 undef $w;
58 75 cf::debug "$id: data received, but cannot detect protocol, closing.\n";
59 &ext::http::server ($id, $fh, $buf);
60
61 } elsif ($buf =~ /^<policy-file-request\/>\x00/) {
62 undef $w; 76 }
63
64 flash_policy_server $fh;
65
66 } elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol
67 undef $w;
68
69 cf::info "$id: protocol detection error\n";
70 } 77 }
71 } else { 78 } else {
72 undef $w; 79 undef $w;
73
74 cf::info "$id: read error during protocol detection\n"; 80 cf::info "$id: read error during protocol detection ($!)\n";
75 } 81 }
76 }; 82 };
77 }; 83 };
78} 84}
79 85

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines