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
|
||||
project(pip)
|
||||
set(pip_MAJOR 2)
|
||||
set(pip_MINOR 27)
|
||||
set(pip_MINOR 28)
|
||||
set(pip_REVISION 0)
|
||||
set(pip_SUFFIX )
|
||||
set(pip_COMPANY SHS)
|
||||
|
||||
@@ -4,3 +4,8 @@
|
||||
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(ð), tcp(&streampacker) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
PIString PICloudBase::serverName() const {
|
||||
return tcp.serverName();
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ PICloudClient::~PICloudClient() {
|
||||
eth.close();
|
||||
if (is_connected) {
|
||||
is_connected = false;
|
||||
disconnected();
|
||||
cond_buff.notifyOne();
|
||||
cond_connect.notifyOne();
|
||||
}
|
||||
@@ -86,6 +87,7 @@ bool PICloudClient::openDevice() {
|
||||
bool PICloudClient::closeDevice() {
|
||||
if (is_connected) {
|
||||
is_connected = false;
|
||||
disconnected();
|
||||
cond_buff.notifyOne();
|
||||
cond_connect.notifyOne();
|
||||
}
|
||||
@@ -122,6 +124,7 @@ void PICloudClient::_readed(PIByteArray & ba) {
|
||||
case PICloud::TCP::Connect:
|
||||
if (tcp.parseConnect(ba) == 1) {
|
||||
is_connected = true;
|
||||
connected();
|
||||
cond_connect.notifyOne();
|
||||
}
|
||||
break;
|
||||
@@ -129,6 +132,7 @@ void PICloudClient::_readed(PIByteArray & ba) {
|
||||
is_connected = false;
|
||||
eth.stop();
|
||||
eth.close();
|
||||
disconnected();
|
||||
break;
|
||||
case PICloud::TCP::Data:
|
||||
if (is_connected) {
|
||||
|
||||
@@ -74,6 +74,8 @@ bool PICloudServer::closeDevice() {
|
||||
}
|
||||
clients_mutex.unlock();
|
||||
eth.close();
|
||||
for (auto c : clients_)
|
||||
delete c;
|
||||
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) {
|
||||
setMode(PIIODevice::ReadWrite);
|
||||
setReopenEnabled(false);
|
||||
is_connected = true;
|
||||
}
|
||||
|
||||
@@ -132,7 +136,6 @@ bool PICloudServer::Client::closeDevice() {
|
||||
|
||||
|
||||
int PICloudServer::Client::readDevice(void * read_to, int max_size) {
|
||||
//piCoutObj << "readDevice";
|
||||
if (!is_connected) return -1;
|
||||
mutex_buff.lock();
|
||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||
@@ -180,9 +183,10 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
||||
clients_mutex.unlock();
|
||||
newConnection(c);
|
||||
}
|
||||
} break;
|
||||
} break;
|
||||
case PICloud::TCP::Disconnect: {
|
||||
uint id = tcp.parseDisconnect(ba);
|
||||
piCoutObj << "remove Client" << id;
|
||||
clients_mutex.lock();
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
clients_mutex.unlock();
|
||||
@@ -190,16 +194,16 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
||||
oc->is_connected = false;
|
||||
oc->close();
|
||||
}
|
||||
} break;
|
||||
} 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.toHex();
|
||||
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
} break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,14 @@ void PICloud::TCP::setRole(PICloud::TCP::Role r) {
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::setServerName(const PIString & server_name) {
|
||||
suuid = PICrypt::hash(server_name);
|
||||
void PICloud::TCP::setServerName(const PIString & 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"
|
||||
|
||||
|
||||
class PICloudBase
|
||||
class PIP_CLOUD_EXPORT PICloudBase
|
||||
{
|
||||
public:
|
||||
PICloudBase();
|
||||
|
||||
PIString serverName() const;
|
||||
|
||||
protected:
|
||||
PIEthernet eth;
|
||||
PIStreamPacker streampacker;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include "piconditionvar.h"
|
||||
|
||||
|
||||
class PIP_CLOUD_EXPORT PICloudClient : public PIIODevice, private PICloudBase
|
||||
class PIP_CLOUD_EXPORT PICloudClient: public PIIODevice, public PICloudBase
|
||||
{
|
||||
PIIODEVICE(PICloudClient)
|
||||
public:
|
||||
@@ -39,6 +39,9 @@ public:
|
||||
void setKeepConnection(bool on);
|
||||
bool isConnected() const {return is_connected;}
|
||||
|
||||
EVENT(connected)
|
||||
EVENT(disconnected)
|
||||
|
||||
protected:
|
||||
bool openDevice();
|
||||
bool closeDevice();
|
||||
@@ -46,7 +49,6 @@ protected:
|
||||
int writeDevice(const void * data, int size);
|
||||
DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;}
|
||||
|
||||
|
||||
private:
|
||||
EVENT_HANDLER1(void, _readed, PIByteArray &, data);
|
||||
PIByteArray buff;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include "piconditionvar.h"
|
||||
|
||||
|
||||
class PIP_CLOUD_EXPORT PICloudServer : public PIIODevice, private PICloudBase
|
||||
class PIP_CLOUD_EXPORT PICloudServer: public PIIODevice, public PICloudBase
|
||||
{
|
||||
PIIODEVICE(PICloudServer)
|
||||
public:
|
||||
|
||||
@@ -57,7 +57,8 @@ public:
|
||||
TCP(PIStreamPacker * s);
|
||||
void setRole(Role r);
|
||||
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 sendConnected(uint client_id);
|
||||
@@ -81,6 +82,7 @@ private:
|
||||
|
||||
Header header;
|
||||
PIByteArray suuid;
|
||||
PIString server_name;
|
||||
PIStreamPacker * streampacker;
|
||||
};
|
||||
|
||||
|
||||
@@ -389,6 +389,12 @@ bool PIIODevice::close() {
|
||||
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) {
|
||||
PIConfig conf(config_file, PIIODevice::ReadOnly);
|
||||
|
||||
@@ -263,7 +263,7 @@ public:
|
||||
bool open(DeviceMode _mode);
|
||||
EVENT_HANDLER2(bool, open, const PIString &, _path, DeviceMode, _mode);
|
||||
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) {;}
|
||||
|
||||
|
||||
20
main.cpp
20
main.cpp
@@ -1,5 +1,16 @@
|
||||
#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<
|
||||
std::is_same<T, PIVector<S>>::value
|
||||
@@ -10,7 +21,12 @@ template <typename S, typename T, typename std::enable_if<
|
||||
|
||||
|
||||
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;});
|
||||
piCout << x;
|
||||
PIDeque<PIDeque<int>> m = x.reshape(2,8);
|
||||
@@ -21,6 +37,6 @@ int main() {
|
||||
PIDeque<int> y;
|
||||
y = m.reshape<int>();
|
||||
piCout << y;
|
||||
piCout << m.reshape<int>(PIDeque<int>::byColumn);
|
||||
piCout << m.reshape<int>(PIDeque<int>::byColumn);*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||
//piCoutObj << "INVALID client" << client;
|
||||
return;
|
||||
}
|
||||
//piCoutObj << "remove client" << client;
|
||||
piCoutObj << "remove client" << client->clientId();
|
||||
map_mutex.lock();
|
||||
clients.removeOne(client);
|
||||
CloudServer * cs = index_c_servers.value(client, nullptr);
|
||||
|
||||
Reference in New Issue
Block a user