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);
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 {my ($stream, $err) = @_; $callback->($conn, $err);});
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 #-----------------------------------------------------------------
132 my ($pkg, $to_host, $to_port, $rproc) = @_;
134 # Create a connection end-point object
137 $conn = $pkg->new($rproc);
139 $conn->{peerhost} = $to_host;
140 $conn->{peerport} = $to_port;
141 $conn->{sort} = 'Outgoing';
143 dbg((ref $conn) . " connecting $conn->{cnum} to $to_host:$to_port") if isdbg('connll');
146 $conn->{sock} = $sock = Mojo::IOLoop::Client->new;
147 $sock->on(connect => sub { dbg((ref $conn) . " connected $conn->{cnum} to $to_host:$to_port") if isdbg('connll');},
148 error => {$conn->disconnect},
149 close => {$conn->disconnect});
151 $sock->connect(address => $to_host, port => $to_port);
153 dbg((ref $conn) . " connected $conn->{cnum} to $to_host:$to_port") if isdbg('connll');
155 if ($conn->{rproc}) {
156 $sock->on(read => sub {my ($stream, $msg) = @_; $conn->_rcv($msg);} );
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((ref $conn) . " Connection $conn->{cnum} $call disconnected") if isdbg('connll');
229 # get rid of any references
231 if (ref($conn->{$_})) {
236 if (defined($sock)) {
240 unless ($main::is_win) {
241 kill 'TERM', $conn->{pid} if exists $conn->{pid};
248 my $rq = $conn->{outqueue};
249 my $sock = $conn->{sock};
251 my $data = shift @$rq;
252 my $lth = length $data;
253 my $call = $conn->{call} || 'none';
256 dbgdump('raw', "$call send $lth: ", $lth);
259 if (defined $sock && !$sock->destroyed) {
263 dbg("_send_stuff $call ending data ignored: $data");
269 my ($conn, $msg) = @_;
270 $conn->enqueue($msg);
280 push @{$conn->{outqueue}}, defined $_[0] ? $_[0] : '';
291 $conn->{sock}->on(drain => sub {$conn->disconnect;});
294 #-----------------------------------------------------------------
295 # Receive side routines
299 # @_ == 4 || die "Msg->new_server (myhost, myport, login_proc)\n";
300 my ($pkg, $my_host, $my_port, $login_proc) = @_;
301 my $conn = $pkg->new($login_proc);
303 $conn->{sock} = Mojo::IOLoop::Server->new;
304 $conn->{sock}->on(accept=>sub{$conn->new_client()});
305 $conn->{sock}->listen(address=>$my_host, port=>$my_port);
307 die "Could not create socket: $! \n" unless $conn->{sock};
320 return if $conn->{disconnecting};
322 if ($conn->{msg} =~ /\cJ/) {
323 my @lines = split /\cM?\cJ/, $conn->{msg};
324 if ($conn->{msg} =~ /\cM?\cJ$/) {
327 $conn->{msg} = pop @lines;
330 last if $conn->{disconnecting};
331 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
336 sub _rcv { # Complement to _send
337 my $conn = shift; # $rcv_now complement of $flush
338 # Find out how much has already been received, if at all
339 my ($msg, $offset, $bytes_to_read, $bytes_read);
340 my $sock = $conn->{sock};
341 return unless defined($sock);
344 # if ($conn->{blocking}) {
345 # blocking($sock, 0);
346 # $conn->{blocking} = 0;
348 $bytes_read = sysread ($sock, $msg, 1024, 0);
349 if (defined ($bytes_read)) {
350 if ($bytes_read > 0) {
351 $total_in += $bytes_read;
353 my $call = $conn->{call} || 'none';
354 dbgdump('raw', "$call read $bytes_read: ", $msg);
357 my @ch = split //, $msg;
362 $conn->{msg} =~ s/.$//;
369 set_event_handler ($sock, write => sub{$conn->_send(0)});
370 push @{$conn->{outqueue}}, $out;
373 $conn->{msg} .= $msg;
377 if (_err_will_block($!)) {
385 if (defined $bytes_read && $bytes_read == 0) {
386 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
389 unless ($conn->{disable_read}) {
390 $conn->dequeue if exists $conn->{msg};
396 my $server_conn = shift;
399 my $conn = $server_conn->new($server_conn->{rproc});
400 my $sock = $conn->{sock} = Mojo::IOLoop::Stream->new($client);
401 $sock->on(read => sub {$conn->_rcv($_[1])});
402 dbg((ref $conn) . "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
404 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $client->peerhost, $conn->{peerport} = $client->peerport);
405 $conn->{sort} = 'Incoming';
407 $conn->{eproc} = $eproc;
410 $conn->{rproc} = $rproc;
411 } else { # Login failed
412 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
420 delete $conn->{sock};
423 # close all clients (this is for forking really)
424 sub close_all_clients
426 foreach my $conn (values %conns) {
434 return defined $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
439 #----------------------------------------------------
440 # Event loop routines used by both client and server
442 sub set_event_handler {
445 my ($pkg, $fn, $line) = caller;
447 foreach (my ($k,$v) = each %args) {
451 dbg("Msg::set_event_handler called from ${pkg}::${fn} line $line doing $s");
456 my ($pkg, $interval) = @_;
458 while (time - $now < $interval) {
466 my $call = $conn->{call} || 'unallocated';
467 my $host = $conn->{peerhost} || '';
468 my $port = $conn->{peerport} || '';
470 dbg((ref $conn) . " Connection $conn->{cnum} $call [$host $port] being destroyed (total $noconns)") if isdbg('connll');