PICloud with streampacker

This commit is contained in:
2021-04-07 14:38:32 +03:00
parent fcf9f0f80e
commit 1fd9851068
13 changed files with 90 additions and 67 deletions

View File

@@ -21,10 +21,19 @@
#include "picloudtcp.h"
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
tcp.setRole(PICloud::TCP::Client);
setName("cloud_client");
is_connected = false;
CONNECTL(&eth, connected, [this](){tcp.sendStart();});
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){
piCoutObj << "disconnected";
opened_ = false;
is_connected = false;
cond_connect.notifyOne();
piMSleep(100);
});
}
@@ -49,17 +58,17 @@ bool PICloudClient::openDevice() {
piCout << "PICloudClient open device" << path();
bool op = eth.connect(path(), false);
if (op) {
CONNECTL(&eth, connected, [this](){tcp.sendStart(&eth);});
CONNECTU(&eth, threadedReadEvent, this, readed);
CONNECTL(&eth, disconnected, [this](bool){
opened_ = false;
eth.close();
piCoutObj << "disconnected";// << !isOpened() << isClosed();
piMSleep(100);
});
mutex_buff.lock();
eth.startThreadedRead();
tcp.sendStart(&eth);
return true;
bool conn_ok = cond_connect.waitFor(mutex_buff, (int)eth.readTimeout(), [this](){return isConnected();});
piCoutObj << "conn_ok" << conn_ok;
mutex_buff.unlock();
if (!conn_ok) {
eth.stop();
eth.close();
piMSleep(100);
}
return isConnected();
} else {
eth.close();
return false;
@@ -92,14 +101,16 @@ int PICloudClient::writeDevice(const void * data, int max_size) {
}
void PICloudClient::readed(uchar *data, int size) {
PIByteArray ba(data, size);
void PICloudClient::_readed(PIByteArray & ba) {
mutex_buff.lock();
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) == 0) is_connected = true;
if (tcp.parseConnect(ba) == 1) {
is_connected = true;
cond_connect.notifyOne();
}
break;
case PICloud::TCP::Disconnect:
is_connected = false;
@@ -107,7 +118,7 @@ void PICloudClient::readed(uchar *data, int size) {
eth.close();
break;
case PICloud::TCP::Data:
buff.append(data, size);
buff.append(ba);
cond_buff.notifyOne();
break;
default: