PICloud with streampacker
This commit is contained in:
@@ -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(ð, connected, [this](){tcp.sendStart();});
|
||||
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
||||
CONNECTL(ð, 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(ð, connected, [this](){tcp.sendStart(ð);});
|
||||
CONNECTU(ð, threadedReadEvent, this, readed);
|
||||
CONNECTL(ð, disconnected, [this](bool){
|
||||
opened_ = false;
|
||||
eth.close();
|
||||
piCoutObj << "disconnected";// << !isOpened() << isClosed();
|
||||
piMSleep(100);
|
||||
});
|
||||
mutex_buff.lock();
|
||||
eth.startThreadedRead();
|
||||
tcp.sendStart(ð);
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user