diff options
| author | ruki <[email protected]> | 2025-06-26 12:59:16 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-26 12:59:16 +0800 |
| commit | e2c60ab5bc58d4e0a769591b1ad58285d638ccca (patch) | |
| tree | 229dfd0e3a542294779ba305d4b0674c0af0ae13 | |
| parent | 2fd95e31bfb024029680bdac00298b04badd4578 (diff) | |
| parent | d3ae88f3054df26b2bc5cc7f308d59923405aa90 (diff) | |
Merge pull request #6585 from xmake-io/ping
improve ping #6579
| -rw-r--r-- | xmake/modules/net/ping.lua | 59 |
1 files changed, 55 insertions, 4 deletions
diff --git a/xmake/modules/net/ping.lua b/xmake/modules/net/ping.lua index 689bf4d94..652e63782 100644 --- a/xmake/modules/net/ping.lua +++ b/xmake/modules/net/ping.lua @@ -23,8 +23,8 @@ import("core.cache.detectcache") import("lib.detect.find_tool") import("async.runjobs") --- ping host -function _ping(ping, host) +-- using ping to ping host +function _ping_via_ping(ping, host) local data = nil if is_host("windows") then data = try { function () return os.iorun("%s -n 1 -w 1000 %s", ping.program, host) end } @@ -47,6 +47,57 @@ function _ping(ping, host) return timeval end +-- using curl to ping host +function _ping_via_curl(curl, host) + local data, dt = try { function () + local t = os.mclock() + local tmpfile = os.tmpfile() + local outdata = os.iorunv(curl.program, {"-o", tmpfile, "-s", "-w", "%{time_total}", "--max-time", "1", host}) + t = os.mclock() - t + os.tryrm(tmpfile) + return outdata, t + end } + local timeval = 65535 + if data then + local t = tonumber(data:trim()) + if t then + timeval = t * 1000 + else + timeval = dt + end + end + return timeval +end + +-- using wget to ping host +function _ping_via_wget(wget, host) + local data = try { function () + local t = os.mclock() + os.runv(wget.program, {"-O", os.nuldev(), "--timeout=1", host}) + t = os.mclock() - t + return t + end } + local timeval = 65535 + if data then + timeval = data + end + return timeval +end + +-- ping host +-- @see https://github.com/xmake-io/xmake/issues/6579 +function _ping(ping, host) + local routers = { + ping = _ping_via_ping, + curl = _ping_via_curl, + wget = _ping_via_wget + } + local router = routers[ping.name] or _ping_via_ping + if router then + return router(ping, host) + end +end + -- send ping to hosts -- -- @param hosts the hosts @@ -55,9 +106,9 @@ end -- @return the time or -1 -- function main(hosts, opt) - opt = opt or {} - local ping = find_tool("ping", opt) + + local ping = find_tool("curl", opt) or find_tool("wget") or find_tool("ping", opt) if not ping then return {} end |
