1
0
mirror of https://github.com/ShadowKatStudios/OC-Minitel.git synced 2024-11-23 10:38:05 +11:00

improved socket:read()

This commit is contained in:
Izaya 2018-08-16 20:54:23 +10:00
parent 3ac99ace7d
commit c01a9fdadd
3 changed files with 44 additions and 18 deletions

View File

@ -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.

View File

@ -59,10 +59,26 @@ local function cwrite(self,data)
end
end
local function cread(self,length)
length = length or "\n"
local 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)

View File

@ -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 <hostname> <local_modem> <remote_modem>* to add a static route, and *rc minitel del_route <hostname>* 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.