mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Added error checking and removed some redundancy from device isOpen()
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user