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.25 by root, Tue Nov 6 01:25:48 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;
18 20
19 # socket policy file, just write answer and hope the kernel accepts it in one go 21 use Data::Dump;
20 syswrite $fh, <<EOF . "\x00" 22 ddx [$MAX_DETECT, \%DETECTORS];
21<?xml version="1.0"?>
22<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
23<cross-domain-policy>
24 <allow-access-from domain="*" to-ports="*"/>
25</cross-domain-policy>
26EOF
27} 23}
24
25sub register($$$$) {
26 my ($name, $max_detect, $detect, $serve) = @_;
27
28 $DETECTORS{$name} = [$max_detect, $detect, $serve];
29 _update_detectors;
30
31 Guard::guard {
32 delete $DETECTORS{$name};
33 _update_detectors;
34 }
35}
36
37our $deliantra_detector = ext::tcp::register deliantra => 10, sub {
38 /^..version /s
39}, sub {
40 my $ns = cf::client::create fileno $_[1], $_[0];
41 $ns->inbuf_append ($_[2]);
42};
28 43
29for (@$BIND_ADDRESSES) { 44for (@$BIND_ADDRESSES) {
30 my ($host, $port) = @$_; 45 my ($host, $port) = @$_;
31 cf::info "listening on ", (format_hostport $host, $port), "\n"; 46 cf::info "listening on ", (format_hostport $host, $port), "\n";
32 47
37 my $lhost = AnyEvent::Socket::format_address 52 my $lhost = AnyEvent::Socket::format_address
38 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; 53 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1];
39 54
40 my $id = format_hostport $host, $port; 55 my $id = format_hostport $host, $port;
41 56
42 cf::info "$id: new connection\n" 57 cf::info "$id: accepted connection.\n"
43 if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs 58 if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs
44 59
45 my $buf; 60 my $buf;
46 my $w; $w = AE::io $fh, 0, sub { 61 my $w; $w = AE::io $fh, 0, sub {
47 my $len = sysread $fh, $buf, 512, length $buf; 62 my $len = sysread $fh, $buf, 512, length $buf;
48 63
49 if ($len) { 64 if ($len) {
50 if ($buf =~ /^..version /s) { # deliantra protocol 65 for ($buf) {
66 while (my ($name, $v) = each %DETECTORS) {
67 if (my $cb = $v->[1]()) {
51 undef $w; 68 undef $w;
69 cf::debug "$id: detected protocol $name.\n";
70 $v->[2]($id, $fh, $buf);
71 return;
72 }
73 }
52 74
53 my $ns = cf::client::create fileno $fh, $host; 75 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; 76 undef $w;
58 77 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; 78 }
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 } 79 }
71 } else { 80 } else {
72 undef $w; 81 undef $w;
73
74 cf::info "$id: read error during protocol detection\n"; 82 cf::info "$id: read error during protocol detection ($!)\n";
75 } 83 }
76 }; 84 };
77 }; 85 };
78} 86}
79 87

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines