… | |
… | |
56 | schedule; |
56 | schedule; |
57 | } |
57 | } |
58 | } |
58 | } |
59 | } |
59 | } |
60 | |
60 | |
|
|
61 | sub listen_on { |
|
|
62 | my $listen = $_[0]; |
|
|
63 | |
|
|
64 | push @listen_sockets, $listen; |
|
|
65 | |
|
|
66 | # the "main thread" |
|
|
67 | async { |
|
|
68 | slog 1, "accepting connections"; |
|
|
69 | while () { |
|
|
70 | $connections->down; |
|
|
71 | push @newcons, [$listen->accept]; |
|
|
72 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
73 | if (@pool) { |
|
|
74 | (pop @pool)->ready; |
|
|
75 | } else { |
|
|
76 | async \&handler; |
|
|
77 | } |
|
|
78 | |
|
|
79 | } |
|
|
80 | }; |
|
|
81 | } |
|
|
82 | |
61 | my $http_port = new Coro::Socket |
83 | my $http_port = new Coro::Socket |
62 | LocalAddr => $SERVER_HOST, |
84 | LocalAddr => $SERVER_HOST, |
63 | LocalPort => $SERVER_PORT, |
85 | LocalPort => $SERVER_PORT, |
64 | ReuseAddr => 1, |
86 | ReuseAddr => 1, |
65 | Listen => 50, |
87 | Listen => 50, |
66 | or die "unable to start server"; |
88 | or die "unable to start server"; |
67 | |
89 | |
68 | push @listen_sockets, $http_port; |
90 | listen_on $http_port; |
|
|
91 | |
|
|
92 | my $http_port = new Coro::Socket |
|
|
93 | LocalAddr => $SERVER_HOST, |
|
|
94 | LocalPort => $SERVER_PORT2, |
|
|
95 | ReuseAddr => 1, |
|
|
96 | Listen => 50, |
|
|
97 | or die "unable to start server"; |
|
|
98 | |
|
|
99 | listen_on $http_port; |
69 | |
100 | |
70 | our $NOW; |
101 | our $NOW; |
71 | our $HTTP_NOW; |
102 | our $HTTP_NOW; |
72 | |
103 | |
73 | Event->timer(interval => 1, hard => 1, cb => sub { |
104 | Event->timer(interval => 1, hard => 1, cb => sub { |
74 | $NOW = time; |
105 | $NOW = time; |
75 | $HTTP_NOW = time2str $NOW; |
106 | $HTTP_NOW = time2str $NOW; |
76 | })->now; |
107 | })->now; |
77 | |
|
|
78 | # the "main thread" |
|
|
79 | async { |
|
|
80 | slog 1, "accepting connections"; |
|
|
81 | while () { |
|
|
82 | $connections->down; |
|
|
83 | push @newcons, [$http_port->accept]; |
|
|
84 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
85 | if (@pool) { |
|
|
86 | (pop @pool)->ready; |
|
|
87 | } else { |
|
|
88 | async \&handler; |
|
|
89 | } |
|
|
90 | |
|
|
91 | } |
|
|
92 | }; |
|
|
93 | |
108 | |
94 | package conn; |
109 | package conn; |
95 | |
110 | |
96 | use Socket; |
111 | use Socket; |
97 | use HTTP::Date; |
112 | use HTTP::Date; |