The guts of the new Msg system
[spider.git] / perl / client.pl
index b36be34715282605f542948529e1c2f96e375dc4..57a5d45b0fa4ea373ac434a08d9c2b623687febe 100755 (executable)
@@ -39,6 +39,7 @@ BEGIN {
 }
 
 use Msg;
+use IntMsg;
 use DXVars;
 use DXDebug;
 use DXUtil;
@@ -78,8 +79,10 @@ sub sig_term
 # terminate a child
 sub sig_chld
 {
-       $SIG{CHLD} = \&sig_chld;
-       $waitedpid = wait;
+       unless ($^O =~ /^MS/i) {
+               $SIG{CHLD} = \&sig_chld;
+               $waitedpid = wait;
+       }
        dbg('connect', "caught $pid");
 }
 
@@ -88,10 +91,13 @@ sub setmode
 {
        if ($mode == 1) {
                $mynl = "\r";
+               $out_lineend = "\r";
        } else {
                $mynl = "\n";
+               $out_lineend = "\r\n";
        }
        $/ = $mynl;
+       $out_lineend = $mynl;
 }
 
 # handle incoming messages
@@ -119,7 +125,7 @@ sub rec_socket
                        if ($buffered) {
                                if (length $outqueue >= $client_buffer_lth) {
                                        print $stdout $outqueue;
-                                       pop @echo if @echo > $maxecho;
+                                       pop @echo while (@echo > $maxecho);
                                        push @echo, $outqueue;
                                        $outqueue = "";
                                }
@@ -146,7 +152,7 @@ sub rec_socket
                } elsif ($sort eq 'B') {
                        if ($buffered && $outqueue) {
                                print $stdout $outqueue;
-                               pop @echo if @echo > $maxecho;
+                               pop @echo while(@echo > $maxecho);
                                push @echo, $outqueue;
                                $outqueue = "";
                        }
@@ -233,7 +239,13 @@ sub doconnect
                $sock->output_record_separator('');
                $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
                $sock->open($host) or die "Can't connect to $host port $port $!";
-               $sock->binmode(1);
+               if ($port == 23) {
+                       $sock->telnetmode(1);
+                       $sock->option_send(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO) if $port == 23;
+               } else {
+                       $sock->telnetmode(0);
+               }
+               $sock->binmode(0);
                $mode = 3;
        } elsif ($sort eq 'ax25' || $sort eq 'prog') {
                my @args = split /\s+/, $line;
@@ -265,6 +277,16 @@ sub dotimeout
        $timeout = $val;
 }
 
+sub dolineend
+{
+       my $val = shift;
+       $out_lineend = $val;
+       $out_lineend =~ s/\\r/\r/g;
+       $out_lineend =~ s/\\n/\n/g;
+       dbg('connect', "lineend set to $val ");
+       $out_lineend = $mynl unless $out_lineend;
+}
+
 sub dochat
 {
        my ($expect, $send) = @_;
@@ -304,9 +326,10 @@ sub dochat
        }
        if ($send) {
                if ($csort eq 'telnet') {
+#                      local $\ = $out_lineend;
                        $sock->print("$send\n");
                } elsif ($csort eq 'ax25') {
-                       local $\ = "\r";
+                       local $\ = $out_lineend;
                        $wfh->print("$send");
                }
                dbg('connect', "sent \"$send\"");
@@ -350,6 +373,7 @@ $maxecho = 5;                  # length of max echo queue
 $pid = 0;                       # the pid of the child program
 $csort = "";                    # the connection type
 $sock = 0;                      # connection socket
+$out_lineend = $mynl;          # connection lineending (used for outgoing connects) 
 
 $stdin = *STDIN;
 $stdout = *STDOUT;
@@ -380,11 +404,12 @@ if ($call eq $mycall) {
 
 $stdout->autoflush(1);
 
-$SIG{'INT'} = \&sig_term;
-$SIG{'TERM'} = \&sig_term;
-$SIG{'HUP'} = \&sig_term;
-$SIG{'CHLD'} = \&sig_chld;
-$SIG{'ALRM'} = \&timeout;
+unless ($^O =~ /^MS/i) {
+       $SIG{'INT'} = \&sig_term;
+       $SIG{'TERM'} = \&sig_term;
+       $SIG{'HUP'} = \&sig_term;
+       $SIG{'CHLD'} = \&sig_chld;
+}
 
 dbgadd('connect');
 
@@ -444,7 +469,9 @@ if ($connsort eq "connect") {
                doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
                doabort($1) if /^\s*a\w*\s+(.*)/io;
                dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
+               dolineend($1) if /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/;
                dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
+               
                if (/^\s*cl\w+\s+(.*)/io) {
                        doclient($1);
                        last;
@@ -497,7 +524,7 @@ if ($ssid) {
 }
 
 
-$conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
+$conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
 if (! $conn) {
        if (-r "$data/offline") {
                open IN, "$data/offline" or die;
@@ -518,7 +545,7 @@ Msg->set_event_handler($stdin, "read" => \&rec_stdin);
 
 for (;;) {
        my $t;
-       Msg->event_loop(1, 1);
+       Msg->event_loop(1, 0.1);
        $t = time;
        if ($t > $lasttime) {
                if ($outqueue) {