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->($conn, $_[1]);});
71 $conn->{sock}->on(close => sub {$callback->($conn);});
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 dbg((ref $conn) . " connected $conn->{cnum} to $conn->{peerhost}:$conn->{peerport}") if isdbg('connll');
144 if ($conn->{on_connect}) {
145 &{$conn->{on_connect}}($conn);
152 my $sock = $conn->{sock};
153 return ref $sock && $sock->isa('Mojo::IOLoop::Stream');
157 my ($pkg, $to_host, $to_port, $rproc, %args) = @_;
158 my $timeout = delete $args{timeout} || $connect_timeout;
160 # Create a connection end-point object
163 $conn = $pkg->new($rproc);
165 $conn->{peerhost} = $to_host;
166 $conn->{peerport} = $to_port;
167 $conn->{sort} = 'Outgoing';
169 dbg((ref $conn) . " connecting $conn->{cnum} to $to_host:$to_port") if isdbg('connll');
172 $conn->{sock} = $sock = Mojo::IOLoop::Client->new;
173 $sock->on(connect => sub {$conn->_on_connect($_[1])} );
174 $sock->on(error => sub {$conn->disconnect});
175 $sock->on(close => sub {$conn->disconnect});
177 # copy any args like on_connect, on_disconnect etc
178 while (my ($k, $v) = each %args) {
182 $sock->connect(address => $to_host, port => $to_port, timeout => $timeout);
189 my ($conn, $line, $sort) = @_;
192 # local $^F = 10000; # make sure it ain't closed on exec
193 # my ($a, $b) = $io_socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
198 # if (defined $pid) {
201 # $conn->{sock} = $a;
202 # $conn->{csort} = $sort;
203 # $conn->{lineend} = "\cM" if $sort eq 'ax25';
204 # $conn->{pid} = $pid;
205 # if ($conn->{rproc}) {
206 # my $callback = sub {$conn->_rcv};
207 # Msg::set_event_handler ($a, read => $callback);
209 # dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
216 # *STDIN = IO::File->new_from_fd($b, 'r') or die;
217 # *STDOUT = IO::File->new_from_fd($b, 'w') or die;
218 # *STDERR = IO::File->new_from_fd($b, 'w') or die;
220 # unless ($main::is_win) {
221 # # $SIG{HUP} = 'IGNORE';
222 # $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
225 # exec "$line" or dbg("exec '$line' failed $!");
228 # dbg("cannot fork for $line");
231 # dbg("no socket pair $! for $line");
239 return if exists $conn->{disconnecting};
241 $conn->{disconnecting} = 1;
242 my $sock = delete $conn->{sock};
243 $conn->{state} = 'E';
244 $conn->{timeout}->del if $conn->{timeout};
246 # be careful to delete the correct one
248 if ($call = $conn->{call}) {
249 my $ref = $conns{$call};
250 delete $conns{$call} if $ref && $ref == $conn;
252 $call ||= 'unallocated';
253 dbg((ref $conn) . " Connection $conn->{cnum} $call disconnected") if isdbg('connll');
255 if ($conn->{on_disconnect}) {
256 &{$conn->{on_disconnect}}($conn);
259 # get rid of any references
261 if (ref($conn->{$_})) {
266 if (defined($sock)) {
267 $sock->close_gracefully;
270 unless ($main::is_win) {
271 kill 'TERM', $conn->{pid} if exists $conn->{pid};
278 my $rq = $conn->{outqueue};
279 my $sock = $conn->{sock};
281 my $data = shift @$rq;
282 my $lth = length $data;
283 my $call = $conn->{call} || 'none';
286 dbgdump('raw', "$call send $lth: ", $lth);
293 dbg("_send_stuff $call ending data ignored: $data");
299 my ($conn, $msg) = @_;
300 $conn->enqueue($msg);
310 my ($conn, $msg) = @_;
311 push @{$conn->{outqueue}}, $msg;
317 push @{$conn->{outqueue}}, defined $_[0] ? $_[0] : '';
328 $conn->{sock}->on(drain => sub {$conn->disconnect;});
331 #-----------------------------------------------------------------
332 # Receive side routines
336 # @_ == 4 || die "Msg->new_server (myhost, myport, login_proc)\n";
337 my ($pkg, $my_host, $my_port, $login_proc) = @_;
338 my $conn = $pkg->new($login_proc);
340 my $sock = $conn->{sock} = Mojo::IOLoop::Server->new;
341 $sock->on(accept=>sub{$conn->new_client($_[1]);});
342 $sock->listen(address=>$my_host, port=>$my_port);
345 die "Could not create socket: $! \n" unless $conn->{sock};
358 return if $conn->{disconnecting};
360 if ($conn->{msg} =~ /\cJ/) {
361 my @lines = split /\cM?\cJ/, $conn->{msg};
362 if ($conn->{msg} =~ /\cM?\cJ$/) {
365 $conn->{msg} = pop @lines;
368 last if $conn->{disconnecting};
369 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
374 sub _rcv { # Complement to _send
375 my $conn = shift; # $rcv_now complement of $flush
377 my $sock = $conn->{sock};
378 return unless defined($sock);
382 my $call = $conn->{call} || 'none';
383 my $lth = length $msg;
384 dbgdump('raw', "$call read $lth: ", $msg);
387 my @ch = split //, $msg;
392 $conn->{msg} =~ s/.$//;
399 $conn->send_raw($out);
402 $conn->{msg} .= $msg;
405 unless ($conn->{disable_read}) {
406 $conn->dequeue if exists $conn->{msg};
411 my $server_conn = shift;
414 my $conn = $server_conn->new($server_conn->{rproc});
415 my $sock = $conn->{sock} = Mojo::IOLoop::Stream->new($client);
416 $sock->on(read => sub {$conn->_rcv($_[1])});
419 dbg((ref $conn) . "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
421 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $client->peerhost, $conn->{peerport} = $client->peerport);
422 $conn->{sort} = 'Incoming';
424 $conn->{eproc} = $eproc;
427 $conn->{rproc} = $rproc;
428 } else { # Login failed
429 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
438 delete $conn->{sock};
441 # close all clients (this is for forking really)
442 sub close_all_clients
444 foreach my $conn (values %conns) {
452 return defined $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
457 #----------------------------------------------------
458 # Event loop routines used by both client and server
460 sub set_event_handler {
463 my ($pkg, $fn, $line) = caller;
465 foreach (my ($k,$v) = each %args) {
469 dbg("Msg::set_event_handler called from ${pkg}::${fn} line $line doing $s");
474 my ($pkg, $interval) = @_;
476 while (time - $now < $interval) {
484 my $call = $conn->{call} || 'unallocated';
485 my $host = $conn->{peerhost} || '';
486 my $port = $conn->{peerport} || '';
488 dbg((ref $conn) . " Connection $conn->{cnum} $call [$host $port] being destroyed (total $noconns)") if isdbg('connll');