pingx
Introduction
pingx is a lightweight Swift library for determining network latency between a client and server using ICMP (Internet Control Message Protocol) packets. It provides a simple and flexible API for sending and managing ping requests to IPv4 addresses.
Installation
CocoaPods
To integrate pingx into your Xcode project using CocoaPods, add the following line to your Podfile:
pod 'pingx'Then, run the following command:
$ pod installSwift Package Manager (SPM)
To integrate pingx into your Xcode project using Swift Package Manager, add the following dependency to your Package.swift file:
dependencies: [
.package(url: "https://github.com/shineRR/pingx", .upToNextMajor(from: "1.1.0"))
]Usage
IPv4 Address Conversion
import pingx
let converter = IPv4AddressStringConverter()
let destination = try converter.convert(address: "8.8.8.8")Request
The Request class represents a single ping request configuration. It encapsulates all necessary information to perform an ICMP ping to a specified IPv4 address.
import pingx
let destination = IPv4Address(address: (8, 8, 8, 8))
let request = Request(
destination: destination, // Destination
timeoutInterval: .seconds(1), // Timeout interval. Available options for timeout interval: .seconds, .milliseconds, .nanoseconds
demand: .max(5) // Send 5 ping requests (default is 1).
) // Available options for demand:
// - .none: send no requests
// - .max(n): send up to n requests
// - .unlimited: send unlimited requestsAsynchronous Pinging
Ping example:
import pingx
let pinger = AsyncPinger()
let request = Request(destination: destination)
let sequence = pinger.ping(request: request)
for try await result in sequence {
print("Result: \(result)")
}Callback-based Pinging
Ping example:
import pingx
let pinger = Pinger()
let request = Request(destination: destination)
pinger.ping(request: request) { result in
print("Result: \(result)")
}Request cancellation example:
import pingx
let pinger = Pinger()
let request = Request(destination: destination)
pinger.ping(request: request) { result in
print("Result: \(result)")
}
pinger.cancel(requestId: request.id)Example
To run the example project, clone the repo, and run pod install from the Example directory first.
Author
pingx is developed and maintained by shineRR.
License
pingx is available under the MIT license. See the LICENSE file for more info.