class QuartzTorrent::UdpTrackerMessage

Constants

ActionAnnounce
ActionConnect
ActionError
ActionScrape
EventCompleted
EventNone
EventStarted
EventStopped

Public Class Methods

packAsNetworkOrder(num, len) click to toggle source

Pack the number ‘num’ as a network byte order signed integer, ‘len’ bytes long. Negative numbers are written in two’s-compliment notation.

# File lib/quartz_torrent/udptrackermsg.rb, line 15
def self.packAsNetworkOrder(num, len)
  result = ""
  len.times do
    result << (num & 0xff)
    num >>= 8
  end
  result.reverse
end
unpackNetworkOrder(str, len = nil) click to toggle source

Unpack the number stored in ‘str’ assuming it is a network byte order signed integer. Negative numbers are assumed to be in two’s-compliment notation.

# File lib/quartz_torrent/udptrackermsg.rb, line 26
def self.unpackNetworkOrder(str, len = nil)
  result = 0
  first = true
  negative = false
  index = 0
  str.each_byte do |b|
    if first
      negative = (b & 0x80) > 0
      first = false
    end
    result <<= 8
    result += b
    index += 1
    break if len && index == len
  end
  if negative
    # Internally the value is being represented unsigned. To make it signed,
    # we first take the ones compliment of the value, remove the sign bit, and add one.
    # This takes the two's compliment of the two's compliment of the number, which results
    # in the absolute value of the original number. Finally we use the unary - operator to
    # make the value negative.
    result = -(((~result) & 0x7fffffffffffffff) + 1)
  end
  result 
end