PICloud connect/disconnect
This commit is contained in:
6
libs/cloud/picloudbase.cpp
Normal file
6
libs/cloud/picloudbase.cpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "picloudbase.h"
|
||||
|
||||
|
||||
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client) {
|
||||
|
||||
}
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "picloudtcp.h"
|
||||
|
||||
|
||||
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), eth(PIEthernet::TCP_Client) {
|
||||
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
|
||||
tcp.setRole(PICloud::TCP::Client);
|
||||
setName("cloud_client");
|
||||
}
|
||||
@@ -39,28 +39,49 @@ void PICloudClient::setServerName(const PIString & server_name) {
|
||||
}
|
||||
|
||||
|
||||
void PICloudClient::setKeepConnection(bool on) {
|
||||
eth.setParameter(PIEthernet::KeepConnection, on);
|
||||
}
|
||||
|
||||
|
||||
bool PICloudClient::openDevice() {
|
||||
piCout << "PICloudClient open device" << path();
|
||||
bool op = eth.connect(path(), false);
|
||||
if (op) {
|
||||
CONNECTL(ð, disconnected, [this](bool){opened_ = false;});
|
||||
CONNECTL(ð, connected, [this](){tcp.sendStart(ð);});
|
||||
CONNECTU(ð, threadedReadEvent, this, readed);
|
||||
CONNECTL(ð, disconnected, [this](bool){
|
||||
opened_ = false;
|
||||
eth.close();
|
||||
piCoutObj << "disconnected";// << !isOpened() << isClosed();
|
||||
piMSleep(100);
|
||||
});
|
||||
eth.startThreadedRead();
|
||||
tcp.sendStart(ð);
|
||||
return true;
|
||||
} else {
|
||||
eth.close();
|
||||
return false;
|
||||
}
|
||||
eth.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool PICloudClient::closeDevice() {
|
||||
return eth.close();
|
||||
eth.stop();
|
||||
eth.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int PICloudClient::readDevice(void * read_to, int max_size) {
|
||||
return eth.read(read_to, max_size);
|
||||
piCoutObj << "readDevice";
|
||||
mutex_buff.lock();
|
||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||
int sz = piMini(max_size, buff.size());
|
||||
memcpy(read_to, buff.data(), sz);
|
||||
buff.remove(0, sz);
|
||||
mutex_buff.unlock();
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,5 +91,15 @@ int PICloudClient::writeDevice(const void * data, int max_size) {
|
||||
|
||||
|
||||
void PICloudClient::readed(uchar *data, int size) {
|
||||
|
||||
PIByteArray ba(data, size);
|
||||
mutex_buff.lock();
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
if (hdr.second == tcp.role()) {
|
||||
piCoutObj << "readed" << ba.toHex();
|
||||
buff.append(data, size);
|
||||
cond_buff.notifyOne();
|
||||
}
|
||||
mutex_buff.unlock();
|
||||
while (buff.size() > threadedReadBufferSize()) piMSleep(100);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,27 +20,41 @@
|
||||
#include "picloudserver.h"
|
||||
|
||||
|
||||
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), eth(PIEthernet::TCP_Client) {
|
||||
PIString name = "cloud_server_" + PIString::fromNumber(randomi()%1000);
|
||||
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
|
||||
PIString name = "PCS_" + PIString::fromNumber(randomi()%1000);
|
||||
tcp.setRole(PICloud::TCP::Server);
|
||||
tcp.setServerName(name);
|
||||
setName(name);
|
||||
setName("cloud_server__" + name);
|
||||
}
|
||||
|
||||
|
||||
PICloudServer::~PICloudServer() {
|
||||
for (auto & c : clients) {
|
||||
c.stop();
|
||||
c.close();
|
||||
}
|
||||
stop();
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::setServerName(const PIString & server_name) {
|
||||
setName("cloud_server__" + server_name);
|
||||
tcp.setServerName(server_name);
|
||||
}
|
||||
|
||||
|
||||
bool PICloudServer::openDevice() {
|
||||
piCout << "PICloudServer open device" << path();
|
||||
bool op = eth.connect(path(), false);
|
||||
if (op) {
|
||||
CONNECTL(ð, disconnected, [this](bool){opened_ = false;});
|
||||
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;
|
||||
@@ -51,7 +65,13 @@ bool PICloudServer::openDevice() {
|
||||
|
||||
|
||||
bool PICloudServer::closeDevice() {
|
||||
return eth.close();
|
||||
eth.stop();
|
||||
for (auto & c : clients) {
|
||||
c.stop();
|
||||
c.close();
|
||||
}
|
||||
eth.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -71,10 +91,14 @@ PICloudServer::Client::Client(PICloudServer * srv) : server(srv) {
|
||||
|
||||
|
||||
bool PICloudServer::Client::openDevice() {
|
||||
return server;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::readed(uchar *data, int size) {
|
||||
|
||||
PIByteArray ba(data, size);
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
if (hdr.second == tcp.role()) {
|
||||
piCoutObj << "readed" << ba.toHex();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,24 @@ void PICloud::TCP::sendStart(PIEthernet * eth) {
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendConnected(PIEthernet * eth, uint client_id) {
|
||||
header.type = PICloud::TCP::Connect;
|
||||
PIByteArray ba;
|
||||
ba << header << client_id;
|
||||
eth->send(ba);
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data) {
|
||||
header.type = PICloud::TCP::Data;
|
||||
PIByteArray ba;
|
||||
ba << header;
|
||||
ba.append(data);
|
||||
// piCout << "sendData" << ba.toHex();
|
||||
eth->send(ba);
|
||||
}
|
||||
|
||||
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
||||
ret.first = Invalid;
|
||||
|
||||
Reference in New Issue
Block a user