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.10 by root, Mon May 5 09:01:58 2008 UTC vs.
Revision 1.25 by root, Tue Nov 6 01:25:48 2012 UTC

1#! perl 1#! perl # mandatory
2 2
3# this listens for new tcp connections and hands them over to the server core 3# this listens for new tcp connections and hands them over to the server core
4# wether this being an extension introduces or reduces stability problems 4# whether this being an extension introduces or reduces stability problems
5# is unknown as of today. 5# is unknown as of today.
6 6
7use Socket; 7use Socket;
8use IO::Socket::INET; 8use AnyEvent::Socket;
9 9
10our $LISTEN = new IO::Socket::INET 10CONF BIND_ADDRESSES = [[undef, 13327]];
11 LocalPort => cf::settings->csport,
12 Listen => 1,
13 Blocking => 0,
14 ReuseAddr => 1;
15 11
16if (!$LISTEN) { 12our $MAX_DETECT; # how many bytes to read to identify the protocol
17 # extension yes, completely stupid, not yet 13
18 warn "unable to establish listen socket, exiting.\n"; 14our @LISTENERS;
19 exit (2); 15our @DETECTORS;
16our %DETECTORS;
17
18sub _update_detectors {
19 $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS;
20
21 use Data::Dump;
22 ddx [$MAX_DETECT, \%DETECTORS];
20} 23}
21 24
22our $LISTENER = EV::io $LISTEN, EV::READ, sub { 25sub register($$$$) {
23 my ($fh, $peername) = $LISTEN->accept 26 my ($name, $max_detect, $detect, $serve) = @_;
24 or return;
25 27
26 my $fd = fileno $fh; 28 $DETECTORS{$name} = [$max_detect, $detect, $serve];
27 my $host = inet_ntoa +(sockaddr_in $peername)[1]; 29 _update_detectors;
28 30
29 warn "new connection from $host\n"; 31 Guard::guard {
32 delete $DETECTORS{$name};
33 _update_detectors;
34 }
35}
30 36
31 # HACK to avoid blocking on common files on log-in. 37our $deliantra_detector = ext::tcp::register deliantra => 10, sub {
32 # remove once async 38 /^..version /s
33 cf::async {#d# 39}, sub {
34 warn "HACK: $cf::CONFDIR/$_", Coro::AIO::aio_load "$cf::CONFDIR/$_", my $dummy for qw(rules news motd);#d# 40 my $ns = cf::client::create fileno $_[1], $_[0];
35 warn "HACK: $cf::VARDIR/crossfiremail", Coro::AIO::aio_load "$cf::VARDIR/crossfiremail", my $dummy;#d# 41 $ns->inbuf_append ($_[2]);
36 cf::client::create $fd, $host;
37 undef $fh;#d#
38 };#d#d
39}; 42};
40 43
44for (@$BIND_ADDRESSES) {
45 my ($host, $port) = @$_;
46 cf::info "listening on ", (format_hostport $host, $port), "\n";
47
48 push @LISTENERS, tcp_server $host, $port, sub {
49 my ($fh, $host, $port) = @_
50 or return;
51
52 my $lhost = AnyEvent::Socket::format_address
53 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1];
54
55 my $id = format_hostport $host, $port;
56
57 cf::info "$id: accepted connection.\n"
58 if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs
59
60 my $buf;
61 my $w; $w = AE::io $fh, 0, sub {
62 my $len = sysread $fh, $buf, 512, length $buf;
63
64 if ($len) {
65 for ($buf) {
66 while (my ($name, $v) = each %DETECTORS) {
67 if (my $cb = $v->[1]()) {
68 undef $w;
69 cf::debug "$id: detected protocol $name.\n";
70 $v->[2]($id, $fh, $buf);
71 return;
72 }
73 }
74
75 if (length >= $MAX_DETECT) { # unable to detect protocol
76 undef $w;
77 cf::debug "$id: data received, but cannot detect protocol, closing.\n";
78 }
79 }
80 } else {
81 undef $w;
82 cf::info "$id: read error during protocol detection ($!)\n";
83 }
84 };
85 };
86}
87

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines