mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Replace concurrentqueue with ringbuffer
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "icsneo/communication/ethernetpacketizer.h"
|
||||
#include "icsneo/communication/packetizer.h"
|
||||
#include "icsneo/communication/decoder.h"
|
||||
#include "icsneo/communication/ringbuffer.h"
|
||||
#include <pcap.h>
|
||||
#include <iphlpapi.h>
|
||||
#pragma comment(lib, "IPHLPAPI.lib")
|
||||
@@ -123,7 +124,9 @@ void PCAP::Find(std::vector<FoundDevice>& found) {
|
||||
auto bs = requestPacket.getBytestream();
|
||||
pcap.sendpacket(iface.fp, bs.data(), (int)bs.size());
|
||||
|
||||
auto timeout = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(5);
|
||||
auto timeout = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(250);
|
||||
constexpr const size_t TempBufferSize = 4096;
|
||||
RingBuffer tempBuffer(TempBufferSize);
|
||||
while(std::chrono::high_resolution_clock::now() <= timeout) { // Wait up to 5ms for the response
|
||||
struct pcap_pkthdr* header;
|
||||
const uint8_t* data;
|
||||
@@ -142,7 +145,8 @@ void PCAP::Find(std::vector<FoundDevice>& found) {
|
||||
continue; // This packet is not for us
|
||||
|
||||
Packetizer packetizer([](APIEvent::Type, APIEvent::Severity) {});
|
||||
if(!packetizer.input(ethPacketizer.outputUp()))
|
||||
tempBuffer.write(ethPacketizer.outputUp());
|
||||
if(!packetizer.input(tempBuffer))
|
||||
continue; // This packet was not well formed
|
||||
|
||||
EthernetPacketizer::EthernetPacket decoded(data, header->caplen);
|
||||
@@ -251,9 +255,8 @@ bool PCAP::close() {
|
||||
pcap.close(iface.fp);
|
||||
iface.fp = nullptr;
|
||||
|
||||
uint8_t flush;
|
||||
WriteOperation flushop;
|
||||
while(readQueue.try_dequeue(flush)) {}
|
||||
readBuffer.clear();
|
||||
while(writeQueue.try_dequeue(flushop)) {}
|
||||
transmitQueue = nullptr;
|
||||
|
||||
@@ -275,7 +278,7 @@ void PCAP::readTask() {
|
||||
|
||||
if(ethPacketizer.inputUp({data, data + header->caplen})) {
|
||||
const auto bytes = ethPacketizer.outputUp();
|
||||
readQueue.enqueue_bulk(bytes.data(), bytes.size());
|
||||
readBuffer.write(bytes.data(), bytes.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -357,9 +357,8 @@ bool VCP::close() {
|
||||
detail->overlappedWait.hEvent = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
uint8_t flush;
|
||||
WriteOperation flushop;
|
||||
while(readQueue.try_dequeue(flush)) {}
|
||||
readBuffer.clear();
|
||||
while(writeQueue.try_dequeue(flushop)) {}
|
||||
|
||||
if(!ret)
|
||||
@@ -391,7 +390,7 @@ void VCP::readTask() {
|
||||
if(ReadFile(detail->handle, readbuf, READ_BUFFER_SIZE, nullptr, &detail->overlappedRead)) {
|
||||
if(GetOverlappedResult(detail->handle, &detail->overlappedRead, &bytesRead, FALSE)) {
|
||||
if(bytesRead)
|
||||
readQueue.enqueue_bulk(readbuf, bytesRead);
|
||||
readBuffer.write(readbuf, bytesRead);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -414,7 +413,7 @@ void VCP::readTask() {
|
||||
auto ret = WaitForSingleObject(detail->overlappedRead.hEvent, 100);
|
||||
if(ret == WAIT_OBJECT_0) {
|
||||
if(GetOverlappedResult(detail->handle, &detail->overlappedRead, &bytesRead, FALSE)) {
|
||||
readQueue.enqueue_bulk(readbuf, bytesRead);
|
||||
readBuffer.write(readbuf, bytesRead);
|
||||
state = LAUNCH;
|
||||
} else
|
||||
report(APIEvent::Type::FailedToRead, APIEvent::Severity::Error);
|
||||
|
||||
Reference in New Issue
Block a user