Class: UDPBucket::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/udpbucket/core.rb

Instance Method Summary collapse

Constructor Details

#initialize(usercfg = {}) ⇒ Core

Returns a new instance of Core.



5
6
7
8
9
10
11
12
13
# File 'lib/udpbucket/core.rb', line 5

def initialize usercfg={}
  cfgs = Asetus.new :name=>'udpbucket', :load=>false
  cfgs.default.debug      = usercfg[:debug] || 0
  cfgs.default.localhost  = usercfg[:localhost] || '0.0.0.0'
  cfgs.default.localport  = usercfg[:localport] || 16265
  cfgs.load
  @cfg = cfgs.cfg
  load
end

Instance Method Details

#debug(output) ⇒ Object



49
50
51
# File 'lib/udpbucket/core.rb', line 49

def debug output
  Log.debug output if @cfg.debug == 1
end

#listenObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/udpbucket/core.rb', line 26

def listen
  queue = Thread.new do
    while true do
      r, w, e = IO.select([@sock], nil, nil)
      if r[0]
        buffer, sockaddr = @sock.recvfrom_nonblock(1500)
        pkt = { :client_sock => @sock, :client_ip => sockaddr[3], :client_port => sockaddr[1], :payload => buffer }
 @rx_queue << pkt
        debug "packet queued (queue size:#{@rx_queue.size}): #{pkt}" if @cfg.debug == 1
      end
    end
  end
  consumer = Thread.new do
    while true do
      begin
        yield @rx_queue.pop
        debug "yield to block (queue size:#{@rx_queue.size})" if @cfg.debug == 1
      end
    end
  end
  consumer.join
end

#loadObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/udpbucket/core.rb', line 15

def load
  @rx_queue         = Queue.new
  if IPAddr.new(@cfg.localhost).ipv6?
    @sock           = UDPSocket.new Socket::AF_INET6
  else
    @sock           = UDPSocket.new
  end
  @sock.bind @cfg.localhost, @cfg.localport
  debug "udpbucket server up: #{@cfg.localhost}:#{@cfg.localport}" if @cfg.debug == 1
end