--- deliantra/server/ext/tcp.ext 2006/12/23 03:38:43 1.5 +++ deliantra/server/ext/tcp.ext 2012/11/11 04:29:11 1.28 @@ -1,32 +1,85 @@ -#! perl +#! perl # mandatory # this listens for new tcp connections and hands them over to the server core -# wether this being an extension introduces or reduces stability problems +# whether this being an extension introduces or reduces stability problems # is unknown as of today. use Socket; -use IO::Socket::INET; +use AnyEvent::Socket; -our $LISTEN = new IO::Socket::INET - LocalPort => cf::settings->csport, - Listen => 1, - Blocking => 0, # bugfix workaround for Event calling handler twice :( - ReuseAddr => 1; - -if (!$LISTEN) { - # extension yes, completely stupid, not yet - warn "unable to establish listen sockect, exiting.\n"; - exit (2); +CONF BIND_ADDRESSES = [[undef, 13327]]; + +our $MAX_DETECT; # how many bytes to read to identify the protocol + +our @LISTENERS; +our @DETECTORS; +our %DETECTORS; + +sub _update_detectors { + $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS; } -Event->io (fd => $LISTEN, nice => 1, poll => 'r', data => cf::WF_AUTOCANCEL, cb => sub { - my ($fh, $peername) = $LISTEN->accept - or return; +sub register($$$$) { + my ($name, $max_detect, $detect, $serve) = @_; - my $fd = fileno $fh; - my $host = inet_ntoa +(sockaddr_in $peername)[1]; + $DETECTORS{$name} = [$max_detect, $detect, $serve, $name]; + _update_detectors; - warn "new connection from $host\n"; + Guard::guard { + delete $DETECTORS{$name}; + _update_detectors if defined &_update_detectors; + } +} + +our $deliantra_detector = ext::tcp::register deliantra => 10, sub { + /^..version /s +}, sub { + my $ns = cf::client::create fileno $_[1], $_[0]; + $ns->run; + $ns->inbuf_append ($_[2]); +}; + +for (@$BIND_ADDRESSES) { + my ($host, $port) = @$_; + cf::info "listening on ", (format_hostport $host, $port), "\n"; + + push @LISTENERS, tcp_server $host, $port, sub { + my ($fh, $host, $port) = @_ + or return; + + my $lhost = AnyEvent::Socket::format_address + +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; + + my $id = format_hostport $host, $port; + + cf::info "$id: accepted connection.\n" + if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs + + my $buf; + my $w; $w = AE::io $fh, 0, sub { + my $len = sysread $fh, $buf, 512, length $buf; + + if ($len) { + for ($buf) { + for my $v (values %DETECTORS) { + if (my $cb = $v->[1]()) { + undef $w; + cf::debug "$id: detected protocol $v->[3].\n"; + $v->[2]($id, $fh, $buf); + return; + } + } + + if (length >= $MAX_DETECT) { # unable to detect protocol + undef $w; + cf::debug "$id: data received, but cannot detect protocol, closing.\n"; + } + } + } else { + undef $w; + cf::info "$id: read error during protocol detection ($!)\n"; + } + }; + }; +} - cf::client::create $fd, $host; -});