Class: DIY::Live
- Inherits:
-
Object
- Object
- DIY::Live
- Defined in:
- lib/diy/live.rb
Instance Attribute Summary collapse
-
#live ⇒ Object
readonly
Returns the value of attribute live.
Instance Method Summary collapse
- #break ⇒ Object
-
#initialize(device_name, args = {}) ⇒ Live
constructor
A new instance of Live.
-
#inject(packet_str) ⇒ Object
(also: #send_packet)
发包.
- #loop(&block) ⇒ Object
- #merge_arguments(default, new) ⇒ Object
- #net ⇒ Object
- #set_filter(reg) ⇒ Object
Constructor Details
#initialize(device_name, args = {}) ⇒ Live
Returns a new instance of Live.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/diy/live.rb', line 7 def initialize(device_name, args = {}) DIY::Logger.info( "Initialize Live: #{device_name}" ) default = { :dev=>device_name, :handler => FFI::PCap::CopyHandler, :promisc => true, :timeout=>1 } default = merge_arguments(default, args) @live = FFI::PCap::Live.new(default) DIY::Logger.info( "Listen on: #{net} " ) @running = false @live.non_blocking= true end |
Instance Attribute Details
#live ⇒ Object (readonly)
Returns the value of attribute live.
17 18 19 |
# File 'lib/diy/live.rb', line 17 def live @live end |
Instance Method Details
#break ⇒ Object
42 43 44 45 |
# File 'lib/diy/live.rb', line 42 def break DIY::Logger.debug "stopping loop recv..." @running = false end |
#inject(packet_str) ⇒ Object Also known as: send_packet
发包
20 21 22 |
# File 'lib/diy/live.rb', line 20 def inject(packet_str) @live.send_packet(packet_str) end |
#loop(&block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/diy/live.rb', line 25 def loop(&block) @running = true while @running do @live.dispatch do |this, pkt| next unless pkt block.call(this, pkt) end block.call(nil, nil) # at least every 0.01 min do it. sleep 0.01 end DIY::Logger.debug "stopped loop recv..." end |
#merge_arguments(default, new) ⇒ Object
51 52 53 |
# File 'lib/diy/live.rb', line 51 def merge_arguments(default, new) default.merge(new) end |
#net ⇒ Object
47 48 49 |
# File 'lib/diy/live.rb', line 47 def net @live.network + " / " + @live.netmask end |
#set_filter(reg) ⇒ Object
38 39 40 |
# File 'lib/diy/live.rb', line 38 def set_filter(reg) @live.set_filter(reg) end |