stop ping all function?
fukemy opened this issue · 5 comments
hi did you provide way to stop all ping when i found the matched ip?
@fukemy I had a problem where the stop()
method did not seem to stop an ongoing ping thread once it is going (I'm using Swift 5 in Xcode 11.4.1) which I have implemented a work-around for (below).
I'm not sure if this is the same thing you are wondering about, or if you are talking about spawning lots of different ping instances and a single command or method that will kill them all for some reason (like you found the one you were looking for). If that is the case, I'm pretty sure you have to call the stop method on each SwiftyPing instance that you had generated. You could do this in a for-in
loop structure or individually like:
externalPingA?.stop()
externalPingB?.stop()
externalPingC?.stop()
...
I would use the same method to stop the pings as you had used to start them (how you used the start()
method).
If instead you are just having trouble getting the stop()
method to work at all like me, my fix was to modify the internal scheduleNextPing()
function in the SwiftyPing.swift file itself. I had noticed that the .stop()
method resets the isPinging
Bool variable to false
. However, I couldn't find an explicit use of this variable anywhere in the SwiftyPing.swift file itself. Perhaps there was something about the socket functions and all that previously worked in Swift 4 (I'm still learning Swift, but I couldn't figure out how this would be the case), but regardless it does not seem to be working for me in Swift 5 in Xcode 11.4.1.
My quick on-line fix for this, which works for me was to modify the conditional return statement at the beginning of scheduleNextPing()
changing from this:
func scheduleNextPing() {
serial.sync {
if self.hasScheduledNextPing {
return
}
...
to this:
func scheduleNextPing() {
serial.sync {
if self.hasScheduledNextPing || !self.isPinging {
return
}
...
This change prevents the next ping call from being dispatched. I hope this helps.
Thank you for the fix, @pfliegster. Should be corrected in the code.
Finally had the time for a refactoring. In my limited testing, stopping should now work. Let me know if it's still not working.
Hi @samiyr - thanks for the update and the work you've put into this project. The stop works as expected now in the update. Thanks!
I did notice that the API has changed slightly (I know you probably still have to do some updates on the readme), but just noting for others that .stop()
has become .stopPinging()
and similarly .start()
is now .startPinging()
.
Another thing I noticed is that the .errorClosure
had gone away ... well, it's still defined in your code (public typealias ErrorClosure
is), but not accessible anymore from my code. I added the following line, and I can now use the errorClosure again (although I'm still learning how to do this):
public var errorClosure: ErrorClosure?
(I added it just after the public var observer ...
definition in my own local copy)
I'm not sure if that was intentional, pushing us to catch thrown errors instead (I noticed that Xcode now forces use of try-catch in the updated code), but just something I thought I'd point out in case it was just an oversight.
Also, like @blackTay , I too couldn't get the timeout working and still does not seem functional in the updates you made. I'm not sure if this is something in SwiftyPing, or something in the way the CFSocket stuff works. Again, I'm in Xcode 11.4.1, Swift 5.
Thanks again for creating and maintaining this nice function & I appreciate any extra work you are willing to put in on this and things like the timeout.
Thanks for catching that, I forgot the readme. And yes, the error closure didn't actually do anything, so I removed it. All errors are thrown either at the initialization time (for example if a host could not be resolved) or are sent to the observer closure.