local g_debugObj = XLGetObject("Xunlei.LocalNetSpeedTest.Obj")

function CheckProxyServer(tab)
	local hubHelper = XLGetObject("Xunlei.HubHelper.Obj")
	local bUseProxy, strHost = hubHelper:GetXLProxyInfo()
	g_debugObj:LuaDebugOutput("DefaultRule: CheckProxyServer: bUseProxy = "..tostring(bUseProxy).." strHost = "..tostring(strHost))
	
	if not bUseProxy then
		return 0
	end
	
	local nPingTimes = 5
	local nPingFailed = 0
	for i=1, nPingTimes do 
		g_debugObj:LuaDebugOutput("DefaultRule: CheckProxyServer: i = "..tostring(i))
		if not PingOper(strHost) then
			nPingFailed = nPingFailed + 1
		end
	end
	g_debugObj:LuaDebugOutput("DefaultRule: CheckProxyServer: nPingFailed = "..tostring(nPingFailed).." nPingTimes = "..tostring(nPingTimes))
	
	local result = 0
	--若失败率为40%以上
	if nPingFailed > 2 then 
		result = 1
	end
	
	return result
end

function PingOper(strHost)
	g_debugObj:LuaDebugOutput("DefaultRule: CheckProxyServer: PingOper")
	local osshellObj = XLGetObject("Xunlei.OSShell.Obj")
	local id = osshellObj:AsynPing(strHost)
	
	local bSuc = true
	for i=1,5 do	--最长等待5秒
		osshellObj:Sleep(1000)
		local state = osshellObj:GetAsynPingResult(id)
		
  		if state==2 then
  			bSuc = true
			break
  		elseif state==1 then
  			bSuc = false
			break
		else
			--state==0 此时不处理
  		end
	end
	
	return bSuc
end
