diff options
author | Steven Barth <steven@midlink.org> | 2009-02-12 19:48:06 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2009-02-12 19:48:06 +0000 |
commit | 5ff898e6248b668430f5f2e7d70de243f191eb04 (patch) | |
tree | 86d8807d89731c37c57981b5c0996138c77fdcf3 /libs/nixio/lua | |
parent | 4327f9250e6d0f4a81ca949289d5e1444a360d4e (diff) |
nixio next
splice() still does not work correctly
Diffstat (limited to 'libs/nixio/lua')
-rw-r--r-- | libs/nixio/lua/nixio/util.lua | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/libs/nixio/lua/nixio/util.lua b/libs/nixio/lua/nixio/util.lua new file mode 100644 index 000000000..962ef9d80 --- /dev/null +++ b/libs/nixio/lua/nixio/util.lua @@ -0,0 +1,70 @@ +--[[ +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$ +]]-- + +local nixio = require "nixio" +local setmetatable, assert = setmetatable, assert + +module "nixio.util" + +local BUFFERSIZE = 8096 +local socket = nixio.socket_meta + +function socket.sendall(self, data) + local sent, code, msg = self:send(data) + + if not sent then + return sent, code, msg, data + end + + while sent < #data do + data = data:sub(sent + 1) + sent, code, msg = self:send(data) + + if not sent then + return sent, code, msg, data + end + end + + return true +end + +function socket.linesource(self, limit) + limit = limit or BUFFERSIZE + local buffer = "" + return function(flush) + local line, endp, _ + + if flush then + line = buffer + buffer = "" + return line + end + + while not line do + _, endp, line = buffer:find("^(.-)\r?\n") + if line then + buffer = buffer:sub(endp+1) + return line + elseif #buffer < limit then + local newblock, code = self:recv(limit - #buffer) + if not newblock then + return nil, code + end + buffer = buffer .. newblock + else + return nil, 0 + end + end + end +end
\ No newline at end of file |