mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
@@ -2,8 +2,12 @@ local skynet = require "skynet"
|
|||||||
local socket = require "http.sockethelper"
|
local socket = require "http.sockethelper"
|
||||||
local internal = require "http.internal"
|
local internal = require "http.internal"
|
||||||
local dns = require "skynet.dns"
|
local dns = require "skynet.dns"
|
||||||
|
|
||||||
local string = string
|
local string = string
|
||||||
local table = table
|
local table = table
|
||||||
|
local pcall = pcall
|
||||||
|
local error = error
|
||||||
|
local pairs = pairs
|
||||||
|
|
||||||
local httpc = {}
|
local httpc = {}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ local internal = require "http.internal"
|
|||||||
|
|
||||||
local string = string
|
local string = string
|
||||||
local type = type
|
local type = type
|
||||||
|
local assert = assert
|
||||||
|
local tonumber = tonumber
|
||||||
|
local pcall = pcall
|
||||||
|
local ipairs = ipairs
|
||||||
|
local pairs = pairs
|
||||||
|
|
||||||
local httpd = {}
|
local httpd = {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
local table = table
|
local table = table
|
||||||
local type = type
|
local type = type
|
||||||
|
local string = string
|
||||||
|
local tonumber = tonumber
|
||||||
|
local pcall = pcall
|
||||||
|
local assert = assert
|
||||||
|
local error = error
|
||||||
|
local pairs = pairs
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,26 @@
|
|||||||
local socket = require "skynet.socket"
|
local socket = require "skynet.socket"
|
||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local coroutine = coroutine
|
||||||
|
local error = error
|
||||||
|
local tostring = tostring
|
||||||
|
|
||||||
local readbytes = socket.read
|
local readbytes = socket.read
|
||||||
local writebytes = socket.write
|
local writebytes = socket.write
|
||||||
|
|
||||||
local sockethelper = {}
|
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
|
sockethelper.socket_error = socket_error
|
||||||
|
|
||||||
@@ -27,7 +42,7 @@ local function preread(fd, str)
|
|||||||
if ret then
|
if ret then
|
||||||
return str .. ret
|
return str .. ret
|
||||||
else
|
else
|
||||||
error(socket_error)
|
error(socket_error("read failed fd = " .. fd))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -36,7 +51,7 @@ local function preread(fd, str)
|
|||||||
if ret then
|
if ret then
|
||||||
return ret
|
return ret
|
||||||
else
|
else
|
||||||
error(socket_error)
|
error(socket_error("read failed fd = " .. fd))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -51,7 +66,7 @@ function sockethelper.readfunc(fd, pre)
|
|||||||
if ret then
|
if ret then
|
||||||
return ret
|
return ret
|
||||||
else
|
else
|
||||||
error(socket_error)
|
error(socket_error("read failed fd = " .. fd))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -62,24 +77,27 @@ function sockethelper.writefunc(fd)
|
|||||||
return function(content)
|
return function(content)
|
||||||
local ok = writebytes(fd, content)
|
local ok = writebytes(fd, content)
|
||||||
if not ok then
|
if not ok then
|
||||||
error(socket_error)
|
error(socket_error("write failed fd = " .. fd))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function sockethelper.connect(host, port, timeout)
|
function sockethelper.connect(host, port, timeout)
|
||||||
local fd
|
local fd, err
|
||||||
|
local is_time_out = false
|
||||||
if timeout then
|
if timeout then
|
||||||
|
is_time_out = true
|
||||||
local drop_fd
|
local drop_fd
|
||||||
local co = coroutine.running()
|
local co = coroutine.running()
|
||||||
-- asynchronous connect
|
-- asynchronous connect
|
||||||
skynet.fork(function()
|
skynet.fork(function()
|
||||||
fd = socket.open(host, port)
|
fd, err = socket.open(host, port)
|
||||||
if drop_fd then
|
if drop_fd then
|
||||||
-- sockethelper.connect already return, and raise socket_error
|
-- sockethelper.connect already return, and raise socket_error
|
||||||
socket.close(fd)
|
socket.close(fd)
|
||||||
else
|
else
|
||||||
-- socket.open before sleep, wakeup.
|
-- socket.open before sleep, wakeup.
|
||||||
|
is_time_out = false
|
||||||
skynet.wakeup(co)
|
skynet.wakeup(co)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@@ -89,13 +107,14 @@ function sockethelper.connect(host, port, timeout)
|
|||||||
drop_fd = true
|
drop_fd = true
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
is_time_out = false
|
||||||
-- block connect
|
-- block connect
|
||||||
fd = socket.open(host, port)
|
fd = socket.open(host, port)
|
||||||
end
|
end
|
||||||
if fd then
|
if fd then
|
||||||
return fd
|
return fd
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function sockethelper.close(fd)
|
function sockethelper.close(fd)
|
||||||
|
|||||||
Reference in New Issue
Block a user