diff --git a/lualib/http/httpc.lua b/lualib/http/httpc.lua index bc18dad0..54595648 100644 --- a/lualib/http/httpc.lua +++ b/lualib/http/httpc.lua @@ -2,8 +2,12 @@ local skynet = require "skynet" local socket = require "http.sockethelper" local internal = require "http.internal" local dns = require "skynet.dns" + local string = string local table = table +local pcall = pcall +local error = error +local pairs = pairs local httpc = {} diff --git a/lualib/http/httpd.lua b/lualib/http/httpd.lua index 1575f5a6..fb3fb2e8 100644 --- a/lualib/http/httpd.lua +++ b/lualib/http/httpd.lua @@ -2,6 +2,11 @@ local internal = require "http.internal" local string = string local type = type +local assert = assert +local tonumber = tonumber +local pcall = pcall +local ipairs = ipairs +local pairs = pairs local httpd = {} diff --git a/lualib/http/internal.lua b/lualib/http/internal.lua index 204767be..a34b7df6 100644 --- a/lualib/http/internal.lua +++ b/lualib/http/internal.lua @@ -1,5 +1,11 @@ local table = table local type = type +local string = string +local tonumber = tonumber +local pcall = pcall +local assert = assert +local error = error +local pairs = pairs local M = {} diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index 10b32346..9f0a6d03 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -1,11 +1,26 @@ local socket = require "skynet.socket" local skynet = require "skynet" +local coroutine = coroutine +local error = error +local tostring = tostring + local readbytes = socket.read local writebytes = socket.write local sockethelper = {} -local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end }) +local socket_error = setmetatable({} , { + __tostring = function(self) + local info = self.err_info + self.err_info = nil + return info or "[Socket Error]" + end, + + __call = function (self, info) + self.err_info = "[Socket Error] : " .. tostring(info) + return self + end +}) sockethelper.socket_error = socket_error @@ -27,7 +42,7 @@ local function preread(fd, str) if ret then return str .. ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -36,7 +51,7 @@ local function preread(fd, str) if ret then return ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -51,7 +66,7 @@ function sockethelper.readfunc(fd, pre) if ret then return ret else - error(socket_error) + error(socket_error("read failed fd = " .. fd)) end end end @@ -62,24 +77,27 @@ function sockethelper.writefunc(fd) return function(content) local ok = writebytes(fd, content) if not ok then - error(socket_error) + error(socket_error("write failed fd = " .. fd)) end end end function sockethelper.connect(host, port, timeout) - local fd + local fd, err + local is_time_out = false if timeout then + is_time_out = true local drop_fd local co = coroutine.running() -- asynchronous connect skynet.fork(function() - fd = socket.open(host, port) + fd, err = socket.open(host, port) if drop_fd then -- sockethelper.connect already return, and raise socket_error socket.close(fd) else -- socket.open before sleep, wakeup. + is_time_out = false skynet.wakeup(co) end end) @@ -89,13 +107,14 @@ function sockethelper.connect(host, port, timeout) drop_fd = true end else + is_time_out = false -- block connect fd = socket.open(host, port) end if fd then return fd end - error(socket_error) + error(socket_error("connect failed host = " .. host .. ' port = '.. port .. ' timeout = ' .. timeout .. ' err = ' .. tostring(err) .. ' is_time_out = '.. tostring(is_time_out))) end function sockethelper.close(fd)