… | |
… | |
15 | our @DETECTORS; |
15 | our @DETECTORS; |
16 | our %DETECTORS; |
16 | our %DETECTORS; |
17 | |
17 | |
18 | sub _update_detectors { |
18 | sub _update_detectors { |
19 | $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS; |
19 | $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS; |
20 | |
|
|
21 | use Data::Dump; |
|
|
22 | ddx [$MAX_DETECT, \%DETECTORS]; |
|
|
23 | } |
20 | } |
24 | |
21 | |
25 | sub register($$$$) { |
22 | sub register($$$$) { |
26 | my ($name, $max_detect, $detect, $serve) = @_; |
23 | my ($name, $max_detect, $detect, $serve) = @_; |
27 | |
24 | |
28 | $DETECTORS{$name} = [$max_detect, $detect, $serve]; |
25 | $DETECTORS{$name} = [$max_detect, $detect, $serve, $name]; |
29 | _update_detectors; |
26 | _update_detectors; |
30 | |
27 | |
31 | Guard::guard { |
28 | Guard::guard { |
32 | delete $DETECTORS{$name}; |
29 | delete $DETECTORS{$name}; |
33 | _update_detectors; |
30 | _update_detectors if defined &_update_detectors; |
34 | } |
31 | } |
35 | } |
32 | } |
36 | |
33 | |
37 | our $deliantra_detector = ext::tcp::register deliantra => 10, sub { |
34 | our $deliantra_detector = ext::tcp::register deliantra => 10, sub { |
38 | /^..version /s |
35 | /^..version /s |
39 | }, sub { |
36 | }, sub { |
40 | my $ns = cf::client::create fileno $_[1], $_[0]; |
37 | my $ns = cf::client::create fileno $_[1], $_[0]; |
|
|
38 | $ns->run; |
41 | $ns->inbuf_append ($_[2]); |
39 | $ns->inbuf_append ($_[2]); |
42 | }; |
40 | }; |
43 | |
41 | |
44 | for (@$BIND_ADDRESSES) { |
42 | for (@$BIND_ADDRESSES) { |
45 | my ($host, $port) = @$_; |
43 | my ($host, $port) = @$_; |
… | |
… | |
61 | my $w; $w = AE::io $fh, 0, sub { |
59 | my $w; $w = AE::io $fh, 0, sub { |
62 | my $len = sysread $fh, $buf, 512, length $buf; |
60 | my $len = sysread $fh, $buf, 512, length $buf; |
63 | |
61 | |
64 | if ($len) { |
62 | if ($len) { |
65 | for ($buf) { |
63 | for ($buf) { |
66 | while (my ($name, $v) = each %DETECTORS) { |
64 | for my $v (values %DETECTORS) { |
67 | if (my $cb = $v->[1]()) { |
65 | if (my $cb = $v->[1]()) { |
68 | undef $w; |
66 | undef $w; |
69 | cf::debug "$id: detected protocol $name.\n"; |
67 | cf::debug "$id: detected protocol $v->[3].\n"; |
70 | $v->[2]($id, $fh, $buf); |
68 | $v->[2]($id, $fh, $buf); |
71 | return; |
69 | return; |
72 | } |
70 | } |
73 | } |
71 | } |
74 | |
72 | |