From c01a9fdadd6dc0b5f56a78af4b0f6d240e3638f0 Mon Sep 17 00:00:00 2001 From: Izaya Date: Thu, 16 Aug 2018 20:54:23 +1000 Subject: [PATCH] improved socket:read() --- OpenOS/README.md | 7 ++++++- OpenOS/usr/lib/minitel.lua | 22 +++++++++++++++++++--- OpenOS/usr/man/minitel | 33 +++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/OpenOS/README.md b/OpenOS/README.md index c7f3d3b..f8c26c8 100644 --- a/OpenOS/README.md +++ b/OpenOS/README.md @@ -67,7 +67,12 @@ The minitel library provides an easy way of interacting with the minitel daemon, *stream:write(data)* - Sends *data* to the node at the other end of the stream -*stream:read(length)* - Reads data from the stream, up to *length* bytes. +*stream:read(length)* - Reads data from the stream, in several modes: + +- If you pass *length* as a number, up to *length* bytes will be read from the socket. +- If *length* == "\*a", everything in the buffer will be returned. +- If you pass *length* as any other string, and there is *length* in the buffer somewhere, the data up to *length* in the buffer will be returned. This ignores all but the first character. +- If *length* is nil, it will read up until the next newline. *stream:close()* - Ends the stream and prevents further writing. diff --git a/OpenOS/usr/lib/minitel.lua b/OpenOS/usr/lib/minitel.lua index 2eccf4b..c180b48 100644 --- a/OpenOS/usr/lib/minitel.lua +++ b/OpenOS/usr/lib/minitel.lua @@ -59,10 +59,26 @@ local function cwrite(self,data) end end local function cread(self,length) + length = length or "\n" local rdata = "" - rdata = self.rbuffer:sub(1,length) - self.rbuffer = self.rbuffer:sub(length+1) - return rdata + if type(length) == "number" then + rdata = self.rbuffer:sub(1,length) + self.rbuffer = self.rbuffer:sub(length+1) + return rdata + elseif type(length) == "string" then + if length:sub(1,2) == "*a" then + rdata = self.rbuffer + self.rbuffer = "" + return rdata + elseif length:len() == 1 then + local pre, post = self.rbuffer:match("(.-)"..length.."(.*)") + if pre and post then + self.rbuffer = post + return pre + end + return nil + end + end end local function socket(addr,port,sclose) diff --git a/OpenOS/usr/man/minitel b/OpenOS/usr/man/minitel index 4d0fef9..f8c26c8 100644 --- a/OpenOS/usr/man/minitel +++ b/OpenOS/usr/man/minitel @@ -1,6 +1,6 @@ # Minitel for OpenOS -This package includes the Minitel daemon, in etc/rc.d/minitel.lua, and the net library for using minitel, in usr/lib/net.lua. +This package includes the Minitel daemon, in etc/rc.d/minitel.lua, and the minitel library for using minitel, in usr/lib/minitel.lua. ## Minitel daemon @@ -39,44 +39,49 @@ To change a setting, one invokes: In addition, one can invoke to get large amounts of debug output, *rc minitel set_route * to add a static route, and *rc minitel del_route * to delete a static route. -## Net library +## Minitel library -The net library provides an easy way of interacting with the minitel daemon, and implements higher-level features of the stack. +The minitel library provides an easy way of interacting with the minitel daemon, and implements higher-level features of the stack. ### API #### Layer 3 -*net.genPacketID()* - returns a string of random data +*minitel.genPacketID()* - returns a string of random data -*net.usend(host, port, data, pid)* - Sends an unreliable packet to *host* on *port* containing *data*, optionally with the packet ID *pid*. +*minitel.usend(host, port, data, pid)* - Sends an unreliable packet to *host* on *port* containing *data*, optionally with the packet ID *pid*. -*net.rsend(host, port, data, block)* - Sends a reliable packet to *host* on *port* containing *data*. If *block* is true, don't wait for a reply. +*minitel.rsend(host, port, data, block)* - Sends a reliable packet to *host* on *port* containing *data*. If *block* is true, don't wait for a reply. #### Layer 4 -*net.send(host, port, data)* - Sends *data* reliably and in order to *host* on *port*. +*minitel.send(host, port, data)* - Sends *data* reliably and in order to *host* on *port*. #### Layer 5 -*net.open(to,port)* - Establishes a stream to *host* on *port* and returns a stream object +*minitel.open(to,port)* - Establishes a stream to *host* on *port* and returns a stream object -*net.listen(port)* - Waits for another node to establish a stream, and returns the stream object. +*minitel.listen(port)* - Waits for another node to establish a stream, and returns the stream object. #### Stream objects *stream:write(data)* - Sends *data* to the node at the other end of the stream -*stream:read(length)* - Reads data from the stream, up to *length* bytes. +*stream:read(length)* - Reads data from the stream, in several modes: + +- If you pass *length* as a number, up to *length* bytes will be read from the socket. +- If *length* == "\*a", everything in the buffer will be returned. +- If you pass *length* as any other string, and there is *length* in the buffer somewhere, the data up to *length* in the buffer will be returned. This ignores all but the first character. +- If *length* is nil, it will read up until the next newline. *stream:close()* - Ends the stream and prevents further writing. #### Variables -*net.mtu = 4096* - The maximum length of the data portion of a packet for *net.send* +*minitel.mtu = 4096* - The maximum length of the data portion of a packet for *minitel.send* -*net.streamdelay = 60* - The time, in seconds, *net.open* will wait for a response while trying to establish a connection. +*minitel.streamdelay = 60* - The time, in seconds, *minitel.open* will wait for a response while trying to establish a connection. -*net.minport = 32768* - The lowest port *net.listen* will allocate to a new connection. +*minitel.minport = 32768* - The lowest port *minitel.listen* will allocate to a new connection. -*net.maxport = 65535* - The highest port *net.listen* will allocate to a new connection. +*minitel.maxport = 65535* - The highest port *minitel.listen* will allocate to a new connection.