Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: ZINETNTM

ZINETNTM.m

Go to the documentation of this file.
  1. %INET ; slt,bar,CHEM,LD ; 29 Dec 98 14:24;Internet function utility - MSM
  1. ;;3.01;BHL IHS Interfaces with GIS;;JUL 01, 2001
  1. ;CHCS TLS_4603; GEN 1; 21-MAY-1999
  1. ;COPYRIGHT 1998 SAIC
  1. ;NOTE: unlike DSM %INET, this version does NOT support multiple client
  1. ; connections to the same address simultaneously by a single process.
  1. Q
  1. OPEN(ZICHAN,ZIX,ZIDOMAIN,ZIPORT,ZIPROT) ;open an internet socket
  1. ;input:
  1. ; ZICHAN --> receives the "IP Address, domain" or "IP Address"
  1. ; ZIX --> variable used as 1st argument to RECV procedure
  1. ; ZIDOMAIN --> domain name i.e. Internet address (null to open a server connection.)
  1. ; ZIPORT --> port number
  1. ; ZIPROT --> null, 0 or 1:tcp, 2:udp
  1. ;
  1. S $ZT="ERR^"_$T(+0)
  1. N %,INDI S INDI=$G(IO,$I)
  1. S ZICHAN=0,ZIX(1)="",ZIX(2)=""
  1. S ZIPROT=$G(ZIPROT),ZIPROT=$S('ZIPROT!(ZIPROT=1):"TCP",1:"UDP")
  1. C 56 O 56::5
  1. ;Xecute statements for ^ZCMS (not proper DSM syntax - duh.)
  1. ; :(:1) sets the flag on the TCP port to change the behavior of
  1. ; a WRITE ! to the socket - it will transmit <CR><LF> instead of
  1. ; merely flushing the buffer. See "MSM-Server User's Guide" in the
  1. ; "Using Peripheral Devices" section labeled "TCP/IP Socket Device"
  1. ; for more information.
  1. X "U 56:(:1)" ; !=$C(13,10)
  1. X "U 56::ZIPROT W /SOCKET(ZIDOMAIN,ZIPORT) S %=$KEY=""""!$ZC" Q:%
  1. X "S ZICHAN=$KEY" ; $KEY is MSM's "connection" (numeric) identifier
  1. U:INDI]"" INDI
  1. Q
  1. ;
  1. CLOSE(ZICHAN) ;close the Internet socket (flush)
  1. ;input:
  1. ; ZICHAN --> "IP Address, domain" or "IP Address"
  1. ;
  1. Q:'$L($G(ZICHAN))
  1. S $ZT="QUIT^"_$T(+0)
  1. X "C:ZICHAN 56:ZICHAN" K ZICHAN
  1. Q
  1. ;
  1. SEND(ZIX,ZICHAN,ZICRLF) ;send a message over socket
  1. ;input:
  1. ; ZIX --> a line of text to be sent
  1. ; ZICHAN --> "IP Address, Domain" or "IP Address"
  1. ; ZICRLF --> carriage return/line feed flag 0:yes, 1:no
  1. ;
  1. S $ZT="ERR^"_$T(+0)
  1. N INDI S INDI=$G(IO,$I) U 56
  1. I '$G(ZICRLF),$L(ZIX)<511 W ZIX_$C(13,10)
  1. E W ZIX W:'$G(ZICRLF) !
  1. U:INDI]"" INDI Q
  1. ;
  1. RECV(ZIX,ZICHAN,ZITO,ZICRLF) ;receive message from socket
  1. ;input:
  1. ; ZIX --> variable/array buffer
  1. ; ZICHAN --> "IP Address, Domain" or "IP Address"
  1. ; ZITO --> numeric time out valued in seconds
  1. ; ZICRLF --> carriage return/line feed flag 0:yes, 1:no
  1. ; (YES means we expect a CR LF and wish to strip it before
  1. ; returning to calling application.)
  1. ;output:
  1. ; ZIX --> received message
  1. ;
  1. S $ZT="ERR^"_$T(+0)
  1. N INDI S INDI=$G(IO,$I) U 56 ; note: 4 separate exit points
  1. S ZICRLF=$G(ZICRLF) K ZIX(0)
  1. S ZITO=$S('$D(ZITO):600,1:ZITO),ZIX(1)=$G(ZIX(1)),ZIX(2)=$G(ZIX(2))
  1. ;
  1. ; If a <CR> <LF> is expected as delimiter, and should be stripped...
  1. I 'ZICRLF D U:INDI]"" INDI Q
  1. .;Loop until we either get a terminator or a read timeout
  1. .N INOK S INOK=0,ZIX="" F D Q:$D(ZIX(0))!(INOK)
  1. ..S ZIX(2)=ZIX(2)_ZIX
  1. ..;If data left in buffer, use that first
  1. ..I $L(ZIX(2)),$F(ZIX(2),$C(10)) D S INOK=1 Q
  1. ...N Y S Y=$F(ZIX(2),$C(10)),ZIX=$E(ZIX(2),1,Y-($A(ZIX(2),Y-2)=13)-2),ZIX(2)=$E(ZIX(2),Y,$L(ZIX(2)))
  1. ...I $L(ZIX)>512 S ZIX(2)=$E(ZIX,513,$L(ZIX))_$C(10)_ZIX(2),ZIX=$E(ZIX,1,512)
  1. ..I $L(ZIX(2))>511 S INOK=1,ZIX=$E(ZIX(2),1,512),ZIX(2)=$E(ZIX(2),513,$L(ZIX(2))) Q
  1. ..;Read more from socket...
  1. ..I ZITO R ZIX:ZITO E S ZIX(0)="Connection timed out",ZIX="" Q
  1. ..I ZITO,$ZC S ZIX(0)="Socket closed: MSM Error $ZB="_$ZB,ZIX="" Q
  1. ..I 'ZITO R ZIX S INOK=1 Q
  1. ..;and append result to the string we will check on next pass
  1. ..S ZIX=ZIX(2)_ZIX,ZIX(2)=""
  1. ;
  1. ;If we are reading just packets, with no terminators (eg CR LF)
  1. ;
  1. ;If data left in buffer, use that first
  1. I ZICRLF,$L(ZIX(2)) D U:INDI]"" INDI Q
  1. .S ZIX=$E(ZIX(2),1,512),ZIX(2)=$E(ZIX(2),513,$L(ZIX(2)))
  1. ;
  1. ;Read more from socket
  1. I ZITO R ZIX:ZITO E S ZIX(0)="Connection timed out",ZIX="" U:INDI]"" INDI Q
  1. I 'ZITO R ZIX
  1. ;
  1. S ZIX=ZIX(2)_ZIX,ZIX(2)=""
  1. ;
  1. I $L(ZIX)>512 S ZIX(2)=$E(ZIX,513,$L(ZIX)),ZIX=$E(ZIX,1,512)
  1. U:INDI]"" INDI
  1. Q
  1. ;
  1. ERR ; Handle error trap
  1. I $ZE["<DSCON" S ZIX(0)="Remote end disconected" X:$G(ZICHAN) "C 56:ZICHAN S ZICHAN=""""" U:INDI]"" INDI Q
  1. G ^%ET
  1. QUIT ; Ignore all errors when closing the channel. Just close it.
  1. Q