PICloud with streampacker
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "picloudbase.h"
|
||||
|
||||
|
||||
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client) {
|
||||
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(ð), tcp(&streampacker) {
|
||||
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -21,10 +21,17 @@
|
||||
|
||||
|
||||
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
|
||||
PIString name = "PCS_" + PIString::fromNumber(randomi()%1000);
|
||||
PIString server_name = "PCS_" + PIString::fromNumber(randomi()%1000);
|
||||
tcp.setRole(PICloud::TCP::Server);
|
||||
tcp.setServerName(name);
|
||||
setName("cloud_server__" + name);
|
||||
tcp.setServerName(server_name);
|
||||
setName("cloud_server__" + server_name);
|
||||
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
||||
CONNECTL(ð, connected, [this](){tcp.sendStart();});
|
||||
CONNECTL(ð, disconnected, [this](bool){
|
||||
piCoutObj << "disconnected";
|
||||
opened_ = false;
|
||||
piMSleep(100);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -49,15 +56,7 @@ bool PICloudServer::openDevice() {
|
||||
piCout << "PICloudServer open device" << path();
|
||||
bool op = eth.connect(path(), false);
|
||||
if (op) {
|
||||
CONNECTU(ð, threadedReadEvent, this, readed);
|
||||
CONNECTL(ð, connected, [this](){tcp.sendStart(ð);});
|
||||
CONNECTL(ð, disconnected, [this](bool){
|
||||
opened_ = false;
|
||||
eth.close();
|
||||
piCoutObj << "disconnected" << !isOpened() << isClosed();
|
||||
});
|
||||
eth.startThreadedRead();
|
||||
tcp.sendStart(ð);
|
||||
return true;
|
||||
}
|
||||
eth.close();
|
||||
@@ -87,12 +86,12 @@ int PICloudServer::writeDevice(const void * data, int max_size) {
|
||||
|
||||
|
||||
void PICloudServer::clientDisconnect(uint client_id) {
|
||||
tcp.sendDisconnected(ð, client_id);
|
||||
tcp.sendDisconnected(client_id);
|
||||
}
|
||||
|
||||
|
||||
int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
|
||||
return tcp.sendData(ð, data, client_id);
|
||||
return tcp.sendData(data, client_id);
|
||||
}
|
||||
|
||||
|
||||
@@ -138,8 +137,7 @@ void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::readed(uchar *data, int size) {
|
||||
PIByteArray ba(data, size);
|
||||
void PICloudServer::_readed(PIByteArray & ba) {
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
if (hdr.second == tcp.role()) {
|
||||
switch (hdr.first) {
|
||||
@@ -147,7 +145,7 @@ void PICloudServer::readed(uchar *data, int size) {
|
||||
uint id = tcp.parseConnect(ba);
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
if (oc) {
|
||||
tcp.sendDisconnected(ð, id);
|
||||
tcp.sendDisconnected(id);
|
||||
} else {
|
||||
Client * c = new Client(this, id);
|
||||
clients << c;
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "picrypt.h"
|
||||
#include "pichunkstream.h"
|
||||
#include "piethernet.h"
|
||||
#include "pistreampacker.h"
|
||||
|
||||
|
||||
const char hash_def_key[] = "_picrypt_";
|
||||
@@ -31,9 +32,9 @@ PICloud::TCP::Header::Header() {
|
||||
}
|
||||
|
||||
|
||||
PICloud::TCP::TCP() {
|
||||
}
|
||||
PICloud::TCP::TCP(PIStreamPacker * s) : streampacker(s) {
|
||||
|
||||
}
|
||||
|
||||
void PICloud::TCP::setRole(PICloud::TCP::Role r) {
|
||||
header.role = r;
|
||||
@@ -45,53 +46,55 @@ void PICloud::TCP::setServerName(const PIString & server_name) {
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendStart(PIEthernet * eth) {
|
||||
void PICloud::TCP::sendStart() {
|
||||
piCout << "sendStart";
|
||||
header.type = PICloud::TCP::Connect;
|
||||
PIByteArray ba;
|
||||
ba << header << sname;
|
||||
eth->send(ba);
|
||||
streampacker->send(ba);
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendConnected(PIEthernet * eth, uint client_id) {
|
||||
void PICloud::TCP::sendConnected(uint client_id) {
|
||||
header.type = PICloud::TCP::Connect;
|
||||
PIByteArray ba;
|
||||
ba << header << client_id;
|
||||
eth->send(ba);
|
||||
streampacker->send(ba);
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendDisconnected(PIEthernet * eth, uint client_id) {
|
||||
void PICloud::TCP::sendDisconnected(uint client_id) {
|
||||
header.type = PICloud::TCP::Disconnect;
|
||||
PIByteArray ba;
|
||||
ba << header << client_id;
|
||||
eth->send(ba);
|
||||
streampacker->send(ba);
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data) {
|
||||
void PICloud::TCP::sendData(const PIByteArray & data) {
|
||||
header.type = PICloud::TCP::Data;
|
||||
PIByteArray ba;
|
||||
ba << header;
|
||||
ba.append(data);
|
||||
// piCout << "sendData" << ba.toHex();
|
||||
eth->send(ba);
|
||||
streampacker->send(ba);
|
||||
}
|
||||
|
||||
|
||||
int PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data, uint client_id) {
|
||||
int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) {
|
||||
header.type = PICloud::TCP::Data;
|
||||
PIByteArray ba;
|
||||
ba << header << client_id;
|
||||
ba.append(data);
|
||||
if (eth->send(ba)) return data.size_s();
|
||||
else return -1;
|
||||
streampacker->send(ba);
|
||||
return data.size_s();
|
||||
}
|
||||
|
||||
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
||||
ret.first = Invalid;
|
||||
ret.first = InvalidType;
|
||||
ret.second = InvalidRole;
|
||||
if (ba.size() < sizeof(Header)) return ret;
|
||||
PICloud::TCP::Header hdr;
|
||||
ba >> hdr;
|
||||
|
||||
Reference in New Issue
Block a user