PIEthernet error 232

PICloud many fixes
PIBroadcast recursive fix
This commit is contained in:
2021-08-20 10:55:47 +03:00
parent 3c20728210
commit 0e65151e9f
5 changed files with 69 additions and 45 deletions

View File

@@ -24,16 +24,16 @@
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
tcp.setRole(PICloud::TCP::Client);
setName("cloud_client");
is_connected = false;
is_connected = is_connecting = false;
CONNECTL(&eth, connected, [this](){opened_ = true; tcp.sendStart();});
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){
// piCoutObj << "disconnected";
opened_ = false;
is_connected = false;
cond_connect.notifyOne();
cond_buff.notifyOne();
piMSleep(100);
//piCoutObj << "eth.disconnected";
bool ned = is_connected;
opened_ = is_connected = false;
notifyBuffer();
if (ned)
disconnected();
});
}
@@ -41,11 +41,10 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
PICloudClient::~PICloudClient() {
eth.close();
if (is_connected) {
is_connected = false;
disconnected();
cond_buff.notifyOne();
cond_connect.notifyOne();
is_connected = false;
}
notifyBuffer();
close();
stop();
}
@@ -63,20 +62,26 @@ void PICloudClient::setKeepConnection(bool on) {
bool PICloudClient::openDevice() {
// piCout << "PICloudClient open device" << path();
if (isOpened()) return true;
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
mutex_buff.lock();
eth.startThreadedRead();
bool conn_ok = cond_connect.waitFor(mutex_buff, (int)eth.readTimeout(), [this](){return isConnected();});
piCoutObj << "conn_ok" << conn_ok;
mutex_buff.unlock();
is_connecting = true;
PITimeMeasurer tm;
while (tm.elapsed_m() < (int)eth.readTimeout()) {
if (isConnected()) break;
if (!is_connecting) return false;
piMSleep(PIP_MIN_MSLEEP);
}
is_connecting = false;
bool conn_ok = isConnected();
//piCoutObj << "conn_ok" << conn_ok;
if (!conn_ok) {
eth.stop();
eth.close();
piMSleep(100);
//piMSleep(100);
}
return isConnected();
return conn_ok;
} else {
eth.close();
return false;
@@ -85,38 +90,47 @@ bool PICloudClient::openDevice() {
bool PICloudClient::closeDevice() {
PIThread::stop(false);
if (is_connected) {
is_connected = false;
disconnected();
cond_buff.notifyOne();
cond_connect.notifyOne();
}
eth.stop();
if (eth.isOpened()) eth.close();
PIThread::stop(false);
if (is_connected)
disconnected();
is_connected = is_connecting = false;
notifyBuffer();
waitForFinish(1000);
if (eth.isOpened())
eth.close();
return true;
}
int PICloudClient::readDevice(void * read_to, int max_size) {
// piCoutObj << "readDevice";
//piCoutObj << "readDevice";
if (!is_connected) return -1;
mutex_buff.lock();
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
int sz = piMini(max_size, buff.size());
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
if (sz > 0) {
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
}
mutex_buff.unlock();
return sz;
}
int PICloudClient::writeDevice(const void * data, int size) {
// piCoutObj << "writeDevice";
//piCoutObj << "writeDevice";
return tcp.sendData(PIByteArray(data, size));
}
void PICloudClient::notifyBuffer() {
mutex_buff.lock();
cond_buff.notifyOne();
mutex_buff.unlock();
}
void PICloudClient::_readed(PIByteArray & ba) {
mutex_buff.lock();
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
@@ -126,14 +140,13 @@ void PICloudClient::_readed(PIByteArray & ba) {
if (tcp.parseConnect(ba) == 1) {
is_connected = true;
connected();
cond_connect.notifyOne();
}
break;
case PICloud::TCP::Disconnect:
is_connected = false;
//piCoutObj << "TCP::Disconnect ...";
static_cast<PIThread*>(&eth)->stop();
eth.close();
disconnected();
//piCoutObj << "TCP::Disconnect ok";
break;
case PICloud::TCP::Data:
if (is_connected) {