Added error checking and removed some redundancy from device isOpen()

This commit is contained in:
EricLiu2000
2019-06-13 16:01:35 -04:00
parent 2806c935f1
commit 965679c370
8 changed files with 73 additions and 35 deletions
+12 -4
View File
@@ -194,14 +194,20 @@ PCAP::PCAP(const device_errorhandler_t& err, neodevice_t& forDevice) : ICommunic
}
bool PCAP::open() {
if(!openable)
if(!openable) {
err(APIError::InvalidNeoDevice);
return false;
}
if(!pcap.ok())
if(!pcap.ok()) {
err(APIError::DriverFailedToOpen);
return false;
}
if(isOpen())
if(isOpen()) {
err(APIError::DeviceCurrentlyOpen);
return false;
}
// Open the interface
interface.fp = pcap.open(interface.nameFromWinPCAP.c_str(), 100, PCAP_OPENFLAG_PROMISCUOUS | PCAP_OPENFLAG_MAX_RESPONSIVENESS, 1, nullptr, errbuf);
@@ -222,8 +228,10 @@ bool PCAP::isOpen() {
}
bool PCAP::close() {
if(!isOpen())
if(!isOpen()) {
err(APIError::DeviceCurrentlyClosed);
return false;
}
closing = true; // Signal the threads that we are closing
readThread.join();
+15 -5
View File
@@ -183,8 +183,10 @@ bool VCP::IsHandleValid(neodevice_handle_t handle) {
}
bool VCP::open(bool fromAsync) {
if(isOpen() || (!fromAsync && opening))
if(isOpen() || (!fromAsync && opening)) {
err(APIError::DeviceCurrentlyOpen);
return false;
}
if(!IsHandleValid(device.handle)) {
err(APIError::DriverFailedToOpen);
@@ -216,6 +218,7 @@ bool VCP::open(bool fromAsync) {
COMMTIMEOUTS timeouts;
if(!GetCommTimeouts(handle, &timeouts)) {
close();
err(APIError::DriverFailedToOpen);
return false;
}
@@ -288,9 +291,11 @@ void VCP::openAsync(fn_boolCallback callback) {
}
bool VCP::close() {
if(!isOpen())
if(!isOpen()) {
err(APIError::DeviceCurrentlyClosed);
return false;
}
closing = true; // Signal the threads that we are closing
for(auto& t : threads)
t->join(); // Wait for the threads to close
@@ -298,9 +303,11 @@ bool VCP::close() {
writeThread.join();
closing = false;
if(!CloseHandle(handle))
if(!CloseHandle(handle)) {
err(APIError::DriverFailedToClose);
return false;
}
handle = INVALID_HANDLE_VALUE;
bool ret = true; // If one of the events fails closing, we probably still want to try and close the others
@@ -325,6 +332,9 @@ bool VCP::close() {
while(readQueue.try_dequeue(flush)) {}
while(writeQueue.try_dequeue(flushop)) {}
if(!ret)
err(APIError::DriverFailedToClose);
// TODO Set up some sort of shared memory, free which COM port we had open so we can try to open it again
return ret;