Wait on the pcap file handle to reduce cpu consumption during sniffing

git-svn-id: file:///home/svn/framework3/trunk@6814 4d416f70-5f16-0410-b530-b9f4589650da
unstable
HD Moore 2009-07-17 02:24:21 +00:00
parent 5e0213c662
commit 697d89c8cf
2 changed files with 21 additions and 6 deletions

View File

@ -389,14 +389,20 @@ static VALUE
rbpcap_capture(VALUE self)
{
rbpcap_t *rbp;
int fno = -1;
Data_Get_Struct(self, rbpcap_t, rbp);
if(! rbpcap_ready(rbp)) return self;
fno = pcap_fileno(rbp->pd);
for(;;) {
VALUE packet = rbpcap_next(self);
VALUE packet;
if(fno != -1) rb_thread_wait_fd(fno);
packet = rbpcap_next(self);
if(packet == Qnil && rbp->type == OFFLINE)
break;

View File

@ -81,14 +81,23 @@ class Pcap::UnitTest < Test::Unit::TestCase
end
def test_pcap_next
=begin
d = Pcap.lookupdev
o = Pcap.open_live(d, 1344, true, 1)
@c = 0
t = Thread.new { while(true); @c += 1; end; }
x = o.next
t = Thread.new { while(true); @c += 1; select(nil, nil, nil, 0.10); end; }
require 'timeout'
begin
Timeout.timeout(10) do
o.each do |pkt|
end
end
rescue ::Timeout::Error
end
t.kill
=end
puts "Background thread ticked #{@c} times while capture was running"
true
end