dugsong / pypcap

Automatically exported from code.google.com/p/pypcap
Other
117 stars 45 forks source link

loop() will exist and lost packet, even if cnt=0 #51

Open yangbh opened 8 years ago

yangbh commented 8 years ago

my env: linux

call loop code:

pc = pcap.pcap(name='eth1',immediate=False)
...
while True:
    pc.loop(0, pypcapCallback)
    nrecv,ndrop,nifdrop=pc.stats()
    logging.warning('%s, %s, %s' % (nrecv,ndrop,nifdrop))

and got

[2016-03-09 16:28:33,409] [Moniter] [WARNING]>> 0, 0, 0
[2016-03-09 16:28:38,772] [Moniter] [WARNING]>> 3, 2, 0
[2016-03-09 16:28:41,432] [Moniter] [WARNING]>> 6, 4, 0

as you see, when packet is few, it will lost a lot of packet because of restart loop func

i recode the loop func in pcap.pyx, but it still does not work

def loop(self, cnt, callback, *args):
        ....
        pcap_ex_setup(self.__pcap)
        while 1:
            try:
                Py_BEGIN_ALLOW_THREADS
                n = pcap_ex_next(self.__pcap, &hdr, &pkt)
                Py_END_ALLOW_THREADS
                if n == 1:
                    callback(hdr.ts.tv_sec + (hdr.ts.tv_usec / 1000000.0),
                             PyBuffer_FromMemory(pkt, hdr.caplen), *args)
                elif n == 0:
                    # change break to continue
                    continue

                elif n == -1:
                    raise KeyboardInterrupt
                elif n == -2:
                    # change break to continue
                    continue
...

any good idea, pleas help me, guys thanks very much