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.20 by root, Fri Feb 3 03:01:45 2012 UTC vs.
Revision 1.28 by root, Sun Nov 11 04:29:11 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; # how many bytes to read to identify the protocol
13
12our @LISTENERS; 14our @LISTENERS;
15our @DETECTORS;
16our %DETECTORS;
17
18sub _update_detectors {
19 $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS;
20}
21
22sub register($$$$) {
23 my ($name, $max_detect, $detect, $serve) = @_;
24
25 $DETECTORS{$name} = [$max_detect, $detect, $serve, $name];
26 _update_detectors;
27
28 Guard::guard {
29 delete $DETECTORS{$name};
30 _update_detectors if defined &_update_detectors;
31 }
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};
13 41
14for (@$BIND_ADDRESSES) { 42for (@$BIND_ADDRESSES) {
15 my ($host, $port) = @$_; 43 my ($host, $port) = @$_;
16 cf::info "listening on ", (format_hostport $host, $port), "\n"; 44 cf::info "listening on ", (format_hostport $host, $port), "\n";
17 45
20 or return; 48 or return;
21 49
22 my $lhost = AnyEvent::Socket::format_address 50 my $lhost = AnyEvent::Socket::format_address
23 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; 51 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1];
24 52
25 cf::info "new connection from ", (format_hostport $host, $port), "\n" 53 my $id = format_hostport $host, $port;
54
55 cf::info "$id: accepted connection.\n"
26 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
27 57
28 cf::client::create fileno $fh, $host; 58 my $buf;
59 my $w; $w = AE::io $fh, 0, sub {
60 my $len = sysread $fh, $buf, 512, length $buf;
61
62 if ($len) {
63 for ($buf) {
64 for my $v (values %DETECTORS) {
65 if (my $cb = $v->[1]()) {
66 undef $w;
67 cf::debug "$id: detected protocol $v->[3].\n";
68 $v->[2]($id, $fh, $buf);
69 return;
70 }
71 }
72
73 if (length >= $MAX_DETECT) { # unable to detect protocol
74 undef $w;
75 cf::debug "$id: data received, but cannot detect protocol, closing.\n";
76 }
77 }
78 } else {
79 undef $w;
80 cf::info "$id: read error during protocol detection ($!)\n";
81 }
82 };
29 }; 83 };
30} 84}
31 85

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines