2004-05-28 22:40:13 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- LuaSocket helper module
|
|
|
|
-- Author: Diego Nehab
|
2012-04-11 22:21:25 +02:00
|
|
|
-- RCS ID: $Id: socket.lua,v 1.22 2005/11/22 08:33:29 diego Exp $
|
2004-05-28 22:40:13 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-09-27 06:01:18 +02:00
|
|
|
-- Declare module and import dependencies
|
2004-05-28 22:40:13 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2005-06-14 06:29:23 +02:00
|
|
|
local base = _G
|
2004-11-28 09:17:16 +01:00
|
|
|
local string = require("string")
|
|
|
|
local math = require("math")
|
2005-06-17 06:04:55 +02:00
|
|
|
local socket = require("socket.core")
|
2004-11-28 09:17:16 +01:00
|
|
|
module("socket")
|
2004-05-28 22:40:13 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2005-06-17 06:04:55 +02:00
|
|
|
-- Exported auxiliar functions
|
2004-05-28 22:40:13 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2005-06-17 06:04:55 +02:00
|
|
|
function connect(address, port, laddress, lport)
|
2004-05-28 22:40:13 +02:00
|
|
|
local sock, err = socket.tcp()
|
|
|
|
if not sock then return nil, err end
|
2005-11-22 09:33:29 +01:00
|
|
|
if laddress then
|
2004-05-28 22:40:13 +02:00
|
|
|
local res, err = sock:bind(laddress, lport, -1)
|
|
|
|
if not res then return nil, err end
|
|
|
|
end
|
|
|
|
local res, err = sock:connect(address, port)
|
|
|
|
if not res then return nil, err end
|
|
|
|
return sock
|
|
|
|
end
|
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
function bind(host, port, backlog)
|
2011-06-14 02:04:37 +02:00
|
|
|
if host == "*" then host = "0.0.0.0" end
|
|
|
|
local addrinfo, err = socket.dns.getaddrinfo(host);
|
|
|
|
if not addrinfo then return nil, err end
|
|
|
|
local sock, err;
|
|
|
|
if addrinfo[1].family == "inet" then
|
|
|
|
sock, err = socket.tcp()
|
|
|
|
else
|
|
|
|
sock, err = socket.tcp6()
|
|
|
|
end
|
2004-05-28 22:40:13 +02:00
|
|
|
if not sock then return nil, err end
|
|
|
|
sock:setoption("reuseaddr", true)
|
|
|
|
local res, err = sock:bind(host, port)
|
|
|
|
if not res then return nil, err end
|
|
|
|
res, err = sock:listen(backlog)
|
|
|
|
if not res then return nil, err end
|
|
|
|
return sock
|
|
|
|
end
|
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
try = newtry()
|
2004-06-18 23:41:44 +02:00
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
function choose(table)
|
2004-05-28 22:40:13 +02:00
|
|
|
return function(name, opt1, opt2)
|
2004-11-28 09:17:16 +01:00
|
|
|
if base.type(name) ~= "string" then
|
2004-05-28 22:40:13 +02:00
|
|
|
name, opt1, opt2 = "default", name, opt1
|
|
|
|
end
|
|
|
|
local f = table[name or "nil"]
|
2004-11-28 09:17:16 +01:00
|
|
|
if not f then base.error("unknown key (".. base.tostring(name) ..")", 3)
|
2004-05-28 22:40:13 +02:00
|
|
|
else return f(opt1, opt2) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Socket sources and sinks, conforming to LTN12
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- create namespaces inside LuaSocket namespace
|
2005-06-17 06:04:55 +02:00
|
|
|
sourcet = {}
|
|
|
|
sinkt = {}
|
2004-05-28 22:40:13 +02:00
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
BLOCKSIZE = 2048
|
2004-05-28 22:40:13 +02:00
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
sinkt["close-when-done"] = function(sock)
|
2004-11-28 09:17:16 +01:00
|
|
|
return base.setmetatable({
|
2004-05-28 22:40:13 +02:00
|
|
|
getfd = function() return sock:getfd() end,
|
|
|
|
dirty = function() return sock:dirty() end
|
2005-11-22 09:33:29 +01:00
|
|
|
}, {
|
2004-05-28 22:40:13 +02:00
|
|
|
__call = function(self, chunk, err)
|
2005-11-22 09:33:29 +01:00
|
|
|
if not chunk then
|
2004-05-28 22:40:13 +02:00
|
|
|
sock:close()
|
|
|
|
return 1
|
|
|
|
else return sock:send(chunk) end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
sinkt["keep-open"] = function(sock)
|
2004-11-28 09:17:16 +01:00
|
|
|
return base.setmetatable({
|
2004-05-28 22:40:13 +02:00
|
|
|
getfd = function() return sock:getfd() end,
|
|
|
|
dirty = function() return sock:dirty() end
|
2005-11-22 09:33:29 +01:00
|
|
|
}, {
|
2004-05-28 22:40:13 +02:00
|
|
|
__call = function(self, chunk, err)
|
2004-07-26 06:03:55 +02:00
|
|
|
if chunk then return sock:send(chunk)
|
|
|
|
else return 1 end
|
2004-05-28 22:40:13 +02:00
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
sinkt["default"] = sinkt["keep-open"]
|
2004-05-28 22:40:13 +02:00
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
sink = choose(sinkt)
|
2004-05-28 22:40:13 +02:00
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
sourcet["by-length"] = function(sock, length)
|
2004-11-28 09:17:16 +01:00
|
|
|
return base.setmetatable({
|
2004-05-28 22:40:13 +02:00
|
|
|
getfd = function() return sock:getfd() end,
|
|
|
|
dirty = function() return sock:dirty() end
|
2005-11-22 09:33:29 +01:00
|
|
|
}, {
|
2004-05-28 22:40:13 +02:00
|
|
|
__call = function()
|
|
|
|
if length <= 0 then return nil end
|
|
|
|
local size = math.min(socket.BLOCKSIZE, length)
|
|
|
|
local chunk, err = sock:receive(size)
|
|
|
|
if err then return nil, err end
|
|
|
|
length = length - string.len(chunk)
|
|
|
|
return chunk
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
sourcet["until-closed"] = function(sock)
|
2004-06-15 08:24:00 +02:00
|
|
|
local done
|
2004-11-28 09:17:16 +01:00
|
|
|
return base.setmetatable({
|
2004-05-28 22:40:13 +02:00
|
|
|
getfd = function() return sock:getfd() end,
|
|
|
|
dirty = function() return sock:dirty() end
|
2005-11-22 09:33:29 +01:00
|
|
|
}, {
|
2004-06-15 08:24:00 +02:00
|
|
|
__call = function()
|
|
|
|
if done then return nil end
|
2004-05-28 22:40:13 +02:00
|
|
|
local chunk, err, partial = sock:receive(socket.BLOCKSIZE)
|
|
|
|
if not err then return chunk
|
2005-11-22 09:33:29 +01:00
|
|
|
elseif err == "closed" then
|
2004-05-28 22:40:13 +02:00
|
|
|
sock:close()
|
2004-06-15 08:24:00 +02:00
|
|
|
done = 1
|
|
|
|
return partial
|
2005-11-22 09:33:29 +01:00
|
|
|
else return nil, err end
|
2004-06-15 08:24:00 +02:00
|
|
|
end
|
2004-05-28 22:40:13 +02:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
sourcet["default"] = sourcet["until-closed"]
|
2004-05-28 22:40:13 +02:00
|
|
|
|
2005-06-17 06:04:55 +02:00
|
|
|
source = choose(sourcet)
|
2004-11-28 09:17:16 +01:00
|
|
|
|