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)
18 use Mojo::IOLoop::Stream;
23 use vars qw($now %conns $noconns $cnum $total_in $total_out $connect_timeout);
25 $total_in = $total_out = 0;
33 #-----------------------------------------------------------------
34 # Generalised initializer
38 my ($pkg, $rproc) = @_;
40 my $class = $obj || $pkg;
51 cnum => (($cnum < 999) ? (++$cnum) : ($cnum = 1)),
56 dbg("$class Connection created (total $noconns)") if isdbg('connll');
57 return bless $conn, $class;
64 $conn->{sock}->on(error => sub {$callback->($_[1]);});
71 $conn->{sock}->on(close => sub {$callback->()});
78 $conn->{rproc} = $callback;
89 $call = $pkg->{call} unless $call;
90 return undef unless $call;
91 dbg((ref $pkg) . " changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call};
92 delete $conns{$pkg->{call}} if exists $pkg->{call} && exists $conns{$pkg->{call}} && $pkg->{call} ne $call;
94 $ref = $conns{$call} = $pkg;
95 dbg((ref $pkg) . " Connection $pkg->{cnum} $call stored") if isdbg('connll');
102 # this is only called by any dependent processes going away unexpectedly
105 my ($pkg, $pid) = @_;
107 my @pid = grep {$_->{pid} == $pid} values %conns;
108 foreach my $p (@pid) {
109 &{$p->{eproc}}($p, "$pid has gorn") if exists $p->{eproc};
117 return $conn->{csort} eq 'ax25';
123 unless ($conn->{peerhost}) {
124 $conn->{peerhost} ||= 'ax25' if $conn->ax25;
125 $conn->{peerhost} ||= $conn->{sock}->handle->peerhost if $conn->{sock};
126 $conn->{peerhost} ||= 'UNKNOWN';
128 return $conn->{peerhost};
131 #-----------------------------------------------------------------
139 my $sock = $conn->{sock} = Mojo::IOLoop::Stream->new($handle);
140 $sock->on(read => sub {$conn->_rcv($_[1]);} );
141 $sock->on(error => sub {delete $conn->{sock}; $conn->disconnect;});
142 $sock->on(close => sub {delete $conn->{sock}; $conn->disconnect;});
145 $conn->{peerhost} = eval { $handle->peerhost; };
146 dbg((ref $conn) . " connected $conn->{cnum} to $conn->{peerhost}:$conn->{peerport}") if isdbg('connll');
147 if ($conn->{on_connect}) {
148 &{$conn->{on_connect}}($conn, $handle);
155 my $sock = $conn->{sock};
156 return ref $sock && $sock->isa('Mojo::IOLoop::Stream');
160 my ($pkg, $to_host, $to_port, %args) = @_;
161 my $timeout = delete $args{timeout} || $connect_timeout;
163 # Create a connection end-point object
166 my $rproc = delete $args{rproc};
167 $conn = $pkg->new($rproc);
169 $conn->{peerhost} = $to_host;
170 $conn->{peerport} = $to_port;
171 $conn->{sort} = 'Outgoing';
173 dbg((ref $conn) . " connecting $conn->{cnum} to $to_host:$to_port") if isdbg('connll');
176 $conn->{sock} = $sock = Mojo::IOLoop::Client->new;
177 $sock->on(connect => sub {$conn->_on_connect($_[1])} );
178 $sock->on(error => sub {&{$conn->{eproc}}($conn, $_[1]) if exists $conn->{eproc}; $conn->disconnect});
179 $sock->on(close => sub {$conn->disconnect});
181 # copy any args like on_connect, on_disconnect etc
182 while (my ($k, $v) = each %args) {
186 $sock->connect(address => $to_host, port => $to_port, timeout => $timeout);
193 my ($conn, $line, $sort) = @_;
196 # local $^F = 10000; # make sure it ain't closed on exec
197 # my ($a, $b) = $io_socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
202 # if (defined $pid) {
205 # $conn->{sock} = $a;
206 # $conn->{csort} = $sort;
207 # $conn->{lineend} = "\cM" if $sort eq 'ax25';
208 # $conn->{pid} = $pid;
209 # if ($conn->{rproc}) {
210 # my $callback = sub {$conn->_rcv};
211 # Msg::set_event_handler ($a, read => $callback);
213 # dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
220 # *STDIN = IO::File->new_from_fd($b, 'r') or die;
221 # *STDOUT = IO::File->new_from_fd($b, 'w') or die;
222 # *STDERR = IO::File->new_from_fd($b, 'w') or die;
224 # unless ($main::is_win) {
225 # # $SIG{HUP} = 'IGNORE';
226 # $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
229 # exec "$line" or dbg("exec '$line' failed $!");
232 # dbg("cannot fork for $line");
235 # dbg("no socket pair $! for $line");
243 return if exists $conn->{disconnecting};
245 $conn->{disconnecting} = 1;
246 my $sock = delete $conn->{sock};
247 $conn->{state} = 'E';
248 $conn->{timeout}->del if $conn->{timeout};
250 # be careful to delete the correct one
252 if ($call = $conn->{call}) {
253 my $ref = $conns{$call};
254 delete $conns{$call} if $ref && $ref == $conn;
256 $call ||= 'unallocated';
257 dbg((ref $conn) . " Connection $conn->{cnum} $call disconnected") if isdbg('connll');
259 if ($conn->{on_disconnect}) {
260 &{$conn->{on_disconnect}}($conn);
263 # get rid of any references
265 if (ref($conn->{$_})) {
270 $sock->close_gracefully if defined $sock && $sock->can('close_gracefully');
273 unless ($main::is_win) {
274 kill 'TERM', $conn->{pid} if exists $conn->{pid};
281 my $rq = $conn->{outqueue};
282 my $sock = $conn->{sock};
283 return unless defined $sock;
284 return if $conn->{disconnecting};
287 my $data = shift @$rq;
288 my $lth = length $data;
289 my $call = $conn->{call} || 'none';
292 dbgdump('raw', "$call send $lth: ", $lth);
299 dbg("_send_stuff $call ending data ignored: $data");
305 my ($conn, $msg) = @_;
306 $conn->enqueue($msg);
316 my ($conn, $msg) = @_;
317 push @{$conn->{outqueue}}, $msg;
323 push @{$conn->{outqueue}}, defined $_[0] ? $_[0] : '';
334 $conn->{sock}->on(drain => sub {$conn->disconnect;});
337 #-----------------------------------------------------------------
338 # Receive side routines
342 # @_ == 4 || die "Msg->new_server (myhost, myport, login_proc)\n";
343 my ($pkg, $my_host, $my_port, $login_proc) = @_;
344 my $conn = $pkg->new($login_proc);
346 my $sock = $conn->{sock} = Mojo::IOLoop::Server->new;
347 $sock->on(accept=>sub{$conn->new_client($_[1]);});
348 $sock->listen(address=>$my_host, port=>$my_port);
351 die "Could not create socket: $! \n" unless $conn->{sock};
364 return if $conn->{disconnecting};
366 if ($conn->{msg} =~ /\cJ/) {
367 my @lines = split /\cM?\cJ/, $conn->{msg};
368 if ($conn->{msg} =~ /\cM?\cJ$/) {
371 $conn->{msg} = pop @lines;
374 last if $conn->{disconnecting};
375 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
380 sub _rcv { # Complement to _send
381 my $conn = shift; # $rcv_now complement of $flush
383 my $sock = $conn->{sock};
384 return unless defined($sock);
385 return if $conn->{disconnecting};
387 $total_in += length $msg;
391 my $call = $conn->{call} || 'none';
392 my $lth = length $msg;
393 dbgdump('raw', "$call read $lth: ", $msg);
396 my @ch = split //, $msg;
401 $conn->{msg} =~ s/.$//;
408 $conn->send_raw($out);
411 $conn->{msg} .= $msg;
414 unless ($conn->{disable_read}) {
415 $conn->dequeue if exists $conn->{msg};
420 my $server_conn = shift;
423 my $conn = $server_conn->new($server_conn->{rproc});
424 my $sock = $conn->{sock} = Mojo::IOLoop::Stream->new($handle);
425 $sock->on(read => sub {$conn->_rcv($_[1])});
428 dbg((ref $conn) . "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
430 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $handle->peerhost, $conn->{peerport} = $handle->peerport);
431 $conn->{sort} = 'Incoming';
433 $conn->{eproc} = $eproc;
436 $conn->{rproc} = $rproc;
437 } else { # Login failed
438 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
447 delete $conn->{sock};
450 # close all clients (this is for forking really)
451 sub close_all_clients
453 foreach my $conn (values %conns) {
461 return defined $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
466 #----------------------------------------------------
467 # Event loop routines used by both client and server
469 sub set_event_handler {
472 my ($pkg, $fn, $line) = caller;
474 foreach (my ($k,$v) = each %args) {
478 dbg("Msg::set_event_handler called from ${pkg}::${fn} line $line doing $s");
483 my ($pkg, $interval) = @_;
485 while (time - $now < $interval) {
493 my $call = $conn->{call} || 'unallocated';
494 my $host = $conn->{peerhost} || '';
495 my $port = $conn->{peerport} || '';
496 my $sock = $conn->{sock};
498 $sock->close_gracefully if defined $sock && $sock->can('close_gracefully');
501 dbg((ref $conn) . " Connection $conn->{cnum} $call [$host $port] being destroyed (total $noconns)") if isdbg('connll');