class SOCKET
****
Bidirectional communication of strings between processes (intra- or inter-machine) using sockets. Blocking reads, potentially blocking writes.




Public


Readonly Shareds
shared num_connect_attempts:INT:=10;
**** Connecting sockets will look once per second for a matching initator, this many times, before failing.
shared unix_socket_directory:STR:="/tmp/";
**** Where Unix-domain server sockets will live. Probably should leave this alone; but if you must change it at runtime, be sure to change it in both communicating processes!

Writable Shareds
shared num_connect_attempts:INT:=10;
**** Connecting sockets will look once per second for a matching initator, this many times, before failing.
shared unix_socket_directory:STR:="/tmp/";
**** Where Unix-domain server sockets will live. Probably should leave this alone; but if you must change it at runtime, be sure to change it in both communicating processes!

Features
block_until_can_read
**** Blocks in a non-busy wait until socket can read. If socket is already dead: raise an exception if using exceptions, else return immediately.
block_until_can_write
**** Blocks in a non-busy wait until socket can write without blocking. If socket is already dead: raise an exception if using exceptions, else return immediately.
can_read_without_block:BOOL
**** Return true if socket is alive and data is available so that `get_str' would not block.
can_write_without_block:BOOL
**** Return true if socket is alive and 'plus' would accept a string without blocking due to full buffers.
close
**** Close the socket and mark it dead. Hopefully, by using this, programs using Internet sockets can avoid the "lingering socket" phenomenon...
create_connecting_inet(host:STR,port:INT):SAME
**** Connect to an existing Internet SOCKET on port `port' of
__machine_`host'.__Will_try_once_per_second_up_to_10_seconds.
`port' must be >= `min_port_num'. Upon error, return a dead socket.
create_connecting_unix(name:STR):SAME
**** Connect to an existing Unix-domain SOCKET named `name'. Will try once per second up to 10 seconds. `name' must be a legal Unix filename. Upon error, return a dead socket.
create_initiating_inet(port:INT):SAME
**** Initate an Internet SOCKET on port `port', then block until another process does a 'create_connecting_inet' to this port. `port' must be >= `min_port_num'. Upon error, return a dead socket.
create_initiating_unix(name:STR):SAME
**** Initate a SOCKET named `name', then block until another process does a 'create_connecting_unix' with the same name, on the same machine. `name' must be a legal Unix filename. Upon error, return a dead socket.
get_str:STR
**** Return the next string from the peer SOCKET. An empty-string result means the sender sent EITHER the empty string or a void string. May block, if no strings are available, until one is sent. If an error occurs, the socket dies, and return void. If socket is already dead: raise an exception if using exceptions, else simply return void.
is_dead:BOOL
**** Indicates whether the socket is alive and capable of performing a `get_str' or `plus'.
is_using_exceptions:BOOL
**** Report whether this socket uses exceptions as described above.
min_port_num:INT
**** Internet socket port numbers must exceed this value.
plus(s:$STR)
**** Send string version of `s' to the peer SOCKET. If 's' is void, the receiver will get the empty string. May block, if buffers are full, until receiver does a `get_str'. If an error occurs, the socket dies. If socket is already dead: raise an exception if using exceptions, else simply return.
plus(s:$STR):SAME
**** Same as above except return self, allowing chaining, eg, skt+x+y+z.
reasonable_filename(s:STR):BOOL
**** Return whether `s' is a reasonable Unix filename and hence suitable for a Unix-domain socket name. Overly cautious, but this ought to be implemented elsewhere anyway.
use_exceptions(b:BOOL)
**** Turn on or off the raising of a SOCKET_EXCEPTION when either (1) the socket dies due to external factors (usually termination of the peer socket's process) in the course of any operation other than an explicit `is_dead' check; or (2) a caller attempts to read or write using an already closed or dead socket. Note that in any case, `is_dead' will be set when a socket dies or is closed. If exceptions are turned on when the socket is already dead, an exception is raised.


Private

die(s:STR)
**** Close the socket (thereby marking it dead), and raise an exception if use_exceptions_var is set.
attr id:INT;
**** Unix descriptor for the socket.
attr id:INT;
**** Unix descriptor for the socket.
attr is_dead_var:BOOL;
**** Indicates whether socket is operational for `get_str' or `plus'.
attr is_dead_var:BOOL;
**** Indicates whether socket is operational for `get_str' or `plus'.
attr use_exceptions_var:BOOL;
**** Flag whether to raise an exception under the conditions described in the routine `use_exceptions'.
attr use_exceptions_var:BOOL;
**** Flag whether to raise an exception under the conditions described in the routine `use_exceptions'.

The Sather Home Page