Merge branch 'thread' of https://git.shs.tools/SHS/pip into thread

This commit is contained in:
2022-11-10 15:03:59 +03:00
3 changed files with 41 additions and 28 deletions

View File

@@ -2,7 +2,7 @@
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(&eth), tcp(&streampacker) {
//eth.setDebug(false);
eth.setDebug(false);
}

View File

@@ -43,12 +43,12 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
PICloudClient::~PICloudClient() {
piCoutObj << "~PICloudClient() ..." << this;
//piCoutObj << "~PICloudClient() ..." << this;
is_deleted = true;
stopAndWait();
close();
internalDisconnect();
piCoutObj << "~PICloudClient() done" << this;
//piCoutObj << "~PICloudClient() done" << this;
}
@@ -70,14 +70,14 @@ void PICloudClient::interrupt() {
bool PICloudClient::openDevice() {
piCoutObj << "open";// << path();
//piCoutObj << "open";// << path();
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
mutex_connect.lock();
eth.startThreadedRead();
piCoutObj << "connecting...";
//piCoutObj << "connecting...";
bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout());
piCoutObj << "conn_ok" << conn_ok << is_connected;
//piCoutObj << "conn_ok" << conn_ok << is_connected;
mutex_connect.unlock();
if (!conn_ok) {
mutex_connect.lock();
@@ -106,7 +106,7 @@ bool PICloudClient::closeDevice() {
ssize_t PICloudClient::readDevice(void * read_to, ssize_t max_size) {
if (is_deleted || max_size <= 0) return -1;
piCoutObj << "readDevice ...";
//piCoutObj << "readDevice ...";
if (!is_connected && eth.isClosed()) openDevice();
ssize_t sz = -1;
mutex_buff.lock();
@@ -122,20 +122,20 @@ ssize_t PICloudClient::readDevice(void * read_to, ssize_t max_size) {
}
mutex_buff.unlock();
if (!is_connected) opened_ = false;
piCoutObj << "readDevice done" << sz;
//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;
//piCoutObj << "writeDevice" << size;
return tcp.sendData(PIByteArray(data, size));
}
void PICloudClient::internalDisconnect() {
piCoutObj << "internalDisconnect";
//piCoutObj << "internalDisconnect";
is_connected = false;
cond_buff.notifyOne();
cond_connect.notifyOne();
@@ -147,7 +147,7 @@ void PICloudClient::internalDisconnect() {
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;
//piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second;
if (hdr.second == tcp.role()) {
switch (hdr.first) {
case PICloud::TCP::Connect:
@@ -167,6 +167,11 @@ void PICloudClient::_readed(PIByteArray & ba) {
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();
@@ -177,6 +182,6 @@ void PICloudClient::_readed(PIByteArray & ba) {
}
//piCoutObj << "readed" << ba.toHex();
}
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
piCoutObj << "_readed done";
if (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
//piCoutObj << "_readed done";
}

View File

@@ -28,10 +28,14 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
is_deleted = false;
eth.setReopenEnabled(false);
CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, connected, [this](){opened_ = true; piCoutObj << "connected" << &eth; tcp.sendStart();});
CONNECTL(&eth, connected, [this](){
opened_ = true;
//piCoutObj << "connected" << &eth;
tcp.sendStart();
});
CONNECTL(&eth, disconnected, [this](bool){
if (is_deleted) return;
piCoutObj << "disconnected" << &eth;
//piCoutObj << "disconnected" << &eth;
for (auto c : clients_) {
delete c;
}
@@ -46,13 +50,13 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
PICloudServer::~PICloudServer() {
piCoutObj << "~PICloudServer ..." << this;
//piCoutObj << "~PICloudServer ..." << this;
is_deleted = true;
stop();
close();
piCout << "wait";
//piCout << "wait";
waitThreadedReadFinished();
piCoutObj << "~PICloudServer done" << this;
//piCoutObj << "~PICloudServer done" << this;
}
@@ -69,7 +73,7 @@ PIVector<PICloudServer::Client *> PICloudServer::clients() const {
bool PICloudServer::openDevice() {
piCout << "PICloudServer open device" << path();
//piCout << "PICloudServer open device" << path();
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
eth.startThreadedRead();
@@ -84,7 +88,7 @@ bool PICloudServer::openDevice() {
bool PICloudServer::closeDevice() {
piCoutObj << "closeDevice" << this;
//piCoutObj << "closeDevice" << this;
eth.stopAndWait();
ping_timer.stop(false);
eth.close();
@@ -135,10 +139,10 @@ PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), clien
PICloudServer::Client::~Client() {
piCoutObj << "~PICloudServer::Client..." << this;
//piCoutObj << "~PICloudServer::Client..." << this;
close();
stopAndWait();
piCoutObj << "~PICloudServer::Client done" << this;
//piCoutObj << "~PICloudServer::Client done" << this;
}
@@ -148,7 +152,7 @@ bool PICloudServer::Client::openDevice() {
bool PICloudServer::Client::closeDevice() {
piCoutObj << "closeDevice" << this;
//piCoutObj << "closeDevice" << this;
if (is_connected) {
server->clientDisconnect(client_id);
is_connected = false;
@@ -191,10 +195,14 @@ void PICloudServer::Client::interrupt() {
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();
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
}
@@ -212,7 +220,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
tcp.sendDisconnected(id);
} else {
Client * c = new Client(this, id);
piCoutObj << "new Client" << id << c;
//piCoutObj << "new Client" << id << c;
CONNECT1(void, PIObject *, c, deleted, this, clientDeleted);
clients_mutex.lock();
clients_ << c;
@@ -223,7 +231,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
} break;
case PICloud::TCP::Disconnect: {
uint id = tcp.parseDisconnect(ba);
piCoutObj << "remove Client" << id;
//piCoutObj << "remove Client" << id;
clients_mutex.lock();
Client * oc = index_clients.value(id, nullptr);
clients_mutex.unlock();
@@ -238,7 +246,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
clients_mutex.lock();
Client * oc = index_clients.value(d.first, nullptr);
clients_mutex.unlock();
piCoutObj << "data for" << d.first << d.second.size();
//piCoutObj << "data for" << d.first << d.second.size();
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
} break;
default: break;
@@ -249,7 +257,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
void PICloudServer::clientDeleted(PIObject * o) {
PICloudServer::Client * c = (PICloudServer::Client*)o;
piCoutObj << "clientDeleted" << c;
//piCoutObj << "clientDeleted" << c;
clients_mutex.lock();
clients_.removeOne(c);
auto it = index_clients.makeIterator();