PICloud small patches
This commit is contained in:
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0)
|
|||||||
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
|
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
|
||||||
project(pip)
|
project(pip)
|
||||||
set(pip_MAJOR 2)
|
set(pip_MAJOR 2)
|
||||||
set(pip_MINOR 27)
|
set(pip_MINOR 28)
|
||||||
set(pip_REVISION 0)
|
set(pip_REVISION 0)
|
||||||
set(pip_SUFFIX )
|
set(pip_SUFFIX )
|
||||||
set(pip_COMPANY SHS)
|
set(pip_COMPANY SHS)
|
||||||
|
|||||||
@@ -4,3 +4,8 @@
|
|||||||
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(ð), tcp(&streampacker) {
|
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(ð), tcp(&streampacker) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIString PICloudBase::serverName() const {
|
||||||
|
return tcp.serverName();
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ PICloudClient::~PICloudClient() {
|
|||||||
eth.close();
|
eth.close();
|
||||||
if (is_connected) {
|
if (is_connected) {
|
||||||
is_connected = false;
|
is_connected = false;
|
||||||
|
disconnected();
|
||||||
cond_buff.notifyOne();
|
cond_buff.notifyOne();
|
||||||
cond_connect.notifyOne();
|
cond_connect.notifyOne();
|
||||||
}
|
}
|
||||||
@@ -86,6 +87,7 @@ bool PICloudClient::openDevice() {
|
|||||||
bool PICloudClient::closeDevice() {
|
bool PICloudClient::closeDevice() {
|
||||||
if (is_connected) {
|
if (is_connected) {
|
||||||
is_connected = false;
|
is_connected = false;
|
||||||
|
disconnected();
|
||||||
cond_buff.notifyOne();
|
cond_buff.notifyOne();
|
||||||
cond_connect.notifyOne();
|
cond_connect.notifyOne();
|
||||||
}
|
}
|
||||||
@@ -122,6 +124,7 @@ void PICloudClient::_readed(PIByteArray & ba) {
|
|||||||
case PICloud::TCP::Connect:
|
case PICloud::TCP::Connect:
|
||||||
if (tcp.parseConnect(ba) == 1) {
|
if (tcp.parseConnect(ba) == 1) {
|
||||||
is_connected = true;
|
is_connected = true;
|
||||||
|
connected();
|
||||||
cond_connect.notifyOne();
|
cond_connect.notifyOne();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -129,6 +132,7 @@ void PICloudClient::_readed(PIByteArray & ba) {
|
|||||||
is_connected = false;
|
is_connected = false;
|
||||||
eth.stop();
|
eth.stop();
|
||||||
eth.close();
|
eth.close();
|
||||||
|
disconnected();
|
||||||
break;
|
break;
|
||||||
case PICloud::TCP::Data:
|
case PICloud::TCP::Data:
|
||||||
if (is_connected) {
|
if (is_connected) {
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ bool PICloudServer::closeDevice() {
|
|||||||
}
|
}
|
||||||
clients_mutex.unlock();
|
clients_mutex.unlock();
|
||||||
eth.close();
|
eth.close();
|
||||||
|
for (auto c : clients_)
|
||||||
|
delete c;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +104,8 @@ int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
|
|||||||
|
|
||||||
|
|
||||||
PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
|
PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
|
||||||
|
setMode(PIIODevice::ReadWrite);
|
||||||
|
setReopenEnabled(false);
|
||||||
is_connected = true;
|
is_connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +136,6 @@ bool PICloudServer::Client::closeDevice() {
|
|||||||
|
|
||||||
|
|
||||||
int PICloudServer::Client::readDevice(void * read_to, int max_size) {
|
int PICloudServer::Client::readDevice(void * read_to, int max_size) {
|
||||||
//piCoutObj << "readDevice";
|
|
||||||
if (!is_connected) return -1;
|
if (!is_connected) return -1;
|
||||||
mutex_buff.lock();
|
mutex_buff.lock();
|
||||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||||
@@ -180,9 +183,10 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
|||||||
clients_mutex.unlock();
|
clients_mutex.unlock();
|
||||||
newConnection(c);
|
newConnection(c);
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case PICloud::TCP::Disconnect: {
|
case PICloud::TCP::Disconnect: {
|
||||||
uint id = tcp.parseDisconnect(ba);
|
uint id = tcp.parseDisconnect(ba);
|
||||||
|
piCoutObj << "remove Client" << id;
|
||||||
clients_mutex.lock();
|
clients_mutex.lock();
|
||||||
Client * oc = index_clients.value(id, nullptr);
|
Client * oc = index_clients.value(id, nullptr);
|
||||||
clients_mutex.unlock();
|
clients_mutex.unlock();
|
||||||
@@ -190,16 +194,16 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
|||||||
oc->is_connected = false;
|
oc->is_connected = false;
|
||||||
oc->close();
|
oc->close();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case PICloud::TCP::Data: {
|
case PICloud::TCP::Data: {
|
||||||
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
|
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
|
||||||
clients_mutex.lock();
|
clients_mutex.lock();
|
||||||
Client * oc = index_clients.value(d.first, nullptr);
|
Client * oc = index_clients.value(d.first, nullptr);
|
||||||
clients_mutex.unlock();
|
clients_mutex.unlock();
|
||||||
|
//piCoutObj << "data for" << d.first << d.second.toHex();
|
||||||
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
|
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
|
||||||
} break;
|
} break;
|
||||||
default:
|
default: break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,8 +41,14 @@ void PICloud::TCP::setRole(PICloud::TCP::Role r) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PICloud::TCP::setServerName(const PIString & server_name) {
|
void PICloud::TCP::setServerName(const PIString & server_name_) {
|
||||||
suuid = PICrypt::hash(server_name);
|
server_name = server_name_;
|
||||||
|
suuid = PICrypt::hash(server_name_);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIString PICloud::TCP::serverName() const {
|
||||||
|
return server_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -28,11 +28,13 @@
|
|||||||
#include "pistreampacker.h"
|
#include "pistreampacker.h"
|
||||||
|
|
||||||
|
|
||||||
class PICloudBase
|
class PIP_CLOUD_EXPORT PICloudBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PICloudBase();
|
PICloudBase();
|
||||||
|
|
||||||
|
PIString serverName() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
PIEthernet eth;
|
PIEthernet eth;
|
||||||
PIStreamPacker streampacker;
|
PIStreamPacker streampacker;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
#include "piconditionvar.h"
|
#include "piconditionvar.h"
|
||||||
|
|
||||||
|
|
||||||
class PIP_CLOUD_EXPORT PICloudClient : public PIIODevice, private PICloudBase
|
class PIP_CLOUD_EXPORT PICloudClient: public PIIODevice, public PICloudBase
|
||||||
{
|
{
|
||||||
PIIODEVICE(PICloudClient)
|
PIIODEVICE(PICloudClient)
|
||||||
public:
|
public:
|
||||||
@@ -39,6 +39,9 @@ public:
|
|||||||
void setKeepConnection(bool on);
|
void setKeepConnection(bool on);
|
||||||
bool isConnected() const {return is_connected;}
|
bool isConnected() const {return is_connected;}
|
||||||
|
|
||||||
|
EVENT(connected)
|
||||||
|
EVENT(disconnected)
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool openDevice();
|
bool openDevice();
|
||||||
bool closeDevice();
|
bool closeDevice();
|
||||||
@@ -46,7 +49,6 @@ protected:
|
|||||||
int writeDevice(const void * data, int size);
|
int writeDevice(const void * data, int size);
|
||||||
DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;}
|
DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EVENT_HANDLER1(void, _readed, PIByteArray &, data);
|
EVENT_HANDLER1(void, _readed, PIByteArray &, data);
|
||||||
PIByteArray buff;
|
PIByteArray buff;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
#include "piconditionvar.h"
|
#include "piconditionvar.h"
|
||||||
|
|
||||||
|
|
||||||
class PIP_CLOUD_EXPORT PICloudServer : public PIIODevice, private PICloudBase
|
class PIP_CLOUD_EXPORT PICloudServer: public PIIODevice, public PICloudBase
|
||||||
{
|
{
|
||||||
PIIODEVICE(PICloudServer)
|
PIIODEVICE(PICloudServer)
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ public:
|
|||||||
TCP(PIStreamPacker * s);
|
TCP(PIStreamPacker * s);
|
||||||
void setRole(Role r);
|
void setRole(Role r);
|
||||||
Role role() const {return (Role)header.role;}
|
Role role() const {return (Role)header.role;}
|
||||||
void setServerName(const PIString & server_name);
|
void setServerName(const PIString & server_name_);
|
||||||
|
PIString serverName() const;
|
||||||
|
|
||||||
void sendStart();
|
void sendStart();
|
||||||
void sendConnected(uint client_id);
|
void sendConnected(uint client_id);
|
||||||
@@ -81,6 +82,7 @@ private:
|
|||||||
|
|
||||||
Header header;
|
Header header;
|
||||||
PIByteArray suuid;
|
PIByteArray suuid;
|
||||||
|
PIString server_name;
|
||||||
PIStreamPacker * streampacker;
|
PIStreamPacker * streampacker;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -389,6 +389,12 @@ bool PIIODevice::close() {
|
|||||||
return !opened_;
|
return !opened_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int PIIODevice::write(PIByteArray data) {
|
||||||
|
if (isOpened())
|
||||||
|
return writeDevice(data.data(), data.size_s());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PIIODevice::configure(const PIString & config_file, const PIString & section, bool parent_section) {
|
bool PIIODevice::configure(const PIString & config_file, const PIString & section, bool parent_section) {
|
||||||
PIConfig conf(config_file, PIIODevice::ReadOnly);
|
PIConfig conf(config_file, PIIODevice::ReadOnly);
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ public:
|
|||||||
bool open(DeviceMode _mode);
|
bool open(DeviceMode _mode);
|
||||||
EVENT_HANDLER2(bool, open, const PIString &, _path, DeviceMode, _mode);
|
EVENT_HANDLER2(bool, open, const PIString &, _path, DeviceMode, _mode);
|
||||||
EVENT_HANDLER(bool, close);
|
EVENT_HANDLER(bool, close);
|
||||||
EVENT_HANDLER1(int, write, PIByteArray, data) {return writeDevice(data.data(), data.size_s());}
|
EVENT_HANDLER1(int, write, PIByteArray, data);
|
||||||
|
|
||||||
EVENT_VHANDLER(void, flush) {;}
|
EVENT_VHANDLER(void, flush) {;}
|
||||||
|
|
||||||
|
|||||||
20
main.cpp
20
main.cpp
@@ -1,5 +1,16 @@
|
|||||||
#include "pip.h"
|
#include "pip.h"
|
||||||
|
|
||||||
|
class A: public PIObject {
|
||||||
|
PIOBJECT(A)
|
||||||
|
public:
|
||||||
|
EVENT_HANDLER1(void, eh, PIByteArray, i) {piCout << "eh" << i.toHex();}
|
||||||
|
};
|
||||||
|
|
||||||
|
class B: public PIObject {
|
||||||
|
PIOBJECT(B)
|
||||||
|
public:
|
||||||
|
EVENT2(eh, PIByteArray, i, PIString, str);
|
||||||
|
};
|
||||||
|
|
||||||
template <typename S, typename T, typename std::enable_if<
|
template <typename S, typename T, typename std::enable_if<
|
||||||
std::is_same<T, PIVector<S>>::value
|
std::is_same<T, PIVector<S>>::value
|
||||||
@@ -10,7 +21,12 @@ template <typename S, typename T, typename std::enable_if<
|
|||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
PIDeque<int> x;
|
A a;
|
||||||
|
B b;
|
||||||
|
CONNECTU_QUEUED(&b, eh, &a, eh, &a);
|
||||||
|
b.eh(PIByteArray::fromHex("102030"), "str");
|
||||||
|
a.maybeCallQueuedEvents();
|
||||||
|
/*PIDeque<int> x;
|
||||||
x.resize(16, [](size_t i) {return i+1;});
|
x.resize(16, [](size_t i) {return i+1;});
|
||||||
piCout << x;
|
piCout << x;
|
||||||
PIDeque<PIDeque<int>> m = x.reshape(2,8);
|
PIDeque<PIDeque<int>> m = x.reshape(2,8);
|
||||||
@@ -21,6 +37,6 @@ int main() {
|
|||||||
PIDeque<int> y;
|
PIDeque<int> y;
|
||||||
y = m.reshape<int>();
|
y = m.reshape<int>();
|
||||||
piCout << y;
|
piCout << y;
|
||||||
piCout << m.reshape<int>(PIDeque<int>::byColumn);
|
piCout << m.reshape<int>(PIDeque<int>::byColumn);*/
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
|||||||
//piCoutObj << "INVALID client" << client;
|
//piCoutObj << "INVALID client" << client;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//piCoutObj << "remove client" << client;
|
piCoutObj << "remove client" << client->clientId();
|
||||||
map_mutex.lock();
|
map_mutex.lock();
|
||||||
clients.removeOne(client);
|
clients.removeOne(client);
|
||||||
CloudServer * cs = index_c_servers.value(client, nullptr);
|
CloudServer * cs = index_c_servers.value(client, nullptr);
|
||||||
|
|||||||
Reference in New Issue
Block a user