Sha256: e3d957d72d91a3a1de71209ec401f7f761e40df8b157d910127df549d281266d

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

module Net
  class Reachability
    def initialize(hostname, &block)
      @internal_callback = Proc.new do |target, flags, pointer|
        block.call(reachable_with_flags?(flags))
      end
      block.weak!
      @reachability = SCNetworkReachabilityCreateWithName(KCFAllocatorDefault,
                                                          hostname.UTF8String)

      start_tracking_network
    end

    def stop
      SCNetworkReachabilityUnscheduleFromRunLoop(@reachability,
                                                 CFRunLoopGetCurrent(),
                                                 KCFRunLoopDefaultMode)
      SCNetworkReachabilitySetCallback(@reachability, nil, @context)
    end

    protected

    def reachable_with_flags?(flags)
      return false unless flags

      if (flags & KSCNetworkReachabilityFlagsReachable) == 0
        return false
      end

      true
    end

    def start_tracking_network
      @context = Pointer.new(SCNetworkReachabilityContext.type)
      if SCNetworkReachabilitySetCallback(@reachability, @internal_callback,
                                          @context)
        if SCNetworkReachabilityScheduleWithRunLoop(@reachability,
                                                    CFRunLoopGetCurrent(),
                                                    KCFRunLoopDefaultMode)
          return true
        end
      end

      false
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
motion-flow-0.1.8 flow/net/cocoa/reachability.rb
motion-flow-0.1.7 flow/net/cocoa/reachability.rb
motion-flow-0.1.6 flow/net/cocoa/reachability.rb
motion-flow-0.1.5 flow/net/cocoa/reachability.rb
motion-flow-0.1.4 flow/net/cocoa/reachability.rb
motion-flow-0.1.3 flow/net/cocoa/reachability.rb
motion-flow-0.1.2 flow/net/cocoa/reachability.rb
motion-flow-0.1.1 flow/net/cocoa/reachability.rb
motion-flow-0.1 flow/net/cocoa/reachability.rb