Class: GamespyQuery::SocketMaster

Inherits:
Base
  • Object
show all
Defined in:
lib/gamespy_query/socket_master.rb

Constant Summary collapse

FILL_UP_ON_SPACE =
true
DEFAULT_MAX_CONNECTIONS =
128

Constants included from Funcs

Funcs::RX_F, Funcs::RX_I, Funcs::RX_S

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Funcs

#clean, #clean_string, #get_string, #handle_chr, #strip_tags

Constructor Details

#initialize(addrs) ⇒ SocketMaster

Returns a new instance of SocketMaster.



10
11
12
13
14
# File 'lib/gamespy_query/socket_master.rb', line 10

def initialize addrs
  @addrs = addrs

  @timeout, @max_connections = Socket::DEFAULT_TIMEOUT, DEFAULT_MAX_CONNECTIONS # Per select iteration
end

Instance Attribute Details

#max_connectionsObject

Returns the value of attribute max_connections.



8
9
10
# File 'lib/gamespy_query/socket_master.rb', line 8

def max_connections
  @max_connections
end

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/gamespy_query/socket_master.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#process!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gamespy_query/socket_master.rb', line 16

def process!
  sockets = []

  until @addrs.empty?
    addrs = @addrs.shift @max_connections
    queue = addrs.map { |addr| Socket.new(addr) }

    sockets += queue

    until queue.empty?
      # Fill up the Sockets pool until max_conn
      if FILL_UP_ON_SPACE && queue.size < @max_connections
        addrs = @addrs.shift (@max_connections - queue.size)
        socks = addrs.map { |addr| Socket.new(addr) }

        queue += socks
        sockets += socks
      end

      write_sockets, read_sockets = queue.reject {|s| s.valid? }.partition {|s| s.handle_state }

      unless ready = IO.select(read_sockets, write_sockets, nil, @timeout)
        Tools.logger.warn "Timeout, no usable sockets in current queue, within timeout period (#{@timeout}s)"
        queue.each{|s| s.close unless s.closed?}
        queue = []
        next
      end

      Tools.debug {"Sockets: #{queue.size}, AddrsLeft: #{@addrs.size}, ReadReady: #{"#{ready[0].size} / #{read_sockets.size}, WriteReady: #{ready[1].size} / #{write_sockets.size}, ExcReady: #{ready[2].size} / #{queue.size}" unless ready.nil?}"}

      # Read
      ready[0].each { |s| queue.delete(s) unless s.handle_read() }

      # Write
      ready[1].each { |s| queue.delete(s) unless s.handle_write() }

      # Exceptions
      #ready[2].each { |s| queue.delete(s) unless s.handle_exc }
    end
  end

  return sockets
end