PICloud connect/disconnect
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user