include "sys.m"; sys := load Sys Sys->PATH; Rread: type chan of (array of byte, string); Rwrite: type chan of (int, string); FileIO: adt { read: chan of (int, int, int, Rread); write: chan of (int, array of byte, int, Rwrite); }; file2chan: fn(dir, file: string): ref FileIO;
File2chan returns a FileIO type holding two channels used to deliver tuples representing the contents of the Tread and Twrite Styx messages received by the system on the server's behalf; see intro(5).
When the client invokes the read system call on the file, the server receives a tuple, say (offset, count, fid, rc), on the read channel. The request asks for count bytes of data, starting at offset bytes from the beginning of the file associated with fid. The server sends its reply to the client by transmitting a tuple, say (data, error), that contains the data for the read, on the channel rc received as part of the read tuple. If the request was successful, the error string should be nil. If an error occurred, error should be a diagnostic string, and the data array should be nil. The client blocks in the read system call until the server sends its reply. The client receives only count bytes even if data is larger.
When the client does a write system call on the file, the server receives a tuple, (offset, data, fid, wc), on the write channel. A (count, error) response is sent back on the wc channel received in the write tuple: count gives the number of bytes written (zero if an error occurs), and error is an empty or nil string or an explanation of the problem.
If the client closes the file for reading (writing), the server will receive a read (write) message with a nil rc (wc). The server can use these signals to determine when to stop processing. The fid received by the server can be used to manage the multiplexing of multiple active clients sharing a single file; see intro(5) for details.
SYS-FILE2CHAN(2 ) | Rev: Thu Feb 15 14:43:27 GMT 2007 |