Module: Process::Roulette::EnhanceSocket

Defined in:
lib/process/roulette/enhance_socket.rb

Overview

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.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#killed_atObject

Returns the value of attribute killed_at.



49
50
51
# File 'lib/process/roulette/enhance_socket.rb', line 49

def killed_at
  @killed_at
end

#usernameObject

Returns the value of attribute username.



47
48
49
# File 'lib/process/roulette/enhance_socket.rb', line 47

def username
  @username
end

#victimsObject

Returns the value of attribute victims.



48
49
50
# File 'lib/process/roulette/enhance_socket.rb', line 48

def victims
  @victims
end

Instance Method Details

#confirmed!(confirm = true) ⇒ Object



68
69
70
# File 'lib/process/roulette/enhance_socket.rb', line 68

def confirmed!(confirm = true)
  @confirmed = confirm
end

#confirmed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/process/roulette/enhance_socket.rb', line 64

def confirmed?
  @confirmed
end

#dead?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/process/roulette/enhance_socket.rb', line 76

def dead?
  Time.now.to_f - @last_ping > 1.0
end

#has_victim?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/process/roulette/enhance_socket.rb', line 55

def has_victim?
  @current_victim != nil
end

#killed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/process/roulette/enhance_socket.rb', line 51

def killed?
  @killed_at != nil
end

#ping!Object



72
73
74
# File 'lib/process/roulette/enhance_socket.rb', line 72

def ping!
  @last_ping = Time.now.to_f
end

#read_packetObject



19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



30
31
32
33
34
35
36
# 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) ⇒ Object



59
60
61
62
# File 'lib/process/roulette/enhance_socket.rb', line 59

def victim=(v)
  @current_victim = v
  (@victims ||= []).push(v) if v
end

#wait_with_pingObject



38
39
40
41
42
43
44
45
# 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