mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
fix: httpc支持ipv6 (#2125)
* fix: httpc支持ipv6 * httpc ip:port解析放在c层 * lua-socket.c listen增加ip:port字符串解析, 相关cluster代码修改 #2124 * socketchannel changehost port可置空 * socketchannel changehost恢复port判空 * httpc解析域名加端口号逻辑优化 * 注释中字符串模式内容格式优化 --------- Co-authored-by: zhuyin.zhu <zhuyin.zhu@bytedance.com>
This commit is contained in:
@@ -68,22 +68,29 @@ end
|
||||
local function connect(host, timeout)
|
||||
local protocol
|
||||
protocol, host = check_protocol(host)
|
||||
local hostaddr, port = host:match"([^:]+):?(%d*)$"
|
||||
if port == "" then
|
||||
|
||||
local hostname, port
|
||||
if async_dns then
|
||||
-- hostname string (ends with ":?%d*") must begin with a substring that doesn't contain colon "[^:]"
|
||||
-- and end with a character that is not a colon or a digit "[^%d%]:]".
|
||||
-- hostname not end with ".", pattern "%." can avoid splitting "127.0.0.1" into "127.0.0." and "1"
|
||||
hostname, port = host:match "^([^:]-[^%d%]:%.]):?(%d*)$"
|
||||
if hostname then
|
||||
local msg
|
||||
host, msg = dns.resolve(hostname)
|
||||
if not host then
|
||||
error(string.format("%s dns resolve failed msg:%s", hostname, msg))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if port == "" or (port == nil and not host:find ":%d+$") then
|
||||
port = protocol=="http" and 80 or protocol=="https" and 443
|
||||
else
|
||||
port = tonumber(port)
|
||||
end
|
||||
local hostname
|
||||
if not hostaddr:match(".*%d+$") then
|
||||
hostname = hostaddr
|
||||
if async_dns then
|
||||
hostaddr = dns.resolve(hostname)
|
||||
end
|
||||
end
|
||||
local fd = socket.connect(hostaddr, port, timeout)
|
||||
|
||||
local fd = socket.connect(host, port, timeout)
|
||||
if not fd then
|
||||
error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, hostaddr, port, timeout))
|
||||
error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, host, port, timeout))
|
||||
end
|
||||
-- print("protocol hostname port", protocol, hostname, port)
|
||||
local interface = gen_interface(protocol, fd, hostname)
|
||||
|
||||
@@ -407,11 +407,6 @@ function socket.disconnected(id)
|
||||
end
|
||||
|
||||
function socket.listen(host, port, backlog)
|
||||
if port == nil then
|
||||
host, port = string.match(host, "(.+):([^:]+)$")
|
||||
host = host:match("^%[(.-)%]$") or host
|
||||
port = tonumber(port)
|
||||
end
|
||||
local id = driver.listen(host, port, backlog)
|
||||
local s = {
|
||||
id = id,
|
||||
|
||||
@@ -26,7 +26,7 @@ socket_channel.error = socket_error
|
||||
function socket_channel.channel(desc)
|
||||
local c = {
|
||||
__host = assert(desc.host),
|
||||
__port = assert(desc.port),
|
||||
__port = desc.port,
|
||||
__backup = desc.backup,
|
||||
__auth = desc.auth,
|
||||
__response = desc.response, -- It's for session mode
|
||||
@@ -328,7 +328,7 @@ local function connect_once(self)
|
||||
self.__overload = true
|
||||
overload(true)
|
||||
else
|
||||
skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d %s:%s)", size, id, self.__host, self.__port))
|
||||
skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id), self.__host, self.__port)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -464,7 +464,7 @@ local function block_connect(self, once)
|
||||
|
||||
r = check_connection(self)
|
||||
if r == nil then
|
||||
skynet.error(string.format("Connect to %s:%d failed (%s)", self.__host, self.__port, err))
|
||||
skynet.error("Connect failed", err, self.__host, self.__port)
|
||||
error(socket_error)
|
||||
else
|
||||
return r
|
||||
@@ -553,9 +553,9 @@ end
|
||||
|
||||
function channel:changehost(host, port)
|
||||
self.__host = host
|
||||
if port then
|
||||
if port then
|
||||
self.__port = port
|
||||
end
|
||||
end
|
||||
if not self.__closed then
|
||||
close_channel_socket(self)
|
||||
end
|
||||
|
||||
@@ -39,10 +39,10 @@ function gateserver.start(handler)
|
||||
function CMD.open( source, conf )
|
||||
assert(not socket)
|
||||
local address = conf.address or "0.0.0.0"
|
||||
local port = assert(conf.port)
|
||||
local port = conf.port
|
||||
maxclient = conf.maxclient or 1024
|
||||
nodelay = conf.nodelay
|
||||
skynet.error(string.format("Listen on %s:%d", address, port))
|
||||
skynet.error("Listen on", address, port)
|
||||
socket = socketdriver.listen(address, port, conf.backlog)
|
||||
listen_context.co = coroutine.running()
|
||||
listen_context.fd = socket
|
||||
|
||||
Reference in New Issue
Block a user