2 # This has been taken from the 'Advanced Perl Programming' book by Sriram Srinivasan
4 # I am presuming that the code is distributed on the same basis as perl itself.
6 # I have modified it to suit my devious purposes (Dirk Koopman G1TLH)
24 use vars qw(%conns $noconns $cnum $total_in $total_out);
26 $total_in = $total_out = 0;
30 #-----------------------------------------------------------------
31 # Generalised initializer
35 my ($pkg, $rproc) = @_;
37 my $class = $obj || $pkg;
48 cnum => (($cnum < 999) ? (++$cnum) : ($cnum = 1)),
53 dbg("Connection created ($noconns)") if isdbg('connll');
54 return bless $conn, $class;
61 $conn->{eproc} = $callback;
68 $conn->{sock}->on_eof(sub {$callback});
75 $conn->{rproc} = $callback;
86 $call = $pkg->{call} unless $call;
87 return undef unless $call;
88 dbg("changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call};
89 delete $conns{$pkg->{call}} if exists $pkg->{call} && exists $conns{$pkg->{call}} && $pkg->{call} ne $call;
91 $ref = $conns{$call} = $pkg;
92 dbg("Connection $pkg->{cnum} $call stored") if isdbg('connll');
99 # this is only called by any dependent processes going away unexpectedly
102 my ($pkg, $pid) = @_;
104 my @pid = grep {$_->{pid} == $pid} values %conns;
105 foreach my $p (@pid) {
106 &{$p->{eproc}}($p, "$pid has gorn") if exists $p->{eproc};
114 return $conn->{csort} eq 'ax25';
120 $conn->{peerhost} ||= 'ax25' if $conn->ax25;
121 $conn->{peerhost} ||= $conn->{sock}->peerhost if $conn->{sock} && $conn->{sock}->isa('IO::Socket::INET');
122 $conn->{peerhost} ||= 'UNKNOWN';
123 return $conn->{peerhost};
126 #-----------------------------------------------------------------
129 my ($pkg, $to_host, $to_port, $rproc) = @_;
131 # Create a connection end-point object
134 $conn = $pkg->new($rproc);
136 $conn->{peerhost} = $to_host;
137 $conn->{peerport} = $to_port;
138 $conn->{sort} = 'Outgoing';
140 my $sock = AnyEvent::Handle->new(
142 connect => [$to_host, $to_port],
144 # on_connect => sub {my $h = shift; $conn->{peerhost} = $h->handle->peername;},
146 on_eof => sub {$conn->disconnect},
148 on_error => sub {$conn->disconnect},
155 $conn->{sock} = $sock;
156 $sock->on_read(sub{$conn->_rcv});
163 my ($conn, $line, $sort) = @_;
166 # local $^F = 10000; # make sure it ain't closed on exec
167 # my ($a, $b) = $io_socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
172 # if (defined $pid) {
175 # $conn->{sock} = $a;
176 # $conn->{csort} = $sort;
177 # $conn->{lineend} = "\cM" if $sort eq 'ax25';
178 # $conn->{pid} = $pid;
179 # if ($conn->{rproc}) {
180 # my $callback = sub {$conn->_rcv};
181 # Msg::set_event_handler ($a, read => $callback);
183 # dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
190 # *STDIN = IO::File->new_from_fd($b, 'r') or die;
191 # *STDOUT = IO::File->new_from_fd($b, 'w') or die;
192 # *STDERR = IO::File->new_from_fd($b, 'w') or die;
194 # unless ($main::is_win) {
195 # # $SIG{HUP} = 'IGNORE';
196 # $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
199 # exec "$line" or dbg("exec '$line' failed $!");
202 # dbg("cannot fork for $line");
205 # dbg("no socket pair $! for $line");
213 return if exists $conn->{disconnecting};
215 $conn->{disconnecting} = 1;
216 my $sock = delete $conn->{sock};
217 $conn->{state} = 'E';
218 $conn->{timeout}->del if $conn->{timeout};
220 # be careful to delete the correct one
222 if ($call = $conn->{call}) {
223 my $ref = $conns{$call};
224 delete $conns{$call} if $ref && $ref == $conn;
226 $call ||= 'unallocated';
227 dbg("Connection $conn->{cnum} $call disconnected") if isdbg('connll');
229 # get rid of any references
231 if (ref($conn->{$_})) {
236 if (defined($sock)) {
237 shutdown($sock->{fh}, 2);
241 unless ($main::is_win) {
242 kill 'TERM', $conn->{pid} if exists $conn->{pid};
249 my $rq = $conn->{outqueue};
250 my $sock = $conn->{sock};
253 my $data = shift @$rq;
254 my $lth = length $data;
255 my $call = $conn->{call} || 'none';
258 dbgdump('raw', "$call send $lth: ", $lth);
261 if (defined $sock && !$sock->destroyed) {
262 $sock->push_write($data);
265 dbg("_send_stuff $call ending data ignored: $data");
271 my ($conn, $msg) = @_;
272 my $rq = $conn->{outqueue};
273 my $sock = $conn->{sock};
275 # this is done like this because enqueueing may be going on independently of
276 # sending (whether later or now)
277 $conn->enqueue($msg);
281 sub send_now { goto &send_later; }
285 my ($conn, $msg) = @_;
286 push @{$conn->{outqueue}}, $msg;
292 push (@{$conn->{outqueue}}, defined $_[0] ? $_[0] : '');
295 sub _err_will_block {
302 $conn->{sock}->on_drain(sub {$conn->disconnect;});
305 #-----------------------------------------------------------------
306 # Receive side routines
309 @_ == 4 || die "Msg->new_server (myhost, myport, login_proc\n";
310 my ($pkg, $my_host, $my_port, $login_proc) = @_;
311 my $self = $pkg->new($login_proc);
313 $self->{sock} = tcp_server $my_host, $my_port, sub { $self->new_client(@_); }, sub { return 256; };
314 die "Could not create socket: $! \n" unless $self->{sock};
322 my $sock = $conn->{sock};
324 # $sock->keepalive(1);
331 if ($conn->{msg} =~ /\n/) {
332 my @lines = split /\r?\n/, $conn->{msg};
333 if ($conn->{msg} =~ /\n$/) {
336 $conn->{msg} = pop @lines;
339 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
344 sub _rcv { # Complement to _send
345 my $conn = shift; # $rcv_now complement of $flush
346 # Find out how much has already been received, if at all
347 my ($msg, $offset, $bytes_to_read, $bytes_read);
348 my $sock = $conn->{sock};
349 return unless defined($sock);
352 $msg = $sock->{rbuf};
353 $bytes_read = length $msg || 0;
356 if ($bytes_read > 0) {
357 $total_in += $bytes_read;
359 my $call = $conn->{call} || 'none';
360 dbgdump('raw', "$call read $bytes_read: ", $msg);
363 my @ch = split //, $msg;
368 $conn->{msg} =~ s/.$//;
375 $conn->send_now($out);
378 $conn->{msg} .= $msg;
382 unless ($conn->{disable_read}) {
383 $conn->dequeue if exists $conn->{msg};
388 my $server_conn = shift;
390 my $peerhost = shift;
391 my $peerport = shift;
393 my $conn = $server_conn->new($server_conn->{rproc});
394 $conn->{sock} = AnyEvent::Handle->new(
398 on_eof => sub {$conn->disconnect},
400 on_error => sub {$conn->disconnect},
406 $conn->{blocking} = 0;
407 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $peerhost, $conn->{peerport} = $peerport);
408 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
409 $conn->{sort} = 'Incoming';
411 $conn->{eproc} = $eproc;
414 $conn->{rproc} = $rproc;
415 $conn->{sock}->on_read(sub {$conn->_rcv});
416 } else { # Login failed
417 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
421 dbg("Msg: error on accept ($!)") if isdbg('err');
431 # close all clients (this is for forking really)
432 sub close_all_clients
434 foreach my $conn (values %conns) {
442 return defined $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
447 my ($pkg, $interval) = @_;
448 my $cv = AnyEvent->condvar;
449 my $wait_a_bit = AnyEvent->timer(
451 cb => sub {$cv->send},
456 sub set_event_handler
460 my ($pkg, $fn, $line) = caller;
462 foreach (my ($k,$v) = each %args) {
466 dbg("Msg::set_event_handler called from ${pkg}::${fn} line $line doing $s");
472 return defined $_[0] ? $conn->{echo} = $_[0] : $_[0];
478 my $call = $conn->{call} || 'unallocated';
479 my $host = $conn->{peerhost} || '';
480 my $port = $conn->{peerport} || '';
481 dbg("Connection $conn->{cnum} $call [$host $port] being destroyed") if isdbg('connll');