Skip to content
Permalink
Browse files
Almost ready for beta3
  • Loading branch information
Diego Nehab committed Nov 27, 2004
1 parent eb0fc85 commit 7c97e8e
Show file tree
Hide file tree
Showing 26 changed files with 311 additions and 191 deletions.
9 TODO
@@ -1,14 +1,12 @@

use wim's filter.chain or something better
make sure standard libraries are "required" by modules before use.
eliminate globals from namespaces created by module().
ftp.send/recv return bytes transfered?
new scheme to choose family/protocol of object to create
change ltn13 to make sure drawbacks are obvious
- check discussion
make sure errors not thrown by try() are not caught by protect()
use mike's "don't set to blocking before closing unless needed" patch?
take a look at DB's smtp patch (add "extra argument" table)
move wsocket.c:sock_send kludge to buffer.c:sendraw (probably)?

optmize aux_getgroupudata (Mike idea)
make aux_newclass receive upvalues
@@ -25,7 +23,10 @@ testar os options!
- proteger ou atomizar o conjunto (timedout, receive), (timedout, send)
- inet_ntoa tamb�m � uma merda.

*use wim's filter.chain or something better
*close wasn't returning 1
*make sure errors not thrown by try() are not caught by protect()
*move wsocket.c:sock_send kludge to buffer.c:sendraw?
*bug on UDP sendto.
*fix PROXY in http.lua
*use new distribution scheme
*create the getstats method.
@@ -163,7 +163,7 @@ <h2 id=ftp>FTP</h2>
local url = require("url")

-- a function that returns a directory listing
function ls(u)
function nlst(u)
local t = {}
local p = url.parse(u)
p.command = "nlst"
@@ -8,11 +8,14 @@
-----------------------------------------------------------------------------
-- Load required modules
-----------------------------------------------------------------------------
local base = require("base")
local string = require("string")
local table = require("table")
local socket = require("socket")
local url = require("socket.url")
local tp = require("socket.tp")

module("socket.dict")
local dict = module("socket.dict")

-----------------------------------------------------------------------------
-- Globals
@@ -28,7 +31,7 @@ local metat = { __index = {} }

function open(host, port)
local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT))
return setmetatable({tp = tp}, metat)
return base.setmetatable({tp = tp}, metat)
end

function metat.__index:greet()
@@ -37,7 +40,8 @@ end

function metat.__index:check(ok)
local code, status = socket.try(self.tp:check(ok))
return code, tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
return code,
base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
end

function metat.__index:getdef()
@@ -116,7 +120,7 @@ local function parse(u)
if cmd == "m" then
arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end)
end
string.gsub(arg, ":([^:]*)$", function(f) t.n = tonumber(f) end)
string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end)
return t
end

@@ -143,6 +147,8 @@ local function sget(u)
end

get = socket.protect(function(gett)
if type(gett) == "string" then return sget(gett)
if base.type(gett) == "string" then return sget(gett)
else return tget(gett) end
end)

base.setmetatable(dict, nil)
@@ -9,9 +9,12 @@
if you have any questions: RFC 1179
]]
-- make sure LuaSocket is loaded
local io = require("io")
local base = require("base")
local string = require("string")
local socket = require("socket")
local ltn12 = require("ltn12")
local test = socket.try
local lp = module("socket.lp")

-- default port
PORT = 515
@@ -28,7 +31,7 @@ local function connect(localhost, option)
local localport = 721
local done, err
repeat
skt = test(socket.tcp())
skt = socket.try(socket.tcp())
try(skt:settimeout(30))
done, err = skt:bind(localhost, localport)
if not done then
@@ -37,8 +40,8 @@ local function connect(localhost, option)
skt = nil
else break end
until localport > 731
test(skt, err)
else skt = test(socket.tcp()) end
socket.try(skt, err)
else skt = socket.try(socket.tcp()) end
try(skt:connect(host, port))
return { skt = skt, try = try }
end
@@ -241,9 +244,9 @@ local format_codes = {
-- lp.send

send = socket.protect(function(file, option)
test(file, "invalid file name")
test(option and type(option) == "table", "invalid options")
local fh = test(io.open(file,"rb"))
socket.try(file, "invalid file name")
socket.try(option and base.type(option) == "table", "invalid options")
local fh = socket.try(io.open(file,"rb"))
local datafile_size = fh:seek("end") -- get total size
fh:seek("set") -- go back to start of file
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
@@ -270,11 +273,11 @@ send = socket.protect(function(file, option)
lpfile,
ctlfn); -- mandatory part of ctl file
if (option.banner) then cfile = cfile .. 'L'..user..'\10' end
if (option.indent) then cfile = cfile .. 'I'..tonumber(option.indent)..'\10' end
if (option.indent) then cfile = cfile .. 'I'..base.tonumber(option.indent)..'\10' end
if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end
if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end
if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then
cfile = cfile .. 'W'..tonumber(option,width)..'\10'
cfile = cfile .. 'W'..base.tonumber(option,width)..'\10'
end

con.skt:settimeout(option.timeout or 65)
@@ -314,3 +317,5 @@ query = socket.protect(function(p)
con.skt:close()
return data
end)

base.setmetatable(lp, nil)
@@ -8,11 +8,14 @@
-----------------------------------------------------------------------------
-- Load required files
-----------------------------------------------------------------------------
local base = require("base")
local table = require("table")
local math = require("math")
local string = require("string")
local socket = require("socket")
local ltn12 = require("ltn12")
local url = require("socket.url")

module("socket.tftp")
local tftp = module("socket.tftp")

-----------------------------------------------------------------------------
-- Program constants
@@ -73,16 +76,18 @@ end
local function tget(gett)
local retries, dgram, sent, datahost, dataport, code
local last = 0
socket.try(gett.host, "missing host")
local con = socket.try(socket.udp())
local try = socket.newtry(function() con:close() end)
-- convert from name to ip if needed
gett.host = try(socket.dns.toip(gett.host))
con:settimeout(1)
-- first packet gives data host/port to be used for data transfers
local path = string.gsub(gett.path or "", "^/", "")
path = url.unescape(path)
retries = 0
repeat
sent = try(con:sendto(RRQ(gett.path, "octet"),
gett.host, gett.port))
sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port))
dgram, datahost, dataport = con:receivefrom()
retries = retries + 1
until dgram or datahost ~= "timeout" or retries > 5
@@ -144,6 +149,8 @@ local function sget(u)
end

