如何使用winpcap分配内存以发送高性能的大型pcap文件(大小大于可用内存)?

我使用了winpcap示例中的代码发送pcap文件(来自此链接的 winpcap文档的原始代码)

它可以很好地发送一个小的pcap文件,但如果我试图发送一个大的pcap文件(大于可用内存大小说2 Gb)它肯定会失败。 此代码用于在内存中分配文件的大小,以便稍后发送

caplen= ftell(capfile)- sizeof(struct pcap_file_header); ... /* Allocate a send queue */ squeue = pcap_sendqueue_alloc(caplen); 

问题是如何使这个工作适用于大型文件(Gb或大于可用于分配的最大内存空间)? 我应该只分配100 Mb,然后发送队列然后接下来的100Mb? 如果是,什么是适当的缓冲区大小? 怎么做?

这里一个重要的问题是发送此文件的性能需要尽可能快地完成(我正在发送video数据包)。

总之如何管理内存来实现这一目标?

有人会有适当的解决方案或建议吗?


示例代码中的代码段(此问题的不必要代码替换为…)

 ... #include  #include  ... void main(int argc, char **argv) { pcap_t *indesc,*outdesc; ... FILE *capfile; int caplen, sync; ... pcap_send_queue *squeue; struct pcap_pkthdr *pktheader; u_char *pktdata; ... /* Retrieve the length of the capture file */ capfile=fopen(argv[1],"rb"); if(!capfile){ printf("Capture file not found!\n"); return; } fseek(capfile , 0, SEEK_END); caplen= ftell(capfile)- sizeof(struct pcap_file_header); fclose(capfile); ... ... /* Open the capture file */ if ( (indesc= pcap_open(source, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL) { fprintf(stderr,"\nUnable to open the file %s.\n", source); return; } /* Open the output adapter */ if ( (outdesc= pcap_open(argv[2], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL) { fprintf(stderr,"\nUnable to open adapter %s.\n", source); return; } ... /* Allocate a send queue */ squeue = pcap_sendqueue_alloc(caplen); /* Fill the queue with the packets from the file */ while ((res = pcap_next_ex( indesc, &pktheader, &pktdata)) == 1) { if (pcap_sendqueue_queue(squeue, pktheader, pktdata) == -1) { printf("Warning: packet buffer too small, not all the packets will be sent.\n"); break; } npacks++; } if (res == -1) { printf("Corrupted input file.\n"); pcap_sendqueue_destroy(squeue); return; } /* Transmit the queue */ if ((res = pcap_sendqueue_transmit(outdesc, squeue, sync)) len) { printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", pcap_geterr(outdesc), res); } /* free the send queue */ pcap_sendqueue_destroy(squeue); /* Close the input file */ pcap_close(indesc); /* * lose the output adapter * IMPORTANT: remember to close the adapter, otherwise there will be no guarantee that all the * packets will be sent! */ pcap_close(outdesc); return; } 

只需将它限制为50 MB(这有点武断,但我认为是合理的起点),并增强代码位,当不是所有数据包都适合队列时发出警告,以便它只发送它到目前为止的内容并开始使用剩余的数据包从头开始填充队列。