This commit is contained in:
2021-08-23 13:56:21 +03:00
parent 1cc46468c1
commit c937d7251a
5 changed files with 52 additions and 28 deletions

View File

@@ -25,26 +25,35 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
tcp.setRole(PICloud::TCP::Client);
setName("cloud_client");
is_connected = false;
CONNECTL(&eth, connected, [this](){/*opened_ = true;*/ tcp.sendStart();});
is_deleted = false;
// setReopenEnabled(false);
CONNECTL(&eth, connected, [this](){opened_ = true; tcp.sendStart();});
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){
piCoutObj << "disconnected";
if (is_deleted) return;
//piCoutObj << "eth disconnected";
static_cast<PIThread*>(&eth)->stop();
//opened_ = false;
opened_ = false;
if (is_connected) disconnected();
internalDisconnect();
piMSleep(100);
//piCoutObj << "eth disconnected done";
});
}
PICloudClient::~PICloudClient() {
piCoutObj << "~PICloudClient()";
//piCoutObj << "~PICloudClient()";
PIThread::stop();
eth.close();
//eth.close();
if (is_connected) disconnected();
is_connected = false;
close();
//piCoutObj << "~PICloudClient() closed";
internalDisconnect();
//close();
//stop(false);
// stop(false);
is_deleted = true;
internalDisconnect();
//piCoutObj << "~PICloudClient() done";
}
@@ -60,20 +69,21 @@ void PICloudClient::setKeepConnection(bool on) {
bool PICloudClient::openDevice() {
piCout << "PICloudClient open device" << path();
//piCoutObj << "open";// << path();
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
mutex_connect.lock();
eth.startThreadedRead();
piCoutObj << "connecting...";
//piCoutObj << "connecting...";
bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout());
piCoutObj << "conn_ok" << conn_ok;
//piCoutObj << "conn_ok" << conn_ok << is_connected;
mutex_connect.unlock();
if (!conn_ok) {
mutex_connect.lock();
eth.stop();
eth.close();
piMSleep(100);
mutex_connect.unlock();
}
mutex_connect.unlock();
return is_connected;
} else {
//eth.close();
@@ -83,7 +93,7 @@ bool PICloudClient::openDevice() {
bool PICloudClient::closeDevice() {
PIThread::stop(false);
//PIThread::stop();
if (is_connected) {
internalDisconnect();
}
@@ -94,8 +104,9 @@ bool PICloudClient::closeDevice() {
int PICloudClient::readDevice(void * read_to, int max_size) {
// piCoutObj << "readDevice";
if (!is_connected) return -1;
if (is_deleted) return -1;
//piCoutObj << "readDevice";
if (!is_connected && eth.isClosed()) openDevice();
int sz = -1;
mutex_buff.lock();
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
@@ -105,11 +116,14 @@ int PICloudClient::readDevice(void * read_to, int max_size) {
buff.remove(0, sz);
}
mutex_buff.unlock();
if (!is_connected) opened_ = false;
//piCoutObj << "readDevice done" << sz;
return sz;
}
int PICloudClient::writeDevice(const void * data, int size) {
if (is_deleted) return -1;
// piCoutObj << "writeDevice";
return tcp.sendData(PIByteArray(data, size));
}
@@ -123,24 +137,30 @@ void PICloudClient::internalDisconnect() {
void PICloudClient::_readed(PIByteArray & ba) {
mutex_buff.lock();
if (is_deleted) return;
//piCoutObj << "_readed";
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
if (hdr.second == tcp.role()) {
switch (hdr.first) {
case PICloud::TCP::Connect:
if (tcp.parseConnect(ba) == 1) {
mutex_connect.lock();
is_connected = true;
connected();
mutex_connect.unlock();
cond_connect.notifyOne();
}
break;
case PICloud::TCP::Disconnect:
static_cast<PIThread*>(&eth)->stop();
opened_ = false;
eth.close();
break;
case PICloud::TCP::Data:
if (is_connected) {
mutex_buff.lock();
buff.append(ba);
mutex_buff.unlock();
cond_buff.notifyOne();
}
break;
@@ -149,7 +169,7 @@ void PICloudClient::_readed(PIByteArray & ba) {
}
//piCoutObj << "readed" << ba.toHex();
}
mutex_buff.unlock();
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
//piCoutObj << "_readed done";
}