| 1 |
root |
1.17 |
#! perl # mandatory |
| 2 |
root |
1.1 |
|
| 3 |
|
|
# this listens for new tcp connections and hands them over to the server core |
| 4 |
root |
1.19 |
# whether this being an extension introduces or reduces stability problems |
| 5 |
root |
1.1 |
# is unknown as of today. |
| 6 |
|
|
|
| 7 |
|
|
use Socket; |
| 8 |
root |
1.12 |
use AnyEvent::Socket; |
| 9 |
root |
1.1 |
|
| 10 |
root |
1.20 |
CONF BIND_ADDRESSES = [[undef, 13327]]; |
| 11 |
|
|
|
| 12 |
root |
1.21 |
our $MAX_DETECT = 16; # how many bytes to raed to identify the protocol |
| 13 |
|
|
|
| 14 |
root |
1.15 |
our @LISTENERS; |
| 15 |
root |
1.12 |
|
| 16 |
root |
1.20 |
for (@$BIND_ADDRESSES) { |
| 17 |
root |
1.12 |
my ($host, $port) = @$_; |
| 18 |
root |
1.16 |
cf::info "listening on ", (format_hostport $host, $port), "\n"; |
| 19 |
root |
1.12 |
|
| 20 |
root |
1.15 |
push @LISTENERS, tcp_server $host, $port, sub { |
| 21 |
root |
1.12 |
my ($fh, $host, $port) = @_ |
| 22 |
|
|
or return; |
| 23 |
|
|
|
| 24 |
root |
1.18 |
my $lhost = AnyEvent::Socket::format_address |
| 25 |
|
|
+(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; |
| 26 |
|
|
|
| 27 |
root |
1.21 |
my $id = format_hostport $host, $port; |
| 28 |
|
|
|
| 29 |
|
|
cf::info "$id: new connection\n" |
| 30 |
root |
1.18 |
if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs |
| 31 |
root |
1.12 |
|
| 32 |
root |
1.21 |
my $buf; |
| 33 |
|
|
my $w; $w = AE::io $fh, 0, sub { |
| 34 |
|
|
my $len = sysread $fh, $buf, 512, length $buf; |
| 35 |
|
|
|
| 36 |
|
|
if ($len) { |
| 37 |
|
|
if ($buf =~ /^..version /s) { # deliantra protocol |
| 38 |
|
|
undef $w; |
| 39 |
|
|
|
| 40 |
|
|
my $ns = cf::client::create fileno $fh, $host; |
| 41 |
|
|
$ns->inbuf_append ($buf); |
| 42 |
|
|
|
| 43 |
|
|
} elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket |
| 44 |
|
|
undef $w; |
| 45 |
|
|
|
| 46 |
|
|
&ext::http::server ($fh, $buf); |
| 47 |
|
|
|
| 48 |
|
|
} elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol |
| 49 |
|
|
undef $w; |
| 50 |
|
|
|
| 51 |
|
|
cf::info "$id: protocol detection error\n"; |
| 52 |
|
|
} |
| 53 |
|
|
} else { |
| 54 |
|
|
undef $w; |
| 55 |
|
|
|
| 56 |
|
|
cf::info "$id: read error during protocol detection\n"; |
| 57 |
|
|
} |
| 58 |
|
|
}; |
| 59 |
root |
1.12 |
}; |
| 60 |
root |
1.1 |
} |
| 61 |
|
|
|