get = socket.protect(function(gett)
if type(gett) == "string" then return sget(gett)
if base.type(gett) == "string" then return sget(gett)
else return tget(gett) end
end)

base.setmetatable(tftp, nil)
@@ -12,17 +12,18 @@
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
OutputDirectory=".\"
IntermediateDirectory=".\"
ConfigurationType="2"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;LUASOCKET_EXPORTS"
AdditionalIncludeDirectories="h:\include"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;LUASOCKET_EXPORTS;LUASOCKET_API=__declspec(dllexport)"
MinimalRebuild="TRUE"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
RuntimeLibrary="5"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
@@ -31,6 +32,7 @@
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="ws2_32.lib"
OutputFile="$(OutDir)/lsocket.dll"
LinkIncremental="2"
GenerateDebugInformation="TRUE"
@@ -69,7 +71,7 @@
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../../include"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;LUASOCKET_EXPORTS;LUASOCKET_API=__declspec(dllexport); LUASOCKET_DEBUG"
RuntimeLibrary="0"
RuntimeLibrary="4"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
@@ -209,10 +211,10 @@
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}">
</Filter>
<File
RelativePath="..\..\lib\lua.lib">
RelativePath="..\..\lib\liblua.lib">
</File>
<File
RelativePath="..\..\lib\lualib.lib">
RelativePath="..\..\lib\liblualib.lib">
</File>
</Files>
<Globals>
@@ -12,17 +12,18 @@
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
OutputDirectory=".\"
IntermediateDirectory=".\"
ConfigurationType="2"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;MIME_EXPORTS"
AdditionalIncludeDirectories="h:\include"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;MIME_EXPORTS;MIME_API=__declspec(dllexport)"
MinimalRebuild="TRUE"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
RuntimeLibrary="5"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
@@ -69,7 +70,7 @@
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../../include"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;MIME_EXPORTS; MIME_API=__declspec(dllexport)"
RuntimeLibrary="0"
RuntimeLibrary="4"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
@@ -136,10 +137,10 @@
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}">
</Filter>
<File
RelativePath="..\..\lib\lua.lib">
RelativePath="..\..\lib\liblua.lib">
</File>
<File
RelativePath="..\..\lib\lualib.lib">
RelativePath="..\..\lib\liblualib.lib">
</File>
</Files>
<Globals>
@@ -158,14 +158,16 @@ int buf_isempty(p_buf buf) {
/*-------------------------------------------------------------------------*\
* Sends a block of data (unbuffered)
\*-------------------------------------------------------------------------*/
#define STEPSIZE 8192

This comment has been minimized.

Copy link
@vschiavoni

vschiavoni Jan 29, 2013

why exactly this number?

static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) {
p_io io = buf->io;
p_tm tm = buf->tm;
size_t total = 0;
int err = IO_DONE;
while (total < count && err == IO_DONE) {
size_t done;
err = io->send(io->ctx, data+total, count-total, &done, tm);
size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE;
err = io->send(io->ctx, data+total, step, &done, tm);
total += done;
}
*sent = total;
@@ -29,11 +29,21 @@ static luaL_reg func[] = {
/*-------------------------------------------------------------------------*\
* Try factory
\*-------------------------------------------------------------------------*/
static void wrap(lua_State *L) {
lua_newtable(L);
lua_pushnumber(L, 1);
lua_pushvalue(L, -3);
lua_settable(L, -3);
lua_insert(L, -2);
lua_pop(L, 1);
}

static int finalize(lua_State *L) {
if (!lua_toboolean(L, 1)) {
lua_pushvalue(L, lua_upvalueindex(1));
lua_pcall(L, 0, 0, 0);
lua_settop(L, 2);
wrap(L);
lua_error(L);
return 0;
} else return lua_gettop(L);
@@ -54,13 +64,23 @@ static int global_newtry(lua_State *L) {
/*-------------------------------------------------------------------------*\
* Protect factory
\*-------------------------------------------------------------------------*/
static int unwrap(lua_State *L) {
if (lua_istable(L, -1)) {
lua_pushnumber(L, 1);
lua_gettable(L, -2);
lua_pushnil(L);
lua_insert(L, -2);
return 1;
} else return 0;
}

static int protected_(lua_State *L) {
lua_pushvalue(L, lua_upvalueindex(1));
lua_insert(L, 1);
if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) {
lua_pushnil(L);
lua_insert(L, 1);
return 2;
if (unwrap(L)) return 2;
else lua_error(L);
return 0;
} else return lua_gettop(L);
}

0 comments on commit 7c97e8e

Please sign in to comment.