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->{rproc} = $callback;
79 $call = $pkg->{call} unless $call;
80 return undef unless $call;
81 dbg("changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call};
82 delete $conns{$pkg->{call}} if exists $pkg->{call} && exists $conns{$pkg->{call}} && $pkg->{call} ne $call;
84 $ref = $conns{$call} = $pkg;
85 dbg("Connection $pkg->{cnum} $call stored") if isdbg('connll');
92 # this is only called by any dependent processes going away unexpectedly
97 my @pid = grep {$_->{pid} == $pid} values %conns;
98 foreach my $p (@pid) {
99 &{$p->{eproc}}($p, "$pid has gorn") if exists $p->{eproc};
107 return $conn->{csort} eq 'ax25';
113 $conn->{peerhost} ||= 'ax25' if $conn->ax25;
114 $conn->{peerhost} ||= $conn->{sock}->peerhost if $conn->{sock} && $conn->{sock}->isa('IO::Socket::INET');
115 $conn->{peerhost} ||= 'UNKNOWN';
116 return $conn->{peerhost};
119 #-----------------------------------------------------------------
122 my ($pkg, $to_host, $to_port, $rproc) = @_;
124 # Create a connection end-point object
127 $conn = $pkg->new($rproc);
129 $conn->{peerhost} = $to_host;
130 $conn->{peerport} = $to_port;
131 $conn->{sort} = 'Outgoing';
133 my $sock = AnyEvent::Handle->new(
135 connect => [$to_host, $to_port],
137 # on_connect => sub {my $h = shift; $conn->{peerhost} = $h->handle->peername;},
139 on_eof => sub {$conn->disconnect},
141 on_error => sub {$conn->disconnect},
148 $conn->{sock} = $sock;
149 $sock->on_read(sub{$conn->_rcv});
156 my ($conn, $line, $sort) = @_;
159 # local $^F = 10000; # make sure it ain't closed on exec
160 # my ($a, $b) = $io_socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
165 # if (defined $pid) {
168 # $conn->{sock} = $a;
169 # $conn->{csort} = $sort;
170 # $conn->{lineend} = "\cM" if $sort eq 'ax25';
171 # $conn->{pid} = $pid;
172 # if ($conn->{rproc}) {
173 # my $callback = sub {$conn->_rcv};
174 # Msg::set_event_handler ($a, read => $callback);
176 # dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
183 # *STDIN = IO::File->new_from_fd($b, 'r') or die;
184 # *STDOUT = IO::File->new_from_fd($b, 'w') or die;
185 # *STDERR = IO::File->new_from_fd($b, 'w') or die;
187 # unless ($main::is_win) {
188 # # $SIG{HUP} = 'IGNORE';
189 # $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
192 # exec "$line" or dbg("exec '$line' failed $!");
195 # dbg("cannot fork for $line");
198 # dbg("no socket pair $! for $line");
206 return if exists $conn->{disconnecting};
208 $conn->{disconnecting} = 1;
209 my $sock = delete $conn->{sock};
210 $conn->{state} = 'E';
211 $conn->{timeout}->del if $conn->{timeout};
213 # be careful to delete the correct one
215 if ($call = $conn->{call}) {
216 my $ref = $conns{$call};
217 delete $conns{$call} if $ref && $ref == $conn;
219 $call ||= 'unallocated';
220 dbg("Connection $conn->{cnum} $call disconnected") if isdbg('connll');
222 # get rid of any references
224 if (ref($conn->{$_})) {
229 if (defined($sock)) {
230 shutdown($sock->{fh}, 2);
234 unless ($main::is_win) {
235 kill 'TERM', $conn->{pid} if exists $conn->{pid};
242 my $rq = $conn->{outqueue};
243 my $sock = $conn->{sock};
246 my $data = shift @$rq;
247 my $lth = length $data;
248 my $call = $conn->{call} || 'none';
251 dbgdump('raw', "$call send $lth: ", $lth);
254 if (defined $sock && !$sock->destroyed) {
255 $sock->push_write($data);
258 dbg("_send_stuff $call ending data ignored: $data");
264 my ($conn, $msg) = @_;
265 my $rq = $conn->{outqueue};
266 my $sock = $conn->{sock};
268 # this is done like this because enqueueing may be going on independently of
269 # sending (whether later or now)
270 $conn->enqueue($msg);
274 sub send_now { goto &send_later; }
278 my ($conn, $msg) = @_;
279 push @{$conn->{outqueue}}, $msg;
285 push (@{$conn->{outqueue}}, defined $_[0] ? $_[0] : '');
288 sub _err_will_block {
295 $conn->{sock}->on_drain(sub {$conn->disconnect;});
298 #-----------------------------------------------------------------
299 # Receive side routines
302 @_ == 4 || die "Msg->new_server (myhost, myport, login_proc\n";
303 my ($pkg, $my_host, $my_port, $login_proc) = @_;
304 my $self = $pkg->new($login_proc);
306 $self->{sock} = tcp_server $my_host, $my_port, sub { $self->new_client(@_); }, sub { return 256; };
307 die "Could not create socket: $! \n" unless $self->{sock};
315 my $sock = $conn->{sock};
317 # $sock->keepalive(1);
324 if ($conn->{msg} =~ /\n/) {
325 my @lines = split /\r?\n/, $conn->{msg};
326 if ($conn->{msg} =~ /\n$/) {
329 $conn->{msg} = pop @lines;
332 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
337 sub _rcv { # Complement to _send
338 my $conn = shift; # $rcv_now complement of $flush
339 # Find out how much has already been received, if at all
340 my ($msg, $offset, $bytes_to_read, $bytes_read);
341 my $sock = $conn->{sock};
342 return unless defined($sock);
345 $msg = $sock->{rbuf};
346 $bytes_read = length $msg || 0;
349 if ($bytes_read > 0) {
350 $total_in += $bytes_read;
352 my $call = $conn->{call} || 'none';
353 dbgdump('raw', "$call read $bytes_read: ", $msg);
356 my @ch = split //, $msg;
361 $conn->{msg} =~ s/.$//;
368 $conn->send_now($out);
371 $conn->{msg} .= $msg;
375 unless ($conn->{disable_read}) {
376 $conn->dequeue if exists $conn->{msg};
381 my $server_conn = shift;
383 my $peerhost = shift;
384 my $peerport = shift;
386 my $conn = $server_conn->new($server_conn->{rproc});
387 $conn->{sock} = AnyEvent::Handle->new(
391 on_eof => sub {$conn->disconnect},
393 on_error => sub {$conn->disconnect},
399 $conn->{blocking} = 0;
400 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $peerhost, $conn->{peerport} = $peerport);
401 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
402 $conn->{sort} = 'Incoming';
404 $conn->{eproc} = $eproc;
407 $conn->{rproc} = $rproc;
408 $conn->{sock}->on_read(sub {$conn->_rcv});
409 } else { # Login failed
410 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
414 dbg("Msg: error on accept ($!)") if isdbg('err');
424 # close all clients (this is for forking really)
425 sub close_all_clients
427 foreach my $conn (values %conns) {
435 return defined $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
440 my ($pkg, $interval) = @_;
441 my $cv = AnyEvent->condvar;
442 my $wait_a_bit = AnyEvent->timer(
444 cb => sub {$cv->send},
449 sub set_event_handler
453 my ($pkg, $fn, $line) = caller;
455 foreach (my ($k,$v) = each %args) {
459 dbg("Msg::set_event_handler called from ${pkg}::${fn} line $line doing $s");
465 my $call = $conn->{call} || 'unallocated';
466 my $host = $conn->{peerhost} || '';
467 my $port = $conn->{peerport} || '';
468 dbg("Connection $conn->{cnum} $call [$host $port] being destroyed") if isdbg('connll');