code format

This commit is contained in:
2022-12-14 14:13:52 +03:00
parent 430a41fefc
commit c2b8a8d6da
297 changed files with 27331 additions and 24162 deletions

View File

@@ -1,7 +1,7 @@
#include "picloudbase.h"
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(&eth), tcp(&streampacker) {
PICloudBase::PICloudBase(): eth(PIEthernet::TCP_Client), streampacker(&eth), tcp(&streampacker) {
eth.setDebug(false);
}

View File

@@ -1,187 +1,190 @@
/*
PIP - Platform Independent Primitives
PICloud Client
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "picloudclient.h"
#include "picloudtcp.h"
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
tcp.setRole(PICloud::TCP::Client);
setThreadedReadBufferSize(eth.threadedReadBufferSize());
setName("cloud_client");
is_connected = false;
is_deleted = false;
// setReopenEnabled(false);
CONNECTL(&eth, connected, [this](){opened_ = true; tcp.sendStart();});
CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){
if (is_deleted) return;
bool need_disconn = is_connected;
//piCoutObj << "eth disconnected";
eth.stop();
opened_ = false;
internalDisconnect();
if (need_disconn) disconnected();
//piCoutObj << "eth disconnected done";
});
}
PICloudClient::~PICloudClient() {
//piCoutObj << "~PICloudClient() ..." << this;
is_deleted = true;
stopAndWait();
close();
internalDisconnect();
//piCoutObj << "~PICloudClient() done" << this;
}
void PICloudClient::setServerName(const PIString & server_name) {
setName("cloud_client__" + server_name);
tcp.setServerName(server_name);
}
void PICloudClient::setKeepConnection(bool on) {
eth.setParameter(PIEthernet::KeepConnection, on);
}
void PICloudClient::interrupt() {
cond_buff.notifyOne();
cond_connect.notifyOne();
}
bool PICloudClient::openDevice() {
//piCoutObj << "open";// << path();
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
mutex_connect.lock();
eth.startThreadedRead();
//piCoutObj << "connecting...";
bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout());
//piCoutObj << "conn_ok" << conn_ok << is_connected;
mutex_connect.unlock();
if (!conn_ok) {
mutex_connect.lock();
eth.stopAndWait();
eth.close();
mutex_connect.unlock();
}
return is_connected;
} else {
//eth.close();
return false;
}
}
bool PICloudClient::closeDevice() {
//PIThread::stop();
if (is_connected) {
internalDisconnect();
}
eth.stopAndWait();
eth.close();
return true;
}
ssize_t PICloudClient::readDevice(void * read_to, ssize_t max_size) {
if (is_deleted || max_size <= 0) return -1;
//piCoutObj << "readDevice ...";
if (!is_connected && eth.isClosed()) openDevice();
ssize_t sz = -1;
mutex_buff.lock();
if (is_connected) {
if (buff.isEmpty()) {
sz = 0;
} else {
sz = piMin<ssize_t>(max_size, buff.size_s());
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
}
if (sz == 0) cond_buff.wait(mutex_buff);
}
mutex_buff.unlock();
if (!is_connected) opened_ = false;
//piCoutObj << "readDevice done" << sz;
return sz;
}
ssize_t PICloudClient::writeDevice(const void * data, ssize_t size) {
if (is_deleted || !is_connected) return -1;
//piCoutObj << "writeDevice" << size;
return tcp.sendData(PIByteArray(data, size));
}
void PICloudClient::internalDisconnect() {
//piCoutObj << "internalDisconnect";
is_connected = false;
cond_buff.notifyOne();
cond_connect.notifyOne();
streampacker.clear();
buff.clear();
}
void PICloudClient::_readed(PIByteArray & ba) {
if (is_deleted) return;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
//piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second;
if (hdr.second == tcp.role()) {
switch (hdr.first) {
case PICloud::TCP::Connect:
if (tcp.parseConnect(ba) == 1) {
mutex_connect.lock();
is_connected = true;
mutex_connect.unlock();
cond_connect.notifyOne();
connected();
}
break;
case PICloud::TCP::Disconnect:
eth.stop();
opened_ = false;
eth.close();
break;
case PICloud::TCP::Data:
if (is_connected) {
mutex_buff.lock();
if (buff.size_s() > threadedReadBufferSize()) {
piCoutObj << "Error: buffer overflow, drop" << ba.size() << "bytes";
mutex_buff.unlock();
return;
}
buff.append(ba);
mutex_buff.unlock();
cond_buff.notifyOne();
}
break;
default:
break;
}
//piCoutObj << "readed" << ba.toHex();
}
//piCoutObj << "_readed done";
}
/*
PIP - Platform Independent Primitives
PICloud Client
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "picloudclient.h"
#include "picloudtcp.h"
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode), PICloudBase() {
tcp.setRole(PICloud::TCP::Client);
setThreadedReadBufferSize(eth.threadedReadBufferSize());
setName("cloud_client");
is_connected = false;
is_deleted = false;
// setReopenEnabled(false);
CONNECTL(&eth, connected, [this]() {
opened_ = true;
tcp.sendStart();
});
CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool) {
if (is_deleted) return;
bool need_disconn = is_connected;
// piCoutObj << "eth disconnected";
eth.stop();
opened_ = false;
internalDisconnect();
if (need_disconn) disconnected();
// piCoutObj << "eth disconnected done";
});
}
PICloudClient::~PICloudClient() {
// piCoutObj << "~PICloudClient() ..." << this;
is_deleted = true;
stopAndWait();
close();
internalDisconnect();
// piCoutObj << "~PICloudClient() done" << this;
}
void PICloudClient::setServerName(const PIString & server_name) {
setName("cloud_client__" + server_name);
tcp.setServerName(server_name);
}
void PICloudClient::setKeepConnection(bool on) {
eth.setParameter(PIEthernet::KeepConnection, on);
}
void PICloudClient::interrupt() {
cond_buff.notifyOne();
cond_connect.notifyOne();
}
bool PICloudClient::openDevice() {
// piCoutObj << "open";// << path();
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
mutex_connect.lock();
eth.startThreadedRead();
// piCoutObj << "connecting...";
bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout());
// piCoutObj << "conn_ok" << conn_ok << is_connected;
mutex_connect.unlock();
if (!conn_ok) {
mutex_connect.lock();
eth.stopAndWait();
eth.close();
mutex_connect.unlock();
}
return is_connected;
} else {
// eth.close();
return false;
}
}
bool PICloudClient::closeDevice() {
// PIThread::stop();
if (is_connected) {
internalDisconnect();
}
eth.stopAndWait();
eth.close();
return true;
}
ssize_t PICloudClient::readDevice(void * read_to, ssize_t max_size) {
if (is_deleted || max_size <= 0) return -1;
// piCoutObj << "readDevice ...";
if (!is_connected && eth.isClosed()) openDevice();
ssize_t sz = -1;
mutex_buff.lock();
if (is_connected) {
if (buff.isEmpty()) {
sz = 0;
} else {
sz = piMin<ssize_t>(max_size, buff.size_s());
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
}
if (sz == 0) cond_buff.wait(mutex_buff);
}
mutex_buff.unlock();
if (!is_connected) opened_ = false;
// piCoutObj << "readDevice done" << sz;
return sz;
}
ssize_t PICloudClient::writeDevice(const void * data, ssize_t size) {
if (is_deleted || !is_connected) return -1;
// piCoutObj << "writeDevice" << size;
return tcp.sendData(PIByteArray(data, size));
}
void PICloudClient::internalDisconnect() {
// piCoutObj << "internalDisconnect";
is_connected = false;
cond_buff.notifyOne();
cond_connect.notifyOne();
streampacker.clear();
buff.clear();
}
void PICloudClient::_readed(PIByteArray & ba) {
if (is_deleted) return;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
// piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second;
if (hdr.second == tcp.role()) {
switch (hdr.first) {
case PICloud::TCP::Connect:
if (tcp.parseConnect(ba) == 1) {
mutex_connect.lock();
is_connected = true;
mutex_connect.unlock();
cond_connect.notifyOne();
connected();
}
break;
case PICloud::TCP::Disconnect:
eth.stop();
opened_ = false;
eth.close();
break;
case PICloud::TCP::Data:
if (is_connected) {
mutex_buff.lock();
if (buff.size_s() > threadedReadBufferSize()) {
piCoutObj << "Error: buffer overflow, drop" << ba.size() << "bytes";
mutex_buff.unlock();
return;
}
buff.append(ba);
mutex_buff.unlock();
cond_buff.notifyOne();
}
break;
default: break;
}
// piCoutObj << "readed" << ba.toHex();
}
// piCoutObj << "_readed done";
}

View File

@@ -1,304 +1,304 @@
/*
PIP - Platform Independent Primitives
PICloud Server
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "picloudserver.h"
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
PIString server_name = "PCS_" + PIString::fromNumber(randomi()%1000);
tcp.setRole(PICloud::TCP::Server);
tcp.setServerName(server_name);
setName("cloud_server__" + server_name);
is_deleted = false;
eth.setReopenEnabled(false);
setThreadedReadBufferSize(eth.threadedReadBufferSize());
CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, connected, [this](){
open_mutex.lock();
opened_ = true;
cvar.notifyOne();
open_mutex.unlock();
//piCoutObj << "connected";
tcp.sendStart();
});
CONNECTL(&eth, disconnected, [this](bool){
if (is_deleted) return;
//piCoutObj << "disconnected";
clients_mutex.lock();
for (auto c : clients_) {
c->is_connected = false;
c->close();
}
removed_clients_.append(clients_);
clients_.clear();
index_clients.clear();
clients_mutex.unlock();
open_mutex.lock();
opened_ = false;
cvar.notifyOne();
open_mutex.unlock();
ping_timer.stop();
});
CONNECTL(&ping_timer, tickEvent, [this] (void *, int){
if (eth.isConnected()) tcp.sendPing();
});
}
PICloudServer::~PICloudServer() {
//piCoutObj << "~PICloudServer ..." << this;
is_deleted = true;
stop();
close();
waitThreadedReadFinished();
//piCout << "wait";
while(removed_clients_.isNotEmpty()) {
Client * c = removed_clients_.take_back();
delete c;
}
//piCoutObj << "~PICloudServer done" << this;
}
void PICloudServer::setServerName(const PIString & server_name) {
setName("cloud_server__" + server_name);
tcp.setServerName(server_name);
}
PIVector<PICloudServer::Client *> PICloudServer::clients() const {
PIMutexLocker _ml(clients_mutex);
return clients_;
}
bool PICloudServer::openDevice() {
//piCout << "PICloudServer open device" << path();
if (is_deleted) return false;
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
eth.startThreadedRead();
ping_timer.start(5000);
return true;
} else {
ping_timer.stop();
eth.close();
return false;
}
}
bool PICloudServer::closeDevice() {
//piCoutObj << "closeDevice" << this;
eth.stopAndWait();
ping_timer.stop();
eth.close();
cvar.notifyOne();
clients_mutex.lock();
for (auto c : clients_) {
c->is_connected = false;
c->close();
}
removed_clients_.append(clients_);
clients_.clear();
index_clients.clear();
clients_mutex.unlock();
return true;
}
ssize_t PICloudServer::readDevice(void * read_to, ssize_t max_size) {
if (is_deleted) return -1;
//piCoutObj << "readDevice";
open_mutex.lock();
if (isOpened()) cvar.wait(open_mutex);
open_mutex.unlock();
//piCoutObj << "opened_ = " << opened_;
//else piMSleep(eth.readTimeout());
return -1;
}
ssize_t PICloudServer::writeDevice(const void * data, ssize_t max_size) {
//piCoutObj << "writeDevice";
return -1;
}
void PICloudServer::interrupt() {
eth.interrupt();
cvar.notifyOne();
}
void PICloudServer::clientDisconnect(uint client_id) {
tcp.sendDisconnected(client_id);
}
int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
if (!opened_) return -1;
return tcp.sendData(data, client_id);
}
PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
setMode(PIIODevice::ReadWrite);
setReopenEnabled(false);
setThreadedReadBufferSize(server->threadedReadBufferSize());
is_connected = true;
}
PICloudServer::Client::~Client() {
//piCoutObj << "~PICloudServer::Client..." << this;
close();
stopAndWait();
//piCoutObj << "~PICloudServer::Client done" << this;
}
bool PICloudServer::Client::openDevice() {
return is_connected;
}
bool PICloudServer::Client::closeDevice() {
//piCoutObj << "closeDevice" << this;
if (is_connected) {
server->clientDisconnect(client_id);
is_connected = false;
}
cond_buff.notifyOne();
return true;
}
ssize_t PICloudServer::Client::readDevice(void * read_to, ssize_t max_size) {
if (!is_connected) return -1;
ssize_t sz = -1;
mutex_buff.lock();
if (is_connected) {
if (buff.isEmpty()) {
sz = 0;
} else {
sz = piMini(max_size, buff.size());
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
}
if (sz == 0) cond_buff.wait(mutex_buff);
}
mutex_buff.unlock();
return sz;
}
ssize_t PICloudServer::Client::writeDevice(const void * data, ssize_t size) {
if (!is_connected) return -1;
return server->sendData(PIByteArray(data, size), client_id);
}
void PICloudServer::Client::interrupt() {
cond_buff.notifyOne();
}
void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
if (!is_connected) return;
mutex_buff.lock();
if (buff.size_s() > threadedReadBufferSize()) {
piCoutObj << "Error: buffer overflow, drop" << ba.size() << "bytes";
mutex_buff.unlock();
return;
}
buff.append(ba);
cond_buff.notifyOne();
mutex_buff.unlock();
}
void PICloudServer::_readed(PIByteArray & ba) {
if (is_deleted) return;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
if (hdr.second == tcp.role()) {
switch (hdr.first) {
case PICloud::TCP::Connect: {
uint id = tcp.parseConnect(ba);
clients_mutex.lock();
Client * oc = index_clients.value(id, nullptr);
clients_mutex.unlock();
if (oc) {
piCoutObj << "Warning: reject client with duplicated ID";
tcp.sendDisconnected(id);
} else {
Client * c = new Client(this, id);
//piCoutObj << "new Client" << id << c;
CONNECT1(void, PIObject *, c, deleted, this, clientDeleted);
clients_mutex.lock();
clients_ << c;
index_clients.insert(id, c);
clients_mutex.unlock();
newConnection(c);
}
} break;
case PICloud::TCP::Disconnect: {
uint id = tcp.parseDisconnect(ba);
//piCoutObj << "Close on logic";
clients_mutex.lock();
Client * oc = index_clients.take(id, nullptr);
clients_.removeOne(oc);
clients_mutex.unlock();
if (oc) {
oc->stopAndWait();
oc->is_connected = false;
oc->close();
removed_clients_ << oc;
//delete oc;
}
} break;
case PICloud::TCP::Data: {
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
clients_mutex.lock();
Client * oc = index_clients.value(d.first, nullptr);
clients_mutex.unlock();
//piCoutObj << "data for" << d.first << d.second.size();
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
} break;
default: break;
}
}
}
void PICloudServer::clientDeleted(PIObject * o) {
PICloudServer::Client * c = (PICloudServer::Client*)o;
//piCoutObj << "clientDeleted" << c;
clients_mutex.lock();
clients_.removeOne(c);
removed_clients_.removeAll(c);
auto it = index_clients.makeIterator();
while (it.next()) {
if (it.value() == c) {
index_clients.remove(it.key());
break;
}
}
clients_mutex.unlock();
}
/*
PIP - Platform Independent Primitives
PICloud Server
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "picloudserver.h"
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode), PICloudBase() {
PIString server_name = "PCS_" + PIString::fromNumber(randomi() % 1000);
tcp.setRole(PICloud::TCP::Server);
tcp.setServerName(server_name);
setName("cloud_server__" + server_name);
is_deleted = false;
eth.setReopenEnabled(false);
setThreadedReadBufferSize(eth.threadedReadBufferSize());
CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, connected, [this]() {
open_mutex.lock();
opened_ = true;
cvar.notifyOne();
open_mutex.unlock();
// piCoutObj << "connected";
tcp.sendStart();
});
CONNECTL(&eth, disconnected, [this](bool) {
if (is_deleted) return;
// piCoutObj << "disconnected";
clients_mutex.lock();
for (auto c: clients_) {
c->is_connected = false;
c->close();
}
removed_clients_.append(clients_);
clients_.clear();
index_clients.clear();
clients_mutex.unlock();
open_mutex.lock();
opened_ = false;
cvar.notifyOne();
open_mutex.unlock();
ping_timer.stop();
});
CONNECTL(&ping_timer, tickEvent, [this](void *, int) {
if (eth.isConnected()) tcp.sendPing();
});
}
PICloudServer::~PICloudServer() {
// piCoutObj << "~PICloudServer ..." << this;
is_deleted = true;
stop();
close();
waitThreadedReadFinished();
// piCout << "wait";
while (removed_clients_.isNotEmpty()) {
Client * c = removed_clients_.take_back();
delete c;
}
// piCoutObj << "~PICloudServer done" << this;
}
void PICloudServer::setServerName(const PIString & server_name) {
setName("cloud_server__" + server_name);
tcp.setServerName(server_name);
}
PIVector<PICloudServer::Client *> PICloudServer::clients() const {
PIMutexLocker _ml(clients_mutex);
return clients_;
}
bool PICloudServer::openDevice() {
// piCout << "PICloudServer open device" << path();
if (is_deleted) return false;
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
eth.startThreadedRead();
ping_timer.start(5000);
return true;
} else {
ping_timer.stop();
eth.close();
return false;
}
}
bool PICloudServer::closeDevice() {
// piCoutObj << "closeDevice" << this;
eth.stopAndWait();
ping_timer.stop();
eth.close();
cvar.notifyOne();
clients_mutex.lock();
for (auto c: clients_) {
c->is_connected = false;
c->close();
}
removed_clients_.append(clients_);
clients_.clear();
index_clients.clear();
clients_mutex.unlock();
return true;
}
ssize_t PICloudServer::readDevice(void * read_to, ssize_t max_size) {
if (is_deleted) return -1;
// piCoutObj << "readDevice";
open_mutex.lock();
if (isOpened()) cvar.wait(open_mutex);
open_mutex.unlock();
// piCoutObj << "opened_ = " << opened_;
// else piMSleep(eth.readTimeout());
return -1;
}
ssize_t PICloudServer::writeDevice(const void * data, ssize_t max_size) {
// piCoutObj << "writeDevice";
return -1;
}
void PICloudServer::interrupt() {
eth.interrupt();
cvar.notifyOne();
}
void PICloudServer::clientDisconnect(uint client_id) {
tcp.sendDisconnected(client_id);
}
int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
if (!opened_) return -1;
return tcp.sendData(data, client_id);
}
PICloudServer::Client::Client(PICloudServer * srv, uint id): server(srv), client_id(id) {
setMode(PIIODevice::ReadWrite);
setReopenEnabled(false);
setThreadedReadBufferSize(server->threadedReadBufferSize());
is_connected = true;
}
PICloudServer::Client::~Client() {
// piCoutObj << "~PICloudServer::Client..." << this;
close();
stopAndWait();
// piCoutObj << "~PICloudServer::Client done" << this;
}
bool PICloudServer::Client::openDevice() {
return is_connected;
}
bool PICloudServer::Client::closeDevice() {
// piCoutObj << "closeDevice" << this;
if (is_connected) {
server->clientDisconnect(client_id);
is_connected = false;
}
cond_buff.notifyOne();
return true;
}
ssize_t PICloudServer::Client::readDevice(void * read_to, ssize_t max_size) {
if (!is_connected) return -1;
ssize_t sz = -1;
mutex_buff.lock();
if (is_connected) {
if (buff.isEmpty()) {
sz = 0;
} else {
sz = piMini(max_size, buff.size());
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
}
if (sz == 0) cond_buff.wait(mutex_buff);
}
mutex_buff.unlock();
return sz;
}
ssize_t PICloudServer::Client::writeDevice(const void * data, ssize_t size) {
if (!is_connected) return -1;
return server->sendData(PIByteArray(data, size), client_id);
}
void PICloudServer::Client::interrupt() {
cond_buff.notifyOne();
}
void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
if (!is_connected) return;
mutex_buff.lock();
if (buff.size_s() > threadedReadBufferSize()) {
piCoutObj << "Error: buffer overflow, drop" << ba.size() << "bytes";
mutex_buff.unlock();
return;
}
buff.append(ba);
cond_buff.notifyOne();
mutex_buff.unlock();
}
void PICloudServer::_readed(PIByteArray & ba) {
if (is_deleted) return;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
if (hdr.second == tcp.role()) {
switch (hdr.first) {
case PICloud::TCP::Connect: {
uint id = tcp.parseConnect(ba);
clients_mutex.lock();
Client * oc = index_clients.value(id, nullptr);
clients_mutex.unlock();
if (oc) {
piCoutObj << "Warning: reject client with duplicated ID";
tcp.sendDisconnected(id);
} else {
Client * c = new Client(this, id);
// piCoutObj << "new Client" << id << c;
CONNECT1(void, PIObject *, c, deleted, this, clientDeleted);
clients_mutex.lock();
clients_ << c;
index_clients.insert(id, c);
clients_mutex.unlock();
newConnection(c);
}
} break;
case PICloud::TCP::Disconnect: {
uint id = tcp.parseDisconnect(ba);
// piCoutObj << "Close on logic";
clients_mutex.lock();
Client * oc = index_clients.take(id, nullptr);
clients_.removeOne(oc);
clients_mutex.unlock();
if (oc) {
oc->stopAndWait();
oc->is_connected = false;
oc->close();
removed_clients_ << oc;
// delete oc;
}
} break;
case PICloud::TCP::Data: {
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
clients_mutex.lock();
Client * oc = index_clients.value(d.first, nullptr);
clients_mutex.unlock();
// piCoutObj << "data for" << d.first << d.second.size();
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
} break;
default: break;
}
}
}
void PICloudServer::clientDeleted(PIObject * o) {
PICloudServer::Client * c = (PICloudServer::Client *)o;
// piCoutObj << "clientDeleted" << c;
clients_mutex.lock();
clients_.removeOne(c);
removed_clients_.removeAll(c);
auto it = index_clients.makeIterator();
while (it.next()) {
if (it.value() == c) {
index_clients.remove(it.key());
break;
}
}
clients_mutex.unlock();
}

View File

@@ -1,181 +1,184 @@
/*
PIP - Platform Independent Primitives
PICloud TCP transport
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "picloudtcp.h"
#include "picrypt.h"
#include "pichunkstream.h"
#include "piethernet.h"
#include "pistreampacker.h"
const char hash_cloud_key[] = "_picloud_";
PICloud::TCP::Header::Header() {
version = Version_2;
}
PICloud::TCP::TCP(PIStreamPacker * s) : streampacker(s) {
streampacker->setMaxPacketSize(63*1024);
}
void PICloud::TCP::setRole(PICloud::TCP::Role r) {
header.role = r;
}
void PICloud::TCP::setServerName(const PIString & server_name_) {
server_name = server_name_;
suuid = PICrypt::hash(PIByteArray(server_name_.data(), server_name_.size()), (const unsigned char *)hash_cloud_key, sizeof(hash_cloud_key));
}
PIString PICloud::TCP::serverName() const {
return server_name;
}
void PICloud::TCP::sendStart() {
//piCout << "sendStart";
if (suuid.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, server not set, invoke setServerName first";
return;
}
header.type = PICloud::TCP::Connect;
PIByteArray ba;
ba << header;
ba.append(suuid);
//mutex_send.lock();
streampacker->send(ba);
//mutex_send.unlock();
}
void PICloud::TCP::sendConnected(uint client_id) {
header.type = PICloud::TCP::Connect;
PIByteArray ba;
ba << header << client_id;
// mutex_send.lock();
streampacker->send(ba);
// mutex_send.unlock();
}
void PICloud::TCP::sendDisconnected(uint client_id) {
header.type = PICloud::TCP::Disconnect;
PIByteArray ba;
ba << header << client_id;
// mutex_send.lock();
streampacker->send(ba);
// mutex_send.unlock();
}
int PICloud::TCP::sendData(const PIByteArray & data) {
header.type = PICloud::TCP::Data;
PIByteArray ba;
ba << header;
ba.append(data);
// piCout << "[PICloud::TCP] sendData" << ba.toHex();
mutex_send.lock();
streampacker->send(ba);
mutex_send.unlock();
return data.size_s();
}
int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) {
header.type = PICloud::TCP::Data;
PIByteArray ba;
ba << header << client_id;
ba.append(data);
mutex_send.lock();
streampacker->send(ba);
mutex_send.unlock();
return data.size_s();
}
void PICloud::TCP::sendPing() {
header.type = PICloud::TCP::Ping;
PIByteArray ba;
ba << header;
ba.append(suuid);
mutex_send.lock();
streampacker->send(ba);
mutex_send.unlock();
}
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
ret.first = InvalidType;
ret.second = InvalidRole;
if (ba.size() < sizeof(Header)) return ret;
PICloud::TCP::Header hdr;
ba >> hdr;
if (hdr.version != header.version) {
piCout << "[PICloud]" << "invalid PICloud::TCP version!";
return ret;
}
ret.first = (Type)hdr.type;
ret.second = (Role)hdr.role;
return ret;
}
bool PICloud::TCP::canParseData(PIByteArray & ba) {
return header.role == Client;
}
PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
PIPair<uint, PIByteArray> ret;
ret.first = 0;
if (header.role == Server) {
ba >> ret.first;
ret.second.swap(ba);
}
return ret;
}
PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
if (ba.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, invalid server uuid";
return PIByteArray();
} else {
return ba;
}
}
uint PICloud::TCP::parseConnect(PIByteArray & ba) {
uint ret = 0;
ba >> ret;
return ret;
}
uint PICloud::TCP::parseDisconnect(PIByteArray & ba) {
uint ret = 0;
ba >> ret;
return ret;
}
/*
PIP - Platform Independent Primitives
PICloud TCP transport
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "picloudtcp.h"
#include "pichunkstream.h"
#include "picrypt.h"
#include "piethernet.h"
#include "pistreampacker.h"
const char hash_cloud_key[] = "_picloud_";
PICloud::TCP::Header::Header() {
version = Version_2;
}
PICloud::TCP::TCP(PIStreamPacker * s): streampacker(s) {
streampacker->setMaxPacketSize(63 * 1024);
}
void PICloud::TCP::setRole(PICloud::TCP::Role r) {
header.role = r;
}
void PICloud::TCP::setServerName(const PIString & server_name_) {
server_name = server_name_;
suuid =
PICrypt::hash(PIByteArray(server_name_.data(), server_name_.size()), (const unsigned char *)hash_cloud_key, sizeof(hash_cloud_key));
}
PIString PICloud::TCP::serverName() const {
return server_name;
}
void PICloud::TCP::sendStart() {
// piCout << "sendStart";
if (suuid.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, server not set, invoke setServerName first";
return;
}
header.type = PICloud::TCP::Connect;
PIByteArray ba;
ba << header;
ba.append(suuid);
// mutex_send.lock();
streampacker->send(ba);
// mutex_send.unlock();
}
void PICloud::TCP::sendConnected(uint client_id) {
header.type = PICloud::TCP::Connect;
PIByteArray ba;
ba << header << client_id;
// mutex_send.lock();
streampacker->send(ba);
// mutex_send.unlock();
}
void PICloud::TCP::sendDisconnected(uint client_id) {
header.type = PICloud::TCP::Disconnect;
PIByteArray ba;
ba << header << client_id;
// mutex_send.lock();
streampacker->send(ba);
// mutex_send.unlock();
}
int PICloud::TCP::sendData(const PIByteArray & data) {
header.type = PICloud::TCP::Data;
PIByteArray ba;
ba << header;
ba.append(data);
// piCout << "[PICloud::TCP] sendData" << ba.toHex();
mutex_send.lock();
streampacker->send(ba);
mutex_send.unlock();
return data.size_s();
}
int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) {
header.type = PICloud::TCP::Data;
PIByteArray ba;
ba << header << client_id;
ba.append(data);
mutex_send.lock();
streampacker->send(ba);
mutex_send.unlock();
return data.size_s();
}
void PICloud::TCP::sendPing() {
header.type = PICloud::TCP::Ping;
PIByteArray ba;
ba << header;
ba.append(suuid);
mutex_send.lock();
streampacker->send(ba);
mutex_send.unlock();
}
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
ret.first = InvalidType;
ret.second = InvalidRole;
if (ba.size() < sizeof(Header)) return ret;
PICloud::TCP::Header hdr;
ba >> hdr;
if (hdr.version != header.version) {
piCout << "[PICloud]"
<< "invalid PICloud::TCP version!";
return ret;
}
ret.first = (Type)hdr.type;
ret.second = (Role)hdr.role;
return ret;
}
bool PICloud::TCP::canParseData(PIByteArray & ba) {
return header.role == Client;
}
PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
PIPair<uint, PIByteArray> ret;
ret.first = 0;
if (header.role == Server) {
ba >> ret.first;
ret.second.swap(ba);
}
return ret;
}
PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
if (ba.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, invalid server uuid";
return PIByteArray();
} else {
return ba;
}
}
uint PICloud::TCP::parseConnect(PIByteArray & ba) {
uint ret = 0;
ba >> ret;
return ret;
}
uint PICloud::TCP::parseDisconnect(PIByteArray & ba) {
uint ret = 0;
ba >> ret;
return ret;
}