PIIODevice destructor fix

This commit is contained in:
2020-08-26 18:43:33 +03:00
parent 2a42d2a341
commit 2ef0ca6946
23 changed files with 62 additions and 35 deletions

View File

@@ -25,7 +25,8 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
PICloudServer::~PICloudServer() {
stop();
close();
}

View File

@@ -78,6 +78,12 @@ PIBinaryLog::PIBinaryLog() {
}
PIBinaryLog::~PIBinaryLog() {
stop();
close();
}
bool PIBinaryLog::openDevice() {
lastrecord.timestamp = PISystemTime();
lastrecord.id = 0;

View File

@@ -31,7 +31,7 @@ class PIP_EXPORT PIBinaryLog: public PIIODevice
PIIODEVICE(PIBinaryLog)
public:
explicit PIBinaryLog();
~PIBinaryLog() {closeDevice();}
virtual ~PIBinaryLog();
//! \brief Play modes for \a PIBinaryLog
enum PlayMode {

View File

@@ -46,6 +46,12 @@ PICAN::PICAN(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(pa
}
PICAN::~PICAN() {
stop();
close();
}
bool PICAN::openDevice() {
#ifdef PIP_CAN
piCout << "PICAN open device" << path();

View File

@@ -31,7 +31,7 @@ class PIP_EXPORT PICAN: public PIIODevice
PIIODEVICE(PICAN)
public:
explicit PICAN(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
~PICAN() {}
virtual ~PICAN();
void setCANID(int id);
int CANID() const;

View File

@@ -244,7 +244,7 @@ PIEthernet::PIEthernet(int sock_, PIString ip_port): PIIODevice("", ReadWrite) {
PIEthernet::~PIEthernet() {
//piCout << "~PIEthernet" << uint(this);
stop();
closeDevice();
close();
//piCoutObj << "~PIEthernet done";
}
@@ -566,7 +566,6 @@ bool PIEthernet::connect(bool threaded) {
connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0);
if (!connected_) {
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
closeSocket(sock);
}
opened_ = connected_;
if (connected_) {
@@ -663,9 +662,8 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
//piCoutObj << "connect to " << ip_ << ":" << port_ << "...";
connected_ = (::connect(sock, (sockaddr * )&(PRIVATE->addr_), sizeof(PRIVATE->addr_)) == 0);
//piCoutObj << "connect to " << ip_ << ":" << port_ << connected_;
if (!connected_) {
if (!connected_)
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
}
opened_ = connected_;
if (connected_) {
connecting_ = false;
@@ -674,10 +672,7 @@ int PIEthernet::readDevice(void * read_to, int max_size) {
piMSleep(10);
//piCout << "connected to" << path();
}
if (!connected_) {
closeSocket(sock);
return -1;
}
if (!connected_) return -1;
errorClear();
rs = ethRecv(sock, read_to, max_size);
//piCoutObj << "readed" << rs;
@@ -742,7 +737,7 @@ int PIEthernet::writeDevice(const void * data, int max_size) {
//piCoutObj << "connect SingleTCP" << ip_s << ":" << port_s << "...";
if (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) != 0) {
//piCoutObj << "Can`t connect to " << ip_s << ":" << port_s << ", " << ethErrorString();
closeSocket(sock);
msleep(PIP_MIN_MSLEEP);
return -1;
}
//piCoutObj << "ok, write SingleTCP" << int(data) << max_size << "bytes ...";
@@ -776,19 +771,15 @@ int PIEthernet::writeDevice(const void * data, int max_size) {
#endif
//piCoutObj << "connect to " << ip << ":" << port_;
connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0);
if (!connected_) {
if (!connected_)
piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
}
opened_ = connected_;
if (connected_) {
connecting_ = false;
connected();
}
}
if (!connected_) {
closeSocket(sock);
return -1;
}
if (!connected_) return -1;
ret = ::send(sock, (const char *)data, max_size, 0);
if (ret < 0) {
connected_ = false;

View File

@@ -154,6 +154,12 @@ bool PIFile::openTemporary(PIIODevice::DeviceMode mode) {
}
PIFile::~PIFile() {
stop();
close();
}
bool PIFile::openDevice() {
close();
PIString p = path();

View File

@@ -91,7 +91,7 @@ public:
//! Open temporary file with open mode "mode"
bool openTemporary(PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
~PIFile() {closeDevice();}
virtual ~PIFile();
//! Immediate write all buffered data to disk
void flush();

View File

@@ -37,8 +37,6 @@ public:
//! Contructs %PIIOByteArray with \"buffer\" content only for read
explicit PIIOByteArray(const PIByteArray & buffer);
~PIIOByteArray() {closeDevice();}
//! Returns content
PIByteArray * byteArray() const {return data_;}

View File

@@ -137,11 +137,6 @@ PIIODevice::PIIODevice(const PIString & path, PIIODevice::DeviceMode mode): PITh
PIIODevice::~PIIODevice() {
stop();
if (opened_) {
closeDevice();
if (!opened_)
closed();
}
}

View File

@@ -37,8 +37,6 @@ public:
//! Contructs %PIIOString with \"string\" content only for read
explicit PIIOString(const PIString & string);
~PIIOString() {closeDevice();}
//! Returns content
PIString * string() const {return str;}

View File

@@ -181,6 +181,7 @@ PIPeer::PIPeer(const PIString & n): PIIODevice(), inited__(false), eth_tcp_srv(P
PIPeer::~PIPeer() {
//piCout << "~PIPeer" << uint(this);
stop();
if (destroyed) return;
destroyed = true;
sync_timer.stop();

View File

@@ -197,7 +197,8 @@ PISerial::PISerial(const PIString & device_, PISerial::Speed speed_, PIFlags<PIS
PISerial::~PISerial() {
closeDevice();
stop();
close();
}

View File

@@ -97,7 +97,7 @@ public:
//! Contructs %PISerial with device name "device", speed "speed" and parameters "params"
explicit PISerial(const PIString & device, PISerial::Speed speed = S115200, PIFlags<PISerial::Parameters> params = 0);
~PISerial();
virtual ~PISerial();
//! Set both input and output speed to "speed"

View File

@@ -89,6 +89,12 @@ PISharedMemory::PISharedMemory(const PIString & shm_name, int size, PIIODevice::
}
PISharedMemory::~PISharedMemory() {
stop();
close();
}
bool PISharedMemory::openDevice() {
close();
//piCoutObj << "try open" << path() << dsize;

View File

@@ -37,7 +37,7 @@ public:
explicit PISharedMemory(const PIString & shm_name, int size, DeviceMode mode = ReadWrite);
virtual ~PISharedMemory() {close();}
virtual ~PISharedMemory();
//! Read all shared memory object content to byte array and return it
PIByteArray readAll();

View File

@@ -59,6 +59,12 @@ PISPI::PISPI(const PIString & path, uint speed, PIIODevice::DeviceMode mode) : P
}
PISPI::~PISPI() {
stop();
close();
}
void PISPI::setSpeed(uint speed_hz) {
spi_speed = speed_hz;
}

View File

@@ -31,7 +31,7 @@ class PIP_EXPORT PISPI: public PIIODevice
PIIODEVICE(PISPI)
public:
explicit PISPI(const PIString & path = PIString(), uint speed_hz = 1000000, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
~PISPI() {}
virtual ~PISPI();
//! \brief Parameters of PISPI
enum Parameters {

View File

@@ -37,6 +37,12 @@ PITransparentDevice::PITransparentDevice() {
}
PITransparentDevice::~PITransparentDevice() {
stop();
close();
}
int PITransparentDevice::readDevice(void * read_to, int max_size) {
if (!canRead()) return -1;
que_mutex.lock();

View File

@@ -34,7 +34,7 @@ public:
//! Contructs empty %PITransparentDevice
explicit PITransparentDevice();
~PITransparentDevice() {closeDevice();}
virtual ~PITransparentDevice();
protected:
bool openDevice();

View File

@@ -32,7 +32,7 @@ class PIP_EXPORT PIUSB: public PIIODevice
PIIODEVICE(PIUSB)
public:
explicit PIUSB(ushort vid = 0, ushort pid = 0);
~PIUSB() {closeDevice();}
virtual ~PIUSB();
struct PIP_EXPORT Endpoint {
Endpoint(uchar a = 0, uchar at = 0, ushort mps = 0) {address = a; attributes = at; max_packet_size = mps; parse();}

View File

@@ -46,6 +46,11 @@ PIUSB::PIUSB(ushort vid, ushort pid): PIIODevice("", ReadWrite) {
}
PIUSB::~PIUSB() {
stop();
close();
}
void PIUSB::Endpoint::parse() {
synchronisation_type = NoSynchonisation;
usage_type = DataEndpoint;

View File

@@ -12,6 +12,7 @@ int main() {
// }
// s.open();
s.startThreadedRead();
piSleep(200);
piSleep(2);
// s.stopThreadedRead();
return 0;
}