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 $conn->{peerhost} ||= 'ax25' if $conn->ax25;
124 $conn->{peerhost} ||= $conn->{sock}->handle->peerhost if $conn->{sock};
125 $conn->{peerhost} ||= 'UNKNOWN';
126 return $conn->{peerhost};
129 #-----------------------------------------------------------------
137 my $sock = $conn->{sock} = Mojo::IOLoop::Stream->new($handle);
138 $sock->on(read => sub {$conn->_rcv($_[1]);} );
139 $sock->on(error => sub {$conn->disconnect;});
140 $sock->on(close => sub {$conn->disconnect;});
143 $conn->{peerhost} = eval { $handle->peerhost; };
144 dbg((ref $conn) . " connected $conn->{cnum} to $conn->{peerhost}:$conn->{peerport}") if isdbg('connll');
145 if ($conn->{on_connect}) {
146 &{$conn->{on_connect}}($conn, $handle);
153 my $sock = $conn->{sock};
154 return ref $sock && $sock->isa('Mojo::IOLoop::Stream');
158 my ($pkg, $to_host, $to_port, %args) = @_;
159 my $timeout = delete $args{timeout} || $connect_timeout;
161 # Create a connection end-point object
164 my $rproc = delete $args{rproc};
165 $conn = $pkg->new($rproc);
167 $conn->{peerhost} = $to_host;
168 $conn->{peerport} = $to_port;
169 $conn->{sort} = 'Outgoing';
171 dbg((ref $conn) . " connecting $conn->{cnum} to $to_host:$to_port") if isdbg('connll');
174 $conn->{sock} = $sock = Mojo::IOLoop::Client->new;
175 $sock->on(connect => sub {$conn->_on_connect($_[1])} );
176 $sock->on(error => sub {&{$conn->{eproc}}($conn, $_[1]) if exists $conn->{eproc}; $conn->disconnect});
177 $sock->on(close => sub {$conn->disconnect});
179 # copy any args like on_connect, on_disconnect etc
180 while (my ($k, $v) = each %args) {
184 $sock->connect(address => $to_host, port => $to_port, timeout => $timeout);
191 my ($conn, $line, $sort) = @_;
194 # local $^F = 10000; # make sure it ain't closed on exec
195 # my ($a, $b) = $io_socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
200 # if (defined $pid) {
203 # $conn->{sock} = $a;
204 # $conn->{csort} = $sort;
205 # $conn->{lineend} = "\cM" if $sort eq 'ax25';
206 # $conn->{pid} = $pid;
207 # if ($conn->{rproc}) {
208 # my $callback = sub {$conn->_rcv};
209 # Msg::set_event_handler ($a, read => $callback);
211 # dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
218 # *STDIN = IO::File->new_from_fd($b, 'r') or die;
219 # *STDOUT = IO::File->new_from_fd($b, 'w') or die;
220 # *STDERR = IO::File->new_from_fd($b, 'w') or die;
222 # unless ($main::is_win) {
223 # # $SIG{HUP} = 'IGNORE';
224 # $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
227 # exec "$line" or dbg("exec '$line' failed $!");
230 # dbg("cannot fork for $line");
233 # dbg("no socket pair $! for $line");
241 return if exists $conn->{disconnecting};
243 $conn->{disconnecting} = 1;
244 my $sock = delete $conn->{sock};
245 $conn->{state} = 'E';
246 $conn->{timeout}->del if $conn->{timeout};
248 # be careful to delete the correct one
250 if ($call = $conn->{call}) {
251 my $ref = $conns{$call};
252 delete $conns{$call} if $ref && $ref == $conn;
254 $call ||= 'unallocated';
255 dbg((ref $conn) . " Connection $conn->{cnum} $call disconnected") if isdbg('connll');
257 if ($conn->{on_disconnect}) {
258 &{$conn->{on_disconnect}}($conn);
261 # get rid of any references
263 if (ref($conn->{$_})) {
268 $sock->close_gracefully if defined $sock && $sock->can('close_gracefully');
271 unless ($main::is_win) {
272 kill 'TERM', $conn->{pid} if exists $conn->{pid};
279 my $rq = $conn->{outqueue};
280 my $sock = $conn->{sock};
282 my $data = shift @$rq;
283 my $lth = length $data;
284 my $call = $conn->{call} || 'none';
287 dbgdump('raw', "$call send $lth: ", $lth);
294 dbg("_send_stuff $call ending data ignored: $data");
300 my ($conn, $msg) = @_;
301 $conn->enqueue($msg);
311 my ($conn, $msg) = @_;
312 push @{$conn->{outqueue}}, $msg;
318 push @{$conn->{outqueue}}, defined $_[0] ? $_[0] : '';
329 $conn->{sock}->on(drain => sub {$conn->disconnect;});
332 #-----------------------------------------------------------------
333 # Receive side routines
337 # @_ == 4 || die "Msg->new_server (myhost, myport, login_proc)\n";
338 my ($pkg, $my_host, $my_port, $login_proc) = @_;
339 my $conn = $pkg->new($login_proc);
341 my $sock = $conn->{sock} = Mojo::IOLoop::Server->new;
342 $sock->on(accept=>sub{$conn->new_client($_[1]);});
343 $sock->listen(address=>$my_host, port=>$my_port);
346 die "Could not create socket: $! \n" unless $conn->{sock};
359 return if $conn->{disconnecting};
361 if ($conn->{msg} =~ /\cJ/) {
362 my @lines = split /\cM?\cJ/, $conn->{msg};
363 if ($conn->{msg} =~ /\cM?\cJ$/) {
366 $conn->{msg} = pop @lines;
369 last if $conn->{disconnecting};
370 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
375 sub _rcv { # Complement to _send
376 my $conn = shift; # $rcv_now complement of $flush
378 my $sock = $conn->{sock};
379 return unless defined($sock);
381 $total_in += length $msg;
385 my $call = $conn->{call} || 'none';
386 my $lth = length $msg;
387 dbgdump('raw', "$call read $lth: ", $msg);
390 my @ch = split //, $msg;
395 $conn->{msg} =~ s/.$//;
402 $conn->send_raw($out);
405 $conn->{msg} .= $msg;
408 unless ($conn->{disable_read}) {
409 $conn->dequeue if exists $conn->{msg};
414 my $server_conn = shift;
417 my $conn = $server_conn->new($server_conn->{rproc});
418 my $sock = $conn->{sock} = Mojo::IOLoop::Stream->new($handle);
419 $sock->on(read => sub {$conn->_rcv($_[1])});
422 dbg((ref $conn) . "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
424 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $handle->peerhost, $conn->{peerport} = $handle->peerport);
425 $conn->{sort} = 'Incoming';
427 $conn->{eproc} = $eproc;
430 $conn->{rproc} = $rproc;
431 } else { # Login failed
432 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
441 delete $conn->{sock};
444 # close all clients (this is for forking really)
445 sub close_all_clients
447 foreach my $conn (values %conns) {
455 return defined $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
460 #----------------------------------------------------
461 # Event loop routines used by both client and server
463 sub set_event_handler {
466 my ($pkg, $fn, $line) = caller;
468 foreach (my ($k,$v) = each %args) {
472 dbg("Msg::set_event_handler called from ${pkg}::${fn} line $line doing $s");
477 my ($pkg, $interval) = @_;
479 while (time - $now < $interval) {
487 my $call = $conn->{call} || 'unallocated';
488 my $host = $conn->{peerhost} || '';
489 my $port = $conn->{peerport} || '';
490 my $sock = $conn->{sock};
492 $sock->close_gracefully if defined $sock && $sock->can('close_gracefully');
495 dbg((ref $conn) . " Connection $conn->{cnum} $call [$host $port] being destroyed (total $noconns)") if isdbg('connll');