Class: DIY::Live

Inherits:
Object
  • Object
show all
Defined in:
lib/diy/live.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_name) ⇒ Live

Returns a new instance of Live.



7
8
9
10
11
12
13
# File 'lib/diy/live.rb', line 7

def initialize(device_name)
  DIY::Logger.info( "Initialize Live: #{device_name}" )
  @live = FFI::PCap::Live.new(:dev=>device_name, :handler => FFI::PCap::CopyHandler, :promisc => true)
  DIY::Logger.info( "Listen on:  #{net} " )
  @running = false
  #~ @live.non_blocking= true
end

Instance Attribute Details

#liveObject (readonly)

~ @live.non_blocking= true



14
15
16
# File 'lib/diy/live.rb', line 14

def live
  @live
end

Instance Method Details

#breakObject



34
35
36
37
# File 'lib/diy/live.rb', line 34

def break
  DIY::Logger.debug "stopping loop recv..."
  @running = false
end

#inject(packet_str) ⇒ Object Also known as: send_packet

发包



17
18
19
# File 'lib/diy/live.rb', line 17

def inject(packet_str)
  @live.send_packet(packet_str)
end

#loop(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/diy/live.rb', line 22

def loop(&block)
  @running = true
  while @running do
    @live.dispatch do |this, pkt|
      next unless pkt
      block.call(this, pkt)
    end
    sleep 0.1
  end
  DIY::Logger.debug "stopped loop recv..."
end

#netObject



39
40
41
# File 'lib/diy/live.rb', line 39

def net
  @live.network + " / " + @live.netmask
end