2009-02-12 19:48:06 +00:00
|
|
|
--[[
|
|
|
|
nixio - Linux I/O library for lua
|
|
|
|
|
|
|
|
Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
]]--
|
|
|
|
|
2009-02-14 14:39:12 +00:00
|
|
|
local table = require "table"
|
2009-02-12 19:48:06 +00:00
|
|
|
local nixio = require "nixio"
|
|
|
|
local setmetatable, assert = setmetatable, assert
|
|
|
|
|
|
|
|
module "nixio.util"
|
|
|
|
|
|
|
|
local BUFFERSIZE = 8096
|
|
|
|
local socket = nixio.socket_meta
|
|
|
|
|
2009-02-15 13:59:30 +00:00
|
|
|
function socket.recvall(self, len)
|
2009-02-14 14:39:12 +00:00
|
|
|
local block, code, msg = self:recv(len)
|
|
|
|
|
|
|
|
if not block then
|
|
|
|
return "", code, msg, len
|
2009-02-15 13:59:30 +00:00
|
|
|
elseif #block == 0 then
|
|
|
|
return "", nil, nil, len
|
2009-02-14 14:39:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local data, total = {block}, #block
|
|
|
|
|
|
|
|
while len > total do
|
|
|
|
block, code, msg = self:recv(len - total)
|
|
|
|
|
|
|
|
if not block then
|
|
|
|
return data, code, msg, len - #data
|
2009-02-15 13:59:30 +00:00
|
|
|
elseif #block == 0 then
|
|
|
|
return data, nil, nil, len - #data
|
2009-02-14 14:39:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
data[#data+1], total = block, total + #block
|
|
|
|
end
|
|
|
|
|
|
|
|
return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0
|
|
|
|
end
|
|
|
|
|
2009-02-12 19:48:06 +00:00
|
|
|
function socket.sendall(self, data)
|
2009-02-14 14:39:12 +00:00
|
|
|
local total, block = 0
|
2009-02-12 19:48:06 +00:00
|
|
|
local sent, code, msg = self:send(data)
|
|
|
|
|
|
|
|
if not sent then
|
2009-02-14 14:39:12 +00:00
|
|
|
return total, code, msg, data
|
2009-02-12 19:48:06 +00:00
|
|
|
end
|
|
|
|
|
2009-02-14 14:39:12 +00:00
|
|
|
while sent < #data do
|
|
|
|
block, total = data:sub(sent + 1), total + sent
|
|
|
|
sent, code, msg = self:send(block)
|
2009-02-12 19:48:06 +00:00
|
|
|
|
|
|
|
if not sent then
|
2009-02-14 14:39:12 +00:00
|
|
|
return total, code, msg, block
|
2009-02-12 19:48:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-14 14:39:12 +00:00
|
|
|
return total + sent, nil, nil, ""
|
2009-02-12 19:48:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function socket.linesource(self, limit)
|
|
|
|
limit = limit or BUFFERSIZE
|
|
|
|
local buffer = ""
|
2009-02-15 13:59:30 +00:00
|
|
|
local bpos = 0
|
2009-02-12 19:48:06 +00:00
|
|
|
return function(flush)
|
2009-02-15 13:59:30 +00:00
|
|
|
local line, endp, _
|
2009-02-12 19:48:06 +00:00
|
|
|
|
|
|
|
if flush then
|
2009-02-15 13:59:30 +00:00
|
|
|
line = buffer:sub(bpos + 1)
|
2009-02-12 19:48:06 +00:00
|
|
|
buffer = ""
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
|
|
|
while not line do
|
2009-02-14 14:39:12 +00:00
|
|
|
_, endp, line = buffer:find("^(.-)\r?\n", bpos + 1)
|
2009-02-12 19:48:06 +00:00
|
|
|
if line then
|
2009-02-14 14:39:12 +00:00
|
|
|
bpos = endp
|
2009-02-12 19:48:06 +00:00
|
|
|
return line
|
2009-02-14 14:39:12 +00:00
|
|
|
elseif #buffer < limit + bpos then
|
|
|
|
local newblock, code = self:recv(limit + bpos - #buffer)
|
2009-02-12 19:48:06 +00:00
|
|
|
if not newblock then
|
|
|
|
return nil, code
|
|
|
|
end
|
2009-02-14 14:39:12 +00:00
|
|
|
buffer = buffer:sub(bpos + 1) .. newblock
|
|
|
|
bpos = 0
|
2009-02-12 19:48:06 +00:00
|
|
|
else
|
|
|
|
return nil, 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|