Bug fix& delete pingtest

This commit is contained in:
qinyuhang
2017-02-02 22:52:35 +08:00
parent 3176109d15
commit f85cc98563
3 changed files with 1 additions and 171 deletions
@@ -12,7 +12,6 @@
083BF8371D82742200831C68 /* PingClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083BF8351D82742200831C68 /* PingClient.swift */; };
083BF8391D82759600831C68 /* StatusItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083BF8381D82759600831C68 /* StatusItemView.swift */; };
083BF83B1D8275A800831C68 /* SystemThemeChangeHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083BF83A1D8275A800831C68 /* SystemThemeChangeHelper.swift */; };
08805F191D878CA5009B53E7 /* PingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08805F181D878CA5009B53E7 /* PingTest.swift */; };
0880CE401D6FE6D900BD39E2 /* example-gui-config.json in Resources */ = {isa = PBXBuildFile; fileRef = 0880CE3F1D6FE6D900BD39E2 /* example-gui-config.json */; };
088EC3971D5F5B8600E40791 /* whitelist.pac in Resources */ = {isa = PBXBuildFile; fileRef = 088EC3961D5F5B8600E40791 /* whitelist.pac */; };
088EC3991D5F5BA300E40791 /* whiteiplist.pac in Resources */ = {isa = PBXBuildFile; fileRef = 088EC3981D5F5BA300E40791 /* whiteiplist.pac */; };
@@ -111,7 +110,6 @@
083BF8381D82759600831C68 /* StatusItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusItemView.swift; sourceTree = "<group>"; };
083BF83A1D8275A800831C68 /* SystemThemeChangeHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SystemThemeChangeHelper.swift; sourceTree = "<group>"; };
085641E91D7188C400116B27 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = "<group>"; };
08805F181D878CA5009B53E7 /* PingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PingTest.swift; sourceTree = "<group>"; };
0880CE3F1D6FE6D900BD39E2 /* example-gui-config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "example-gui-config.json"; sourceTree = "<group>"; };
088EC3961D5F5B8600E40791 /* whitelist.pac */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = whitelist.pac; sourceTree = "<group>"; };
088EC3981D5F5BA300E40791 /* whiteiplist.pac */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = whiteiplist.pac; sourceTree = "<group>"; };
@@ -335,7 +333,6 @@
9B3FFF201D08826E0019A709 /* PACUtils.swift */,
083BF8341D82742200831C68 /* NetWorkMonitor.swift */,
083BF8351D82742200831C68 /* PingClient.swift */,
08805F181D878CA5009B53E7 /* PingTest.swift */,
9B3FFF131D0705810019A709 /* Notifications.swift */,
9B3FFF1D1D0732660019A709 /* Utils.m */,
9B3FFF1F1D0734060019A709 /* Utils.h */,
@@ -692,7 +689,6 @@
9B3FFF171D072FDE0019A709 /* LaunchAtLoginController.m in Sources */,
9B3FFF4F1D09D9D50019A709 /* ProxyConfHelper.m in Sources */,
9BB706A71D1B982300551F0E /* SWBApplication.m in Sources */,
08805F191D878CA5009B53E7 /* PingTest.swift in Sources */,
9B3FFF1E1D0732660019A709 /* Utils.m in Sources */,
9B3FFF321D08CEE40019A709 /* SWBQRCodeWindowController.m in Sources */,
08BDBB481E2361BD00C4BBC2 /* VersionChecker.swift in Sources */,
-166
View File
@@ -1,166 +0,0 @@
//
// PingTest.swift
// ShadowsocksX-NG
//
// Created by 秦宇航 on 16/9/12.
// Copyright © 2016年 qiuyuzhou. All rights reserved.
//
import Foundation
class PingTest: NSObject, SimplePingDelegate {
static let instance = PingTest(hostName: "")
//then 写一个init函数,传入hostName来init
var hostName = "jp10.hexiehao.pw"
var pinger: SimplePing?
var sendTimer: Timer?
var dateReference: Date?
/// Called by the table view selection delegate callback to start the ping.
init(hostName:String) {
self.hostName = hostName
}
func start() {
NSLog("start ping %@",self.hostName)
let pinger = SimplePing(hostName: self.hostName)
self.pinger = pinger
pinger.delegate = self
pinger.start()
}
/// Called by the table view selection delegate callback to stop the ping.
func stop() {
NSLog("stop")
self.pinger?.stop()
self.pinger = nil
self.sendTimer?.invalidate()
self.sendTimer = nil
}
/// Sends a ping.
///
/// Called to send a ping, both directly (as soon as the SimplePing object starts up) and
/// via a timer (to continue sending pings periodically).
func sendPing() {
self.pinger!.send(with: nil)
}
// MARK: pinger delegate callback
func simplePing(_ pinger: SimplePing, didStartWithAddress address: Data) {
NSLog("pinging %@",hostName)
// Send the first ping straight away.
self.sendPing()
// And start a timer to send the subsequent pings.
assert(self.sendTimer == nil)
self.sendTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(PingTest.sendPing), userInfo: nil, repeats: true)
}
func simplePing(_ pinger: SimplePing, didFailWithError error: Error) {
NSLog("failed: %@", PingTest.shortErrorFromError(error as NSError))
self.stop()
}
func simplePing(_ pinger: SimplePing, didSendPacket packet: Data, sequenceNumber: UInt16) {
NSLog("#%u sent", sequenceNumber)
dateReference = Date()
}
func simplePing(_ pinger: SimplePing, didFailToSendPacket packet: Data, sequenceNumber: UInt16, error: Error) {
NSLog("#%u send failed: %@", sequenceNumber, PingTest.shortErrorFromError(error as NSError))
}
func simplePing(_ pinger: SimplePing, didReceivePingResponsePacket packet: Data, sequenceNumber: UInt16) {
pinger.stop()
guard let dateReference = dateReference else { return }
//timeIntervalSinceDate returns seconds, so we convert to milis
DispatchQueue.main.async(execute: {
let latency = Date().timeIntervalSince(dateReference) * 1000
NSLog("#%u received, host=%@, size=%zu, latency=%.f", sequenceNumber, self.hostName , packet.count,latency)
let userNote = NSUserNotification()
userNote.title = "\(self.hostName) ping \(latency)".localized
userNote.subtitle = "Address can't not be recognized".localized
NSUserNotificationCenter.default
.deliver(userNote)
})
}
func simplePing(_ pinger: SimplePing, didReceiveUnexpectedPacket packet: Data) {
NSLog("unexpected packet, size=%zu", packet.count)
}
// MARK: utilities
/// Returns the string representation of the supplied address.
///
/// - parameter address: Contains a `(struct sockaddr)` with the address to render.
///
/// - returns: A string representation of that address.
static func displayAddressForAddress(_ address: Data) -> String {
var hostStr = [Int8](repeating: 0, count: Int(NI_MAXHOST))
let success = getnameinfo(
(address as NSData).bytes.bindMemory(to: sockaddr.self, capacity: address.count),
socklen_t(address.count),
&hostStr,
socklen_t(hostStr.count),
nil,
0,
NI_NUMERICHOST
) == 0
let result: String
if success {
result = String(cString: hostStr)
} else {
result = "?"
}
return result
}
/// Returns a short error string for the supplied error.
///
/// - parameter error: The error to render.
///
/// - returns: A short string representing that error.
static func shortErrorFromError(_ error: NSError) -> String {
if error.domain == kCFErrorDomainCFNetwork as String && error.code == Int(CFNetworkErrors.cfHostErrorUnknown.rawValue) {
if let failureObj = error.userInfo[kCFGetAddrInfoFailureKey as AnyHashable] {
if let failureNum = failureObj as? NSNumber {
if failureNum.int32Value != 0 {
let f = gai_strerror(failureNum.int32Value)
if f != nil {
return String(cString: f!)
}
}
}
}
}
if let result = error.localizedFailureReason {
return result
}
return error.localizedDescription
}
class func pingArray(){//hostList : [String]) -> [[String : String]]{
// return [["apple.com":"20"],["baidu.com":"20"]]
}
}
@@ -194,7 +194,7 @@ class PreferencesWindowController: NSWindowController
}
func updateProfileBoxVisible() {
if profileMgr.profiles.count <= 1 {
if profileMgr.profiles.count <= 0 {
removeButton.isEnabled = false
}else{
removeButton.isEnabled = true