95 lines · 3.9 KB · Text
RawDownload
= msgio: getmsg, sendmsg, senderrmsg, getstring, putstring, getbytearray, putbytearray, puterror: exchange data on delimited and undelimited streams     include "msgio.m";    msgio := load Msgio Msgio->PATH;     init:         fn();     getmsg:       fn(fd: ref Sys->FD): array of byte;    sendmsg:      fn(fd: ref Sys->FD, buf: array of byte, n: int): int;    senderrmsg:   fn(fd: ref Sys->FD, s: string): int;     getstring:    fn(fd: ref Sys->FD): (string, string);    putstring:    fn(fd: ref Sys->FD, s: string): int;    getbytearray: fn(fd: ref Sys->FD): (array of byte, string);    putbytearray: fn(fd: ref Sys->FD, a: array of byte, n: int): int;    puterror:     fn(fd: ref Sys->FD, s: string): int; Msgio  provides  two  complementary sets of functions for theexchange of data over a network connection that uses  a  con‐nection-oriented transport protocols.  The connection is rep‐resented by a file descriptor fd. Init must be called before any other operation of the module. The first set allows arbitrary data, packed  into  arrays  ofbytes, to be exchanged on network connections using protocolssuch as TCP/IP that do not preserve record boundaries.   Theyare  used  to  implement  various  authentication  protocols,including auth(6). Each data message is transmitted with a five-byte header con‐taining  a  four-character zero-padded decimal count n termi‐nated by a newline, followed by n bytes of message data.   Anerror  message has a similar structure, except that the firstcharacter of the count is replaced  by  an  exclamation  mark(!);  the  message  data  following  contains  the diagnosticstring in its UTF-8 encoding (see utf(6)). Getmsg reads the next message from fd and  returns  its  datacontent. Sendmsg  sends  the  first n bytes of buf as a message on fd,and returns n. Senderrmsg sends the error message s. The second set of functions provide  I/O  for  strings,  bytearrays  and  error strings over network connections that pro‐vide a record structure for communication  (as  provided  forarbitrary networks by ssl(3)). Putstring  writes  string  s to fd.  It returns the number ofbytes written, or -1 if an error occurred.  Messages  writtenby putstring are truncated to 4096 bytes. Getstring  reads a string as written by putstring from fd andreturns a tuple (result,error).   If  successful,  the  errorstring is nil. Putbytearray  writes  the array of bytes a to fd.  It returnsthe number of bytes written, or  -1  if  an  error  occurred.Messages written by putbytearray are truncated to 4096 bytes. Getbytearray reads an array of bytes as written by putbytear‐ray from fd and returns a tuple of the  form  (result,error).If successful, the error string is nil. Puterror  writes  an error string s to fd.  It can be used inplace of putstring or putbytearray to cause  a  correspondinggetstring or getbytearray to fail (in the receiving process),forcing them to return the error string s.   It  may  not  belonger than Sys->ERRMAX bytes. == source/appl/lib/msgio.b == diagnosticsThe  output  functions return an int which is -1 if there wasan I/O error, and a non-negative value otherwise;  they  alsoset the system error string.  Getmsg returns nil if there wasan error reading from fd; it sets the system error string  toreflect  the  cause.  It also returns nil if an error messagewas received instead of a  data  message;  the  system  errorstring  will  contain  the  error  message's diagnostic.  Theother input functions (for streams with delimiters) return  atuple  that  includes  a  string  indicating the cause of theerror, as the second element of the tuple. == bugsThe module  is  really  intended  to  retrofit  the  originalInferno  authentication  protocols  into a new regime, and isnot yet intended for general-purpose use, hence the irregularnaming  and  calling conventions, inherited from the originalimplementation in Keyring.