Sha256: 236962cdbe85153ac26b7cae1ca77c2589c56bfc9fff348d1202198e0e56729f

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

require 'bson'
require 'socket'

module RubySkynet
  module Common

    # Returns a BSON document read from the socket.
    # Returns nil if the operation times out or if a network
    #         connection failure occurs
    def self.read_bson_document(socket)
      bytebuf = BSON::ByteBuffer.new
      # Read 4 byte size of following BSON document
      bytes = socket.read(4)

      # No more data
      return unless bytes

      # Read BSON document
      sz = bytes.unpack("V")[0]
      raise "Invalid Data received from server:#{bytes.inspect}" unless sz

      bytebuf.append!(bytes)
      bytebuf.append!(socket.read(sz - 4))
      raise "Socket is not returning #{sz} requested bytes. #{bytebuf.length} bytes returned" unless sz == bytebuf.length
      return BSON.deserialize(bytebuf)
    end

    # Returns the local ip address being used by this machine to talk to the
    # internet. By default connects to Google and determines what IP Address is used locally
    def self.local_ip_address(remote_ip = 'google.com')
      @@local_ip_address ||= ::UDPSocket.open {|s| s.connect(remote_ip, 1); s.addr.last }
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ruby_skynet-1.3.0.alpha3 lib/ruby_skynet/common.rb
ruby_skynet-1.3.0.alpha2 lib/ruby_skynet/common.rb
ruby_skynet-1.3.0.alpha1 lib/ruby_skynet/common.rb
ruby_skynet-1.2.7 lib/ruby_skynet/common.rb
ruby_skynet-1.2.6 lib/ruby_skynet/common.rb
ruby_skynet-1.2.5 lib/ruby_skynet/common.rb
ruby_skynet-1.2.4 lib/ruby_skynet/common.rb
ruby_skynet-1.2.3 lib/ruby_skynet/common.rb
ruby_skynet-1.2.2 lib/ruby_skynet/common.rb
ruby_skynet-1.2.1 lib/ruby_skynet/common.rb
ruby_skynet-1.2.0 lib/ruby_skynet/common.rb
ruby_skynet-1.1.1 lib/ruby_skynet/common.rb