--- deliantra/server/ext/tcp.ext 2012/02/03 03:01:45 1.20 +++ deliantra/server/ext/tcp.ext 2012/10/30 17:32:47 1.22 @@ -9,6 +9,8 @@ CONF BIND_ADDRESSES = [[undef, 13327]]; +our $MAX_DETECT = 16; # how many bytes to raed to identify the protocol + our @LISTENERS; for (@$BIND_ADDRESSES) { @@ -22,10 +24,38 @@ my $lhost = AnyEvent::Socket::format_address +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; - cf::info "new connection from ", (format_hostport $host, $port), "\n" + my $id = format_hostport $host, $port; + + cf::info "$id: new connection\n" if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs - cf::client::create fileno $fh, $host; + my $buf; + my $w; $w = AE::io $fh, 0, sub { + my $len = sysread $fh, $buf, 512, length $buf; + + if ($len) { + if ($buf =~ /^..version /s) { # deliantra protocol + undef $w; + + my $ns = cf::client::create fileno $fh, $host; + $ns->inbuf_append ($buf); + + } elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket + undef $w; + + &ext::http::server ($id, $fh, $buf); + + } elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol + undef $w; + + cf::info "$id: protocol detection error\n"; + } + } else { + undef $w; + + cf::info "$id: read error during protocol detection\n"; + } + }; }; }