module Process::Roulette::EnhanceSocket

A module that adds helper methods to socket objects. In particular, it makes it easier to read and write entire packets (where a packet is defined as a 4-byte length field, followed by a variable length body, and the body is a marshalled Ruby object.)

Attributes

killed_at[RW]
username[RW]
victims[RW]

Public Instance Methods

confirmed!(confirm = true) click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 68
def confirmed!(confirm = true)
  @confirmed = confirm
end
confirmed?() click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 64
def confirmed?
  @confirmed
end
dead?() click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 76
def dead?
  Time.now.to_f - @last_ping > 1.0
end
has_victim?() click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 55
def has_victim?
  @current_victim != nil
end
killed?() click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 51
def killed?
  @killed_at != nil
end
ping!() click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 72
def ping!
  @last_ping = Time.now.to_f
end
read_packet() click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 19
def read_packet
  raw = recv(4, 0)
  return nil if raw.empty?

  length = raw.unpack('N').first
  raw = recv(length, 0)
  return nil if raw.empty?

  Marshal.load(raw)
end
send_packet(payload) click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 30
def send_packet(payload)
  body = Marshal.dump(payload)
  len = [body.length].pack('N')
  send(len, 0)
  send(body, 0)
  body.length
end
victim=(v) click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 59
def victim=(v)
  @current_victim = v
  (@victims ||= []).push(v) if v
end
wait_with_ping() click to toggle source
# File lib/process/roulette/enhance_socket.rb, line 38
def wait_with_ping
  loop do
    ready, = IO.select([self], [], [], 0.2)
    return read_packet if ready && ready.any?

    send_packet('PING')
  end
end