2 nixio - Linux I/O library for lua
4 Copyright 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 local table = require "table"
16 local nixio = require "nixio"
17 local setmetatable, assert = setmetatable, assert
21 local BUFFERSIZE = 8096
22 local socket = nixio.socket_meta
24 function socket.recvall(self, len)
25 local block, code, msg = self:recv(len)
28 return "", code, msg, len
29 elseif #block == 0 then
30 return "", nil, nil, len
33 local data, total = {block}, #block
36 block, code, msg = self:recv(len - total)
39 return data, code, msg, len - #data
40 elseif #block == 0 then
41 return data, nil, nil, len - #data
44 data[#data+1], total = block, total + #block
47 return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0
50 function socket.sendall(self, data)
51 local total, block = 0
52 local sent, code, msg = self:send(data)
55 return total, code, msg, data
59 block, total = data:sub(sent + 1), total + sent
60 sent, code, msg = self:send(block)
63 return total, code, msg, block
67 return total + sent, nil, nil, ""
70 function socket.linesource(self, limit)
71 limit = limit or BUFFERSIZE
74 return function(flush)
78 line = buffer:sub(bpos + 1)
85 _, endp, line = buffer:find("(.-)\r?\n", bpos + 1)
89 elseif #buffer < limit + bpos then
90 local newblock, code = self:recv(limit + bpos - #buffer)
93 elseif #newblock == 0 then
96 buffer = buffer:sub(bpos + 1) .. newblock
105 function socket.blocksource(self, bs, limit)
106 bs = bs or BUFFERSIZE
112 elseif limit < toread then
117 local block, code, msg = self:recv(toread)
121 elseif #block == 0 then
125 limit = limit - #block