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() { PICloudServer::~PICloudServer() {
stop();
close();
} }

View File

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

View File

@@ -31,7 +31,7 @@ class PIP_EXPORT PIBinaryLog: public PIIODevice
PIIODEVICE(PIBinaryLog) PIIODEVICE(PIBinaryLog)
public: public:
explicit PIBinaryLog(); explicit PIBinaryLog();
~PIBinaryLog() {closeDevice();} virtual ~PIBinaryLog();
//! \brief Play modes for \a PIBinaryLog //! \brief Play modes for \a PIBinaryLog
enum PlayMode { 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() { bool PICAN::openDevice() {
#ifdef PIP_CAN #ifdef PIP_CAN
piCout << "PICAN open device" << path(); piCout << "PICAN open device" << path();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -37,8 +37,6 @@ public:
//! Contructs %PIIOString with \"string\" content only for read //! Contructs %PIIOString with \"string\" content only for read
explicit PIIOString(const PIString & string); explicit PIIOString(const PIString & string);
~PIIOString() {closeDevice();}
//! Returns content //! Returns content
PIString * string() const {return str;} 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() { PIPeer::~PIPeer() {
//piCout << "~PIPeer" << uint(this); //piCout << "~PIPeer" << uint(this);
stop();
if (destroyed) return; if (destroyed) return;
destroyed = true; destroyed = true;
sync_timer.stop(); sync_timer.stop();

View File

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

View File

@@ -97,7 +97,7 @@ public:
//! Contructs %PISerial with device name "device", speed "speed" and parameters "params" //! 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); 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" //! 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() { bool PISharedMemory::openDevice() {
close(); close();
//piCoutObj << "try open" << path() << dsize; //piCoutObj << "try open" << path() << dsize;

View File

@@ -37,7 +37,7 @@ public:
explicit PISharedMemory(const PIString & shm_name, int size, DeviceMode mode = ReadWrite); 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 //! Read all shared memory object content to byte array and return it
PIByteArray readAll(); 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) { void PISPI::setSpeed(uint speed_hz) {
spi_speed = speed_hz; spi_speed = speed_hz;
} }

View File

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

View File

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

View File

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

View File

@@ -32,7 +32,7 @@ class PIP_EXPORT PIUSB: public PIIODevice
PIIODEVICE(PIUSB) PIIODEVICE(PIUSB)
public: public:
explicit PIUSB(ushort vid = 0, ushort pid = 0); explicit PIUSB(ushort vid = 0, ushort pid = 0);
~PIUSB() {closeDevice();} virtual ~PIUSB();
struct PIP_EXPORT Endpoint { struct PIP_EXPORT Endpoint {
Endpoint(uchar a = 0, uchar at = 0, ushort mps = 0) {address = a; attributes = at; max_packet_size = mps; parse();} 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() { void PIUSB::Endpoint::parse() {
synchronisation_type = NoSynchonisation; synchronisation_type = NoSynchonisation;
usage_type = DataEndpoint; usage_type = DataEndpoint;

View File

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