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.readall(self, len)
25 local block, code, msg = self:recv(len)
28 return "", code, msg, len
31 local data, total = {block}, #block
34 block, code, msg = self:recv(len - total)
37 return data, code, msg, len - #data
40 data[#data+1], total = block, total + #block
43 return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0
46 function socket.sendall(self, data)
47 local total, block = 0
48 local sent, code, msg = self:send(data)
51 return total, code, msg, data
55 block, total = data:sub(sent + 1), total + sent
56 sent, code, msg = self:send(block)
59 return total, code, msg, block
63 return total + sent, nil, nil, ""
66 function socket.linesource(self, limit)
67 limit = limit or BUFFERSIZE
69 return function(flush)
70 local bpos, line, endp, _ = 0
79 _, endp, line = buffer:find("^(.-)\r?\n", bpos + 1)
83 elseif #buffer < limit + bpos then
84 local newblock, code = self:recv(limit + bpos - #buffer)
88 buffer = buffer:sub(bpos + 1) .. newblock