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 9P 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.
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. Each distinct open of the file will yield a distinct fid, unique to that open, which will appear in the read and write messages corresponding to reads and writes on that open instance of the file.
When the client closes the file, the server will be sent a read message with a nil rc, then a write message with a nil wc. A server typically ignores the former and uses the latter as a signal to stop processing for the given client (more precisely, the given fid). Note that the fid can later be re-used by another client.
SYS-FILE2CHAN(2 ) | Rev: Tue Mar 31 02:42:38 GMT 2015 |