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.21 by root, Tue Oct 30 17:07:50 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 = 16; # how many bytes to raed 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;
17
18sub _update_detectors {
19 $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS;
20
21 use Data::Dump;
22 ddx [$MAX_DETECT, \%DETECTORS];
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};
15 43
16for (@$BIND_ADDRESSES) { 44for (@$BIND_ADDRESSES) {
17 my ($host, $port) = @$_; 45 my ($host, $port) = @$_;
18 cf::info "listening on ", (format_hostport $host, $port), "\n"; 46 cf::info "listening on ", (format_hostport $host, $port), "\n";
19 47
24 my $lhost = AnyEvent::Socket::format_address 52 my $lhost = AnyEvent::Socket::format_address
25 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; 53 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1];
26 54
27 my $id = format_hostport $host, $port; 55 my $id = format_hostport $host, $port;
28 56
29 cf::info "$id: new connection\n" 57 cf::info "$id: accepted connection.\n"
30 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
31 59
32 my $buf; 60 my $buf;
33 my $w; $w = AE::io $fh, 0, sub { 61 my $w; $w = AE::io $fh, 0, sub {
34 my $len = sysread $fh, $buf, 512, length $buf; 62 my $len = sysread $fh, $buf, 512, length $buf;
35 63
36 if ($len) { 64 if ($len) {
37 if ($buf =~ /^..version /s) { # deliantra protocol 65 for ($buf) {
66 while (my ($name, $v) = each %DETECTORS) {
67 if (my $cb = $v->[1]()) {
38 undef $w; 68 undef $w;
69 cf::debug "$id: detected protocol $name.\n";
70 $v->[2]($id, $fh, $buf);
71 return;
72 }
73 }
39 74
40 my $ns = cf::client::create fileno $fh, $host; 75 if (length >= $MAX_DETECT) { # unable to detect protocol
41 $ns->inbuf_append ($buf);
42
43 } elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket
44 undef $w; 76 undef $w;
45 77 cf::debug "$id: data received, but cannot detect protocol, closing.\n";
46 &ext::http::server ($fh, $buf);
47
48 } elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol
49 undef $w; 78 }
50
51 cf::info "$id: protocol detection error\n";
52 } 79 }
53 } else { 80 } else {
54 undef $w; 81 undef $w;
55
56 cf::info "$id: read error during protocol detection\n"; 82 cf::info "$id: read error during protocol detection ($!)\n";
57 } 83 }
58 }; 84 };
59 }; 85 };
60} 86}
61 87

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines