Driver: Block between read attempts

Driver:

* Refactored to limit accessibility of member fields;

Communication:

* readTask() now calls for a blocking wait;
This commit is contained in:
Kurt Wachowski
2024-08-13 13:55:12 +00:00
committed by Kyle Schwarz
parent f25a0a4a81
commit 75af3220b0
20 changed files with 116 additions and 104 deletions
@@ -87,6 +87,9 @@ public:
std::unique_ptr<Driver> driver;
device_eventhandler_t report;
size_t readTaskWakeLimit = 1;
std::chrono::milliseconds readTaskWakeTimeout = std::chrono::milliseconds(1000);
protected:
static int messageCallbackIDCounter;
std::mutex messageCallbacksLock;
+12 -8
View File
@@ -24,9 +24,11 @@ public:
virtual bool isOpen() = 0;
virtual void modeChangeIncoming() {}
virtual void awaitModeChangeComplete() {}
virtual bool isDisconnected() { return disconnected; };
virtual bool close() = 0;
inline bool isDisconnected() const { return disconnected; };
inline bool isClosing() const { return closing; }
bool waitForRx(size_t limit, std::chrono::milliseconds timeout);
bool waitForRx(std::function<bool()> predicate, std::chrono::milliseconds timeout);
bool readWait(std::vector<uint8_t>& bytes, std::chrono::milliseconds timeout = std::chrono::milliseconds(100), size_t limit = 0);
@@ -52,8 +54,8 @@ protected:
WAIT
};
virtual void readTask() = 0;
virtual void writeTask() = 0;
inline void setIsClosing(bool isClosing) { closing = isClosing; }
inline void setIsDisconnected(bool isDisconnected) { disconnected = isDisconnected; }
// Overridable in case the driver doesn't want to use writeTask and writeQueue
virtual bool writeQueueFull() { return writeQueue.size_approx() > writeQueueSize; }
@@ -61,13 +63,15 @@ protected:
virtual bool writeInternal(const std::vector<uint8_t>& b) { return writeQueue.enqueue(WriteOperation(b)); }
bool pushRx(const uint8_t* buf, size_t numReceived);
RingBuffer readBuffer = RingBuffer(ICSNEO_DRIVER_RINGBUFFER_SIZE);
std::atomic<bool> hasRxWaitRequest = false;
std::condition_variable rxWaitRequestCv;
std::mutex rxWaitMutex;
void clearBuffers();
moodycamel::BlockingConcurrentQueue<WriteOperation> writeQueue;
std::thread readThread, writeThread;
private:
RingBuffer readBuffer = RingBuffer(ICSNEO_DRIVER_RINGBUFFER_SIZE);
std::condition_variable rxWaitCv;
std::mutex rxWaitMutex;
std::atomic<bool> closing{false};
std::atomic<bool> disconnected{false};
};
+4 -2
View File
@@ -22,8 +22,10 @@ public:
private:
neodevice_t& device;
std::optional<void*> handle;
void readTask() override;
void writeTask() override;
std::thread readThread, writeThread;
void readTask();
void writeTask();
};
}
+4 -2
View File
@@ -47,8 +47,10 @@ private:
static std::string HandleToTTY(neodevice_handle_t handle);
void readTask() override;
void writeTask() override;
std::thread readThread, writeThread;
void readTask();
void writeTask();
bool fdIsValid();
};
+5 -2
View File
@@ -25,8 +25,11 @@ public:
bool isOpen() override;
bool close() override;
private:
void readTask() override;
void writeTask() override;
std::thread readThread, writeThread;
void readTask();
void writeTask();
bool writeQueueFull() override;
bool writeQueueAlmostFull() override;
bool writeInternal(const std::vector<uint8_t>& bytes) override;
+3
View File
@@ -57,8 +57,11 @@ private:
static std::vector<std::string> handles;
static bool ErrorIsDisconnection(int errorCode);
std::thread readThread, writeThread;
void readTask();
void writeTask();
bool openable; // Set to false in the constructor if the object has not been found in searchResultDevices
neodevice_t& device;
+4 -2
View File
@@ -30,8 +30,10 @@ private:
uint8_t deviceMAC[6];
bool openable = true;
EthernetPacketizer ethPacketizer;
void readTask() override;
void writeTask() override;
std::thread readThread, writeThread;
void readTask();
void writeTask();
class NetworkInterface {
public:
+4 -2
View File
@@ -47,8 +47,10 @@ private:
uint32_t dstIP;
uint16_t dstPort;
std::unique_ptr<Socket> socket;
void readTask() override;
void writeTask() override;
std::thread readThread, writeThread;
void readTask();
void writeTask();
};
}
+1
View File
@@ -32,6 +32,7 @@ private:
bool openable = true;
EthernetPacketizer ethPacketizer;
std::thread readThread, writeThread;
std::thread transmitThread;
pcap_send_queue* transmitQueue = nullptr;
std::condition_variable transmitQueueCV;
+1
View File
@@ -37,6 +37,7 @@ private:
std::shared_ptr<Detail> detail;
std::vector<std::shared_ptr<std::thread>> threads;
std::thread readThread, writeThread;
void readTask();
void writeTask();
};